1 /* Generic Communication Code HTTCP.c
2 ** ==========================
4 ** This code is in common between client and server sides.
6 ** 16 Jan 92 Fix strtol() undefined on CMU Mach. - TBL
11 #include "tcp.h" /* Defines SHORT_NAMES if necessary */
13 #define HTInetStatus HTInStat
14 #define HTInetString HTInStri
15 #define HTParseInet HTPaInet
18 /* Module-Wide variables
21 PRIVATE char *hostname=0; /* The name of this host */
27 /* PUBLIC struct sockaddr_in HTHostAddress; */ /* The internet address of the host */
28 /* Valid after call to HTHostName() */
30 /* Encode INET status (as in sys/errno.h) inet_status()
34 ** where gives a description of what caused the error
35 ** global errno gives the error number in the unix way.
38 ** returns a negative status in the unix way.
42 extern int uerrno; /* Deposit of error info (as perr errno.h) */
43 extern int vmserrno; /* Deposit of VMS error info */
44 extern volatile noshare int errno; /* noshare to avoid PSECT conflict */
56 /* extern char *sys_errlist[]; */
64 /* Report Internet Error
65 ** ---------------------
68 PUBLIC int HTInetStatus(char *where)
70 PUBLIC int HTInetStatus(where)
74 CTRACE(tfp, "TCP: Error %d in `errno' after call to %s() failed.\n\t%s\n",
77 "(Error number not translated)"); /* What Is the VM equiv? */
78 #define ER_NO_TRANS_DONE
81 "(Error number not translated)");
82 #define ER_NO_TRANS_DONE
86 #define ER_NO_TRANS_DONE
90 #define ER_NO_TRANS_DONE
93 #ifndef ER_NO_TRANS_DONE
94 errno < sys_nerr ? sys_errlist[errno] : "Unknown error" );
99 CTRACE(tfp, " Unix error number (uerrno) = %ld dec\n", uerrno);
100 CTRACE(tfp, " VMS error (vmserrno) = %lx hex\n", vmserrno);
106 /* Parse a cardinal value parse_cardinal()
107 ** ----------------------
110 ** *pp points to first character to be interpreted, terminated by
111 ** non 0:9 character.
112 ** *pstatus points to status already valid
113 ** maxvalue gives the largest allowable value.
116 ** *pp points to first unread character
117 ** *pstatus points to status updated iff bad
120 PUBLIC unsigned int HTCardinal(int *pstatus,
122 unsigned int max_value)
124 PUBLIC unsigned int HTCardinal(pstatus, pp, max_value)
127 unsigned int max_value;
131 if ( (**pp<'0') || (**pp>'9')) { /* Null string is error */
132 *pstatus = -3; /* No number where one expeceted */
137 while ((**pp>='0') && (**pp<='9')) n = n*10 + *((*pp)++) - '0';
140 *pstatus = -4; /* Cardinal outside range */
148 /* Produce a string for an inernet address
149 ** ---------------------------------------
152 ** returns a pointer to a static string which must be copied if
156 PUBLIC const char * HTInetString(struct sockaddr_in* sin)
158 PUBLIC char * HTInetString(sin)
159 struct sockaddr_in *sin;
162 static char string[16];
163 sprintf(string, "%d.%d.%d.%d",
164 (int)*((unsigned char *)(&sin->sin_addr)+0),
165 (int)*((unsigned char *)(&sin->sin_addr)+1),
166 (int)*((unsigned char *)(&sin->sin_addr)+2),
167 (int)*((unsigned char *)(&sin->sin_addr)+3));
172 /* Parse an internet node address and port
173 ** ---------------------------------------
176 ** str points to a string with a node name or number,
177 ** with optional trailing colon and port number.
178 ** sin points to the binary internet address field.
181 ** *sin is filled in. If no port is specified in str, that
182 ** field is left unchanged in *sin.
185 PUBLIC int HTParseInet(struct sockaddr_in* sin, const char *str)
187 PUBLIC int HTParseInet(sin, str)
188 struct sockaddr_in *sin;
194 struct hostent *phost; /* Pointer to host - See netdb.h */
195 strcpy(host, str); /* Take a copy we can mutilate */
199 /* Parse port number if present
201 if (port=strchr(host, ':')) {
202 *port++ = 0; /* Chop off port */
203 if (port[0]>='0' && port[0]<='9') {
205 sin->sin_port = htons(atol(port));
207 sin->sin_port = htons(strtol(port, (char**)0 , 10));
210 #ifdef SUPPRESS /* 1. crashes!?!. 2. Not recommended */
211 struct servent * serv = getservbyname(port, (char*)0);
212 if (serv) sin->sin_port = serv->s_port;
213 else if (TRACE) printf("TCP: Unknown service %s\n", port);
218 /* Parse host number if present
220 if (*host>='0' && *host<='9') { /* Numeric node address: */
221 sin->sin_addr.s_addr = inet_addr(host); /* See arpa/inet.h */
223 } else { /* Alphanumeric node name: */
224 printf("HTTCP: Calling gethostbyname2(\"%s\",AF_INET)\n", host);
225 phost=gethostbyname2(host,AF_INET); /* See netdb.h */
228 "HTTPAccess: Can't find internet node name `%s', h_errno: %d.\n",host,h_errno);
229 return -1; /* Fail? */
231 memcpy(&sin->sin_addr, phost->h_addr, phost->h_length);
235 "TCP: Parsed address as port %d, IP address %d.%d.%d.%d\n",
236 (unsigned int)ntohs(sin->sin_port),
237 (int)*((unsigned char *)(&sin->sin_addr)+0),
238 (int)*((unsigned char *)(&sin->sin_addr)+1),
239 (int)*((unsigned char *)(&sin->sin_addr)+2),
240 (int)*((unsigned char *)(&sin->sin_addr)+3));
247 /* Derive the name of the host on which we are
248 ** -------------------------------------------
252 PRIVATE void get_host_details(void)
254 PRIVATE void get_host_details()
257 #ifndef MAXHOSTNAMELEN
258 #define MAXHOSTNAMELEN 64 /* Arbitrary limit */
262 char name[MAXHOSTNAMELEN+1]; /* The name of this host */
263 #ifdef NEED_HOST_ADDRESS /* no -- needs name server! */
264 struct hostent * phost; /* Pointer to host -- See netdb.h */
266 int namelength = sizeof(name);
268 if (hostname) return; /* Already done */
269 gethostname(name, namelength); /* Without domain */
270 CTRACE(tfp, "TCP: Local host name is %s\n", name);
271 StrAllocCopy(hostname, name);
273 #ifdef NEED_HOST_ADDRESS /* no -- needs name server! */
274 phost=gethostbyname(name); /* See netdb.h */
277 "TCP: Can't find my own internet node address for `%s'!!\n",
281 StrAllocCopy(hostname, phost->h_name);
282 memcpy(&HTHostAddress, &phost->h_addr, phost->h_length);
283 if (TRACE) printf(" Name server says that is `%s' = %s\n",
284 hostname, HTInetString(&HTHostAddress));
289 PUBLIC const char * HTHostName(void)
291 PUBLIC char * HTHostName()