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