More debianization
[erwise] / Cl / WWWLibrary / HTTCP.c
1 /*                      Generic Communication Code              HTTCP.c
2 **                      ==========================
3 **
4 **      This code is in common between client and server sides.
5 **
6 **      16 Jan 92       Fix strtol() undefined on CMU Mach. - TBL
7 */
8
9
10 #include "HTUtils.h"
11 #include "tcp.h"                /* Defines SHORT_NAMES if necessary */
12 #ifdef SHORT_NAMES
13 #define HTInetStatus            HTInStat
14 #define HTInetString            HTInStri
15 #define HTParseInet             HTPaInet
16 #endif
17
18 /*      Module-Wide variables
19 */
20
21 PRIVATE char *hostname=0;               /* The name of this host */
22
23
24 /*      PUBLIC VARIABLES
25 */
26
27 /* PUBLIC struct sockaddr_in HTHostAddress; */  /* The internet address of the host */
28                                         /* Valid after call to HTHostName() */
29
30 /*      Encode INET status (as in sys/errno.h)                    inet_status()
31 **      ------------------
32 **
33 ** On entry,
34 **      where           gives a description of what caused the error
35 **      global errno    gives the error number in the unix way.
36 **
37 ** On return,
38 **      returns         a negative status in the unix way.
39 */
40 #ifndef PCNFS
41 #ifdef vms
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 */
45 #else
46 #ifndef errno
47 extern int errno;
48 #endif
49 #endif
50
51 #ifndef VM
52 #ifndef vms
53 #ifndef NeXT
54 #ifndef THINK_C
55 /* Toni */
56 /* extern char *sys_errlist[];  */
57 extern int sys_nerr;
58 #endif  /* think c */
59 #endif  /* NeXT */
60 #endif  /* vms */
61 #endif  /* VM */
62 #endif  /* PCNFS */
63
64 /*      Report Internet Error
65 **      ---------------------
66 */
67 #ifdef __STDC__
68 PUBLIC int HTInetStatus(char *where)
69 #else
70 PUBLIC int HTInetStatus(where)
71     char    *where;
72 #endif
73 {
74     CTRACE(tfp, "TCP: Error %d in `errno' after call to %s() failed.\n\t%s\n",
75             errno,  where,
76 #ifdef VM
77             "(Error number not translated)");   /* What Is the VM equiv? */
78 #define ER_NO_TRANS_DONE
79 #endif
80 #ifdef vms
81             "(Error number not translated)");
82 #define ER_NO_TRANS_DONE
83 #endif
84 #ifdef NeXT
85             strerror(errno));
86 #define ER_NO_TRANS_DONE
87 #endif
88 #ifdef THINK_C
89             strerror(errno));
90 #define ER_NO_TRANS_DONE
91 #endif
92
93 #ifndef ER_NO_TRANS_DONE
94             errno < sys_nerr ? sys_errlist[errno] : "Unknown error" );
95 #endif
96
97
98 #ifdef vms
99     CTRACE(tfp, "         Unix error number (uerrno) = %ld dec\n", uerrno);
100     CTRACE(tfp, "         VMS error (vmserrno)       = %lx hex\n", vmserrno);
101 #endif
102     return -errno;
103 }
104
105
106 /*      Parse a cardinal value                                 parse_cardinal()
107 **      ----------------------
108 **
109 ** On entry,
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.
114 **
115 ** On exit,
116 **      *pp         points to first unread character
117 **      *pstatus    points to status updated iff bad
118 */
119 #ifdef __STDC__
120 PUBLIC unsigned int HTCardinal(int *pstatus,
121         char            **pp,
122         unsigned int    max_value)
123 #else
124 PUBLIC unsigned int HTCardinal(pstatus, pp, max_value)
125    int                  *pstatus;
126    char                 **pp;
127    unsigned int         max_value;
128 #endif
129 {
130     int   n;
131     if ( (**pp<'0') || (**pp>'9')) {        /* Null string is error */
132         *pstatus = -3;  /* No number where one expeceted */
133         return 0;
134     }
135
136     n=0;
137     while ((**pp>='0') && (**pp<='9')) n = n*10 + *((*pp)++) - '0';
138
139     if (n>max_value) {
140         *pstatus = -4;  /* Cardinal outside range */
141         return 0;
142     }
143
144     return n;
145 }
146
147
148 /*      Produce a string for an inernet address
149 **      ---------------------------------------
150 **
151 ** On exit,
152 **      returns a pointer to a static string which must be copied if
153 **              it is to be kept.
154 */
155 #ifdef __STDC__
156 PUBLIC const char * HTInetString(struct sockaddr_in* sin)
157 #else
158 PUBLIC char * HTInetString(sin)
159     struct sockaddr_in *sin;
160 #endif
161 {
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));
168     return string;
169 }
170
171
172 /*      Parse an internet node address and port
173 **      ---------------------------------------
174 **
175 ** On entry,
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.
179 **
180 ** On exit,
181 **      *sin    is filled in. If no port is specified in str, that
182 **              field is left unchanged in *sin.
183 */
184 #ifdef __STDC__
185 PUBLIC int HTParseInet(struct sockaddr_in* sin, const char *str)
186 #else
187 PUBLIC int HTParseInet(sin, str)
188     struct sockaddr_in *sin;
189     char *str;
190 #endif
191 {
192     char *port;
193     char host[256];
194     struct hostent  *phost;     /* Pointer to host - See netdb.h */
195     strcpy(host, str);          /* Take a copy we can mutilate */
196
197
198
199 /*      Parse port number if present
200 */    
201     if (port=strchr(host, ':')) {
202         *port++ = 0;            /* Chop off port */
203         if (port[0]>='0' && port[0]<='9') {
204 #ifdef unix
205             sin->sin_port = htons(atol(port));
206 #else
207             sin->sin_port = htons(strtol(port, (char**)0 , 10));
208 #endif
209         } else {
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);
214 #endif
215         }
216     }
217
218 /*      Parse host number if present
219 */  
220     if (*host>='0' && *host<='9') {   /* Numeric node address: */
221         sin->sin_addr.s_addr = inet_addr(host); /* See arpa/inet.h */
222
223     } else {                /* Alphanumeric node name: */
224         printf("HTTCP: Calling gethostbyname2(\"%s\",AF_INET)\n", host);
225         phost=gethostbyname2(host,AF_INET);     /* See netdb.h */
226         if (!phost) {
227             printf(
228                     "HTTPAccess: Can't find internet node name `%s', h_errno: %d.\n",host,h_errno);
229             return -1;  /* Fail? */
230         }
231         memcpy(&sin->sin_addr, phost->h_addr, phost->h_length);
232     }
233
234     if (TRACE) printf( 
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));
241
242     return 0;   /* OK */
243 }
244
245
246
247 /*      Derive the name of the host on which we are
248 **      -------------------------------------------
249 **
250 */
251 #ifdef __STDC__
252 PRIVATE void get_host_details(void)
253 #else
254 PRIVATE void get_host_details()
255 #endif
256
257 #ifndef MAXHOSTNAMELEN
258 #define MAXHOSTNAMELEN 64               /* Arbitrary limit */
259 #endif
260
261 {
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 */
265 #endif
266     int namelength = sizeof(name);
267     
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);
272
273 #ifdef NEED_HOST_ADDRESS                /* no -- needs name server! */
274     phost=gethostbyname(name);          /* See netdb.h */
275     if (!phost) {
276         printf(
277                 "TCP: Can't find my own internet node address for `%s'!!\n",
278                 name);
279         return;  /* Fail! */
280     }
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));
285 #endif
286 }
287
288 #ifdef __STDC__
289 PUBLIC const char * HTHostName(void)
290 #else
291 PUBLIC char * HTHostName()
292 #endif
293 {
294     get_host_details();
295     return hostname;
296 }
297