annotate src/osdep/unix/tcp_unix.c @ 4:d741b3ecc917 draft

imapext-2007f
author HIROSE Yuuji <yuuji@gentei.org>
date Thu, 30 Oct 2014 00:03:05 +0900
parents 2366b362676d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1 /* ========================================================================
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2 * Copyright 1988-2008 University of Washington
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
3 *
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
4 * Licensed under the Apache License, Version 2.0 (the "License");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
5 * you may not use this file except in compliance with the License.
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
6 * You may obtain a copy of the License at
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
7 *
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
8 * http://www.apache.org/licenses/LICENSE-2.0
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
9 *
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
10 *
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
11 * ========================================================================
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
12 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
13
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
14 /*
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
15 * Program: UNIX TCP/IP routines
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
16 *
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
17 * Author: Mark Crispin
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
18 * Networks and Distributed Computing
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
19 * Computing & Communications
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
20 * University of Washington
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
21 * Administration Building, AG-44
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
22 * Seattle, WA 98195
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
23 * Internet: MRC@CAC.Washington.EDU
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
24 *
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
25 * Date: 1 August 1988
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
26 * Last Edited: 13 January 2008
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
27 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
28
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
29 #include "ip_unix.c"
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
30
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
31 #undef write /* don't use redefined write() */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
32
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
33 static tcptimeout_t tmoh = NIL; /* TCP timeout handler routine */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
34 static long ttmo_open = 0; /* TCP timeouts, in seconds */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
35 static long ttmo_read = 0;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
36 static long ttmo_write = 0;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
37 static long rshtimeout = 15; /* rsh timeout */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
38 static char *rshcommand = NIL; /* rsh command */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
39 static char *rshpath = NIL; /* rsh path */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
40 static long sshtimeout = 15; /* ssh timeout */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
41 static char *sshcommand = NIL; /* ssh command */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
42 static char *sshpath = NIL; /* ssh path */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
43 static long allowreversedns = T;/* allow reverse DNS lookup */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
44 static long tcpdebug = NIL; /* extra TCP debugging telemetry */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
45 static char *myClientAddr = NIL;/* client IP address */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
46 static char *myClientHost = NIL;/* client DNS name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
47 static long myClientPort = -1; /* client port number */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
48 static char *myServerAddr = NIL;/* server IP address */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
49 static char *myServerHost = NIL;/* server DNS name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
50 static long myServerPort = -1; /* server port number */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
51
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
52 extern long maxposint; /* get this from write.c */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
53
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
54 /* Local function prototypes */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
55
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
56 int tcp_socket_open (int family,void *adr,size_t adrlen,unsigned short port,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
57 char *tmp,int *ctr,char *hst);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
58 static char *tcp_getline_work (TCPSTREAM *stream,unsigned long *size,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
59 long *contd);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
60 long tcp_abort (TCPSTREAM *stream);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
61 char *tcp_name (struct sockaddr *sadr,long flag);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
62 char *tcp_name_valid (char *s);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
63
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
64 /* TCP/IP manipulate parameters
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
65 * Accepts: function code
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
66 * function-dependent value
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
67 * Returns: function-dependent return value
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
68 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
69
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
70 void *tcp_parameters (long function,void *value)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
71 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
72 void *ret = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
73 switch ((int) function) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
74 case SET_TIMEOUT:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
75 tmoh = (tcptimeout_t) value;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
76 case GET_TIMEOUT:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
77 ret = (void *) tmoh;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
78 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
79 case SET_OPENTIMEOUT:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
80 ttmo_open = (long) value;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
81 case GET_OPENTIMEOUT:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
82 ret = (void *) ttmo_open;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
83 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
84 case SET_READTIMEOUT:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
85 ttmo_read = (long) value;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
86 case GET_READTIMEOUT:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
87 ret = (void *) ttmo_read;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
88 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
89 case SET_WRITETIMEOUT:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
90 ttmo_write = (long) value;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
91 case GET_WRITETIMEOUT:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
92 ret = (void *) ttmo_write;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
93 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
94 case SET_ALLOWREVERSEDNS:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
95 allowreversedns = (long) value;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
96 case GET_ALLOWREVERSEDNS:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
97 ret = (void *) allowreversedns;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
98 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
99 case SET_TCPDEBUG:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
100 tcpdebug = (long) value;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
101 case GET_TCPDEBUG:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
102 ret = (void *) tcpdebug;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
103 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
104
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
105 case SET_RSHTIMEOUT:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
106 rshtimeout = (long) value;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
107 case GET_RSHTIMEOUT:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
108 ret = (void *) rshtimeout;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
109 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
110 case SET_RSHCOMMAND:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
111 if (rshcommand) fs_give ((void **) &rshcommand);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
112 rshcommand = cpystr ((char *) value);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
113 case GET_RSHCOMMAND:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
114 ret = (void *) rshcommand;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
115 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
116 case SET_RSHPATH:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
117 if (rshpath) fs_give ((void **) &rshpath);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
118 rshpath = cpystr ((char *) value);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
119 case GET_RSHPATH:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
120 ret = (void *) rshpath;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
121 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
122 case SET_SSHTIMEOUT:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
123 sshtimeout = (long) value;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
124 case GET_SSHTIMEOUT:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
125 ret = (void *) sshtimeout;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
126 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
127 case SET_SSHCOMMAND:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
128 if (sshcommand) fs_give ((void **) &sshcommand);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
129 sshcommand = cpystr ((char *) value);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
130 case GET_SSHCOMMAND:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
131 ret = (void *) sshcommand;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
132 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
133 case SET_SSHPATH:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
134 if (sshpath) fs_give ((void **) &sshpath);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
135 sshpath = cpystr ((char *) value);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
136 case GET_SSHPATH:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
137 ret = (void *) sshpath;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
138 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
139 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
140 return ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
141 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
142
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
143 /* TCP/IP open
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
144 * Accepts: host name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
145 * contact service name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
146 * contact port number and optional silent flag
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
147 * Returns: TCP/IP stream if success else NIL
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
148 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
149
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
150 TCPSTREAM *tcp_open (char *host,char *service,unsigned long port)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
151 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
152 TCPSTREAM *stream = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
153 int family;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
154 int sock = -1;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
155 int ctr = 0;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
156 int silent = (port & NET_SILENT) ? T : NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
157 int *ctrp = (port & NET_NOOPENTIMEOUT) ? NIL : &ctr;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
158 char *s,*hostname,tmp[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
159 void *adr;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
160 size_t adrlen;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
161 struct servent *sv = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
162 blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
163 void *data,*next;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
164 port &= 0xffff; /* erase flags */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
165 /* lookup service */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
166 if (service && (sv = getservbyname (service,"tcp")))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
167 port = ntohs (sv->s_port);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
168 /* The domain literal form is used (rather than simply the dotted decimal
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
169 as with other Unix programs) because it has to be a valid "host name"
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
170 in mailsystem terminology. */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
171 /* look like domain literal? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
172 if (host[0] == '[' && host[(strlen (host))-1] == ']') {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
173 strcpy (tmp,host+1); /* yes, copy number part */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
174 tmp[(strlen (tmp))-1] = '\0';
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
175 if (adr = ip_stringtoaddr (tmp,&adrlen,&family)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
176 (*bn) (BLOCK_TCPOPEN,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
177 /* get an open socket for this system */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
178 sock = tcp_socket_open (family,adr,adrlen,port,tmp,ctrp,hostname = host);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
179 (*bn) (BLOCK_NONE,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
180 fs_give ((void **) &adr);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
181 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
182 else sprintf (tmp,"Bad format domain-literal: %.80s",host);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
183 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
184
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
185 else { /* lookup host name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
186 if (tcpdebug) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
187 sprintf (tmp,"DNS resolution %.80s",host);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
188 mm_log (tmp,TCPDEBUG);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
189 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
190 (*bn) (BLOCK_DNSLOOKUP,NIL);/* quell alarms */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
191 data = (*bn) (BLOCK_SENSITIVE,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
192 if (!(s = ip_nametoaddr (host,&adrlen,&family,&hostname,&next)))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
193 sprintf (tmp,"No such host as %.80s",host);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
194 (*bn) (BLOCK_NONSENSITIVE,data);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
195 (*bn) (BLOCK_NONE,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
196 if (s) { /* DNS resolution won? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
197 if (tcpdebug) mm_log ("DNS resolution done",TCPDEBUG);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
198 do {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
199 (*bn) (BLOCK_TCPOPEN,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
200 if (((sock = tcp_socket_open (family,s,adrlen,port,tmp,ctrp,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
201 hostname)) < 0) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
202 (s = ip_nametoaddr (NIL,&adrlen,&family,&hostname,&next)) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
203 !silent) mm_log (tmp,WARN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
204 (*bn) (BLOCK_NONE,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
205 } while ((sock < 0) && s);/* repeat until success or no more addreses */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
206 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
207 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
208 if (sock >= 0) { /* won */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
209 stream = (TCPSTREAM *) memset (fs_get (sizeof (TCPSTREAM)),0,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
210 sizeof (TCPSTREAM));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
211 stream->port = port; /* port number */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
212 /* init sockets */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
213 stream->tcpsi = stream->tcpso = sock;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
214 /* stash in the snuck-in byte */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
215 if (stream->ictr = ctr) *(stream->iptr = stream->ibuf) = tmp[0];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
216 /* copy official host name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
217 stream->host = cpystr (hostname);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
218 if (tcpdebug) mm_log ("Stream open and ready for read",TCPDEBUG);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
219 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
220 else if (!silent) mm_log (tmp,ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
221 return stream; /* return success */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
222 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
223
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
224 /* Open a TCP socket
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
225 * Accepts: protocol family
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
226 * address to connect to
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
227 * address length
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
228 * port
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
229 * scratch buffer
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
230 * pointer to "first byte read in" storage or NIL
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
231 * host name for error message
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
232 * Returns: socket if success, else -1 with error string in scratch buffer
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
233 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
234
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
235 int tcp_socket_open (int family,void *adr,size_t adrlen,unsigned short port,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
236 char *tmp,int *ctr,char *hst)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
237 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
238 int i,ti,sock,flgs;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
239 size_t len;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
240 time_t now;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
241 struct protoent *pt = getprotobyname ("tcp");
3
2366b362676d imap-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 1
diff changeset
242 fd_set rfds,wfds,efds;
0
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
243 struct timeval tmo;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
244 struct sockaddr *sadr = ip_sockaddr (family,adr,adrlen,port,&len);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
245 blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
246 /* fetid Solaris */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
247 void *data = (*bn) (BLOCK_SENSITIVE,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
248 sprintf (tmp,"Trying IP address [%s]",ip_sockaddrtostring (sadr));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
249 mm_log (tmp,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
250 /* make a socket */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
251 if ((sock = socket (sadr->sa_family,SOCK_STREAM,pt ? pt->p_proto : 0)) < 0) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
252 sprintf (tmp,"Unable to create TCP socket: %s",strerror (errno));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
253 (*bn) (BLOCK_NONSENSITIVE,data);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
254 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
255 else if (sock >= FD_SETSIZE) {/* unselectable sockets are useless */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
256 sprintf (tmp,"Unable to create selectable TCP socket (%d >= %d)",
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
257 sock,FD_SETSIZE);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
258 (*bn) (BLOCK_NONSENSITIVE,data);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
259 close (sock);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
260 sock = -1;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
261 errno = EMFILE;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
262 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
263
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
264 else { /* get current socket flags */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
265 flgs = fcntl (sock,F_GETFL,0);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
266 /* set non-blocking if want open timeout */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
267 if (ctr) fcntl (sock,F_SETFL,flgs | FNDELAY);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
268 /* open connection */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
269 while ((i = connect (sock,sadr,len)) < 0 && (errno == EINTR));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
270 (*bn) (BLOCK_NONSENSITIVE,data);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
271 if (i < 0) switch (errno) { /* failed? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
272 case EAGAIN: /* DG brain damage */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
273 case EINPROGRESS: /* what we expect to happen */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
274 case EALREADY: /* or another form of it */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
275 case EISCONN: /* restart after interrupt? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
276 case EADDRINUSE: /* restart after interrupt? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
277 break; /* well, not really, it was interrupted */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
278 default:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
279 sprintf (tmp,"Can't connect to %.80s,%u: %s",hst,(unsigned int) port,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
280 strerror (errno));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
281 close (sock); /* flush socket */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
282 sock = -1;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
283 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
284 if ((sock >= 0) && ctr) { /* want open timeout? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
285 now = time (0); /* open timeout */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
286 ti = ttmo_open ? now + ttmo_open : 0;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
287 tmo.tv_usec = 0;
3
2366b362676d imap-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 1
diff changeset
288 FD_ZERO (&rfds); /* initialize selection vector */
2366b362676d imap-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 1
diff changeset
289 FD_ZERO (&wfds); /* initialize selection vector */
0
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
290 FD_ZERO (&efds); /* handle errors too */
3
2366b362676d imap-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 1
diff changeset
291 FD_SET (sock,&rfds); /* block for error or readable or writable */
2366b362676d imap-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 1
diff changeset
292 FD_SET (sock,&wfds);
0
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
293 FD_SET (sock,&efds);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
294 do { /* block under timeout */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
295 tmo.tv_sec = ti ? ti - now : 0;
3
2366b362676d imap-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 1
diff changeset
296 i = select (sock+1,&rfds,&wfds,&efds,ti ? &tmo : NIL);
0
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
297 now = time (0); /* fake timeout if interrupt & time expired */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
298 if ((i < 0) && (errno == EINTR) && ti && (ti <= now)) i = 0;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
299 } while ((i < 0) && (errno == EINTR));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
300 if (i > 0) { /* success, make sure really connected */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
301 /* restore blocking status */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
302 fcntl (sock,F_SETFL,flgs);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
303 /* This used to be a zero-byte read(), but that crashes Solaris */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
304 /* get socket status */
3
2366b362676d imap-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 1
diff changeset
305 if(FD_ISSET(sock, &rfds)) while (((i = *ctr = read (sock,tmp,1)) < 0) && (errno == EINTR));
0
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
306 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
307 if (i <= 0) { /* timeout or error? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
308 i = i ? errno : ETIMEDOUT;/* determine error code */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
309 close (sock); /* flush socket */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
310 sock = -1;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
311 errno = i; /* return error code */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
312 sprintf (tmp,"Connection failed to %.80s,%lu: %s",hst,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
313 (unsigned long) port,strerror (errno));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
314 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
315 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
316 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
317 fs_give ((void **) &sadr);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
318 return sock; /* return the socket */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
319 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
320
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
321 /* TCP/IP authenticated open
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
322 * Accepts: host name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
323 * service name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
324 * returned user name buffer
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
325 * Returns: TCP/IP stream if success else NIL
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
326 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
327
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
328 #define MAXARGV 20
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
329
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
330 TCPSTREAM *tcp_aopen (NETMBX *mb,char *service,char *usrbuf)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
331 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
332 TCPSTREAM *stream = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
333 void *adr;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
334 char host[MAILTMPLEN],tmp[MAILTMPLEN],*path,*argv[MAXARGV+1],*r;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
335 int i,ti,pipei[2],pipeo[2];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
336 size_t len;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
337 time_t now;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
338 struct timeval tmo;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
339 fd_set fds,efds;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
340 blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
341 #ifdef SSHPATH /* ssh path defined yet? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
342 if (!sshpath) sshpath = cpystr (SSHPATH);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
343 #endif
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
344 #ifdef RSHPATH /* rsh path defined yet? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
345 if (!rshpath) rshpath = cpystr (RSHPATH);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
346 #endif
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
347 if (*service == '*') { /* want ssh? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
348 /* return immediately if ssh disabled */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
349 if (!(sshpath && (ti = sshtimeout))) return NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
350 /* ssh command prototype defined yet? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
351 if (!sshcommand) sshcommand = cpystr ("%s %s -l %s exec /etc/r%sd");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
352 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
353 /* want rsh? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
354 else if (rshpath && (ti = rshtimeout)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
355 /* rsh command prototype defined yet? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
356 if (!rshcommand) rshcommand = cpystr ("%s %s -l %s exec /etc/r%sd");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
357 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
358 else return NIL; /* rsh disabled */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
359 /* look like domain literal? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
360 if (mb->host[0] == '[' && mb->host[i = (strlen (mb->host))-1] == ']') {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
361 strcpy (host,mb->host+1); /* yes, copy without brackets */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
362 host[i-1] = '\0';
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
363 /* validate domain literal */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
364 if (adr = ip_stringtoaddr (host,&len,&i)) fs_give ((void **) &adr);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
365 else {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
366 sprintf (tmp,"Bad format domain-literal: %.80s",host);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
367 mm_log (tmp,ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
368 return NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
369 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
370 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
371 else strcpy (host,tcp_canonical (mb->host));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
372
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
373 if (*service == '*') /* build ssh command */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
374 sprintf (tmp,sshcommand,sshpath,host,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
375 mb->user[0] ? mb->user : myusername (),service + 1);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
376 else sprintf (tmp,rshcommand,rshpath,host,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
377 mb->user[0] ? mb->user : myusername (),service);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
378 if (tcpdebug) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
379 char msg[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
380 sprintf (msg,"Trying %.100s",tmp);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
381 mm_log (msg,TCPDEBUG);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
382 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
383 /* parse command into argv */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
384 for (i = 1,path = argv[0] = strtok_r (tmp," ",&r);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
385 (i < MAXARGV) && (argv[i] = strtok_r (NIL," ",&r)); i++);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
386 argv[i] = NIL; /* make sure argv tied off */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
387 /* make command pipes */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
388 if (pipe (pipei) < 0) return NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
389 if ((pipei[0] >= FD_SETSIZE) || (pipei[1] >= FD_SETSIZE) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
390 (pipe (pipeo) < 0)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
391 close (pipei[0]); close (pipei[1]);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
392 return NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
393 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
394 (*bn) (BLOCK_TCPOPEN,NIL); /* quell alarm up here for NeXT */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
395 if ((pipeo[0] >= FD_SETSIZE) || (pipeo[1] >= FD_SETSIZE) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
396 ((i = fork ()) < 0)) { /* make inferior process */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
397 close (pipei[0]); close (pipei[1]);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
398 close (pipeo[0]); close (pipeo[1]);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
399 (*bn) (BLOCK_NONE,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
400 return NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
401 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
402 if (!i) { /* if child */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
403 alarm (0); /* never have alarms in children */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
404 if (!fork ()) { /* make grandchild so it's inherited by init */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
405 int cf; /* don't alter parent vars in case vfork() */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
406 int maxfd = max (20,max (max(pipei[0],pipei[1]),max(pipeo[0],pipeo[1])));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
407 dup2 (pipei[1],1); /* parent's input is my output */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
408 dup2 (pipei[1],2); /* parent's input is my error output too */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
409 dup2 (pipeo[0],0); /* parent's output is my input */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
410 /* close all unnecessary descriptors */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
411 for (cf = 3; cf <= maxfd; cf++) close (cf);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
412 setpgrp (0,getpid ()); /* be our own process group */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
413 _exit (execv (path,argv));/* now run it */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
414 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
415 _exit (1); /* child is done */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
416 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
417 grim_pid_reap (i,NIL); /* reap child; grandchild now owned by init */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
418 close (pipei[1]); /* close child's side of the pipes */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
419 close (pipeo[0]);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
420
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
421 /* create TCP/IP stream */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
422 stream = (TCPSTREAM *) memset (fs_get (sizeof (TCPSTREAM)),0,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
423 sizeof (TCPSTREAM));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
424 /* copy remote host name from argument */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
425 stream->remotehost = cpystr (stream->host = cpystr (host));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
426 stream->tcpsi = pipei[0]; /* init sockets */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
427 stream->tcpso = pipeo[1];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
428 stream->ictr = 0; /* init input counter */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
429 stream->port = 0xffffffff; /* no port number */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
430 ti += now = time (0); /* open timeout */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
431 tmo.tv_usec = 0; /* initialize usec timeout */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
432 FD_ZERO (&fds); /* initialize selection vector */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
433 FD_ZERO (&efds); /* handle errors too */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
434 FD_SET (stream->tcpsi,&fds); /* set bit in selection vector */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
435 FD_SET (stream->tcpsi,&efds); /* set bit in error selection vector */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
436 FD_SET (stream->tcpso,&efds); /* set bit in error selection vector */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
437 do { /* block under timeout */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
438 tmo.tv_sec = ti - now;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
439 i = select (max (stream->tcpsi,stream->tcpso)+1,&fds,NIL,&efds,&tmo);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
440 now = time (0); /* fake timeout if interrupt & time expired */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
441 if ((i < 0) && (errno == EINTR) && ti && (ti <= now)) i = 0;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
442 } while ((i < 0) && (errno == EINTR));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
443 if (i <= 0) { /* timeout or error? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
444 sprintf (tmp,i ? "error in %s to IMAP server" :
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
445 "%s to IMAP server timed out",(*service == '*') ? "ssh" : "rsh");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
446 mm_log (tmp,WARN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
447 tcp_close (stream); /* punt stream */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
448 stream = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
449 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
450 (*bn) (BLOCK_NONE,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
451 /* return user name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
452 strcpy (usrbuf,mb->user[0] ? mb->user : myusername ());
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
453 return stream; /* return success */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
454 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
455
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
456 /* TCP receive line
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
457 * Accepts: TCP stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
458 * Returns: text line string or NIL if failure
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
459 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
460
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
461 char *tcp_getline (TCPSTREAM *stream)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
462 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
463 unsigned long n,contd;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
464 char *ret = tcp_getline_work (stream,&n,&contd);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
465 if (ret && contd) { /* got a line needing continuation? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
466 STRINGLIST *stl = mail_newstringlist ();
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
467 STRINGLIST *stc = stl;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
468 do { /* collect additional lines */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
469 stc->text.data = (unsigned char *) ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
470 stc->text.size = n;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
471 stc = stc->next = mail_newstringlist ();
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
472 ret = tcp_getline_work (stream,&n,&contd);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
473 } while (ret && contd);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
474 if (ret) { /* stash final part of line on list */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
475 stc->text.data = (unsigned char *) ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
476 stc->text.size = n;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
477 /* determine how large a buffer we need */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
478 for (n = 0, stc = stl; stc; stc = stc->next) n += stc->text.size;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
479 ret = fs_get (n + 1); /* copy parts into buffer */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
480 for (n = 0, stc = stl; stc; n += stc->text.size, stc = stc->next)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
481 memcpy (ret + n,stc->text.data,stc->text.size);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
482 ret[n] = '\0';
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
483 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
484 mail_free_stringlist (&stl);/* either way, done with list */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
485 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
486 return ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
487 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
488
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
489 /* TCP receive line or partial line
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
490 * Accepts: TCP stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
491 * pointer to return size
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
492 * pointer to return continuation flag
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
493 * Returns: text line string, size and continuation flag, or NIL if failure
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
494 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
495
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
496 static char *tcp_getline_work (TCPSTREAM *stream,unsigned long *size,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
497 long *contd)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
498 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
499 unsigned long n;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
500 char *s,*ret,c,d;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
501 *contd = NIL; /* assume no continuation */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
502 /* make sure have data */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
503 if (!tcp_getdata (stream)) return NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
504 for (s = stream->iptr, n = 0, c = '\0'; stream->ictr--; n++, c = d) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
505 d = *stream->iptr++; /* slurp another character */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
506 if ((c == '\015') && (d == '\012')) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
507 ret = (char *) fs_get (n--);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
508 memcpy (ret,s,*size = n); /* copy into a free storage string */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
509 ret[n] = '\0'; /* tie off string with null */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
510 return ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
511 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
512 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
513 /* copy partial string from buffer */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
514 memcpy ((ret = (char *) fs_get (n)),s,*size = n);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
515 /* get more data from the net */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
516 if (!tcp_getdata (stream)) fs_give ((void **) &ret);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
517 /* special case of newline broken by buffer */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
518 else if ((c == '\015') && (*stream->iptr == '\012')) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
519 stream->iptr++; /* eat the line feed */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
520 stream->ictr--;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
521 ret[*size = --n] = '\0'; /* tie off string with null */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
522 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
523 else *contd = LONGT; /* continuation needed */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
524 return ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
525 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
526
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
527 /* TCP/IP receive buffer
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
528 * Accepts: TCP/IP stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
529 * size in bytes
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
530 * buffer to read into
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
531 * Returns: T if success, NIL otherwise
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
532 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
533
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
534 long tcp_getbuffer (TCPSTREAM *stream,unsigned long size,char *s)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
535 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
536 unsigned long n;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
537 /* make sure socket still alive */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
538 if (stream->tcpsi < 0) return NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
539 /* can transfer bytes from buffer? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
540 if (n = min (size,stream->ictr)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
541 memcpy (s,stream->iptr,n); /* yes, slurp as much as we can from it */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
542 s += n; /* update pointer */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
543 stream->iptr +=n;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
544 size -= n; /* update # of bytes to do */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
545 stream->ictr -=n;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
546 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
547 if (size) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
548 int i;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
549 fd_set fds,efds;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
550 struct timeval tmo;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
551 time_t t = time (0);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
552 blocknotify_t bn=(blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
553 (*bn) (BLOCK_TCPREAD,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
554 while (size > 0) { /* until request satisfied */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
555 time_t tl = time (0);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
556 time_t now = tl;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
557 time_t ti = ttmo_read ? now + ttmo_read : 0;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
558 if (tcpdebug) mm_log ("Reading TCP buffer",TCPDEBUG);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
559 tmo.tv_usec = 0;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
560 FD_ZERO (&fds); /* initialize selection vector */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
561 FD_ZERO (&efds); /* handle errors too */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
562 /* set bit in selection vectors */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
563 FD_SET (stream->tcpsi,&fds);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
564 FD_SET (stream->tcpsi,&efds);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
565 errno = NIL; /* initially no error */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
566 do { /* block under timeout */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
567 tmo.tv_sec = ti ? ti - now : 0;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
568 i = select (stream->tcpsi+1,&fds,NIL,&efds,ti ? &tmo : NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
569 now = time (0); /* fake timeout if interrupt & time expired */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
570 if ((i < 0) && (errno == EINTR) && ti && (ti <= now)) i = 0;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
571 } while ((i < 0) && (errno == EINTR));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
572 if (i) { /* non-timeout result from select? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
573 if (i > 0) /* read what we can */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
574 while (((i = read (stream->tcpsi,s,(int) min (maxposint,size))) < 0)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
575 && (errno == EINTR));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
576 if (i <= 0) { /* error seen? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
577 if (tcpdebug) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
578 char tmp[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
579 if (i) sprintf (s = tmp,"TCP buffer read I/O error %d",errno);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
580 else s = "TCP buffer read end of file";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
581 mm_log (s,TCPDEBUG);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
582 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
583 return tcp_abort (stream);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
584 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
585 s += i; /* success, point at new place to write */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
586 size -= i; /* reduce byte count */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
587 if (tcpdebug) mm_log ("Successfully read TCP buffer",TCPDEBUG);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
588 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
589 /* timeout, punt unless told not to */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
590 else if (!tmoh || !(*tmoh) (now - t,now - tl)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
591 if (tcpdebug) mm_log ("TCP buffer read timeout",TCPDEBUG);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
592 return tcp_abort (stream);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
593 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
594 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
595 (*bn) (BLOCK_NONE,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
596 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
597 *s = '\0'; /* tie off string */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
598 return LONGT;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
599 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
600
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
601 /* TCP/IP receive data
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
602 * Accepts: TCP/IP stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
603 * Returns: T if success, NIL otherwise
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
604 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
605
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
606 long tcp_getdata (TCPSTREAM *stream)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
607 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
608 int i;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
609 fd_set fds,efds;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
610 struct timeval tmo;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
611 time_t t = time (0);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
612 blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
613 if (stream->tcpsi < 0) return NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
614 (*bn) (BLOCK_TCPREAD,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
615 while (stream->ictr < 1) { /* if nothing in the buffer */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
616 time_t tl = time (0); /* start of request */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
617 time_t now = tl;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
618 time_t ti = ttmo_read ? now + ttmo_read : 0;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
619 if (tcpdebug) mm_log ("Reading TCP data",TCPDEBUG);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
620 tmo.tv_usec = 0;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
621 FD_ZERO (&fds); /* initialize selection vector */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
622 FD_ZERO (&efds); /* handle errors too */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
623 FD_SET (stream->tcpsi,&fds);/* set bit in selection vectors */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
624 FD_SET (stream->tcpsi,&efds);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
625 errno = NIL; /* initially no error */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
626 do { /* block under timeout */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
627 tmo.tv_sec = ti ? ti - now : 0;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
628 i = select (stream->tcpsi+1,&fds,NIL,&efds,ti ? &tmo : NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
629 now = time (0); /* fake timeout if interrupt & time expired */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
630 if ((i < 0) && (errno == EINTR) && ti && (ti <= now)) i = 0;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
631 } while ((i < 0) && (errno == EINTR));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
632 if (i) { /* non-timeout result from select? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
633 /* read what we can */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
634 if (i > 0) while (((i = read (stream->tcpsi,stream->ibuf,BUFLEN)) < 0) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
635 (errno == EINTR));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
636 if (i <= 0) { /* error seen? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
637 if (tcpdebug) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
638 char *s,tmp[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
639 if (i) sprintf (s = tmp,"TCP data read I/O error %d",errno);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
640 else s = "TCP data read end of file";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
641 mm_log (s,TCPDEBUG);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
642 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
643 return tcp_abort (stream);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
644 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
645 stream->ictr = i; /* success, set new count and pointer */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
646 stream->iptr = stream->ibuf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
647 if (tcpdebug) mm_log ("Successfully read TCP data",TCPDEBUG);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
648 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
649 /* timeout, punt unless told not to */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
650 else if (!tmoh || !(*tmoh) (now - t,now - tl)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
651 if (tcpdebug) mm_log ("TCP data read timeout",TCPDEBUG);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
652 return tcp_abort (stream);/* error or timeout no-continue */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
653 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
654 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
655 (*bn) (BLOCK_NONE,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
656 return T;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
657 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
658
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
659 /* TCP/IP send string as record
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
660 * Accepts: TCP/IP stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
661 * string pointer
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
662 * Returns: T if success else NIL
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
663 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
664
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
665 long tcp_soutr (TCPSTREAM *stream,char *string)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
666 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
667 return tcp_sout (stream,string,(unsigned long) strlen (string));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
668 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
669
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
670
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
671 /* TCP/IP send string
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
672 * Accepts: TCP/IP stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
673 * string pointer
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
674 * byte count
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
675 * Returns: T if success else NIL
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
676 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
677
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
678 long tcp_sout (TCPSTREAM *stream,char *string,unsigned long size)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
679 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
680 int i;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
681 fd_set fds,efds;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
682 struct timeval tmo;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
683 time_t t = time (0);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
684 blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
685 if (stream->tcpso < 0) return NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
686 (*bn) (BLOCK_TCPWRITE,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
687 while (size > 0) { /* until request satisfied */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
688 time_t tl = time (0); /* start of request */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
689 time_t now = tl;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
690 time_t ti = ttmo_write ? now + ttmo_write : 0;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
691 if (tcpdebug) mm_log ("Writing to TCP",TCPDEBUG);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
692 tmo.tv_usec = 0;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
693 FD_ZERO (&fds); /* initialize selection vector */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
694 FD_ZERO (&efds); /* handle errors too */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
695 FD_SET (stream->tcpso,&fds);/* set bit in selection vector */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
696 FD_SET(stream->tcpso,&efds);/* set bit in error selection vector */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
697 errno = NIL; /* block and write */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
698 do { /* block under timeout */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
699 tmo.tv_sec = ti ? ti - now : 0;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
700 i = select (stream->tcpso+1,NIL,&fds,&efds,ti ? &tmo : NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
701 now = time (0); /* fake timeout if interrupt & time expired */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
702 if ((i < 0) && (errno == EINTR) && ti && (ti <= now)) i = 0;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
703 } while ((i < 0) && (errno == EINTR));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
704 if (i) { /* non-timeout result from select? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
705 /* write what we can */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
706 if (i > 0) while (((i = write (stream->tcpso,string,size)) < 0) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
707 (errno == EINTR));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
708 if (i <= 0) { /* error seen? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
709 if (tcpdebug) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
710 char tmp[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
711 sprintf (tmp,"TCP write I/O error %d",errno);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
712 mm_log (tmp,TCPDEBUG);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
713 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
714 return tcp_abort (stream);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
715 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
716 string += i; /* how much we sent */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
717 size -= i; /* count this size */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
718 if (tcpdebug) mm_log ("successfully wrote to TCP",TCPDEBUG);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
719 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
720 /* timeout, punt unless told not to */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
721 else if (!tmoh || !(*tmoh) (now - t,now - tl)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
722 if (tcpdebug) mm_log ("TCP write timeout",TCPDEBUG);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
723 return tcp_abort (stream);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
724 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
725 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
726 (*bn) (BLOCK_NONE,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
727 return T; /* all done */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
728 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
729
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
730 /* TCP/IP close
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
731 * Accepts: TCP/IP stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
732 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
733
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
734 void tcp_close (TCPSTREAM *stream)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
735 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
736 tcp_abort (stream); /* nuke the stream */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
737 /* flush host names */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
738 if (stream->host) fs_give ((void **) &stream->host);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
739 if (stream->remotehost) fs_give ((void **) &stream->remotehost);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
740 if (stream->localhost) fs_give ((void **) &stream->localhost);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
741 fs_give ((void **) &stream); /* flush the stream */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
742 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
743
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
744
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
745 /* TCP/IP abort stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
746 * Accepts: TCP/IP stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
747 * Returns: NIL always
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
748 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
749
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
750 long tcp_abort (TCPSTREAM *stream)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
751 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
752 blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
753 if (stream->tcpsi >= 0) { /* no-op if no socket */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
754 (*bn) (BLOCK_TCPCLOSE,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
755 close (stream->tcpsi); /* nuke the socket */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
756 if (stream->tcpsi != stream->tcpso) close (stream->tcpso);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
757 stream->tcpsi = stream->tcpso = -1;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
758 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
759 (*bn) (BLOCK_NONE,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
760 return NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
761 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
762
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
763 /* TCP/IP get host name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
764 * Accepts: TCP/IP stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
765 * Returns: host name for this stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
766 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
767
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
768 char *tcp_host (TCPSTREAM *stream)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
769 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
770 return stream->host; /* use tcp_remotehost() if want guarantees */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
771 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
772
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
773
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
774 /* TCP/IP get remote host name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
775 * Accepts: TCP/IP stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
776 * Returns: host name for this stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
777 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
778
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
779 char *tcp_remotehost (TCPSTREAM *stream)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
780 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
781 if (!stream->remotehost) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
782 size_t sadrlen;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
783 struct sockaddr *sadr = ip_newsockaddr (&sadrlen);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
784 stream->remotehost = /* get socket's peer name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
785 getpeername (stream->tcpsi,sadr,(void *) &sadrlen) ?
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
786 cpystr (stream->host) : tcp_name (sadr,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
787 fs_give ((void **) &sadr);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
788 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
789 return stream->remotehost;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
790 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
791
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
792
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
793 /* TCP/IP return port for this stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
794 * Accepts: TCP/IP stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
795 * Returns: port number for this stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
796 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
797
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
798 unsigned long tcp_port (TCPSTREAM *stream)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
799 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
800 return stream->port; /* return port number */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
801 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
802
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
803
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
804 /* TCP/IP get local host name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
805 * Accepts: TCP/IP stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
806 * Returns: local host name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
807 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
808
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
809 char *tcp_localhost (TCPSTREAM *stream)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
810 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
811 if (!stream->localhost) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
812 size_t sadrlen;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
813 struct sockaddr *sadr = ip_newsockaddr (&sadrlen);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
814 stream->localhost = /* get socket's name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
815 ((stream->port & 0xffff000) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
816 getsockname (stream->tcpsi,sadr,(void *) &sadrlen)) ?
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
817 cpystr (mylocalhost ()) : tcp_name (sadr,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
818 fs_give ((void **) &sadr);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
819 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
820 return stream->localhost; /* return local host name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
821 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
822
4
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
823 /* Get $TCPREMOTEHOST || $TCPREMOTEHOST
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
824 * If invoked via tcpserver or couriertcpd, consult $TCPREMOTEHOST
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
825 * or $TCPREMOTEIP
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
826 */
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
827 char* gettcpremoteip()
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
828 {
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
829 if (getenv("TCPREMOTEHOST") && strcmp("0", getenv("TCPREMOTEHOST"))) {
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
830 return getenv("TCPREMOTEHOST");
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
831 } else if (getenv("TCPREMOTEIP")) {
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
832 return getenv("TCPREMOTEIP");
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
833 }
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
834 return "NON-IPv4";
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
835 }
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
836
0
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
837 /* TCP/IP get client host address (server calls only)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
838 * Returns: client host address
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
839 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
840
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
841 char *tcp_clientaddr ()
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
842 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
843 if (!myClientAddr) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
844 size_t sadrlen;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
845 struct sockaddr *sadr = ip_newsockaddr (&sadrlen);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
846 if (getpeername (0,sadr,(void *) &sadrlen))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
847 myClientAddr = cpystr ("UNKNOWN");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
848 else { /* get stdin's peer name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
849 myClientAddr = cpystr (ip_sockaddrtostring (sadr));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
850 if (myClientPort < 0) myClientPort = ip_sockaddrtoport (sadr);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
851 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
852 fs_give ((void **) &sadr);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
853 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
854 return myClientAddr;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
855 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
856
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
857
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
858 /* TCP/IP get client host name (server calls only)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
859 * Returns: client host name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
860 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
861
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
862 char *tcp_clienthost ()
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
863 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
864 if (!myClientHost) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
865 size_t sadrlen;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
866 struct sockaddr *sadr = ip_newsockaddr (&sadrlen);
4
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
867 #ifdef INET6
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
868 if (getenv("TCPREMOTEIP")) {
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
869 myClientHost =
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
870 cpystr((getenv("TCPREMOTEHOST")
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
871 && strcasecmp("UNKNOWN", getenv("TCPREMOTEHOST")))
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
872 ? getenv("TCPREMOTEHOST")
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
873 : getenv("TCPREMOTEIP"));
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
874 } else
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
875 #endif
0
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
876 if (getpeername (0,sadr,(void *) &sadrlen)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
877 char *s,*t,*v,tmp[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
878 if ((s = getenv (t = "SSH_CLIENT")) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
879 (s = getenv (t = "KRB5REMOTEADDR")) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
880 (s = getenv (t = "SSH2_CLIENT"))) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
881 if (v = strchr (s,' ')) *v = '\0';
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
882 sprintf (v = tmp,"%.80s=%.80s",t,s);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
883 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
884 else v = "UNKNOWN";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
885 myClientHost = cpystr (v);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
886 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
887 else { /* get stdin's peer name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
888 myClientHost = tcp_name (sadr,T);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
889 if (!myClientAddr) myClientAddr = cpystr (ip_sockaddrtostring (sadr));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
890 if (myClientPort < 0) myClientPort = ip_sockaddrtoport (sadr);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
891 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
892 fs_give ((void **) &sadr);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
893 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
894 return myClientHost;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
895 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
896
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
897
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
898 /* TCP/IP get client port number (server calls only)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
899 * Returns: client port number
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
900 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
901
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
902 long tcp_clientport ()
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
903 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
904 if (!myClientHost && !myClientAddr) tcp_clientaddr ();
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
905 return myClientPort;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
906 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
907
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
908 /* TCP/IP get server host address (server calls only)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
909 * Returns: server host address
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
910 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
911
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
912 char *tcp_serveraddr ()
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
913 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
914 if (!myServerAddr) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
915 size_t sadrlen;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
916 struct sockaddr *sadr = ip_newsockaddr (&sadrlen);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
917 if (getsockname (0,sadr,(void *) &sadrlen))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
918 myServerAddr = cpystr ("UNKNOWN");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
919 else { /* get stdin's name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
920 myServerAddr = cpystr (ip_sockaddrtostring (sadr));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
921 if (myServerPort < 0) myServerPort = ip_sockaddrtoport (sadr);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
922 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
923 fs_give ((void **) &sadr);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
924 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
925 return myServerAddr;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
926 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
927
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
928
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
929 /* TCP/IP get server host name (server calls only)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
930 * Returns: server host name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
931 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
932
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
933 char *tcp_serverhost ()
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
934 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
935 if (!myServerHost) { /* once-only */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
936 size_t sadrlen;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
937 struct sockaddr *sadr = ip_newsockaddr (&sadrlen);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
938 /* get stdin's name */
4
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
939 if (getsockname (0,sadr,(void *) &sadrlen) ||
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
940 (myServerPort = ip_sockaddrtoport (sadr)) < 0)
0
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
941 myServerHost = cpystr (mylocalhost ());
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
942 else { /* get stdin's name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
943 myServerHost = tcp_name (sadr,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
944 if (!myServerAddr) myServerAddr = cpystr (ip_sockaddrtostring (sadr));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
945 if (myServerPort < 0) myServerPort = ip_sockaddrtoport (sadr);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
946 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
947 fs_give ((void **) &sadr);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
948 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
949 return myServerHost;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
950 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
951
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
952
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
953 /* TCP/IP get server port number (server calls only)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
954 * Returns: server port number
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
955 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
956
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
957 long tcp_serverport ()
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
958 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
959 if (!myServerHost && !myServerAddr) tcp_serveraddr ();
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
960 return myServerPort;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
961 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
962
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
963 /* TCP/IP return canonical form of host name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
964 * Accepts: host name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
965 * Returns: canonical form of host name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
966 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
967
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
968 char *tcp_canonical (char *name)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
969 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
970 char *ret,host[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
971 blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
972 void *data;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
973 /* look like domain literal? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
974 if (name[0] == '[' && name[strlen (name) - 1] == ']') return name;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
975 (*bn) (BLOCK_DNSLOOKUP,NIL); /* quell alarms */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
976 data = (*bn) (BLOCK_SENSITIVE,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
977 if (tcpdebug) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
978 sprintf (host,"DNS canonicalization %.80s",name);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
979 mm_log (host,TCPDEBUG);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
980 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
981 /* get canonical name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
982 if (!ip_nametoaddr (name,NIL,NIL,&ret,NIL)) ret = name;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
983 (*bn) (BLOCK_NONSENSITIVE,data);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
984 (*bn) (BLOCK_NONE,NIL); /* alarms OK now */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
985 if (tcpdebug) mm_log ("DNS canonicalization done",TCPDEBUG);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
986 return ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
987 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
988
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
989 /* TCP/IP return name from socket
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
990 * Accepts: socket
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
991 * verbose flag
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
992 * Returns: cpystr name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
993 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
994
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
995 char *tcp_name (struct sockaddr *sadr,long flag)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
996 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
997 char *ret,*t,adr[MAILTMPLEN],tmp[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
998 sprintf (ret = adr,"[%.80s]",ip_sockaddrtostring (sadr));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
999 if (allowreversedns) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1000 blocknotify_t bn = (blocknotify_t)mail_parameters(NIL,GET_BLOCKNOTIFY,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1001 void *data;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1002 if (tcpdebug) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1003 sprintf (tmp,"Reverse DNS resolution %s",adr);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1004 mm_log (tmp,TCPDEBUG);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1005 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1006 (*bn) (BLOCK_DNSLOOKUP,NIL);/* quell alarms */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1007 data = (*bn) (BLOCK_SENSITIVE,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1008 /* translate address to name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1009 if (t = tcp_name_valid (ip_sockaddrtoname (sadr))) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1010 /* produce verbose form if needed */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1011 if (flag) sprintf (ret = tmp,"%s %s",t,adr);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1012 else ret = t;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1013 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1014 (*bn) (BLOCK_NONSENSITIVE,data);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1015 (*bn) (BLOCK_NONE,NIL); /* alarms OK now */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1016 if (tcpdebug) mm_log ("Reverse DNS resolution done",TCPDEBUG);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1017 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1018 return cpystr (ret);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1019 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1020
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1021
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1022 /* TCP/IP validate name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1023 * Accepts: domain name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1024 * Returns: name if valid, NIL otherwise
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1025 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1026
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1027 char *tcp_name_valid (char *s)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1028 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1029 int c;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1030 char *ret,*tail;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1031 /* must be non-empty and not too long */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1032 if ((ret = (s && *s) ? s : NIL) && (tail = ret + NETMAXHOST)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1033 /* must be alnum, dot, or hyphen */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1034 while ((c = *s++) && (s <= tail) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1035 (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1036 ((c >= '0') && (c <= '9')) || (c == '-') || (c == '.')));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1037 if (c) ret = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1038 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1039 return ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1040 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1041
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1042 /* TCP/IP check if client is given host name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1043 * Accepts: candidate host name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1044 * Returns: T if match, NIL otherwise
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1045 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1046
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1047 long tcp_isclienthost (char *host)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1048 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1049 int family;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1050 size_t adrlen,sadrlen,len;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1051 void *adr,*next;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1052 struct sockaddr *sadr;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1053 long ret = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1054 /* make sure that myClientAddr is set */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1055 if (tcp_clienthost () && myClientAddr)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1056 /* get sockaddr of client */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1057 for (adr = ip_nametoaddr (host,&adrlen,&family,NIL,&next); adr && !ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1058 adr = ip_nametoaddr (NIL,&adrlen,&family,NIL,&next)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1059 /* build sockaddr of given address */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1060 sadr = ip_sockaddr (family,adr,adrlen,1,&len);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1061 if (!strcmp (myClientAddr,ip_sockaddrtostring (sadr))) ret = LONGT;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1062 fs_give ((void **) &sadr); /* done with client sockaddr */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1063 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1064 return ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1065 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1066
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1067 /* Following statement must be at end of this module */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1068
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1069 #undef fork /* undo any use of vfork() */

yatex.org