imapext-2007
diff src/osdep/dos/tcp_dos.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/dos/tcp_dos.c Mon Sep 14 15:17:45 2009 +0900 1.3 @@ -0,0 +1,434 @@ 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: MS-DOS 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: 11 April 1989 1.29 + * Last Edited: 13 January 2008 1.30 + */ 1.31 + 1.32 +static tcptimeout_t tmoh = NIL; /* TCP timeout handler routine */ 1.33 +static long ttmo_read = 0; /* TCP timeouts, in seconds */ 1.34 +static long ttmo_write = 0; 1.35 + 1.36 +static char *tcp_getline_work (TCPSTREAM *stream,unsigned long *size, 1.37 + long *contd); 1.38 + 1.39 +/* TCP/IP manipulate parameters 1.40 + * Accepts: function code 1.41 + * function-dependent value 1.42 + * Returns: function-dependent return value 1.43 + */ 1.44 + 1.45 +void *tcp_parameters (long function,void *value) 1.46 +{ 1.47 + void *ret = NIL; 1.48 + switch ((int) function) { 1.49 + case SET_TIMEOUT: 1.50 + tmoh = (tcptimeout_t) value; 1.51 + case GET_TIMEOUT: 1.52 + ret = (void *) tmoh; 1.53 + break; 1.54 + case SET_READTIMEOUT: 1.55 + ttmo_read = (long) value; 1.56 + case GET_READTIMEOUT: 1.57 + ret = (void *) ttmo_read; 1.58 + break; 1.59 + case SET_WRITETIMEOUT: 1.60 + ttmo_write = (long) value; 1.61 + case GET_WRITETIMEOUT: 1.62 + ret = (void *) ttmo_write; 1.63 + break; 1.64 + } 1.65 + return ret; 1.66 +} 1.67 + 1.68 +/* TCP/IP open 1.69 + * Accepts: host name 1.70 + * contact service name 1.71 + * contact port number 1.72 + * Returns: TCP/IP stream if success else NIL 1.73 + */ 1.74 + 1.75 +TCPSTREAM *tcp_open (char *host,char *service,unsigned long port) 1.76 +{ 1.77 + TCPSTREAM *stream = NIL; 1.78 + struct sockaddr_in sin; 1.79 + int sock; 1.80 + char *s,tmp[MAILTMPLEN]; 1.81 + port &= 0xffff; /* erase flags */ 1.82 + /* The domain literal form is used (rather than simply the dotted decimal 1.83 + as with other Unix programs) because it has to be a valid "host name" 1.84 + in mailsystem terminology. */ 1.85 + sin.sin_family = AF_INET; /* family is always Internet */ 1.86 + /* look like domain literal? */ 1.87 + if (host[0] == '[' && host[(strlen (host))-1] == ']') { 1.88 + strcpy (tmp,host+1); /* yes, copy number part */ 1.89 + tmp[strlen (tmp)-1] = '\0'; 1.90 + if ((sin.sin_addr.s_addr = inet_addr (tmp)) == -1) { 1.91 + sprintf (tmp,"Bad format domain-literal: %.80s",host); 1.92 + mm_log (tmp,ERROR); 1.93 + return NIL; 1.94 + } 1.95 + } 1.96 + /* look up host name */ 1.97 + else if (!lookuphost (&host,&sin)) { 1.98 + sprintf (tmp,"Host not found: %s",host); 1.99 + mm_log (tmp,ERROR); 1.100 + return NIL; 1.101 + } 1.102 + 1.103 + /* copy port number in network format */ 1.104 + if (!(sin.sin_port = htons (port))) fatal ("Bad port argument to tcp_open"); 1.105 + /* get a TCP stream */ 1.106 + if ((sock = socket (sin.sin_family,SOCK_STREAM,0)) < 0) { 1.107 + sprintf (tmp,"Unable to create TCP socket (%d)",errno); 1.108 + mm_log (tmp,ERROR); 1.109 + fs_give ((void **) &host); 1.110 + return NIL; 1.111 + } 1.112 +#if 0 1.113 + /* needed? */ 1.114 + else if (sock >= FD_SETSIZE) {/* unselectable sockets are useless */ 1.115 + sprintf (tmp,"Unable to create selectable TCP socket (%d >= %d)", 1.116 + sock,FD_SETSIZE); 1.117 + close (sock); 1.118 + errno = ENOBUFS; /* just in case */ 1.119 + return NIL; 1.120 + } 1.121 +#endif 1.122 + /* open connection */ 1.123 + if (connect (sock,(struct sockaddr *) &sin,sizeof (sin)) < 0) { 1.124 + switch (errno) { /* analyze error */ 1.125 + case ECONNREFUSED: 1.126 + s = "Refused"; 1.127 + break; 1.128 + case ENOBUFS: 1.129 + s = "Insufficient system resources"; 1.130 + break; 1.131 + case ETIMEDOUT: 1.132 + s = "Timed out"; 1.133 + break; 1.134 + default: 1.135 + s = "Unknown error"; 1.136 + break; 1.137 + } 1.138 + sprintf (tmp,"Can't connect to %.80s,%ld: %s (%d)",host,port,s,errno); 1.139 + mm_log (tmp,ERROR); 1.140 + close (sock); 1.141 + fs_give ((void **) &host); 1.142 + return NIL; 1.143 + } 1.144 + /* create TCP/IP stream */ 1.145 + stream = (TCPSTREAM *) fs_get (sizeof (TCPSTREAM)); 1.146 + stream->host = host; /* official host name */ 1.147 + stream->localhost = cpystr (mylocalhost ()); 1.148 + stream->port = port; /* port number */ 1.149 + stream->tcps = sock; /* init socket */ 1.150 + stream->ictr = 0; /* init input counter */ 1.151 + return stream; /* return success */ 1.152 +} 1.153 + 1.154 +/* TCP/IP authenticated open 1.155 + * Accepts: NETMBX specifier 1.156 + * service name 1.157 + * returned user name buffer 1.158 + * Returns: TCP/IP stream if success else NIL 1.159 + */ 1.160 + 1.161 +TCPSTREAM *tcp_aopen (NETMBX *mb,char *service,char *usrbuf) 1.162 +{ 1.163 + return NIL; /* always NIL on DOS */ 1.164 +} 1.165 + 1.166 +/* TCP receive line 1.167 + * Accepts: TCP stream 1.168 + * Returns: text line string or NIL if failure 1.169 + */ 1.170 + 1.171 +char *tcp_getline (TCPSTREAM *stream) 1.172 +{ 1.173 + unsigned long n,contd; 1.174 + char *ret = tcp_getline_work (stream,&n,&contd); 1.175 + if (ret && contd) { /* got a line needing continuation? */ 1.176 + STRINGLIST *stl = mail_newstringlist (); 1.177 + STRINGLIST *stc = stl; 1.178 + do { /* collect additional lines */ 1.179 + stc->text.data = (unsigned char *) ret; 1.180 + stc->text.size = n; 1.181 + stc = stc->next = mail_newstringlist (); 1.182 + ret = tcp_getline_work (stream,&n,&contd); 1.183 + } while (ret && contd); 1.184 + if (ret) { /* stash final part of line on list */ 1.185 + stc->text.data = (unsigned char *) ret; 1.186 + stc->text.size = n; 1.187 + /* determine how large a buffer we need */ 1.188 + for (n = 0, stc = stl; stc; stc = stc->next) n += stc->text.size; 1.189 + ret = fs_get (n + 1); /* copy parts into buffer */ 1.190 + for (n = 0, stc = stl; stc; n += stc->text.size, stc = stc->next) 1.191 + memcpy (ret + n,stc->text.data,stc->text.size); 1.192 + ret[n] = '\0'; 1.193 + } 1.194 + mail_free_stringlist (&stl);/* either way, done with list */ 1.195 + } 1.196 + return ret; 1.197 +} 1.198 + 1.199 +/* TCP receive line or partial line 1.200 + * Accepts: TCP stream 1.201 + * pointer to return size 1.202 + * pointer to return continuation flag 1.203 + * Returns: text line string, size and continuation flag, or NIL if failure 1.204 + */ 1.205 + 1.206 +static char *tcp_getline_work (TCPSTREAM *stream,unsigned long *size, 1.207 + long *contd) 1.208 +{ 1.209 + unsigned long n; 1.210 + char *s,*ret,c,d; 1.211 + *contd = NIL; /* assume no continuation */ 1.212 + /* make sure have data */ 1.213 + if (!tcp_getdata (stream)) return NIL; 1.214 + for (s = stream->iptr, n = 0, c = '\0'; stream->ictr--; n++, c = d) { 1.215 + d = *stream->iptr++; /* slurp another character */ 1.216 + if ((c == '\015') && (d == '\012')) { 1.217 + ret = (char *) fs_get (n--); 1.218 + memcpy (ret,s,*size = n); /* copy into a free storage string */ 1.219 + ret[n] = '\0'; /* tie off string with null */ 1.220 + return ret; 1.221 + } 1.222 + } 1.223 + /* copy partial string from buffer */ 1.224 + memcpy ((ret = (char *) fs_get (n)),s,*size = n); 1.225 + /* get more data from the net */ 1.226 + if (!tcp_getdata (stream)) fs_give ((void **) &ret); 1.227 + /* special case of newline broken by buffer */ 1.228 + else if ((c == '\015') && (*stream->iptr == '\012')) { 1.229 + stream->iptr++; /* eat the line feed */ 1.230 + stream->ictr--; 1.231 + ret[*size = --n] = '\0'; /* tie off string with null */ 1.232 + } 1.233 + else *contd = LONGT; /* continuation needed */ 1.234 + return ret; 1.235 +} 1.236 + 1.237 +/* TCP/IP receive buffer 1.238 + * Accepts: TCP/IP stream 1.239 + * size in bytes 1.240 + * buffer to read into 1.241 + * Returns: T if success, NIL otherwise 1.242 + */ 1.243 + 1.244 +long tcp_getbuffer (TCPSTREAM *stream,unsigned long size,char *buffer) 1.245 +{ 1.246 + unsigned long n; 1.247 + char *bufptr = buffer; 1.248 + while (size > 0) { /* until request satisfied */ 1.249 + if (!tcp_getdata (stream)) return NIL; 1.250 + n = min (size,stream->ictr);/* number of bytes to transfer */ 1.251 + /* do the copy */ 1.252 + memcpy (bufptr,stream->iptr,n); 1.253 + bufptr += n; /* update pointer */ 1.254 + stream->iptr +=n; 1.255 + size -= n; /* update # of bytes to do */ 1.256 + stream->ictr -=n; 1.257 + } 1.258 + bufptr[0] = '\0'; /* tie off string */ 1.259 + return T; 1.260 +} 1.261 + 1.262 +/* TCP/IP receive data 1.263 + * Accepts: TCP/IP stream 1.264 + * Returns: T if success, NIL otherwise 1.265 + */ 1.266 + 1.267 +long tcp_getdata (TCPSTREAM *stream) 1.268 +{ 1.269 + int i; 1.270 + fd_set fds,efds; 1.271 + struct timeval tmo; 1.272 + time_t t = time (0); 1.273 + if (stream->tcps < 0) return NIL; 1.274 + while (stream->ictr < 1) { /* if nothing in the buffer */ 1.275 + time_t tl = time (0); /* start of request */ 1.276 + tmo.tv_sec = ttmo_read; /* read timeout */ 1.277 + tmo.tv_usec = 0; 1.278 + FD_ZERO (&fds); /* initialize selection vector */ 1.279 + FD_ZERO (&efds); /* handle errors too */ 1.280 + FD_SET (stream->tcps,&fds);/* set bit in selection vector */ 1.281 + FD_SET(stream->tcps,&efds);/* set bit in error selection vector */ 1.282 + errno = NIL; /* block and read */ 1.283 + while (((i = select (stream->tcps+1,&fds,0,&efds,ttmo_read ? &tmo : 0))<0) 1.284 + && (errno == EINTR)); 1.285 + if (!i) { /* timeout? */ 1.286 + time_t tc = time (0); 1.287 + if (tmoh && ((*tmoh) (tc - t,tc - tl))) continue; 1.288 + else return tcp_abort (stream); 1.289 + } 1.290 + else if (i < 0) return tcp_abort (stream); 1.291 + while (((i = read (stream->tcps,stream->ibuf,BUFLEN)) < 0) && 1.292 + (errno == EINTR)); 1.293 + if (i < 1) return tcp_abort (stream); 1.294 + stream->iptr = stream->ibuf;/* point at TCP buffer */ 1.295 + stream->ictr = i; /* set new byte count */ 1.296 + } 1.297 + return T; 1.298 +} 1.299 + 1.300 +/* TCP/IP send string as record 1.301 + * Accepts: TCP/IP stream 1.302 + * string pointer 1.303 + * Returns: T if success else NIL 1.304 + */ 1.305 + 1.306 +long tcp_soutr (TCPSTREAM *stream,char *string) 1.307 +{ 1.308 + return tcp_sout (stream,string,(unsigned long) strlen (string)); 1.309 +} 1.310 + 1.311 + 1.312 +/* TCP/IP send string 1.313 + * Accepts: TCP/IP stream 1.314 + * string pointer 1.315 + * byte count 1.316 + * Returns: T if success else NIL 1.317 + */ 1.318 + 1.319 +long tcp_sout (TCPSTREAM *stream,char *string,unsigned long size) 1.320 +{ 1.321 + int i; 1.322 + fd_set fds; 1.323 + struct timeval tmo; 1.324 + time_t t = time (0); 1.325 + if (stream->tcps < 0) return NIL; 1.326 + while (size > 0) { /* until request satisfied */ 1.327 + time_t tl = time (0); /* start of request */ 1.328 + tmo.tv_sec = ttmo_write; /* write timeout */ 1.329 + tmo.tv_usec = 0; 1.330 + FD_ZERO (&fds); /* initialize selection vector */ 1.331 + FD_SET (stream->tcps,&fds);/* set bit in selection vector */ 1.332 + errno = NIL; /* block and write */ 1.333 + while (((i = select (stream->tcps+1,0,&fds,0,ttmo_write ? &tmo : 0)) < 0) 1.334 + && (errno == EINTR)); 1.335 + if (!i) { /* timeout? */ 1.336 + time_t tc = time (0); 1.337 + if (tmoh && ((*tmoh) (tc - t,tc - tl))) continue; 1.338 + else return tcp_abort (stream); 1.339 + } 1.340 + else if (i < 0) return tcp_abort (stream); 1.341 + while (((i = write (stream->tcps,string,size)) < 0) && (errno == EINTR)); 1.342 + if (i < 0) return tcp_abort (stream); 1.343 + size -= i; /* how much we sent */ 1.344 + string += i; 1.345 + } 1.346 + return T; /* all done */ 1.347 +} 1.348 + 1.349 +/* TCP/IP close 1.350 + * Accepts: TCP/IP stream 1.351 + */ 1.352 + 1.353 +void tcp_close (TCPSTREAM *stream) 1.354 +{ 1.355 + tcp_abort (stream); /* nuke the socket */ 1.356 + /* flush host names */ 1.357 + fs_give ((void **) &stream->host); 1.358 + fs_give ((void **) &stream->localhost); 1.359 + fs_give ((void **) &stream); /* flush the stream */ 1.360 +} 1.361 + 1.362 + 1.363 +/* TCP/IP abort stream 1.364 + * Accepts: TCP/IP stream 1.365 + * Returns: NIL always 1.366 + */ 1.367 + 1.368 +long tcp_abort (TCPSTREAM *stream) 1.369 +{ 1.370 + if (stream->tcps >= 0) close (stream->tcps); 1.371 + stream->tcps = -1; 1.372 + return NIL; 1.373 +} 1.374 + 1.375 +/* TCP/IP get host name 1.376 + * Accepts: TCP/IP stream 1.377 + * Returns: host name for this stream 1.378 + */ 1.379 + 1.380 +char *tcp_host (TCPSTREAM *stream) 1.381 +{ 1.382 + return stream->host; /* return host name */ 1.383 +} 1.384 + 1.385 + 1.386 +/* TCP/IP get remote host name 1.387 + * Accepts: TCP/IP stream 1.388 + * Returns: host name for this stream 1.389 + */ 1.390 + 1.391 +char *tcp_remotehost (TCPSTREAM *stream) 1.392 +{ 1.393 + return stream->host; /* all we can do for now */ 1.394 +} 1.395 + 1.396 + 1.397 +/* TCP/IP return port for this stream 1.398 + * Accepts: TCP/IP stream 1.399 + * Returns: port number for this stream 1.400 + */ 1.401 + 1.402 +unsigned long tcp_port (TCPSTREAM *stream) 1.403 +{ 1.404 + return stream->port; /* return port number */ 1.405 +} 1.406 + 1.407 + 1.408 +/* TCP/IP get local host name 1.409 + * Accepts: TCP/IP stream 1.410 + * Returns: local host name 1.411 + */ 1.412 + 1.413 +char *tcp_localhost (TCPSTREAM *stream) 1.414 +{ 1.415 + return stream->localhost; /* return local host name */ 1.416 +} 1.417 + 1.418 + 1.419 +/* TCP/IP return canonical form of host name 1.420 + * Accepts: host name 1.421 + * Returns: canonical form of host name 1.422 + */ 1.423 + 1.424 +char *tcp_canonical (char *name) 1.425 +{ 1.426 + return name; 1.427 +} 1.428 + 1.429 + 1.430 +/* TCP/IP get client host name (server calls only) 1.431 + * Returns: client host name 1.432 + */ 1.433 + 1.434 +char *tcp_clienthost () 1.435 +{ 1.436 + return "UNKNOWN"; 1.437 +}