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