imapext-2007

view 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 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: MS-DOS TCP/IP routines
16 *
17 * Author: Mark Crispin
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 */
29 static tcptimeout_t tmoh = NIL; /* TCP timeout handler routine */
30 static long ttmo_read = 0; /* TCP timeouts, in seconds */
31 static long ttmo_write = 0;
33 static char *tcp_getline_work (TCPSTREAM *stream,unsigned long *size,
34 long *contd);
36 /* TCP/IP manipulate parameters
37 * Accepts: function code
38 * function-dependent value
39 * Returns: function-dependent return value
40 */
42 void *tcp_parameters (long function,void *value)
43 {
44 void *ret = NIL;
45 switch ((int) function) {
46 case SET_TIMEOUT:
47 tmoh = (tcptimeout_t) value;
48 case GET_TIMEOUT:
49 ret = (void *) tmoh;
50 break;
51 case SET_READTIMEOUT:
52 ttmo_read = (long) value;
53 case GET_READTIMEOUT:
54 ret = (void *) ttmo_read;
55 break;
56 case SET_WRITETIMEOUT:
57 ttmo_write = (long) value;
58 case GET_WRITETIMEOUT:
59 ret = (void *) ttmo_write;
60 break;
61 }
62 return ret;
63 }
65 /* TCP/IP open
66 * Accepts: host name
67 * contact service name
68 * contact port number
69 * Returns: TCP/IP stream if success else NIL
70 */
72 TCPSTREAM *tcp_open (char *host,char *service,unsigned long port)
73 {
74 TCPSTREAM *stream = NIL;
75 struct sockaddr_in sin;
76 int sock;
77 char *s,tmp[MAILTMPLEN];
78 port &= 0xffff; /* erase flags */
79 /* The domain literal form is used (rather than simply the dotted decimal
80 as with other Unix programs) because it has to be a valid "host name"
81 in mailsystem terminology. */
82 sin.sin_family = AF_INET; /* family is always Internet */
83 /* look like domain literal? */
84 if (host[0] == '[' && host[(strlen (host))-1] == ']') {
85 strcpy (tmp,host+1); /* yes, copy number part */
86 tmp[strlen (tmp)-1] = '\0';
87 if ((sin.sin_addr.s_addr = inet_addr (tmp)) == -1) {
88 sprintf (tmp,"Bad format domain-literal: %.80s",host);
89 mm_log (tmp,ERROR);
90 return NIL;
91 }
92 }
93 /* look up host name */
94 else if (!lookuphost (&host,&sin)) {
95 sprintf (tmp,"Host not found: %s",host);
96 mm_log (tmp,ERROR);
97 return NIL;
98 }
100 /* copy port number in network format */
101 if (!(sin.sin_port = htons (port))) fatal ("Bad port argument to tcp_open");
102 /* get a TCP stream */
103 if ((sock = socket (sin.sin_family,SOCK_STREAM,0)) < 0) {
104 sprintf (tmp,"Unable to create TCP socket (%d)",errno);
105 mm_log (tmp,ERROR);
106 fs_give ((void **) &host);
107 return NIL;
108 }
109 #if 0
110 /* needed? */
111 else if (sock >= FD_SETSIZE) {/* unselectable sockets are useless */
112 sprintf (tmp,"Unable to create selectable TCP socket (%d >= %d)",
113 sock,FD_SETSIZE);
114 close (sock);
115 errno = ENOBUFS; /* just in case */
116 return NIL;
117 }
118 #endif
119 /* open connection */
120 if (connect (sock,(struct sockaddr *) &sin,sizeof (sin)) < 0) {
121 switch (errno) { /* analyze error */
122 case ECONNREFUSED:
123 s = "Refused";
124 break;
125 case ENOBUFS:
126 s = "Insufficient system resources";
127 break;
128 case ETIMEDOUT:
129 s = "Timed out";
130 break;
131 default:
132 s = "Unknown error";
133 break;
134 }
135 sprintf (tmp,"Can't connect to %.80s,%ld: %s (%d)",host,port,s,errno);
136 mm_log (tmp,ERROR);
137 close (sock);
138 fs_give ((void **) &host);
139 return NIL;
140 }
141 /* create TCP/IP stream */
142 stream = (TCPSTREAM *) fs_get (sizeof (TCPSTREAM));
143 stream->host = host; /* official host name */
144 stream->localhost = cpystr (mylocalhost ());
145 stream->port = port; /* port number */
146 stream->tcps = sock; /* init socket */
147 stream->ictr = 0; /* init input counter */
148 return stream; /* return success */
149 }
151 /* TCP/IP authenticated open
152 * Accepts: NETMBX specifier
153 * service name
154 * returned user name buffer
155 * Returns: TCP/IP stream if success else NIL
156 */
158 TCPSTREAM *tcp_aopen (NETMBX *mb,char *service,char *usrbuf)
159 {
160 return NIL; /* always NIL on DOS */
161 }
163 /* TCP receive line
164 * Accepts: TCP stream
165 * Returns: text line string or NIL if failure
166 */
168 char *tcp_getline (TCPSTREAM *stream)
169 {
170 unsigned long n,contd;
171 char *ret = tcp_getline_work (stream,&n,&contd);
172 if (ret && contd) { /* got a line needing continuation? */
173 STRINGLIST *stl = mail_newstringlist ();
174 STRINGLIST *stc = stl;
175 do { /* collect additional lines */
176 stc->text.data = (unsigned char *) ret;
177 stc->text.size = n;
178 stc = stc->next = mail_newstringlist ();
179 ret = tcp_getline_work (stream,&n,&contd);
180 } while (ret && contd);
181 if (ret) { /* stash final part of line on list */
182 stc->text.data = (unsigned char *) ret;
183 stc->text.size = n;
184 /* determine how large a buffer we need */
185 for (n = 0, stc = stl; stc; stc = stc->next) n += stc->text.size;
186 ret = fs_get (n + 1); /* copy parts into buffer */
187 for (n = 0, stc = stl; stc; n += stc->text.size, stc = stc->next)
188 memcpy (ret + n,stc->text.data,stc->text.size);
189 ret[n] = '\0';
190 }
191 mail_free_stringlist (&stl);/* either way, done with list */
192 }
193 return ret;
194 }
196 /* TCP receive line or partial line
197 * Accepts: TCP stream
198 * pointer to return size
199 * pointer to return continuation flag
200 * Returns: text line string, size and continuation flag, or NIL if failure
201 */
203 static char *tcp_getline_work (TCPSTREAM *stream,unsigned long *size,
204 long *contd)
205 {
206 unsigned long n;
207 char *s,*ret,c,d;
208 *contd = NIL; /* assume no continuation */
209 /* make sure have data */
210 if (!tcp_getdata (stream)) return NIL;
211 for (s = stream->iptr, n = 0, c = '\0'; stream->ictr--; n++, c = d) {
212 d = *stream->iptr++; /* slurp another character */
213 if ((c == '\015') && (d == '\012')) {
214 ret = (char *) fs_get (n--);
215 memcpy (ret,s,*size = n); /* copy into a free storage string */
216 ret[n] = '\0'; /* tie off string with null */
217 return ret;
218 }
219 }
220 /* copy partial string from buffer */
221 memcpy ((ret = (char *) fs_get (n)),s,*size = n);
222 /* get more data from the net */
223 if (!tcp_getdata (stream)) fs_give ((void **) &ret);
224 /* special case of newline broken by buffer */
225 else if ((c == '\015') && (*stream->iptr == '\012')) {
226 stream->iptr++; /* eat the line feed */
227 stream->ictr--;
228 ret[*size = --n] = '\0'; /* tie off string with null */
229 }
230 else *contd = LONGT; /* continuation needed */
231 return ret;
232 }
234 /* TCP/IP receive buffer
235 * Accepts: TCP/IP stream
236 * size in bytes
237 * buffer to read into
238 * Returns: T if success, NIL otherwise
239 */
241 long tcp_getbuffer (TCPSTREAM *stream,unsigned long size,char *buffer)
242 {
243 unsigned long n;
244 char *bufptr = buffer;
245 while (size > 0) { /* until request satisfied */
246 if (!tcp_getdata (stream)) return NIL;
247 n = min (size,stream->ictr);/* number of bytes to transfer */
248 /* do the copy */
249 memcpy (bufptr,stream->iptr,n);
250 bufptr += n; /* update pointer */
251 stream->iptr +=n;
252 size -= n; /* update # of bytes to do */
253 stream->ictr -=n;
254 }
255 bufptr[0] = '\0'; /* tie off string */
256 return T;
257 }
259 /* TCP/IP receive data
260 * Accepts: TCP/IP stream
261 * Returns: T if success, NIL otherwise
262 */
264 long tcp_getdata (TCPSTREAM *stream)
265 {
266 int i;
267 fd_set fds,efds;
268 struct timeval tmo;
269 time_t t = time (0);
270 if (stream->tcps < 0) return NIL;
271 while (stream->ictr < 1) { /* if nothing in the buffer */
272 time_t tl = time (0); /* start of request */
273 tmo.tv_sec = ttmo_read; /* read timeout */
274 tmo.tv_usec = 0;
275 FD_ZERO (&fds); /* initialize selection vector */
276 FD_ZERO (&efds); /* handle errors too */
277 FD_SET (stream->tcps,&fds);/* set bit in selection vector */
278 FD_SET(stream->tcps,&efds);/* set bit in error selection vector */
279 errno = NIL; /* block and read */
280 while (((i = select (stream->tcps+1,&fds,0,&efds,ttmo_read ? &tmo : 0))<0)
281 && (errno == EINTR));
282 if (!i) { /* timeout? */
283 time_t tc = time (0);
284 if (tmoh && ((*tmoh) (tc - t,tc - tl))) continue;
285 else return tcp_abort (stream);
286 }
287 else if (i < 0) return tcp_abort (stream);
288 while (((i = read (stream->tcps,stream->ibuf,BUFLEN)) < 0) &&
289 (errno == EINTR));
290 if (i < 1) return tcp_abort (stream);
291 stream->iptr = stream->ibuf;/* point at TCP buffer */
292 stream->ictr = i; /* set new byte count */
293 }
294 return T;
295 }
297 /* TCP/IP send string as record
298 * Accepts: TCP/IP stream
299 * string pointer
300 * Returns: T if success else NIL
301 */
303 long tcp_soutr (TCPSTREAM *stream,char *string)
304 {
305 return tcp_sout (stream,string,(unsigned long) strlen (string));
306 }
309 /* TCP/IP send string
310 * Accepts: TCP/IP stream
311 * string pointer
312 * byte count
313 * Returns: T if success else NIL
314 */
316 long tcp_sout (TCPSTREAM *stream,char *string,unsigned long size)
317 {
318 int i;
319 fd_set fds;
320 struct timeval tmo;
321 time_t t = time (0);
322 if (stream->tcps < 0) return NIL;
323 while (size > 0) { /* until request satisfied */
324 time_t tl = time (0); /* start of request */
325 tmo.tv_sec = ttmo_write; /* write timeout */
326 tmo.tv_usec = 0;
327 FD_ZERO (&fds); /* initialize selection vector */
328 FD_SET (stream->tcps,&fds);/* set bit in selection vector */
329 errno = NIL; /* block and write */
330 while (((i = select (stream->tcps+1,0,&fds,0,ttmo_write ? &tmo : 0)) < 0)
331 && (errno == EINTR));
332 if (!i) { /* timeout? */
333 time_t tc = time (0);
334 if (tmoh && ((*tmoh) (tc - t,tc - tl))) continue;
335 else return tcp_abort (stream);
336 }
337 else if (i < 0) return tcp_abort (stream);
338 while (((i = write (stream->tcps,string,size)) < 0) && (errno == EINTR));
339 if (i < 0) return tcp_abort (stream);
340 size -= i; /* how much we sent */
341 string += i;
342 }
343 return T; /* all done */
344 }
346 /* TCP/IP close
347 * Accepts: TCP/IP stream
348 */
350 void tcp_close (TCPSTREAM *stream)
351 {
352 tcp_abort (stream); /* nuke the socket */
353 /* flush host names */
354 fs_give ((void **) &stream->host);
355 fs_give ((void **) &stream->localhost);
356 fs_give ((void **) &stream); /* flush the stream */
357 }
360 /* TCP/IP abort stream
361 * Accepts: TCP/IP stream
362 * Returns: NIL always
363 */
365 long tcp_abort (TCPSTREAM *stream)
366 {
367 if (stream->tcps >= 0) close (stream->tcps);
368 stream->tcps = -1;
369 return NIL;
370 }
372 /* TCP/IP get host name
373 * Accepts: TCP/IP stream
374 * Returns: host name for this stream
375 */
377 char *tcp_host (TCPSTREAM *stream)
378 {
379 return stream->host; /* return host name */
380 }
383 /* TCP/IP get remote host name
384 * Accepts: TCP/IP stream
385 * Returns: host name for this stream
386 */
388 char *tcp_remotehost (TCPSTREAM *stream)
389 {
390 return stream->host; /* all we can do for now */
391 }
394 /* TCP/IP return port for this stream
395 * Accepts: TCP/IP stream
396 * Returns: port number for this stream
397 */
399 unsigned long tcp_port (TCPSTREAM *stream)
400 {
401 return stream->port; /* return port number */
402 }
405 /* TCP/IP get local host name
406 * Accepts: TCP/IP stream
407 * Returns: local host name
408 */
410 char *tcp_localhost (TCPSTREAM *stream)
411 {
412 return stream->localhost; /* return local host name */
413 }
416 /* TCP/IP return canonical form of host name
417 * Accepts: host name
418 * Returns: canonical form of host name
419 */
421 char *tcp_canonical (char *name)
422 {
423 return name;
424 }
427 /* TCP/IP get client host name (server calls only)
428 * Returns: client host name
429 */
431 char *tcp_clienthost ()
432 {
433 return "UNKNOWN";
434 }

UW-IMAP'd extensions by yuuji