1 /* $Cambridge: exim/src/src/ip.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 /* Copyright (c) University of Cambridge 1995 - 2004 */
8 /* See the file NOTICE for conditions of use and distribution. */
10 /* Functions for doing things with sockets. With the advent of IPv6 this has
11 got messier, so that it's worth pulling out the code into separate functions
12 that other parts of Exim can call, expecially as there are now several
13 different places in the code where sockets are used. */
19 /*************************************************
21 *************************************************/
23 /* Socket creation happens in a number of places so it's packaged here for
27 type SOCK_DGRAM or SOCK_STREAM
28 af AF_INET or AF_INET6
30 Returns: socket number or -1 on failure
34 ip_socket(int type, int af)
36 int sock = socket(af, type, 0);
38 log_write(0, LOG_MAIN, "IPv%c socket creation failed: %s",
39 (af == AF_INET6)? '6':'4', strerror(errno));
47 /*************************************************
48 * Convert printing address to numeric *
49 *************************************************/
51 /* This function converts the textual form of an IP address into a numeric form
52 in an appropriate structure in an IPv6 environment. The getaddrinfo() function
53 can (apparently) handle more complicated addresses (e.g. those containing
54 scopes) than inet_pton() in some environments. We use hints to tell it that the
55 input must be a numeric address.
57 However, apparently some operating systems (or libraries) don't support
58 getaddrinfo(), so there is a build-time option to revert to inet_pton() (which
59 does not support scopes).
62 address textual form of the address
63 addr where to copy back the answer
65 Returns: nothing - failure provokes a panic-die
69 ip_addrinfo(uschar *address, struct sockaddr_in6 *saddr)
71 #ifdef IPV6_USE_INET_PTON
73 if (inet_pton(AF_INET6, CS address, &saddr->sin6_addr) != 1)
74 log_write(0, LOG_MAIN|LOG_PANIC_DIE, "unable to parse \"%s\" as an "
75 "IP address", address);
76 saddr->sin6_family = AF_INET6;
81 struct addrinfo hints, *res;
82 memset(&hints, 0, sizeof(hints));
83 hints.ai_family = AF_INET6;
84 hints.ai_socktype = SOCK_STREAM;
85 hints.ai_flags = AI_NUMERICHOST;
86 if ((rc = getaddrinfo(CS address, NULL, &hints, &res)) != 0 || res == NULL)
87 log_write(0, LOG_MAIN|LOG_PANIC_DIE, "unable to parse \"%s\" as an "
88 "IP address: %s", address,
89 (rc == 0)? "NULL result returned" : gai_strerror(rc));
90 memcpy(saddr, res->ai_addr, res->ai_addrlen);
95 #endif /* HAVE_IPV6 */
98 /*************************************************
99 * Bind socket to interface and port *
100 *************************************************/
102 /* This function binds a socket to a local interface address and port. For a
103 wildcard IPv6 bind, the address is ":".
107 af AF_INET or AF_INET6 - the socket type
108 address the IP address, in text form
109 port the IP port (host order)
111 Returns: the result of bind()
115 ip_bind(int sock, int af, uschar *address, int port)
118 union sockaddr_46 sin;
119 memset(&sin, 0, sizeof(sin));
121 /* Setup code when using an IPv6 socket. The wildcard address is ":", to
122 ensure an IPv6 socket is used. */
127 if (address[0] == ':' && address[1] == 0)
129 sin.v6.sin6_family = AF_INET6;
130 sin.v6.sin6_addr = in6addr_any;
134 ip_addrinfo(address, &sin.v6); /* Panic-dies on error */
136 sin.v6.sin6_port = htons(port);
137 s_len = sizeof(sin.v6);
140 #else /* HAVE_IPv6 */
141 af = af; /* Avoid compiler warning */
142 #endif /* HAVE_IPV6 */
144 /* Setup code when using IPv4 socket. The wildcard address is "". */
147 sin.v4.sin_family = AF_INET;
148 sin.v4.sin_port = htons(port);
149 s_len = sizeof(sin.v4);
151 sin.v4.sin_addr.s_addr = (S_ADDR_TYPE)INADDR_ANY;
153 sin.v4.sin_addr.s_addr = (S_ADDR_TYPE)inet_addr(CS address);
156 /* Now we can call the bind() function */
158 return bind(sock, (struct sockaddr *)&sin, s_len);
163 /*************************************************
164 * Connect socket to remote host *
165 *************************************************/
167 /* This function connects a socket to a remote address and port. The socket may
168 or may not have previously been bound to a local interface.
172 af AF_INET6 or AF_INET for the socket type
173 address the remote address, in text form
177 Returns: 0 on success; -1 on failure, with errno set
181 ip_connect(int sock, int af, uschar *address, int port, int timeout)
183 struct sockaddr_in s_in4;
184 struct sockaddr *s_ptr;
185 int s_len, rc, save_errno;
187 /* For an IPv6 address, use an IPv6 sockaddr structure. */
190 struct sockaddr_in6 s_in6;
193 memset(&s_in6, 0, sizeof(s_in6));
194 ip_addrinfo(address, &s_in6); /* Panic-dies on error */
195 s_in6.sin6_port = htons(port);
196 s_ptr = (struct sockaddr *)&s_in6;
197 s_len = sizeof(s_in6);
200 #else /* HAVE_IPV6 */
201 af = af; /* Avoid compiler warning */
202 #endif /* HAVE_IPV6 */
204 /* For an IPv4 address, use an IPv4 sockaddr structure, even on a system with
208 memset(&s_in4, 0, sizeof(s_in4));
209 s_in4.sin_family = AF_INET;
210 s_in4.sin_port = htons(port);
211 s_in4.sin_addr.s_addr = (S_ADDR_TYPE)inet_addr(CS address);
212 s_ptr = (struct sockaddr *)&s_in4;
213 s_len = sizeof(s_in4);
216 /* If no connection timeout is set, just call connect() without setting a
217 timer, thereby allowing the inbuilt OS timeout to operate. */
219 sigalrm_seen = FALSE;
220 if (timeout > 0) alarm(timeout);
221 rc = connect(sock, s_ptr, s_len);
225 /* There is a testing facility for simulating a connection timeout, as I
226 can't think of any other way of doing this. It converts a connection refused
229 I had to add a second fudge to keep the tests working. Attempts to connect to
230 10.x.x.x are expected to timeout, but sometimes they now give "No route to
233 if (running_in_test_harness)
235 if ((save_errno == ECONNREFUSED && timeout == 999999) ||
236 (save_errno == EHOSTUNREACH && timeout > 0 &&
237 Ustrncmp(address, "10.", 3) == 0))
247 if (rc >= 0) return 0;
249 /* A failure whose error code is "Interrupted system call" is in fact
250 an externally applied timeout if the signal handler has been run. */
253 errno = (save_errno == EINTR && sigalrm_seen)? ETIMEDOUT : save_errno;
259 /*************************************************
260 * Set keepalive on a socket *
261 *************************************************/
263 /* Can be called for both incoming and outgoing sockets.
267 address the remote host address, for failure logging
268 torf true for outgoing connection, false for incoming
274 ip_keepalive(int sock, uschar *address, BOOL torf)
277 if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
278 (uschar *)(&fodder), sizeof(fodder)) != 0)
279 log_write(0, LOG_MAIN, "setsockopt(SO_KEEPALIVE) on connection %s %s "
280 "failed: %s", torf? "to":"from", address, strerror(errno));
285 /*************************************************
286 * Receive from a socket with timeout *
287 *************************************************/
289 /* The timeout is implemented using select(), and we loop to cover select()
290 getting interrupted, and the possibility of select() returning with a positive
291 result but no ready descriptor. Is this in fact possible?
296 bufsize the buffer size
299 Returns: > 0 => that much data read
300 <= 0 on error or EOF; errno set - zero for EOF
304 ip_recv(int sock, uschar *buffer, int buffsize, int timeout)
308 int start_recv = time(NULL);
311 /* Wait until the socket is ready */
315 FD_ZERO (&select_inset);
316 FD_SET (sock, &select_inset);
320 DEBUG(D_transport) debug_printf("waiting for data on socket\n");
321 rc = select(sock + 1, (SELECT_ARG2_TYPE *)&select_inset, NULL, NULL, &tv);
323 /* If some interrupt arrived, just retry. We presume this to be rare,
324 but it can happen (e.g. the SIGUSR1 signal sent by exiwhat causes
327 Aug 2004: Somebody set up a cron job that ran exiwhat every 2 minutes, making
328 the interrupt not at all rare. Since the timeout is typically more than 2
329 minutes, the effect was to block the timeout completely. To prevent this
330 happening again, we do an explicit time test. */
332 if (rc < 0 && errno == EINTR)
334 DEBUG(D_transport) debug_printf("EINTR while waiting for socket data\n");
335 if (time(NULL) - start_recv < timeout) continue;
336 DEBUG(D_transport) debug_printf("total wait time exceeds timeout\n");
339 /* Handle a timeout, and treat any other select error as a timeout, including
340 an EINTR when we have been in this loop for longer than timeout. */
348 /* If the socket is ready, break out of the loop. */
350 if (FD_ISSET(sock, &select_inset)) break;
353 /* The socket is ready, read from it (via TLS if it's active). On EOF (i.e.
354 close down of the connection), set errno to zero; otherwise leave it alone. */
357 if (tls_active == sock)
358 rc = tls_read(buffer, buffsize);
361 rc = recv(sock, buffer, buffsize, 0);
363 if (rc > 0) return rc;
364 if (rc == 0) errno = 0;