|
|
1.1 root 1: #include <stdio.h>
2: #include <sys/types.h>
3: #include <sys/socket.h>
4: #include <netinet/in.h>
5: #include <netdb.h>
6: #include <errno.h>
7:
8: #define FIRSTPORT 1425
9: #define LASTPORT 64000
10:
11: /* some useful definitions */
12: #define TRUE 1
13: #define FALSE 0
14: #define bool char
15: #define HOSTNAMESIZE 32
16:
17: int errno; /* the magic global */
18: char ourhostname[HOSTNAMESIZE]; /* local host name */
19: long ourhostaddr; /* our host's address */
20: bool havehost = FALSE; /* true if ourhost* is valid */
21:
22:
23: /* get info about local host */
24: getmyhoststuff()
25: {
26: char *temp;
27:
28: gethostname (ourhostname, HOSTNAMESIZE);
29: netaddr (ourhostname, &ourhostaddr);
30: havehost = TRUE;
31: }
32:
33: /* return our host's name */
34: char *
35: myhostname()
36: {
37: if (!havehost) {
38: getmyhoststuff ();
39: }
40: return ourhostname;
41: }
42:
43: /* return our host's address */
44: myhostaddr()
45: {
46: if (!havehost) {
47: getmyhoststuff ();
48: }
49: return ourhostaddr;
50: }
51:
52:
53: /*
54: * make a datagram socket
55: */
56: makedgsocket (pptr)
57: int *pptr; /* port number */
58: {
59: int sock, i, rv;
60: struct sockaddr_in sin;
61:
62: /* set up INET address */
63: sin.sin_family = AF_INET;
64: sin.sin_addr.s_addr = myhostaddr();
65: bzero (sin.sin_zero, 8);
66:
67: /* get a socket */
68: sock = socket (AF_INET, SOCK_DGRAM, 0, 0);
69: if (sock < 0) {
70: error ("opening datagram socket");
71: }
72: if (*pptr == 0) {
73: for (*pptr = FIRSTPORT; *pptr < LASTPORT; *pptr += 11) {
74: sin.sin_port = htons((u_short)*pptr);
75: rv = bind (sock, &sin, sizeof(sin), 0);
76: if (rv == 0) {
77: break;
78: }
79: }
80: } else {
81: sin.sin_port = htons((u_short)*pptr);
82: rv = bind (sock, &sin, sizeof(sin), 0);
83: }
84: if (rv != 0) {
85: close (sock);
86: error ("binding datagram socket");
87: }
88:
89: return (sock);
90: }
91:
92:
93: /* get the network address of a machine */
94: long
95: netaddr (name, addrptr)
96: char *name;
97: char *addrptr;
98: {
99: long iaddr;
100: struct hostent *hp, *gethostbyname();
101:
102: hp = gethostbyname(name);
103: bcopy (hp->h_addr, addrptr, hp->h_length);
104: }
105:
106: /* are these two names synonymns? */
107: aresynonyms (name1, name2)
108: char *name1, *name2;
109: {
110: struct hostent *hp, *gethostbyname();
111: char **ptr;
112:
113: hp = gethostbyname(name1);
114: if (hp == 0)
115: return (FALSE);
116: if (strcmp (name2, hp->h_name) == 0)
117: return (TRUE);
118: for (ptr = hp->h_aliases; *ptr != 0; ptr++) {
119: if (strcmp (name2, *ptr) == 0)
120: return (TRUE);
121: }
122: return (FALSE);
123: }
124:
125: /* receive a datagram via the inet */
126: recvdg (fd, buf, len)
127: int fd; /* socket to receive over */
128: char * buf; /* buffer to receive into */
129: int len; /* size in bytes of that buffer */
130: {
131: struct sockaddr faddr;
132: int flen;
133: int rv, i;
134:
135: flen = sizeof (faddr);
136: rv = recvfrom (fd, buf, len, 0, &faddr, &flen);
137:
138: return (rv);
139: }
140:
141: /* send an inet datagram */
142: senddg (fd, buf, len, destname, destport)
143: int fd; /* socket to send via */
144: char *buf; /* buffer to send */
145: int len; /* number of bytes to send */
146: char *destname; /* name of host we're sending to */
147: int destport; /* the udp port on that node */
148: {
149: struct sockaddr_in sin;
150: int rv, i;
151: char *ptr;
152:
153: sin.sin_family = AF_INET;
154: bzero (sin.sin_zero, 8);
155: netaddr (destname, &(sin.sin_addr));
156: sin.sin_port = htons ((u_short)destport);
157: rv = sendto (fd, buf, len, 0, &sin, sizeof(sin));
158: return (rv);
159: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.