imapext-2007
diff src/osdep/amiga/tcp_ami.c @ 0:ada5e610ab86
imap-2007e
author | yuuji@gentei.org |
---|---|
date | Mon, 14 Sep 2009 15:17:45 +0900 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/osdep/amiga/tcp_ami.c Mon Sep 14 15:17:45 2009 +0900 1.3 @@ -0,0 +1,797 @@ 1.4 +/* ======================================================================== 1.5 + * Copyright 1988-2008 University of Washington 1.6 + * 1.7 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.8 + * you may not use this file except in compliance with the License. 1.9 + * You may obtain a copy of the License at 1.10 + * 1.11 + * http://www.apache.org/licenses/LICENSE-2.0 1.12 + * 1.13 + * 1.14 + * ======================================================================== 1.15 + */ 1.16 + 1.17 +/* 1.18 + * Program: Amiga TCP/IP routines 1.19 + * 1.20 + * Author: Mark Crispin 1.21 + * Networks and Distributed Computing 1.22 + * Computing & Communications 1.23 + * University of Washington 1.24 + * Administration Building, AG-44 1.25 + * Seattle, WA 98195 1.26 + * Internet: MRC@CAC.Washington.EDU 1.27 + * 1.28 + * Date: 1 August 1988 1.29 + * Last Edited: 13 January 2008 1.30 + */ 1.31 + 1.32 +#undef write /* don't use redefined write() */ 1.33 + 1.34 +static tcptimeout_t tmoh = NIL; /* TCP timeout handler routine */ 1.35 +static long ttmo_open = 0; /* TCP timeouts, in seconds */ 1.36 +static long ttmo_read = 0; 1.37 +static long ttmo_write = 0; 1.38 +static long allowreversedns = T;/* allow reverse DNS lookup */ 1.39 +static long tcpdebug = NIL; /* extra TCP debugging telemetry */ 1.40 + 1.41 +extern long maxposint; /* get this from write.c */ 1.42 + 1.43 +/* Local function prototypes */ 1.44 + 1.45 +int tcp_socket_open (struct sockaddr_in *sin,char *tmp,int *ctr,char *hst, 1.46 + unsigned long port); 1.47 +static char *tcp_getline_work (TCPSTREAM *stream,unsigned long *size, 1.48 + long *contd); 1.49 +long tcp_abort (TCPSTREAM *stream); 1.50 +char *tcp_name (struct sockaddr_in *sin,long flag); 1.51 +char *tcp_name_valid (char *s); 1.52 + 1.53 +/* TCP/IP manipulate parameters 1.54 + * Accepts: function code 1.55 + * function-dependent value 1.56 + * Returns: function-dependent return value 1.57 + */ 1.58 + 1.59 +void *tcp_parameters (long function,void *value) 1.60 +{ 1.61 + void *ret = NIL; 1.62 + switch ((int) function) { 1.63 + case SET_TIMEOUT: 1.64 + tmoh = (tcptimeout_t) value; 1.65 + case GET_TIMEOUT: 1.66 + ret = (void *) tmoh; 1.67 + break; 1.68 + case SET_OPENTIMEOUT: 1.69 + ttmo_open = (long) value; 1.70 + case GET_OPENTIMEOUT: 1.71 + ret = (void *) ttmo_open; 1.72 + break; 1.73 + case SET_READTIMEOUT: 1.74 + ttmo_read = (long) value; 1.75 + case GET_READTIMEOUT: 1.76 + ret = (void *) ttmo_read; 1.77 + break; 1.78 + case SET_WRITETIMEOUT: 1.79 + ttmo_write = (long) value; 1.80 + case GET_WRITETIMEOUT: 1.81 + ret = (void *) ttmo_write; 1.82 + break; 1.83 + case SET_ALLOWREVERSEDNS: 1.84 + allowreversedns = (long) value; 1.85 + case GET_ALLOWREVERSEDNS: 1.86 + ret = (void *) allowreversedns; 1.87 + break; 1.88 + case SET_TCPDEBUG: 1.89 + tcpdebug = (long) value; 1.90 + case GET_TCPDEBUG: 1.91 + ret = (void *) tcpdebug; 1.92 + break; 1.93 + } 1.94 + return ret; 1.95 +} 1.96 + 1.97 +/* TCP/IP open 1.98 + * Accepts: host name 1.99 + * contact service name 1.100 + * contact port number and optional silent flag 1.101 + * Returns: TCP/IP stream if success else NIL 1.102 + */ 1.103 + 1.104 +TCPSTREAM *tcp_open (char *host,char *service,unsigned long port) 1.105 +{ 1.106 + TCPSTREAM *stream = NIL; 1.107 + int i; 1.108 + int sock = -1; 1.109 + int ctr = 0; 1.110 + int silent = (port & NET_SILENT) ? T : NIL; 1.111 + int *ctrp = (port & NET_NOOPENTIMEOUT) ? NIL : &ctr; 1.112 + char *s; 1.113 + struct sockaddr_in sin; 1.114 + struct hostent *he; 1.115 + char hostname[MAILTMPLEN]; 1.116 + char tmp[MAILTMPLEN]; 1.117 + struct servent *sv = NIL; 1.118 + blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL); 1.119 + void *data; 1.120 + port &= 0xffff; /* erase flags */ 1.121 + /* lookup service */ 1.122 + if (service && (sv = getservbyname (service,"tcp"))) 1.123 + port = ntohs (sin.sin_port = sv->s_port); 1.124 + /* copy port number in network format */ 1.125 + else sin.sin_port = htons (port); 1.126 + /* The domain literal form is used (rather than simply the dotted decimal 1.127 + as with other Amiga programs) because it has to be a valid "host name" 1.128 + in mailsystem terminology. */ 1.129 + /* look like domain literal? */ 1.130 + if (host[0] == '[' && host[(strlen (host))-1] == ']') { 1.131 + strcpy (hostname,host+1); /* yes, copy number part */ 1.132 + hostname[(strlen (hostname))-1] = '\0'; 1.133 + if ((sin.sin_addr.s_addr = inet_addr (hostname)) == -1) 1.134 + sprintf (tmp,"Bad format domain-literal: %.80s",host); 1.135 + else { 1.136 + sin.sin_family = AF_INET; /* family is always Internet */ 1.137 + strcpy (hostname,host); /* hostname is user's argument */ 1.138 + (*bn) (BLOCK_TCPOPEN,NIL); 1.139 + /* get an open socket for this system */ 1.140 + sock = tcp_socket_open (&sin,tmp,ctrp,hostname,port); 1.141 + (*bn) (BLOCK_NONE,NIL); 1.142 + } 1.143 + } 1.144 + 1.145 + else { /* lookup host name */ 1.146 + if (tcpdebug) { 1.147 + sprintf (tmp,"DNS resolution %.80s",host); 1.148 + mm_log (tmp,TCPDEBUG); 1.149 + } 1.150 + (*bn) (BLOCK_DNSLOOKUP,NIL);/* quell alarms */ 1.151 + data = (*bn) (BLOCK_SENSITIVE,NIL); 1.152 + if (!(he = gethostbyname (lcase (strcpy (hostname,host))))) 1.153 + sprintf (tmp,"No such host as %.80s",host); 1.154 + (*bn) (BLOCK_NONSENSITIVE,data); 1.155 + (*bn) (BLOCK_NONE,NIL); 1.156 + if (he) { /* DNS resolution won? */ 1.157 + if (tcpdebug) mm_log ("DNS resolution done",TCPDEBUG); 1.158 + /* copy address type */ 1.159 + sin.sin_family = he->h_addrtype; 1.160 + /* copy host name */ 1.161 + strcpy (hostname,he->h_name); 1.162 +#ifdef HOST_NOT_FOUND /* muliple addresses only on DNS systems */ 1.163 + for (sock = -1,i = 0; (sock < 0) && (s = he->h_addr_list[i]); i++) { 1.164 + if (i && !silent) mm_log (tmp,WARN); 1.165 + memcpy (&sin.sin_addr,s,he->h_length); 1.166 + (*bn) (BLOCK_TCPOPEN,NIL); 1.167 + sock = tcp_socket_open (&sin,tmp,ctrp,hostname,port); 1.168 + (*bn) (BLOCK_NONE,NIL); 1.169 + } 1.170 +#else /* the one true address then */ 1.171 + memcpy (&sin.sin_addr,he->h_addr,he->h_length); 1.172 + (*bn) (BLOCK_TCPOPEN,NIL); 1.173 + sock = tcp_socket_open (&sin,tmp,ctrp,hostname,port); 1.174 + (*bn) (BLOCK_NONE,NIL); 1.175 +#endif 1.176 + } 1.177 + } 1.178 + if (sock >= 0) { /* won */ 1.179 + stream = (TCPSTREAM *) memset (fs_get (sizeof (TCPSTREAM)),0, 1.180 + sizeof (TCPSTREAM)); 1.181 + stream->port = port; /* port number */ 1.182 + /* init sockets */ 1.183 + stream->tcpsi = stream->tcpso = sock; 1.184 + /* stash in the snuck-in byte */ 1.185 + if (stream->ictr = ctr) *(stream->iptr = stream->ibuf) = tmp[0]; 1.186 + /* copy official host name */ 1.187 + stream->host = cpystr (hostname); 1.188 + if (tcpdebug) mm_log ("Stream open and ready for read",TCPDEBUG); 1.189 + } 1.190 + else if (!silent) mm_log (tmp,ERROR); 1.191 + return stream; /* return success */ 1.192 +} 1.193 + 1.194 +/* Open a TCP socket 1.195 + * Accepts: Internet socket address block 1.196 + * scratch buffer 1.197 + * pointer to "first byte read in" storage or NIL 1.198 + * host name for error message 1.199 + * port number for error message 1.200 + * Returns: socket if success, else -1 with error string in scratch buffer 1.201 + */ 1.202 + 1.203 +int tcp_socket_open (struct sockaddr_in *sin,char *tmp,int *ctr,char *hst, 1.204 + unsigned long port) 1.205 +{ 1.206 + int i,ti,sock,flgs; 1.207 + time_t now; 1.208 + struct protoent *pt = getprotobyname ("tcp"); 1.209 + fd_set fds,efds; 1.210 + struct timeval tmo; 1.211 + blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL); 1.212 + void *data = (*bn) (BLOCK_SENSITIVE,NIL); 1.213 + sprintf (tmp,"Trying IP address [%s]",inet_ntoa (sin->sin_addr)); 1.214 + mm_log (tmp,NIL); 1.215 + /* make a socket */ 1.216 + if ((sock = socket (sin->sin_family,SOCK_STREAM,pt ? pt->p_proto : 0)) < 0) { 1.217 + sprintf (tmp,"Unable to create TCP socket: %s",strerror (errno)); 1.218 + (*bn) (BLOCK_NONSENSITIVE,data); 1.219 + return -1; 1.220 + } 1.221 + else if (sock >= FD_SETSIZE) {/* unselectable sockets are useless */ 1.222 + sprintf (tmp,"Unable to create selectable TCP socket (%d >= %d)", 1.223 + sock,FD_SETSIZE); 1.224 + (*bn) (BLOCK_NONSENSITIVE,data); 1.225 + close (sock); 1.226 + errno = EMFILE; 1.227 + return -1; 1.228 + } 1.229 + flgs = fcntl (sock,F_GETFL,0);/* get current socket flags */ 1.230 + /* set non-blocking if want open timeout */ 1.231 + if (ctr) fcntl (sock,F_SETFL,flgs | FNDELAY); 1.232 + /* open connection */ 1.233 + while ((i = connect (sock,(struct sockaddr *) sin, 1.234 + sizeof (struct sockaddr_in))) < 0 && (errno == EINTR)); 1.235 + (*bn) (BLOCK_NONSENSITIVE,data); 1.236 + if (i < 0) switch (errno) { /* failed? */ 1.237 + case EAGAIN: /* DG brain damage */ 1.238 + case EINPROGRESS: /* what we expect to happen */ 1.239 + case EALREADY: /* or another form of it */ 1.240 + case EISCONN: /* restart after interrupt? */ 1.241 + case EADDRINUSE: /* restart after interrupt? */ 1.242 + break; /* well, not really, it was interrupted */ 1.243 + default: 1.244 + sprintf (tmp,"Can't connect to %.80s,%lu: %s",hst,port,strerror (errno)); 1.245 + close (sock); /* flush socket */ 1.246 + return -1; 1.247 + } 1.248 + 1.249 + if (ctr) { /* want open timeout */ 1.250 + now = time (0); /* open timeout */ 1.251 + ti = ttmo_open ? now + ttmo_open : 0; 1.252 + tmo.tv_usec = 0; 1.253 + FD_ZERO (&fds); /* initialize selection vector */ 1.254 + FD_ZERO (&efds); /* handle errors too */ 1.255 + FD_SET (sock,&fds); /* block for error or readable */ 1.256 + FD_SET (sock,&efds); 1.257 + do { /* block under timeout */ 1.258 + tmo.tv_sec = ti ? ti - now : 0; 1.259 + i = select (sock+1,&fds,0,&efds,ti ? &tmo : 0); 1.260 + now = time (0); /* fake timeout if interrupt & time expired */ 1.261 + if ((i < 0) && (errno == EINTR) && ti && (ti <= now)) i = 0; 1.262 + } while ((i < 0) && (errno == EINTR)); 1.263 + if (i > 0) { /* success, make sure really connected */ 1.264 + fcntl (sock,F_SETFL,flgs);/* restore blocking status */ 1.265 + /* This used to be a zero-byte read(), but that crashes Solaris */ 1.266 + /* get socket status */ 1.267 + while (((i = *ctr = read (sock,tmp,1)) < 0) && (errno == EINTR)); 1.268 + } 1.269 + if (i <= 0) { /* timeout or error? */ 1.270 + i = i ? errno : ETIMEDOUT;/* determine error code */ 1.271 + close (sock); /* flush socket */ 1.272 + errno = i; /* return error code */ 1.273 + sprintf (tmp,"Connection failed to %.80s,%lu: %s",hst,port, 1.274 + strerror (errno)); 1.275 + return -1; 1.276 + } 1.277 + } 1.278 + return sock; /* return the socket */ 1.279 +} 1.280 + 1.281 +/* TCP/IP authenticated open 1.282 + * Accepts: host name 1.283 + * service name 1.284 + * returned user name buffer 1.285 + * Returns: TCP/IP stream if success else NIL 1.286 + */ 1.287 + 1.288 +TCPSTREAM *tcp_aopen (NETMBX *mb,char *service,char *usrbuf) 1.289 +{ 1.290 + return NIL; /* disabled */ 1.291 +} 1.292 + 1.293 +/* TCP receive line 1.294 + * Accepts: TCP stream 1.295 + * Returns: text line string or NIL if failure 1.296 + */ 1.297 + 1.298 +char *tcp_getline (TCPSTREAM *stream) 1.299 +{ 1.300 + unsigned long n,contd; 1.301 + char *ret = tcp_getline_work (stream,&n,&contd); 1.302 + if (ret && contd) { /* got a line needing continuation? */ 1.303 + STRINGLIST *stl = mail_newstringlist (); 1.304 + STRINGLIST *stc = stl; 1.305 + do { /* collect additional lines */ 1.306 + stc->text.data = (unsigned char *) ret; 1.307 + stc->text.size = n; 1.308 + stc = stc->next = mail_newstringlist (); 1.309 + ret = tcp_getline_work (stream,&n,&contd); 1.310 + } while (ret && contd); 1.311 + if (ret) { /* stash final part of line on list */ 1.312 + stc->text.data = (unsigned char *) ret; 1.313 + stc->text.size = n; 1.314 + /* determine how large a buffer we need */ 1.315 + for (n = 0, stc = stl; stc; stc = stc->next) n += stc->text.size; 1.316 + ret = fs_get (n + 1); /* copy parts into buffer */ 1.317 + for (n = 0, stc = stl; stc; n += stc->text.size, stc = stc->next) 1.318 + memcpy (ret + n,stc->text.data,stc->text.size); 1.319 + ret[n] = '\0'; 1.320 + } 1.321 + mail_free_stringlist (&stl);/* either way, done with list */ 1.322 + } 1.323 + return ret; 1.324 +} 1.325 + 1.326 +/* TCP receive line or partial line 1.327 + * Accepts: TCP stream 1.328 + * pointer to return size 1.329 + * pointer to return continuation flag 1.330 + * Returns: text line string, size and continuation flag, or NIL if failure 1.331 + */ 1.332 + 1.333 +static char *tcp_getline_work (TCPSTREAM *stream,unsigned long *size, 1.334 + long *contd) 1.335 +{ 1.336 + unsigned long n; 1.337 + char *s,*ret,c,d; 1.338 + *contd = NIL; /* assume no continuation */ 1.339 + /* make sure have data */ 1.340 + if (!tcp_getdata (stream)) return NIL; 1.341 + for (s = stream->iptr, n = 0, c = '\0'; stream->ictr--; n++, c = d) { 1.342 + d = *stream->iptr++; /* slurp another character */ 1.343 + if ((c == '\015') && (d == '\012')) { 1.344 + ret = (char *) fs_get (n--); 1.345 + memcpy (ret,s,*size = n); /* copy into a free storage string */ 1.346 + ret[n] = '\0'; /* tie off string with null */ 1.347 + return ret; 1.348 + } 1.349 + } 1.350 + /* copy partial string from buffer */ 1.351 + memcpy ((ret = (char *) fs_get (n)),s,*size = n); 1.352 + /* get more data from the net */ 1.353 + if (!tcp_getdata (stream)) fs_give ((void **) &ret); 1.354 + /* special case of newline broken by buffer */ 1.355 + else if ((c == '\015') && (*stream->iptr == '\012')) { 1.356 + stream->iptr++; /* eat the line feed */ 1.357 + stream->ictr--; 1.358 + ret[*size = --n] = '\0'; /* tie off string with null */ 1.359 + } 1.360 + else *contd = LONGT; /* continuation needed */ 1.361 + return ret; 1.362 +} 1.363 + 1.364 +/* TCP/IP receive buffer 1.365 + * Accepts: TCP/IP stream 1.366 + * size in bytes 1.367 + * buffer to read into 1.368 + * Returns: T if success, NIL otherwise 1.369 + */ 1.370 + 1.371 +long tcp_getbuffer (TCPSTREAM *stream,unsigned long size,char *s) 1.372 +{ 1.373 + unsigned long n; 1.374 + /* make sure socket still alive */ 1.375 + if (stream->tcpsi < 0) return NIL; 1.376 + /* can transfer bytes from buffer? */ 1.377 + if (n = min (size,stream->ictr)) { 1.378 + memcpy (s,stream->iptr,n); /* yes, slurp as much as we can from it */ 1.379 + s += n; /* update pointer */ 1.380 + stream->iptr +=n; 1.381 + size -= n; /* update # of bytes to do */ 1.382 + stream->ictr -=n; 1.383 + } 1.384 + if (size) { 1.385 + int i; 1.386 + fd_set fds,efds; 1.387 + struct timeval tmo; 1.388 + time_t t = time (0); 1.389 + blocknotify_t bn=(blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL); 1.390 + (*bn) (BLOCK_TCPREAD,NIL); 1.391 + while (size > 0) { /* until request satisfied */ 1.392 + time_t tl = time (0); 1.393 + time_t now = tl; 1.394 + time_t ti = ttmo_read ? now + ttmo_read : 0; 1.395 + if (tcpdebug) mm_log ("Reading TCP buffer",TCPDEBUG); 1.396 + tmo.tv_usec = 0; 1.397 + FD_ZERO (&fds); /* initialize selection vector */ 1.398 + FD_ZERO (&efds); /* handle errors too */ 1.399 + FD_SET (stream->tcpsi,&fds); 1.400 + FD_SET (stream->tcpsi,&efds); 1.401 + errno = NIL; /* block and read */ 1.402 + do { /* block under timeout */ 1.403 + tmo.tv_sec = ti ? ti - now : 0; 1.404 + i = select (stream->tcpsi+1,&fds,0,&efds,ti ? &tmo : 0); 1.405 + now = time (0); /* fake timeout if interrupt & time expired */ 1.406 + if ((i < 0) && (errno == EINTR) && ti && (ti <= now)) i = 0; 1.407 + } while ((i < 0) && (errno == EINTR)); 1.408 + if (i > 0) { /* select says there's data to read? */ 1.409 + while (((i = read (stream->tcpsi,s,(int) min (maxposint,size))) < 0) && 1.410 + (errno == EINTR)); 1.411 + if (i < 1) return tcp_abort (stream); 1.412 + s += i; /* point at new place to write */ 1.413 + size -= i; /* reduce byte count */ 1.414 + if (tcpdebug) mm_log ("Successfully read TCP buffer",TCPDEBUG); 1.415 + } 1.416 + else if (i || !tmoh || !(*tmoh) (now - t,now - tl)) 1.417 + return tcp_abort (stream); 1.418 + } 1.419 + (*bn) (BLOCK_NONE,NIL); 1.420 + } 1.421 + *s = '\0'; /* tie off string */ 1.422 + return T; 1.423 +} 1.424 + 1.425 +/* TCP/IP receive data 1.426 + * Accepts: TCP/IP stream 1.427 + * Returns: T if success, NIL otherwise 1.428 + */ 1.429 + 1.430 +long tcp_getdata (TCPSTREAM *stream) 1.431 +{ 1.432 + int i; 1.433 + fd_set fds,efds; 1.434 + struct timeval tmo; 1.435 + time_t t = time (0); 1.436 + blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL); 1.437 + if (stream->tcpsi < 0) return NIL; 1.438 + (*bn) (BLOCK_TCPREAD,NIL); 1.439 + while (stream->ictr < 1) { /* if nothing in the buffer */ 1.440 + time_t tl = time (0); /* start of request */ 1.441 + time_t now = tl; 1.442 + time_t ti = ttmo_read ? now + ttmo_read : 0; 1.443 + if (tcpdebug) mm_log ("Reading TCP data",TCPDEBUG); 1.444 + tmo.tv_usec = 0; 1.445 + FD_ZERO (&fds); /* initialize selection vector */ 1.446 + FD_ZERO (&efds); /* handle errors too */ 1.447 + FD_SET (stream->tcpsi,&fds);/* set bit in selection vector */ 1.448 + FD_SET(stream->tcpsi,&efds);/* set bit in error selection vector */ 1.449 + errno = NIL; /* block and read */ 1.450 + do { /* block under timeout */ 1.451 + tmo.tv_sec = ti ? ti - now : 0; 1.452 + i = select (stream->tcpsi+1,&fds,0,&efds,ti ? &tmo : 0); 1.453 + now = time (0); /* fake timeout if interrupt & time expired */ 1.454 + if ((i < 0) && (errno == EINTR) && ti && (ti <= now)) i = 0; 1.455 + } while ((i < 0) && (errno == EINTR)); 1.456 + if (i > 0) { /* got data? */ 1.457 + while (((i = read (stream->tcpsi,stream->ibuf,BUFLEN)) < 0) && 1.458 + (errno == EINTR)); 1.459 + if (i < 1) return tcp_abort (stream); 1.460 + stream->iptr = stream->ibuf;/* point at TCP buffer */ 1.461 + stream->ictr = i; /* set new byte count */ 1.462 + if (tcpdebug) mm_log ("Successfully read TCP data",TCPDEBUG); 1.463 + } 1.464 + else if (i || !tmoh || !(*tmoh) (now - t,now - tl)) 1.465 + return tcp_abort (stream);/* error or timeout no-continue */ 1.466 + } 1.467 + (*bn) (BLOCK_NONE,NIL); 1.468 + return T; 1.469 +} 1.470 + 1.471 +/* TCP/IP send string as record 1.472 + * Accepts: TCP/IP stream 1.473 + * string pointer 1.474 + * Returns: T if success else NIL 1.475 + */ 1.476 + 1.477 +long tcp_soutr (TCPSTREAM *stream,char *string) 1.478 +{ 1.479 + return tcp_sout (stream,string,(unsigned long) strlen (string)); 1.480 +} 1.481 + 1.482 + 1.483 +/* TCP/IP send string 1.484 + * Accepts: TCP/IP stream 1.485 + * string pointer 1.486 + * byte count 1.487 + * Returns: T if success else NIL 1.488 + */ 1.489 + 1.490 +long tcp_sout (TCPSTREAM *stream,char *string,unsigned long size) 1.491 +{ 1.492 + int i; 1.493 + fd_set fds,efds; 1.494 + struct timeval tmo; 1.495 + time_t t = time (0); 1.496 + blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL); 1.497 + if (stream->tcpso < 0) return NIL; 1.498 + (*bn) (BLOCK_TCPWRITE,NIL); 1.499 + while (size > 0) { /* until request satisfied */ 1.500 + time_t tl = time (0); /* start of request */ 1.501 + time_t now = tl; 1.502 + time_t ti = ttmo_write ? now + ttmo_write : 0; 1.503 + if (tcpdebug) mm_log ("Writing to TCP",TCPDEBUG); 1.504 + tmo.tv_usec = 0; 1.505 + FD_ZERO (&fds); /* initialize selection vector */ 1.506 + FD_ZERO (&efds); /* handle errors too */ 1.507 + FD_SET (stream->tcpso,&fds);/* set bit in selection vector */ 1.508 + FD_SET(stream->tcpso,&efds);/* set bit in error selection vector */ 1.509 + errno = NIL; /* block and write */ 1.510 + do { /* block under timeout */ 1.511 + tmo.tv_sec = ti ? ti - now : 0; 1.512 + i = select (stream->tcpso+1,0,&fds,&efds,ti ? &tmo : 0); 1.513 + now = time (0); /* fake timeout if interrupt & time expired */ 1.514 + if ((i < 0) && (errno == EINTR) && ti && (ti <= now)) i = 0; 1.515 + } while ((i < 0) && (errno == EINTR)); 1.516 + if (i > 0) { /* OK to send data? */ 1.517 + while (((i = write (stream->tcpso,string,size)) < 0) &&(errno == EINTR)); 1.518 + if (i < 0) return tcp_abort (stream); 1.519 + size -= i; /* how much we sent */ 1.520 + string += i; 1.521 + if (tcpdebug) mm_log ("successfully wrote to TCP",TCPDEBUG); 1.522 + } 1.523 + else if (i || !tmoh || !(*tmoh) (now - t,now - tl)) 1.524 + return tcp_abort (stream);/* error or timeout no-continue */ 1.525 + } 1.526 + (*bn) (BLOCK_NONE,NIL); 1.527 + return T; /* all done */ 1.528 +} 1.529 + 1.530 +/* TCP/IP close 1.531 + * Accepts: TCP/IP stream 1.532 + */ 1.533 + 1.534 +void tcp_close (TCPSTREAM *stream) 1.535 +{ 1.536 + tcp_abort (stream); /* nuke the stream */ 1.537 + /* flush host names */ 1.538 + if (stream->host) fs_give ((void **) &stream->host); 1.539 + if (stream->remotehost) fs_give ((void **) &stream->remotehost); 1.540 + if (stream->localhost) fs_give ((void **) &stream->localhost); 1.541 + fs_give ((void **) &stream); /* flush the stream */ 1.542 +} 1.543 + 1.544 + 1.545 +/* TCP/IP abort stream 1.546 + * Accepts: TCP/IP stream 1.547 + * Returns: NIL always 1.548 + */ 1.549 + 1.550 +long tcp_abort (TCPSTREAM *stream) 1.551 +{ 1.552 + blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL); 1.553 + if (stream->tcpsi >= 0) { /* no-op if no socket */ 1.554 + (*bn) (BLOCK_TCPCLOSE,NIL); 1.555 + close (stream->tcpsi); /* nuke the socket */ 1.556 + if (stream->tcpsi != stream->tcpso) close (stream->tcpso); 1.557 + stream->tcpsi = stream->tcpso = -1; 1.558 + } 1.559 + (*bn) (BLOCK_NONE,NIL); 1.560 + return NIL; 1.561 +} 1.562 + 1.563 +/* TCP/IP get host name 1.564 + * Accepts: TCP/IP stream 1.565 + * Returns: host name for this stream 1.566 + */ 1.567 + 1.568 +char *tcp_host (TCPSTREAM *stream) 1.569 +{ 1.570 + return stream->host; /* use tcp_remotehost() if want guarantees */ 1.571 +} 1.572 + 1.573 + 1.574 +/* TCP/IP get remote host name 1.575 + * Accepts: TCP/IP stream 1.576 + * Returns: host name for this stream 1.577 + */ 1.578 + 1.579 +char *tcp_remotehost (TCPSTREAM *stream) 1.580 +{ 1.581 + if (!stream->remotehost) { 1.582 + struct sockaddr_in sin; 1.583 + int sinlen = sizeof (struct sockaddr_in); 1.584 + stream->remotehost = /* get socket's peer name */ 1.585 + (getpeername (stream->tcpsi,(struct sockaddr *) &sin,(void *) &sinlen) || 1.586 + (sin.sin_family != AF_INET)) ? 1.587 + cpystr (stream->host) : tcp_name (&sin,NIL); 1.588 + } 1.589 + return stream->remotehost; 1.590 +} 1.591 + 1.592 + 1.593 +/* TCP/IP return port for this stream 1.594 + * Accepts: TCP/IP stream 1.595 + * Returns: port number for this stream 1.596 + */ 1.597 + 1.598 +unsigned long tcp_port (TCPSTREAM *stream) 1.599 +{ 1.600 + return stream->port; /* return port number */ 1.601 +} 1.602 + 1.603 + 1.604 +/* TCP/IP get local host name 1.605 + * Accepts: TCP/IP stream 1.606 + * Returns: local host name 1.607 + */ 1.608 + 1.609 +char *tcp_localhost (TCPSTREAM *stream) 1.610 +{ 1.611 + if (!stream->localhost) { 1.612 + struct sockaddr_in sin; 1.613 + int sinlen = sizeof (struct sockaddr_in); 1.614 + stream->localhost = /* get socket's name */ 1.615 + ((stream->port & 0xffff000) || 1.616 + getsockname (stream->tcpsi,(struct sockaddr *) &sin,(void *) &sinlen) || 1.617 + (sin.sin_family != AF_INET)) ? 1.618 + cpystr (mylocalhost ()) : tcp_name (&sin,NIL); 1.619 + } 1.620 + return stream->localhost; /* return local host name */ 1.621 +} 1.622 + 1.623 +/* TCP/IP get client host address (server calls only) 1.624 + * Returns: client host address 1.625 + */ 1.626 + 1.627 +static char *myClientAddr = NIL; 1.628 + 1.629 +char *tcp_clientaddr () 1.630 +{ 1.631 + if (!myClientAddr) { 1.632 + struct sockaddr_in sin; 1.633 + int sinlen = sizeof (struct sockaddr_in); 1.634 + myClientAddr = /* get stdin's peer name */ 1.635 + cpystr (getpeername (0,(struct sockaddr *) &sin,(void *) &sinlen) ? 1.636 + "UNKNOWN" : ((sin.sin_family == AF_INET) ? 1.637 + inet_ntoa (sin.sin_addr) : "NON-IPv4")); 1.638 + } 1.639 + return myClientAddr; 1.640 +} 1.641 + 1.642 + 1.643 +/* TCP/IP get client host name (server calls only) 1.644 + * Returns: client host name 1.645 + */ 1.646 + 1.647 +static char *myClientHost = NIL; 1.648 + 1.649 +char *tcp_clienthost () 1.650 +{ 1.651 + if (!myClientHost) { 1.652 + struct sockaddr_in sin; 1.653 + int sinlen = sizeof (struct sockaddr_in); 1.654 + myClientHost = /* get stdin's peer name */ 1.655 + getpeername (0,(struct sockaddr *) &sin,(void *) &sinlen) ? 1.656 + cpystr ("UNKNOWN") : ((sin.sin_family == AF_INET) ? 1.657 + tcp_name (&sin,T) : cpystr ("NON-IPv4")); 1.658 + } 1.659 + return myClientHost; 1.660 +} 1.661 + 1.662 +/* TCP/IP get server host address (server calls only) 1.663 + * Returns: server host address 1.664 + */ 1.665 + 1.666 +static char *myServerAddr = NIL; 1.667 + 1.668 +char *tcp_serveraddr () 1.669 +{ 1.670 + if (!myServerAddr) { 1.671 + struct sockaddr_in sin; 1.672 + int sinlen = sizeof (struct sockaddr_in); 1.673 + myServerAddr = /* get stdin's peer name */ 1.674 + cpystr (getsockname (0,(struct sockaddr *) &sin,(void *) &sinlen) ? 1.675 + "UNKNOWN" : ((sin.sin_family == AF_INET) ? 1.676 + inet_ntoa (sin.sin_addr) : "NON-IPv4")); 1.677 + } 1.678 + return myServerAddr; 1.679 +} 1.680 + 1.681 + 1.682 +/* TCP/IP get server host name (server calls only) 1.683 + * Returns: server host name 1.684 + */ 1.685 + 1.686 +static char *myServerHost = NIL; 1.687 +static long myServerPort = -1; 1.688 + 1.689 +char *tcp_serverhost () 1.690 +{ 1.691 + if (!myServerHost) { 1.692 + struct sockaddr_in sin; 1.693 + int sinlen = sizeof (struct sockaddr_in); 1.694 + /* get stdin's name */ 1.695 + if (getsockname (0,(struct sockaddr *) &sin,(void *) &sinlen) || 1.696 + (sin.sin_family != AF_INET)) myServerHost = cpystr (mylocalhost ()); 1.697 + else { 1.698 + myServerHost = tcp_name (&sin,NIL); 1.699 + myServerPort = ntohs (sin.sin_port); 1.700 + } 1.701 + } 1.702 + return myServerHost; 1.703 +} 1.704 + 1.705 + 1.706 +/* TCP/IP get server port number (server calls only) 1.707 + * Returns: server port number 1.708 + */ 1.709 + 1.710 +long tcp_serverport () 1.711 +{ 1.712 + if (!myServerHost) tcp_serverhost (); 1.713 + return myServerPort; 1.714 +} 1.715 + 1.716 +/* TCP/IP return canonical form of host name 1.717 + * Accepts: host name 1.718 + * Returns: canonical form of host name 1.719 + */ 1.720 + 1.721 +char *tcp_canonical (char *name) 1.722 +{ 1.723 + char *ret,host[MAILTMPLEN]; 1.724 + struct hostent *he; 1.725 + blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL); 1.726 + void *data; 1.727 + /* look like domain literal? */ 1.728 + if (name[0] == '[' && name[strlen (name) - 1] == ']') return name; 1.729 + (*bn) (BLOCK_DNSLOOKUP,NIL); /* quell alarms */ 1.730 + data = (*bn) (BLOCK_SENSITIVE,NIL); 1.731 + if (tcpdebug) { 1.732 + sprintf (host,"DNS canonicalization %.80s",name); 1.733 + mm_log (host,TCPDEBUG); 1.734 + } 1.735 + /* note that Amiga requires lowercase! */ 1.736 + ret = (he = gethostbyname (lcase (strcpy (host,name)))) ? 1.737 + (char *) he->h_name : name; 1.738 + (*bn) (BLOCK_NONSENSITIVE,data); 1.739 + (*bn) (BLOCK_NONE,NIL); /* alarms OK now */ 1.740 + if (tcpdebug) mm_log ("DNS canonicalization done",TCPDEBUG); 1.741 + return ret; 1.742 +} 1.743 + 1.744 + 1.745 +/* TCP/IP return name from socket 1.746 + * Accepts: socket 1.747 + * verbose flag 1.748 + * Returns: cpystr name 1.749 + */ 1.750 + 1.751 +char *tcp_name (struct sockaddr_in *sin,long flag) 1.752 +{ 1.753 + char *ret,*t,adr[MAILTMPLEN],tmp[MAILTMPLEN]; 1.754 + sprintf (ret = adr,"[%.80s]",inet_ntoa (sin->sin_addr)); 1.755 + if (allowreversedns) { 1.756 + struct hostent *he; 1.757 + blocknotify_t bn = (blocknotify_t)mail_parameters(NIL,GET_BLOCKNOTIFY,NIL); 1.758 + void *data; 1.759 + if (tcpdebug) { 1.760 + sprintf (tmp,"Reverse DNS resolution %s",adr); 1.761 + mm_log (tmp,TCPDEBUG); 1.762 + } 1.763 + (*bn) (BLOCK_DNSLOOKUP,NIL);/* quell alarms */ 1.764 + data = (*bn) (BLOCK_SENSITIVE,NIL); 1.765 + /* translate address to name */ 1.766 + if (t = tcp_name_valid ((he = gethostbyaddr ((char *) &sin->sin_addr, 1.767 + sizeof (struct in_addr), 1.768 + sin->sin_family)) ? 1.769 + (char *) he->h_name : NIL)) { 1.770 + /* produce verbose form if needed */ 1.771 + if (flag) sprintf (ret = tmp,"%s %s",t,adr); 1.772 + else ret = t; 1.773 + } 1.774 + (*bn) (BLOCK_NONSENSITIVE,data); 1.775 + (*bn) (BLOCK_NONE,NIL); /* alarms OK now */ 1.776 + if (tcpdebug) mm_log ("Reverse DNS resolution done",TCPDEBUG); 1.777 + } 1.778 + return cpystr (ret); 1.779 +} 1.780 + 1.781 + 1.782 +/* Validate name 1.783 + * Accepts: domain name 1.784 + * Returns: T if valid, NIL otherwise 1.785 + */ 1.786 + 1.787 +char *tcp_name_valid (char *s) 1.788 +{ 1.789 + int c; 1.790 + char *ret,*tail; 1.791 + /* must be non-empty and not too long */ 1.792 + if ((ret = (s && *s) ? s : NIL) && (tail = ret + NETMAXHOST)) { 1.793 + /* must be alnum, dot, or hyphen */ 1.794 + while ((c = *s++) && (s <= tail) && 1.795 + (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')) || 1.796 + ((c >= '0') && (c <= '9')) || (c == '-') || (c == '.'))); 1.797 + if (c) ret = NIL; 1.798 + } 1.799 + return ret; 1.800 +}