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