earlybrowserreborn - Blame information for rev 2

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 roytam 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 */
2 roytam 40 #define THINK_C
1 roytam 41 #ifndef PCNFS
42 #ifdef vms
43 extern int uerrno;      /* Deposit of error info (as perr errno.h) */
44 extern int vmserrno;    /* Deposit of VMS error info */
45 extern volatile noshare int errno;  /* noshare to avoid PSECT conflict */
46 #else
47 #ifndef errno
48 extern int errno;
49 #endif
50 #endif
51  
52 #ifndef VM
53 #ifndef vms
54 #ifndef NeXT
55 #ifndef THINK_C
56 extern char *sys_errlist[];             /* see man perror on cernvax */
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 #ifdef MVS      /* Oustanding problem with crsh in MVS gethostbyname */
225         if(TRACE)printf("HTTCP: Calling gethostbyname(%s)\n", host);
226 #endif
2 roytam 227         phost=gethostbyname2(host,AF_INET);     /* See netdb.h */
1 roytam 228 #ifdef MVS
229         if(TRACE)printf("HTTCP: gethostbyname() returned %d\n", phost);
230 #endif
231         if (!phost) {
232             if (TRACE) printf(
233                     "HTTPAccess: Can't find internet node name `%s'.\n",host);
234             return -1;  /* Fail? */
235         }
236         memcpy(&sin->sin_addr, phost->h_addr, phost->h_length);
237     }
238  
239     if (TRACE) printf(
240         "TCP: Parsed address as port %d, IP address %d.%d.%d.%d\n",
241                 (unsigned int)ntohs(sin->sin_port),
242                 (int)*((unsigned char *)(&sin->sin_addr)+0),
243                 (int)*((unsigned char *)(&sin->sin_addr)+1),
244                 (int)*((unsigned char *)(&sin->sin_addr)+2),
245                 (int)*((unsigned char *)(&sin->sin_addr)+3));
246  
247     return 0;   /* OK */
248 }
249  
250  
251  
252 /*      Derive the name of the host on which we are
253 **      -------------------------------------------
254 **
255 */
256 #ifdef __STDC__
257 PRIVATE void get_host_details(void)
258 #else
259 PRIVATE void get_host_details()
260 #endif
261  
262 #ifndef MAXHOSTNAMELEN
263 #define MAXHOSTNAMELEN 64               /* Arbitrary limit */
264 #endif
265  
266 {
267     char name[MAXHOSTNAMELEN+1];        /* The name of this host */
268 #ifdef NEED_HOST_ADDRESS                /* no -- needs name server! */
269     struct hostent * phost;             /* Pointer to host -- See netdb.h */
270 #endif
271     int namelength = sizeof(name);
272  
273     if (hostname) return;               /* Already done */
274     gethostname(name, namelength);      /* Without domain */
275     CTRACE(tfp, "TCP: Local host name is %s\n", name);
276     StrAllocCopy(hostname, name);
277  
278 #ifdef NEED_HOST_ADDRESS                /* no -- needs name server! */
279     phost=gethostbyname(name);          /* See netdb.h */
280     if (!phost) {
281         if (TRACE) printf(
282                 "TCP: Can't find my own internet node address for `%s'!!\n",
283                 name);
284         return;  /* Fail! */
285     }
286     StrAllocCopy(hostname, phost->h_name);
287     memcpy(&HTHostAddress, &phost->h_addr, phost->h_length);
288     if (TRACE) printf("     Name server says that is `%s' = %s\n",
289             hostname, HTInetString(&HTHostAddress));
290 #endif
291 }
292  
293 #ifdef __STDC__
294 PUBLIC const char * HTHostName(void)
295 #else
296 PUBLIC char * HTHostName()
297 #endif
298 {
299     get_host_details();
300     return hostname;
301 }
302