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