imapext-2007

view src/osdep/dos/tcp_wsk.c @ 0:ada5e610ab86

imap-2007e
author yuuji@gentei.org
date Mon, 14 Sep 2009 15:17:45 +0900
parents
children
line source
1 /* ========================================================================
2 * Copyright 1988-2008 University of Washington
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 *
11 * ========================================================================
12 */
14 /*
15 * Program: Winsock TCP/IP routines
16 *
17 * Author: Mark Crispin from Mike Seibel's Winsock code
18 * Networks and Distributed Computing
19 * Computing & Communications
20 * University of Washington
21 * Administration Building, AG-44
22 * Seattle, WA 98195
23 * Internet: MRC@CAC.Washington.EDU
24 *
25 * Date: 11 April 1989
26 * Last Edited: 13 January 2008
27 */
30 #define TCPMAXSEND 32768
32 /* Private functions */
34 int tcp_socket_open (struct sockaddr_in *sin,char *tmp,char *hst,
35 unsigned long port);
36 static char *tcp_getline_work (TCPSTREAM *stream,unsigned long *size,
37 long *contd);
38 long tcp_abort (TCPSTREAM *stream);
39 long tcp_close_socket (SOCKET *sock);
40 char *tcp_name (struct sockaddr_in *sin,long flag);
41 char *tcp_name_valid (char *s);
44 /* Private data */
46 int wsa_initted = 0; /* init ? */
47 static int wsa_sock_open = 0; /* keep track of open sockets */
48 static tcptimeout_t tmoh = NIL; /* TCP timeout handler routine */
49 static long ttmo_read = 0; /* TCP timeouts, in seconds */
50 static long ttmo_write = 0;
51 static long allowreversedns = T;/* allow reverse DNS lookup */
52 static long tcpdebug = NIL; /* extra TCP debugging telemetry */
54 /* TCP/IP manipulate parameters
55 * Accepts: function code
56 * function-dependent value
57 * Returns: function-dependent return value
58 */
60 void *tcp_parameters (long function,void *value)
61 {
62 void *ret = NIL;
63 switch ((int) function) {
64 case SET_TIMEOUT:
65 tmoh = (tcptimeout_t) value;
66 case GET_TIMEOUT:
67 ret = (void *) tmoh;
68 break;
69 case SET_READTIMEOUT:
70 ttmo_read = (long) value;
71 case GET_READTIMEOUT:
72 ret = (void *) ttmo_read;
73 break;
74 case SET_WRITETIMEOUT:
75 ttmo_write = (long) value;
76 case GET_WRITETIMEOUT:
77 ret = (void *) ttmo_write;
78 break;
79 case SET_ALLOWREVERSEDNS:
80 allowreversedns = (long) value;
81 case GET_ALLOWREVERSEDNS:
82 ret = (void *) allowreversedns;
83 break;
84 case SET_TCPDEBUG:
85 tcpdebug = (long) value;
86 case GET_TCPDEBUG:
87 ret = (void *) tcpdebug;
88 break;
89 }
90 return ret;
91 }
93 /* TCP/IP open
94 * Accepts: host name
95 * contact service name
96 * contact port number and optional silent flag
97 * Returns: TCP/IP stream if success else NIL
98 */
100 TCPSTREAM *tcp_open (char *host,char *service,unsigned long port)
101 {
102 TCPSTREAM *stream = NIL;
103 int i;
104 SOCKET sock = INVALID_SOCKET;
105 int silent = (port & NET_SILENT) ? T : NIL;
106 char *s;
107 struct sockaddr_in sin;
108 struct hostent *he;
109 char hostname[MAILTMPLEN];
110 char tmp[MAILTMPLEN];
111 struct servent *sv = NIL;
112 blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
113 if (!wsa_initted++) { /* init Windows Sockets */
114 WSADATA wsock;
115 if (i = (int) WSAStartup (WSA_VERSION,&wsock)) {
116 wsa_initted = 0; /* in case we try again */
117 sprintf (tmp,"Unable to start Windows Sockets (%d)",i);
118 mm_log (tmp,ERROR);
119 return NIL;
120 }
121 }
122 port &= 0xffff; /* erase flags */
123 /* lookup service */
124 if (service && (sv = getservbyname (service,"tcp")))
125 port = ntohs (sin.sin_port = sv->s_port);
126 /* copy port number in network format */
127 else sin.sin_port = htons ((u_short) port);
128 /* The domain literal form is used (rather than simply the dotted decimal
129 as with other Windows programs) because it has to be a valid "host name"
130 in mailsystem terminology. */
131 sin.sin_family = AF_INET; /* family is always Internet */
132 /* look like domain literal? */
133 if (host[0] == '[' && host[(strlen (host))-1] == ']') {
134 strcpy (tmp,host+1); /* yes, copy number part */
135 tmp[strlen (tmp)-1] = '\0';
136 if ((sin.sin_addr.s_addr = inet_addr (tmp)) == INADDR_NONE) {
137 sprintf (tmp,"Bad format domain-literal: %.80s",host);
138 mm_log (tmp,ERROR);
139 return NIL;
140 }
141 else {
142 sin.sin_family = AF_INET; /* family is always Internet */
143 strcpy (hostname,host);
144 (*bn) (BLOCK_TCPOPEN,NIL);
145 sock = tcp_socket_open (&sin,tmp,hostname,port);
146 (*bn) (BLOCK_NONE,NIL);
147 }
148 }
150 else { /* lookup host name */
151 if (tcpdebug) {
152 sprintf (tmp,"DNS resolution %.80s",host);
153 mm_log (tmp,TCPDEBUG);
154 }
155 (*bn) (BLOCK_DNSLOOKUP,NIL);/* look up name */
156 if (!(he = gethostbyname (lcase (strcpy (tmp,host)))))
157 sprintf (tmp,"Host not found (#%d): %s",WSAGetLastError(),host);
158 (*bn) (BLOCK_NONE,NIL);
159 if (he) { /* DNS resolution won? */
160 if (tcpdebug) mm_log ("DNS resolution done",TCPDEBUG);
161 /* copy address type */
162 sin.sin_family = he->h_addrtype;
163 /* copy host name */
164 strcpy (hostname,he->h_name);
165 wsa_sock_open++; /* prevent tcp_close_socket() from freeing in
166 loop */
167 for (i = 0; (sock == INVALID_SOCKET) && (s = he->h_addr_list[i]); i++) {
168 if (i && !silent) mm_log (tmp,WARN);
169 memcpy (&sin.sin_addr,s,he->h_length);
170 (*bn) (BLOCK_TCPOPEN,NIL);
171 sock = tcp_socket_open (&sin,tmp,hostname,port);
172 (*bn) (BLOCK_NONE,NIL);
173 }
174 wsa_sock_open--; /* undo protection */
175 }
176 }
177 if (sock == INVALID_SOCKET) { /* error? */
178 if (!silent) mm_log (tmp,ERROR);
179 tcp_close_socket (&sock); /* do possible cleanup action */
180 }
181 else { /* got a socket, create TCP/IP stream */
182 stream = (TCPSTREAM *) memset (fs_get (sizeof (TCPSTREAM)),0,
183 sizeof (TCPSTREAM));
184 stream->port = port; /* port number */
185 /* init socket */
186 stream->tcpsi = stream->tcpso = sock;
187 stream->ictr = 0; /* init input counter */
188 /* copy official host name */
189 stream->host = cpystr (hostname);
190 if (tcpdebug) mm_log ("Stream open and ready for read",TCPDEBUG);
191 }
192 return stream; /* return success */
193 }
195 /* Open a TCP socket
196 * Accepts: Internet socket address block
197 * scratch buffer
198 * host name for error message
199 * port number for error message
200 * Returns: socket if success, else -1 with error string in scratch buffer
201 */
203 int tcp_socket_open (struct sockaddr_in *sin,char *tmp,char *hst,
204 unsigned long port)
205 {
206 int sock;
207 char *s;
208 sprintf (tmp,"Trying IP address [%s]",inet_ntoa (sin->sin_addr));
209 mm_log (tmp,NIL);
210 /* get a TCP stream */
211 if ((sock = socket (sin->sin_family,SOCK_STREAM,0)) == INVALID_SOCKET) {
212 sprintf (tmp,"Unable to create TCP socket (%d)",WSAGetLastError());
213 return -1;
214 }
215 wsa_sock_open++; /* count this socket as open */
216 /* open connection */
217 if (connect (sock,(struct sockaddr *) sin,sizeof (struct sockaddr_in)) ==
218 SOCKET_ERROR) {
219 switch (WSAGetLastError ()){/* analyze error */
220 case WSAECONNREFUSED:
221 s = "Refused";
222 break;
223 case WSAENOBUFS:
224 s = "Insufficient system resources";
225 break;
226 case WSAETIMEDOUT:
227 s = "Timed out";
228 break;
229 case WSAEHOSTUNREACH:
230 s = "Host unreachable";
231 break;
232 default:
233 s = "Unknown error";
234 break;
235 }
236 sprintf (tmp,"Can't connect to %.80s,%ld: %s (%d)",hst,port,s,
237 WSAGetLastError ());
238 tcp_close_socket (&sock); /* flush socket */
239 sock = INVALID_SOCKET;
240 }
241 return sock; /* return the socket */
242 }
244 /* TCP/IP authenticated open
245 * Accepts: NETMBX specifier
246 * service name
247 * returned user name buffer
248 * Returns: TCP/IP stream if success else NIL
249 */
251 TCPSTREAM *tcp_aopen (NETMBX *mb,char *service,char *usrbuf)
252 {
253 return NIL; /* always NIL on Windows */
254 }
256 /* TCP receive line
257 * Accepts: TCP stream
258 * Returns: text line string or NIL if failure
259 */
261 char *tcp_getline (TCPSTREAM *stream)
262 {
263 unsigned long n,contd;
264 char *ret = tcp_getline_work (stream,&n,&contd);
265 if (ret && contd) { /* got a line needing continuation? */
266 STRINGLIST *stl = mail_newstringlist ();
267 STRINGLIST *stc = stl;
268 do { /* collect additional lines */
269 stc->text.data = (unsigned char *) ret;
270 stc->text.size = n;
271 stc = stc->next = mail_newstringlist ();
272 ret = tcp_getline_work (stream,&n,&contd);
273 } while (ret && contd);
274 if (ret) { /* stash final part of line on list */
275 stc->text.data = (unsigned char *) ret;
276 stc->text.size = n;
277 /* determine how large a buffer we need */
278 for (n = 0, stc = stl; stc; stc = stc->next) n += stc->text.size;
279 ret = fs_get (n + 1); /* copy parts into buffer */
280 for (n = 0, stc = stl; stc; n += stc->text.size, stc = stc->next)
281 memcpy (ret + n,stc->text.data,stc->text.size);
282 ret[n] = '\0';
283 }
284 mail_free_stringlist (&stl);/* either way, done with list */
285 }
286 return ret;
287 }
289 /* TCP receive line or partial line
290 * Accepts: TCP stream
291 * pointer to return size
292 * pointer to return continuation flag
293 * Returns: text line string, size and continuation flag, or NIL if failure
294 */
296 static char *tcp_getline_work (TCPSTREAM *stream,unsigned long *size,
297 long *contd)
298 {
299 unsigned long n;
300 char *s,*ret,c,d;
301 *contd = NIL; /* assume no continuation */
302 /* make sure have data */
303 if (!tcp_getdata (stream)) return NIL;
304 for (s = stream->iptr, n = 0, c = '\0'; stream->ictr--; n++, c = d) {
305 d = *stream->iptr++; /* slurp another character */
306 if ((c == '\015') && (d == '\012')) {
307 ret = (char *) fs_get (n--);
308 memcpy (ret,s,*size = n); /* copy into a free storage string */
309 ret[n] = '\0'; /* tie off string with null */
310 return ret;
311 }
312 }
313 /* copy partial string from buffer */
314 memcpy ((ret = (char *) fs_get (n)),s,*size = n);
315 /* get more data from the net */
316 if (!tcp_getdata (stream)) fs_give ((void **) &ret);
317 /* special case of newline broken by buffer */
318 else if ((c == '\015') && (*stream->iptr == '\012')) {
319 stream->iptr++; /* eat the line feed */
320 stream->ictr--;
321 ret[*size = --n] = '\0'; /* tie off string with null */
322 }
323 else *contd = LONGT; /* continuation needed */
324 return ret;
325 }
327 /* TCP/IP receive buffer
328 * Accepts: TCP/IP stream
329 * size in bytes
330 * buffer to read into
331 * Returns: T if success, NIL otherwise
332 */
334 long tcp_getbuffer (TCPSTREAM *stream,unsigned long size,char *s)
335 {
336 unsigned long n;
337 /* make sure socket still alive */
338 if (stream->tcpsi == INVALID_SOCKET) return NIL;
339 /* can transfer bytes from buffer? */
340 if (n = min (size,stream->ictr)) {
341 memcpy (s,stream->iptr,n); /* yes, slurp as much as we can from it */
342 s += n; /* update pointer */
343 stream->iptr +=n;
344 size -= n; /* update # of bytes to do */
345 stream->ictr -=n;
346 }
347 if (size) {
348 int i;
349 fd_set fds;
350 struct timeval tmo;
351 time_t tc,t = time (0);
352 blocknotify_t bn=(blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
353 (*bn) (BLOCK_TCPREAD,NIL);
354 while (size > 0) { /* until request satisfied */
355 time_t tl = time (0);
356 if (tcpdebug) mm_log ("Reading TCP buffer",TCPDEBUG);
357 FD_ZERO (&fds); /* initialize selection vector */
358 FD_SET (stream->tcpsi,&fds);/* set bit in selection vector */
359 tmo.tv_sec = ttmo_read;
360 tmo.tv_usec = 0;
361 /* block and read */
362 switch ((stream->tcpsi == stream->tcpso) ?
363 select (stream->tcpsi+1,&fds,0,0,
364 ttmo_read ? &tmo : (struct timeval *) 0) : 1) {
365 case SOCKET_ERROR: /* error */
366 if (WSAGetLastError () != WSAEINTR) return tcp_abort (stream);
367 break;
368 case 0: /* timeout */
369 tc = time (0);
370 if (tmoh && ((*tmoh) (tc - t,tc - tl))) break;
371 return tcp_abort (stream);
372 default:
373 if (stream->tcpsi == stream->tcpso)
374 while (((i = recv (stream->tcpsi,s,(int) min (maxposint,size),0)) ==
375 SOCKET_ERROR) && (WSAGetLastError () == WSAEINTR));
376 else while (((i = read (stream->tcpsi,s,(int) min (maxposint,size))) <
377 0) && (errno == EINTR));
378 switch (i) {
379 case SOCKET_ERROR: /* error */
380 case 0: /* no data read */
381 return tcp_abort (stream);
382 default:
383 s += i; /* point at new place to write */
384 size -= i; /* reduce byte count */
385 if (tcpdebug) mm_log ("Successfully read TCP buffer",TCPDEBUG);
386 }
387 }
388 }
389 (*bn) (BLOCK_NONE,NIL);
390 }
391 *s = '\0'; /* tie off string */
392 return T;
393 }
395 /* TCP/IP receive data
396 * Accepts: TCP/IP stream
397 * Returns: T if success, NIL otherwise
398 */
400 long tcp_getdata (TCPSTREAM *stream)
401 {
402 struct timeval tmo;
403 int i;
404 fd_set fds;
405 time_t tc,t = time (0);
406 blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
407 FD_ZERO (&fds); /* initialize selection vector */
408 if (stream->tcpsi == INVALID_SOCKET) return NIL;
409 (*bn) (BLOCK_TCPREAD,NIL);
410 tmo.tv_sec = ttmo_read;
411 tmo.tv_usec = 0;
412 while (stream->ictr < 1) { /* if nothing in the buffer */
413 time_t tl = time (0);
414 if (tcpdebug) mm_log ("Reading TCP data",TCPDEBUG);
415 FD_SET (stream->tcpsi,&fds);/* set bit in selection vector */
416 /* block and read */
417 switch ((stream->tcpsi == stream->tcpso) ?
418 select (stream->tcpsi+1,&fds,0,0,
419 ttmo_read ? &tmo : (struct timeval *) 0) : 1) {
420 case SOCKET_ERROR: /* error */
421 if (WSAGetLastError () != WSAEINTR) return tcp_abort (stream);
422 break;
423 case 0: /* timeout */
424 tc = time (0);
425 if (tmoh && ((*tmoh) (tc - t,tc - tl))) break;
426 return tcp_abort (stream);
427 default:
428 if (stream->tcpsi == stream->tcpso)
429 while (((i = recv (stream->tcpsi,stream->ibuf,BUFLEN,0)) ==
430 SOCKET_ERROR) && (WSAGetLastError () == WSAEINTR));
431 else while (((i = read (stream->tcpsi,stream->ibuf,BUFLEN)) < 0) &&
432 (errno == EINTR));
433 switch (i) {
434 case SOCKET_ERROR: /* error */
435 case 0: /* no data read */
436 return tcp_abort (stream);
437 default:
438 stream->ictr = i; /* set new byte count */
439 /* point at TCP buffer */
440 stream->iptr = stream->ibuf;
441 if (tcpdebug) mm_log ("Successfully read TCP data",TCPDEBUG);
442 }
443 }
444 }
445 (*bn) (BLOCK_NONE,NIL);
446 return T;
447 }
449 /* TCP/IP send string as record
450 * Accepts: TCP/IP stream
451 * string pointer
452 * Returns: T if success else NIL
453 */
455 long tcp_soutr (TCPSTREAM *stream,char *string)
456 {
457 return tcp_sout (stream,string,(unsigned long) strlen (string));
458 }
461 /* TCP/IP send string
462 * Accepts: TCP/IP stream
463 * string pointer
464 * byte count
465 * Returns: T if success else NIL
466 */
468 long tcp_sout (TCPSTREAM *stream,char *string,unsigned long size)
469 {
470 int i;
471 struct timeval tmo;
472 fd_set fds;
473 time_t tc,t = time (0);
474 blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
475 tmo.tv_sec = ttmo_write;
476 tmo.tv_usec = 0;
477 FD_ZERO (&fds); /* initialize selection vector */
478 if (stream->tcpso == INVALID_SOCKET) return NIL;
479 (*bn) (BLOCK_TCPWRITE,NIL);
480 while (size > 0) { /* until request satisfied */
481 time_t tl = time (0);
482 if (tcpdebug) mm_log ("Writing to TCP",TCPDEBUG);
483 FD_SET (stream->tcpso,&fds);/* set bit in selection vector */
484 /* block and write */
485 switch ((stream->tcpsi == stream->tcpso) ?
486 select (stream->tcpso+1,NULL,&fds,NULL,
487 tmo.tv_sec ? &tmo : (struct timeval *) 0) : 1) {
488 case SOCKET_ERROR: /* error */
489 if (WSAGetLastError () != WSAEINTR) return tcp_abort (stream);
490 break;
491 case 0: /* timeout */
492 tc = time (0);
493 if (tmoh && ((*tmoh) (tc - t,tc - tl))) break;
494 return tcp_abort (stream);
495 default:
496 if (stream->tcpsi == stream->tcpso)
497 while (((i = send (stream->tcpso,string,
498 (int) min (size,TCPMAXSEND),0)) == SOCKET_ERROR) &&
499 (WSAGetLastError () == WSAEINTR));
500 else while (((i = write (stream->tcpso,string,
501 min (size,TCPMAXSEND))) < 0) &&
502 (errno == EINTR));
503 if (i == SOCKET_ERROR) return tcp_abort (stream);
504 size -= i; /* count this size */
505 if (tcpdebug) mm_log ("successfully wrote to TCP",TCPDEBUG);
506 string += i;
507 }
508 }
509 (*bn) (BLOCK_NONE,NIL);
510 return T; /* all done */
511 }
514 /* TCP/IP close
515 * Accepts: TCP/IP stream
516 */
518 void tcp_close (TCPSTREAM *stream)
519 {
520 tcp_abort (stream); /* nuke the sockets */
521 /* flush host names */
522 if (stream->host) fs_give ((void **) &stream->host);
523 if (stream->remotehost) fs_give ((void **) &stream->remotehost);
524 if (stream->localhost) fs_give ((void **) &stream->localhost);
525 fs_give ((void **) &stream); /* flush the stream */
526 }
529 /* TCP/IP abort sockets
530 * Accepts: TCP/IP stream
531 * Returns: NIL, always
532 */
534 long tcp_abort (TCPSTREAM *stream)
535 {
536 if (stream->tcpsi != stream->tcpso) tcp_close_socket (&stream->tcpso);
537 else stream->tcpso = INVALID_SOCKET;
538 return tcp_close_socket (&stream->tcpsi);
539 }
542 /* TCP/IP abort stream
543 * Accepts: WinSock socket
544 * Returns: NIL, always
545 */
547 long tcp_close_socket (SOCKET *sock)
548 {
549 blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
550 /* something to close? */
551 if (sock && (*sock != INVALID_SOCKET)) {
552 (*bn) (BLOCK_TCPCLOSE,NIL);
553 closesocket (*sock); /* WinSock socket close */
554 *sock = INVALID_SOCKET;
555 (*bn) (BLOCK_NONE,NIL);
556 wsa_sock_open--; /* drop this socket */
557 }
558 /* no more open streams? */
559 if (wsa_initted && !wsa_sock_open) {
560 mm_log ("Winsock cleanup",NIL);
561 wsa_initted = 0; /* no more sockets, so... */
562 WSACleanup (); /* free up resources until needed */
563 }
564 return NIL;
565 }
567 /* TCP/IP get host name
568 * Accepts: TCP/IP stream
569 * Returns: host name for this stream
570 */
572 char *tcp_host (TCPSTREAM *stream)
573 {
574 return stream->host; /* use tcp_remotehost() if want guarantees */
575 }
578 /* TCP/IP get remote host name
579 * Accepts: TCP/IP stream
580 * Returns: host name for this stream
581 */
583 char *tcp_remotehost (TCPSTREAM *stream)
584 {
585 if (!stream->remotehost) {
586 struct sockaddr_in sin;
587 int sinlen = sizeof (struct sockaddr_in);
588 stream->remotehost = /* get socket's peer name */
589 ((getpeername (stream->tcpsi,(struct sockaddr *) &sin,&sinlen) ==
590 SOCKET_ERROR) || (sinlen <= 0)) ?
591 cpystr (stream->host) : tcp_name (&sin,NIL);
592 }
593 return stream->remotehost;
594 }
597 /* TCP/IP return port for this stream
598 * Accepts: TCP/IP stream
599 * Returns: port number for this stream
600 */
602 unsigned long tcp_port (TCPSTREAM *stream)
603 {
604 return stream->port; /* return port number */
605 }
608 /* TCP/IP get local host name
609 * Accepts: TCP/IP stream
610 * Returns: local host name
611 */
613 char *tcp_localhost (TCPSTREAM *stream)
614 {
615 if (!stream->localhost) {
616 struct sockaddr_in sin;
617 int sinlen = sizeof (struct sockaddr_in);
618 stream->localhost = /* get socket's name */
619 ((stream->port & 0xffff000) ||
620 ((getsockname (stream->tcpsi,(struct sockaddr *) &sin,&sinlen) ==
621 SOCKET_ERROR) || (sinlen <= 0))) ?
622 cpystr (mylocalhost ()) : tcp_name (&sin,NIL);
623 }
624 return stream->localhost; /* return local host name */
625 }
627 /* TCP/IP get client host address (server calls only)
628 * Returns: client host address
629 */
631 char *tcp_clientaddr ()
632 {
633 if (!myClientAddr) {
634 struct sockaddr_in sin;
635 int sinlen = sizeof (struct sockaddr_in);
636 myClientAddr = /* get stdin's peer name */
637 ((getpeername (0,(struct sockaddr *) &sin,&sinlen) == SOCKET_ERROR) ||
638 (sinlen <= 0)) ? cpystr ("UNKNOWN") : cpystr (inet_ntoa (sin.sin_addr));
639 }
640 return myClientAddr;
641 }
644 /* TCP/IP get client host name (server calls only)
645 * Returns: client host name
646 */
648 char *tcp_clienthost ()
649 {
650 if (!myClientHost) {
651 struct sockaddr_in sin;
652 int sinlen = sizeof (struct sockaddr_in);
653 myClientHost = /* get stdin's peer name */
654 ((getpeername (0,(struct sockaddr *) &sin,&sinlen) == SOCKET_ERROR) ||
655 (sinlen <= 0)) ? cpystr ("UNKNOWN") : tcp_name (&sin,T);
656 }
657 return myClientHost;
658 }
660 /* TCP/IP get server host address (server calls only)
661 * Returns: server host address
662 */
664 char *tcp_serveraddr ()
665 {
666 if (!myServerAddr) {
667 struct sockaddr_in sin;
668 int sinlen = sizeof (struct sockaddr_in);
669 myServerAddr = /* get stdin's peer name */
670 ((getsockname (0,(struct sockaddr *) &sin,&sinlen) == SOCKET_ERROR) ||
671 (sinlen <= 0)) ? cpystr ("UNKNOWN") : cpystr (inet_ntoa (sin.sin_addr));
672 }
673 return myServerAddr;
674 }
677 /* TCP/IP get server host name (server calls only)
678 * Returns: server host name
679 */
681 static long myServerPort = -1;
683 char *tcp_serverhost ()
684 {
685 if (!myServerHost) {
686 struct sockaddr_in sin;
687 int sinlen = sizeof (struct sockaddr_in);
688 if (!wsa_initted++) { /* init Windows Sockets */
689 WSADATA wsock;
690 if (WSAStartup (WSA_VERSION,&wsock)) {
691 wsa_initted = 0;
692 return "random-pc"; /* try again later? */
693 }
694 }
695 /* get stdin's name */
696 if ((getsockname (0,(struct sockaddr *) &sin,&sinlen) == SOCKET_ERROR) ||
697 (sinlen <= 0)) myServerHost = cpystr (mylocalhost ());
698 else {
699 myServerHost = tcp_name (&sin,NIL);
700 myServerPort = ntohs (sin.sin_port);
701 }
702 }
703 return myServerHost;
704 }
707 /* TCP/IP get server port number (server calls only)
708 * Returns: server port number
709 */
711 long tcp_serverport ()
712 {
713 if (!myServerHost) tcp_serverhost ();
714 return myServerPort;
715 }
717 /* TCP/IP return canonical form of host name
718 * Accepts: host name
719 * Returns: canonical form of host name
720 */
722 char *tcp_canonical (char *name)
723 {
724 char *ret,host[MAILTMPLEN];
725 struct hostent *he;
726 blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
727 /* look like domain literal? */
728 if (name[0] == '[' && name[strlen (name) - 1] == ']') return name;
729 (*bn) (BLOCK_DNSLOOKUP,NIL);
730 if (tcpdebug) {
731 sprintf (host,"DNS canonicalization %.80s",name);
732 mm_log (host,TCPDEBUG);
733 }
734 /* note that NT requires lowercase! */
735 ret = (he = gethostbyname (lcase (strcpy (host,name)))) ? he->h_name : name;
736 (*bn) (BLOCK_NONE,NIL);
737 if (tcpdebug) mm_log ("DNS canonicalization done",TCPDEBUG);
738 return ret;
739 }
742 /* TCP/IP return name from socket
743 * Accepts: socket
744 * verbose flag
745 * Returns: cpystr name
746 */
748 char *tcp_name (struct sockaddr_in *sin,long flag)
749 {
750 char *ret,*t,adr[MAILTMPLEN],tmp[MAILTMPLEN];
751 sprintf (ret = adr,"[%.80s]",inet_ntoa (sin->sin_addr));
752 if (allowreversedns) {
753 struct hostent *he;
754 blocknotify_t bn = (blocknotify_t)mail_parameters(NIL,GET_BLOCKNOTIFY,NIL);
755 void *data;
756 if (tcpdebug) {
757 sprintf (tmp,"Reverse DNS resolution %s",adr);
758 mm_log (tmp,TCPDEBUG);
759 }
760 (*bn) (BLOCK_DNSLOOKUP,NIL);/* quell alarms */
761 data = (*bn) (BLOCK_SENSITIVE,NIL);
762 /* translate address to name */
763 if (t = tcp_name_valid ((he = gethostbyaddr ((char *) &sin->sin_addr,
764 sizeof (struct in_addr),
765 sin->sin_family)) ?
766 (char *) he->h_name : NIL)) {
767 /* produce verbose form if needed */
768 if (flag) sprintf (ret = tmp,"%s %s",t,adr);
769 else ret = t;
770 }
771 (*bn) (BLOCK_NONSENSITIVE,data);
772 (*bn) (BLOCK_NONE,NIL); /* alarms OK now */
773 if (tcpdebug) mm_log ("Reverse DNS resolution done",TCPDEBUG);
774 }
775 return cpystr (ret);
776 }
778 /* Return my local host name
779 * Returns: my local host name
780 */
782 char *mylocalhost (void)
783 {
784 if (!myLocalHost) {
785 char tmp[MAILTMPLEN];
786 if (!wsa_initted++) { /* init Windows Sockets */
787 WSADATA wsock;
788 if (WSAStartup (WSA_VERSION,&wsock)) {
789 wsa_initted = 0;
790 return "random-pc"; /* try again later? */
791 }
792 }
793 myLocalHost = cpystr ((gethostname (tmp,MAILTMPLEN-1) == SOCKET_ERROR) ?
794 "random-pc" : tcp_canonical (tmp));
795 }
796 return myLocalHost;
797 }
800 /* Validate name
801 * Accepts: domain name
802 * Returns: T if valid, NIL otherwise
803 */
805 char *tcp_name_valid (char *s)
806 {
807 int c;
808 char *ret,*tail;
809 /* must be non-empty and not too long */
810 if ((ret = (s && *s) ? s : NIL) && (tail = ret + NETMAXHOST)) {
811 /* must be alnum, dot, or hyphen */
812 while ((c = *s++) && (s <= tail) &&
813 (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')) ||
814 ((c >= '0') && (c <= '9')) || (c == '-') || (c == '.')));
815 if (c) ret = NIL;
816 }
817 return ret;
818 }

UW-IMAP'd extensions by yuuji