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