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