Improve support for IPv4 vs IPv6 virtual hosts (fixes SF bugs #1087699, #1087668).
[ircu2.10.12-pk.git] / ircd / os_generic.c
1 /*
2  * IRC - Internet Relay Chat, ircd/os_generic.c
3  * Copyright (C) 1999 Thomas Helvey
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 1, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /** @file
20  * @brief Implementation of OS-dependent operations.
21  * @version $Id$
22  */
23 #include "config.h"
24
25 #define _XOPEN_SOURCE   500 /**< make limits.h #define IOV_MAX */
26 #define __EXTENSIONS__  1   /**< make Solaris netinet/in.h know IPv6 */
27
28 #include "ircd_osdep.h"
29 #include "msgq.h"
30 #include "ircd_log.h"
31 #include "res.h"
32 #include "s_bsd.h"
33 #include "sys.h"
34
35 /* Include file dependency notes:
36  * FreeBSD requires struct timeval from sys/time.h before struct
37  * rusage in sys/resource.h.
38  * Solaris requires sys/time.h before struct rusage (indirectly) in
39  * netinet/in.h.
40  */
41 /* #include <assert.h> -- Now using assert in ircd_log.h */
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <limits.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <sys/ioctl.h>
48 #include <sys/types.h>
49 #include <sys/time.h>
50 #include <netinet/in.h>
51 #include <sys/resource.h>
52 #include <sys/socket.h>
53 #include <sys/uio.h>
54
55 #if HAVE_SYS_PARAM_H
56 #include <sys/param.h>
57 #endif
58
59 #if HAVE_UNISTD_H
60 #include <unistd.h>
61 #endif
62
63 #ifndef IOV_MAX
64 #define IOV_MAX 16      /**< minimum required length of an iovec array */
65 #endif
66
67 #ifdef HPUX
68 #include <sys/syscall.h>
69 #define getrusage(a,b) syscall(SYS_GETRUSAGE, a, b)
70 #endif
71
72 #ifdef IPV6
73 /** Native socket address type. */
74 #define sockaddr_native sockaddr_in6
75 /** Field name inside sockaddr_native to find address family. */
76 #define sn_family sin6_family
77
78 /** Convert native socket address to IRC format.
79  * @param[in] v6 Native socket address.
80  * @param[out] irc IRC format socket address.
81  */
82 void sockaddr_to_irc(const struct sockaddr_in6 *v6, struct irc_sockaddr *irc)
83 {
84     if (v6->sin6_family == AF_INET6) {
85         memcpy(&irc->addr.in6_16[0], &v6->sin6_addr, sizeof(v6->sin6_addr));
86         irc->port = ntohs(v6->sin6_port);
87     }
88     else if (v6->sin6_family == AF_INET) {
89         const struct sockaddr_in *v4 = (const struct sockaddr_in*)v6;
90         memset(&irc->addr, 0, 5*sizeof(int16_t));
91         irc->addr.in6_16[5] = 0xffff;
92         memcpy(&irc->addr.in6_16[6], &v4->sin_addr, sizeof(v4->sin_addr));
93         irc->port = ntohs(v4->sin_port);
94     }
95     else assert(0 && "Unhandled native address family");
96 }
97
98 /** Convert IRC socket address to native format.
99  * @param[out] v6 Native socket address.
100  * @param[in] irc IRC socket address.
101  * @param[in] compat_fd If non-negative, an FD specifying address family.
102  * @return Length of address written to \a v6.
103  */
104 int sockaddr_from_irc(struct sockaddr_in6 *v6, const struct irc_sockaddr *irc, int compat_fd)
105 {
106     struct sockaddr_in6 sin6;
107     socklen_t slen;
108     int family;
109
110     assert(irc != 0);
111     slen = sizeof(sin6);
112     if ((0 <= compat_fd)
113         && (0 == getsockname(compat_fd, (struct sockaddr*)&sin6, &slen)))
114         family = sin6.sin6_family;
115     else if ((irc == &VirtualHost_v4) || irc_in_addr_is_ipv4(&irc->addr))
116         family = AF_INET;
117     else
118         family = AF_INET6;
119
120     memset(v6, 0, sizeof(*v6));
121     if (family == AF_INET) {
122         struct sockaddr_in *v4 = (struct sockaddr_in*)v6;
123         v4->sin_family = AF_INET;
124         memcpy(&v4->sin_addr, &irc->addr.in6_16[6], sizeof(v4->sin_addr));
125         v4->sin_port = htons(irc->port);
126         return sizeof(*v4);
127     }
128     else {
129         v6->sin6_family = AF_INET6;
130         memcpy(&v6->sin6_addr, &irc->addr.in6_16[0], sizeof(v6->sin6_addr));
131         v6->sin6_port = htons(irc->port);
132         return sizeof(*v6);
133     }
134 }
135
136 #else
137 #define sockaddr_native sockaddr_in
138 #define sn_family sin_family
139
140 void sockaddr_to_irc(const struct sockaddr_in *v4, struct irc_sockaddr *irc)
141 {
142     assert(v4->sin_family == AF_INET);
143     memset(&irc->addr, 0, 6*sizeof(irc->addr.in6_16[0]));
144     memcpy(&irc->addr.in6_16[6], &v4->sin_addr, sizeof(v4->sin_addr));
145     irc->port = ntohs(v4->sin_port);
146 }
147
148 int sockaddr_from_irc(struct sockaddr_in *v4, const struct irc_sockaddr *irc, int compat_fd)
149 {
150     assert(irc != 0);
151     v4->sin_family = AF_INET;
152     if (irc) {
153         assert(!irc->addr.in6_16[0] && !irc->addr.in6_16[1] && !irc->addr.in6_16[2] && !irc->addr.in6_16[3] && !irc->addr.in6_16[4] && (!irc->addr.in6_16[5] || irc->addr.in6_16[5] == 0xffff));
154         memcpy(&v4->sin_addr, &irc->addr.in6_16[6], sizeof(v4->sin_addr));
155         v4->sin_port = htons(irc->port);
156     } else{
157         memset(&v4, 0, sizeof(v4));
158     }
159     (void)compat_fd;
160     return sizeof(*v4);
161 }
162
163 #endif
164
165 /*
166  * This is part of the STATS replies. There is no offical numeric for this
167  * since this isnt an official command, in much the same way as HASH isnt.
168  * It is also possible that some systems wont support this call or have
169  * different field names for "struct rusage".
170  * -avalon
171  */
172 /** Send resource usage information to a client.
173  * @param[in] cptr Client requesting information.
174  * @param[in] uptime Wall time in seconds since the server started.
175  * @param[in] enumerator Function to call to send a line to \a cptr.
176  * @return Zero if some usage reports could not be sent, non-zero on success.
177  */
178 int os_get_rusage(struct Client *cptr, int uptime, EnumFn enumerator)
179 {
180 #ifdef HAVE_GETRUSAGE
181   char buf[256];
182   struct rusage rus;
183   time_t secs;
184
185 #ifdef  hz
186 #  define hzz hz
187 #else
188 #  ifdef HZ
189 #    define hzz HZ
190 #  else
191   int hzz = 1;
192 #  ifdef HPUX
193   hzz = sysconf(_SC_CLK_TCK);
194 #  endif
195 #endif
196 #endif
197
198   assert(0 != enumerator);
199   if (getrusage(RUSAGE_SELF, &rus) == -1)
200     return 0;
201
202   secs = rus.ru_utime.tv_sec + rus.ru_stime.tv_sec;
203   if (secs == 0)
204     secs = 1;
205
206   sprintf(buf, "CPU Secs %ld:%ld User %ld:%ld System %ld:%ld",
207           (long)(secs / 60), (long)(secs % 60),
208           rus.ru_utime.tv_sec / 60, rus.ru_utime.tv_sec % 60,
209           rus.ru_stime.tv_sec / 60, rus.ru_stime.tv_sec % 60);
210   (*enumerator)(cptr, buf);
211
212   sprintf(buf, "RSS %ld ShMem %ld Data %ld Stack %ld",
213           rus.ru_maxrss,
214           rus.ru_ixrss / (uptime * hzz), rus.ru_idrss / (uptime * hzz),
215           rus.ru_isrss / (uptime * hzz));
216   (*enumerator)(cptr, buf);
217
218   sprintf(buf, "Swaps %ld Reclaims %ld Faults %ld",
219           rus.ru_nswap, rus.ru_minflt, rus.ru_majflt);
220   (*enumerator)(cptr, buf);
221
222   sprintf(buf, "Block in %ld out %ld", rus.ru_inblock, rus.ru_oublock);
223   (*enumerator)(cptr, buf);
224
225   sprintf(buf, "Msg Rcv %ld Send %ld", rus.ru_msgrcv, rus.ru_msgsnd);
226   (*enumerator)(cptr, buf);
227
228   sprintf(buf, "Signals %ld Context Vol. %ld Invol %ld",
229           rus.ru_nsignals, rus.ru_nvcsw, rus.ru_nivcsw);
230   (*enumerator)(cptr, buf);
231
232 #else /* HAVE_GETRUSAGE */
233 #if HAVE_TIMES
234   char buf[256];
235   struct tms tmsbuf;
236   time_t secs, mins;
237   int hzz = 1, ticpermin;
238   int umin, smin, usec, ssec;
239
240   assert(0 != enumerator);
241 #ifdef HPUX
242   hzz = sysconf(_SC_CLK_TCK);
243 #endif
244   ticpermin = hzz * 60;
245
246   umin = tmsbuf.tms_utime / ticpermin;
247   usec = (tmsbuf.tms_utime % ticpermin) / (float)hzz;
248   smin = tmsbuf.tms_stime / ticpermin;
249   ssec = (tmsbuf.tms_stime % ticpermin) / (float)hzz;
250   secs = usec + ssec;
251   mins = (secs / 60) + umin + smin;
252   secs %= hzz;
253
254   if (times(&tmsbuf) == -1)
255     return 0;
256   secs = tmsbuf.tms_utime + tmsbuf.tms_stime;
257
258   sprintf(buf, "CPU Secs %d:%d User %d:%d System %d:%d", 
259           mins, secs, umin, usec, smin, ssec);
260   (*enumerator)(cptr, buf);
261 #endif /* HAVE_TIMES */
262 #endif /* HAVE_GETRUSAGE */
263   return 1;
264 }
265
266 /** Look up the most recent socket error for a socket file descriptor.
267  * @param[in] fd File descriptor to check.
268  * @return Error code from the socket, or 0 if the OS does not support this.
269  */
270 int os_get_sockerr(int fd)
271 {
272   int    err = 0;
273 #if defined(SO_ERROR)
274   unsigned int len = sizeof(err);
275   getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
276 #endif
277   return err;
278 }
279
280 /** Set a file descriptor to non-blocking mode.
281  * @param[in] fd %Socket file descriptor.
282  * @return Non-zero on success, or zero on failure.
283  */
284 int os_set_nonblocking(int fd)
285 {
286   int res;
287 #ifndef NBLOCK_SYSV
288   int nonb = 0;
289 #endif
290
291   /*
292    * NOTE: consult ALL your relevant manual pages *BEFORE* changing
293    * these ioctl's. There are quite a few variations on them,
294    * as can be seen by the PCS one. They are *NOT* all the same.
295    * Heed this well. - Avalon.
296    */
297 #ifdef  NBLOCK_POSIX
298   nonb |= O_NONBLOCK;
299 #endif
300 #ifdef  NBLOCK_BSD
301   nonb |= O_NDELAY;
302 #endif
303 #ifdef  NBLOCK_SYSV
304   /* This portion of code might also apply to NeXT. -LynX */
305   res = 1;
306
307   if (ioctl(fd, FIONBIO, &res) == -1)
308     return 0;
309 #else
310   if ((res = fcntl(fd, F_GETFL, 0)) == -1)
311     return 0;
312   else if (fcntl(fd, F_SETFL, res | nonb) == -1)
313     return 0;
314 #endif
315   return 1;
316 }
317
318 /** Mark a socket's address as reusable.
319  * @param[in] fd %Socket file descriptor to manipulate.
320  * @return Non-zero on success, or zero on failure.
321  */
322 int os_set_reuseaddr(int fd)
323 {
324   unsigned int opt = 1;
325   return (0 == setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
326                           (const char*) &opt, sizeof(opt)));
327 }
328
329 /** Set a socket's send and receive buffer sizes.
330  * @param[in] fd %Socket file descriptor to manipulate.
331  * @param[in] ssize New send buffer size.
332  * @param[in] rsize New receive buffer size.
333  * @return Non-zero on success, or zero on failure.
334  */
335 int os_set_sockbufs(int fd, unsigned int ssize, unsigned int rsize)
336 {
337   unsigned int sopt = ssize;
338   unsigned int ropt = rsize;
339   return (0 == setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
340                           (const char*) &ropt, sizeof(ropt)) &&
341           0 == setsockopt(fd, SOL_SOCKET, SO_SNDBUF,
342                           (const char*) &sopt, sizeof(sopt)));
343 }
344
345 /** Set a socket's "type of service" value.
346  * @param[in] fd %Socket file descriptor to manipulate.
347  * @param[in] tos New type of service value to use.
348  * @return Non-zero on success, or zero on failure.
349  */
350 int os_set_tos(int fd,int tos)
351 {
352 #if defined(IP_TOS) && defined(IPPROTO_IP)
353   unsigned int opt = tos;
354   return (0 == setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt)));
355 #else
356   return 1;
357 #endif
358 }
359
360 /** Disable IP options on a socket.
361  * @param[in] fd %Socket file descriptor to manipulate.
362  * @return Non-zero on success, or zero on failure.
363  */
364 int os_disable_options(int fd)
365 {
366 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
367   return (0 == setsockopt(fd, IPPROTO_IP, IP_OPTIONS, NULL, 0));
368 #else
369   return 1;
370 #endif
371 }
372
373 /*
374  * Try and find the correct name to use with getrlimit() for setting the max.
375  * number of files allowed to be open by this process.
376  */
377 #ifdef RLIMIT_FDMAX
378 #define RLIMIT_FD_MAX   RLIMIT_FDMAX
379 #else
380 #ifdef RLIMIT_NOFILE
381 #define RLIMIT_FD_MAX RLIMIT_NOFILE
382 #else
383 #ifdef RLIMIT_OPEN_MAX
384 #define RLIMIT_FD_MAX RLIMIT_OPEN_MAX
385 #else
386 #undef RLIMIT_FD_MAX
387 #endif
388 #endif
389 #endif
390
391 /** Set file descriptor limit for the process.
392  * @param[in] max_descriptors Ideal number of file descriptors.
393  * @return Zero on success; -1 on error; positive number of possible
394  * file descriptors if \a max_descriptors is too high.
395  */
396 int os_set_fdlimit(unsigned int max_descriptors)
397 {
398 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_FD_MAX)
399   struct rlimit limit;
400
401   if (!getrlimit(RLIMIT_FD_MAX, &limit)) {
402     if (limit.rlim_max < max_descriptors)
403       return limit.rlim_max;
404     limit.rlim_cur = limit.rlim_max;    /* make soft limit the max */
405     return setrlimit(RLIMIT_FD_MAX, &limit);
406   }
407 #endif /* defined(HAVE_SETRLIMIT) && defined(RLIMIT_FD_MAX) */
408   return 0;
409 }
410
411 /** Attempt to read from a non-blocking socket.
412  * @param[in] fd File descriptor to read from.
413  * @param[out] buf Output buffer to read into.
414  * @param[in] length Number of bytes to read.
415  * @param[out] count_out Receives number of bytes actually read.
416  * @return An IOResult value indicating status.
417  */
418 IOResult os_recv_nonb(int fd, char* buf, unsigned int length,
419                  unsigned int* count_out)
420 {
421   int res;
422   assert(0 != buf);
423   assert(0 != count_out);
424   *count_out = 0;
425   errno = 0;
426
427   if (0 < (res = recv(fd, buf, length, 0))) {
428     *count_out = (unsigned) res;
429     return IO_SUCCESS;
430   }
431   else if (res < 0) {
432     if (EWOULDBLOCK == errno || EAGAIN == errno
433 #ifdef ENOMEM
434         || ENOMEM == errno
435 #endif
436 #ifdef ENOBUFS
437         || ENOBUFS == errno
438 #endif
439         )
440       return IO_BLOCKED;
441     else
442       return IO_FAILURE;
443   }
444   /*
445    * 0   == client closed the connection
446    * < 1 == error
447    */
448   return IO_FAILURE;
449 }
450
451 /** Attempt to read from a non-blocking UDP socket.
452  * @param[in] fd File descriptor to read from.
453  * @param[out] buf Output buffer to read into.
454  * @param[in] length Number of bytes to read.
455  * @param[out] length_out Receives number of bytes actually read.
456  * @param[out] addr_out Peer address that sent the message.
457  * @return An IOResult value indicating status.
458  */
459 IOResult os_recvfrom_nonb(int fd, char* buf, unsigned int length,
460                           unsigned int* length_out,
461                           struct irc_sockaddr* addr_out)
462 {
463   struct sockaddr_native addr;
464   unsigned int len = sizeof(addr);
465   int    res;
466   assert(0 != buf);
467   assert(0 != length_out);
468   assert(0 != addr_out);
469   errno = 0;
470   *length_out = 0;
471
472   res = recvfrom(fd, buf, length, 0, (struct sockaddr*) &addr, &len);
473   if (-1 == res) {
474     if (EWOULDBLOCK == errno || ENOMEM == errno
475 #ifdef ENOMEM
476         || ENOMEM == errno
477 #endif
478 #ifdef ENOBUFS
479         || ENOBUFS == errno
480 #endif
481         )
482       return IO_BLOCKED;
483     return IO_FAILURE;
484   }
485   sockaddr_to_irc(&addr, addr_out);
486   *length_out = res;
487   return IO_SUCCESS;
488 }
489
490 /** Attempt to write on a non-blocking UDP socket.
491  * @param[in] fd File descriptor to write to.
492  * @param[in] buf Output buffer to send from.
493  * @param[in] length Number of bytes to write.
494  * @param[out] count_out Receives number of bytes actually written.
495  * @param[in] flags Flags for call to sendto().
496  * @param[in] peer Destination address of the message.
497  * @return An IOResult value indicating status.
498  */
499 IOResult os_sendto_nonb(int fd, const char* buf, unsigned int length,
500                         unsigned int* count_out, unsigned int flags,
501                         const struct irc_sockaddr* peer)
502 {
503   struct sockaddr_native addr;
504   int res, size;
505   assert(0 != buf);
506   if (count_out)
507     *count_out = 0;
508   errno = 0;
509
510   size = sockaddr_from_irc(&addr, peer, fd);
511   assert((addr.sn_family == AF_INET) == irc_in_addr_is_ipv4(&peer->addr));
512   if (-1 < (res = sendto(fd, buf, length, flags, (struct sockaddr*)&addr, size))) {
513     if (count_out)
514       *count_out = (unsigned) res;
515     return IO_SUCCESS;
516   }
517   else if (EWOULDBLOCK == errno || EAGAIN == errno
518 #ifdef ENOMEM
519            || ENOMEM == errno
520 #endif
521 #ifdef ENOBUFS
522            || ENOBUFS == errno
523 #endif
524       )
525     return IO_BLOCKED;
526   return IO_FAILURE;
527 }
528
529 /** Attempt to write on a connected socket.
530  * @param[in] fd File descriptor to write to.
531  * @param[in] buf Output buffer to send from.
532  * @param[in] length Number of bytes to write.
533  * @param[out] count_out Receives number of bytes actually written.
534  * @return An IOResult value indicating status.
535  */
536 IOResult os_send_nonb(int fd, const char* buf, unsigned int length, 
537                  unsigned int* count_out)
538 {
539   int res;
540   assert(0 != buf);
541   assert(0 != count_out);
542   *count_out = 0;
543   errno = 0;
544
545   if (-1 < (res = send(fd, buf, length, 0))) {
546     *count_out = (unsigned) res;
547     return IO_SUCCESS;
548   }
549   else if (EWOULDBLOCK == errno || EAGAIN == errno
550 #ifdef ENOMEM
551            || ENOMEM == errno
552 #endif
553 #ifdef ENOBUFS
554            || ENOBUFS == errno
555 #endif
556       )
557     return IO_BLOCKED;
558   return IO_FAILURE;
559 }
560
561 /** Attempt a vectored write on a connected socket.
562  * @param[in] fd File descriptor to write to.
563  * @param[in] buf Message queue to send from.
564  * @param[out] count_in Number of bytes mapped from \a buf.
565  * @param[out] count_out Receives number of bytes actually written.
566  * @return An IOResult value indicating status.
567  */
568 IOResult os_sendv_nonb(int fd, struct MsgQ* buf, unsigned int* count_in,
569                        unsigned int* count_out)
570 {
571   int res;
572   int count;
573   struct iovec iov[IOV_MAX];
574
575   assert(0 != buf);
576   assert(0 != count_in);
577   assert(0 != count_out);
578
579   *count_in = 0;
580   *count_out = 0;
581   errno = 0;
582
583   count = msgq_mapiov(buf, iov, IOV_MAX, count_in);
584
585   if (-1 < (res = writev(fd, iov, count))) {
586     *count_out = (unsigned) res;
587     return IO_SUCCESS;
588   }
589   else if (EWOULDBLOCK == errno || EAGAIN == errno
590 #ifdef ENOMEM
591            || ENOMEM == errno
592 #endif
593 #ifdef ENOBUFS
594            || ENOBUFS == errno
595 #endif
596       )
597     return IO_BLOCKED;
598
599   return IO_FAILURE;
600 }
601
602 /** Open a TCP or UDP socket on a particular address.
603  * @param[in] local Local address to bind to.
604  * @param[in] type SOCK_STREAM or SOCK_DGRAM.
605  * @param[in] port_name Port name (used in error diagnostics).
606  * @return Bound descriptor, or -1 on error.
607  */
608 int os_socket(const struct irc_sockaddr* local, int type, const char* port_name)
609 {
610   struct sockaddr_native addr;
611   int size, fd;
612
613   assert(local != 0);
614   size = sockaddr_from_irc(&addr, local, -1);
615   fd = socket(addr.sn_family, type, 0);
616   if (fd < 0) {
617     report_error(SOCKET_ERROR_MSG, port_name, errno);
618     return -1;
619   }
620   if (fd > MAXCLIENTS - 1) {
621     report_error(CONNLIMIT_ERROR_MSG, port_name, 0);
622     close(fd);
623     return -1;
624   }
625   if (!os_set_reuseaddr(fd)) {
626     report_error(REUSEADDR_ERROR_MSG, port_name, errno);
627     close(fd);
628     return -1;
629   }
630   if (!os_set_nonblocking(fd)) {
631     report_error(NONB_ERROR_MSG, port_name, errno);
632     close(fd);
633     return -1;
634   }
635   if (local) {
636     if (bind(fd, (struct sockaddr*)&addr, size)) {
637       report_error(BIND_ERROR_MSG, port_name, errno);
638       close(fd);
639       return -1;
640     }
641   }
642   return fd;
643 }
644
645 /** Accept a connection on a socket.
646  * @param[in] fd Listening file descriptor.
647  * @param[out] peer Peer address of connection.
648  * @return File descriptor for accepted connection.
649  */
650 int os_accept(int fd, struct irc_sockaddr* peer)
651 {
652   struct sockaddr_native addr;
653   socklen_t addrlen;
654   int new_fd;
655
656   addrlen = sizeof(addr);
657   new_fd = accept(fd, (struct sockaddr*)&addr, &addrlen);
658   if (new_fd < 0)
659     memset(peer, 0, sizeof(*peer));
660   else
661     sockaddr_to_irc(&addr, peer);
662   return new_fd;
663 }
664
665 /** Start a non-blocking connection.
666  * @param[in] fd Disconnected file descriptor.
667  * @param[in] sin Target address for connection.
668  * @return IOResult code indicating status.
669  */
670 IOResult os_connect_nonb(int fd, const struct irc_sockaddr* sin)
671 {
672   struct sockaddr_native addr;
673   int size;
674
675   size = sockaddr_from_irc(&addr, sin, fd);
676   if (connect(fd, (struct sockaddr*) &addr, size))
677     return (errno == EINPROGRESS) ? IO_BLOCKED : IO_FAILURE;
678   return IO_SUCCESS;
679 }
680
681 /** Get local address of a socket.
682  * @param[in] fd File descriptor to operate on.
683  * @param[out] sin_out Receives local socket address.
684  * @return Non-zero on success; zero on error.
685  */
686 int os_get_sockname(int fd, struct irc_sockaddr* sin_out)
687 {
688   struct sockaddr_native addr;
689   unsigned int len = sizeof(addr);
690
691   assert(0 != sin_out);
692   if (getsockname(fd, (struct sockaddr*) &addr, &len))
693     return 0;
694   sockaddr_to_irc(&addr, sin_out);
695   return 1;
696 }
697
698 /** Get remote address of a socket.
699  * @param[in] fd File descriptor to operate on.
700  * @param[out] sin_out Receives remote socket address.
701  * @return Non-zero on success; zero on error.
702  */
703 int os_get_peername(int fd, struct irc_sockaddr* sin_out)
704 {
705   struct sockaddr_native addr;
706   unsigned int len = sizeof(addr);
707
708   assert(0 != sin_out);
709   if (getpeername(fd, (struct sockaddr*) &addr, &len))
710     return 0;
711   sockaddr_to_irc(&addr, sin_out);
712   return 1;
713 }
714
715 /** Start listening on a socket.
716  * @param[in] fd Disconnected file descriptor.
717  * @param[in] backlog Maximum number of un-accept()ed connections to keep.
718  * @return Non-zero on success; zero on error.
719  */
720 int os_set_listen(int fd, int backlog)
721 {
722   return (0 == listen(fd, backlog));
723 }