2 * IRC - Internet Relay Chat, ircd/os_generic.c
3 * Copyright (C) 1999 Thomas Helvey
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)
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.
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.
24 #define _XOPEN_SOURCE 500 /* make limits.h #define IOV_MAX */
25 #define __EXTENSIONS__ 1 /* make Solaris netinet/in.h know IPv6 */
27 #include "ircd_osdep.h"
33 /* Include file dependency notes:
34 * FreeBSD requires struct timeval from sys/time.h before struct
35 * rusage in sys/resource.h.
36 * Solaris requires sys/time.h before struct rusage (indirectly) in
45 #include <sys/ioctl.h>
46 #include <sys/types.h>
48 #include <netinet/in.h>
49 #include <sys/resource.h>
50 #include <sys/socket.h>
54 #include <sys/param.h>
62 #define IOV_MAX 16 /* minimum required */
66 #include <sys/syscall.h>
67 #define getrusage(a,b) syscall(SYS_GETRUSAGE, a, b)
71 #define sockaddr_native sockaddr_in6
72 #define sn_family sin6_family
74 void sockaddr_to_irc(const struct sockaddr_in6 *v6, struct irc_sockaddr *irc)
76 if (v6->sin6_family == AF_INET6) {
77 memcpy(&irc->addr.in6_16[0], &v6->sin6_addr, sizeof(v6->sin6_addr));
78 irc->port = ntohs(v6->sin6_port);
80 else if (v6->sin6_family == AF_INET) {
81 const struct sockaddr_in *v4 = (const struct sockaddr_in*)v6;
82 memset(&irc->addr, 0, 5*sizeof(int16_t));
83 irc->addr.in6_16[5] = 0xffff;
84 memcpy(&irc->addr.in6_16[6], &v4->sin_addr, sizeof(v4->sin_addr));
85 irc->port = ntohs(v4->sin_port);
87 else assert(0 && "Unhandled native address family");
90 int sockaddr_from_irc(struct sockaddr_in6 *v6, const struct irc_sockaddr *irc, int persist)
92 memset(v6, 0, sizeof(*v6));
94 memset(v6, 0, sizeof(v6));
95 v6->sin6_family = AF_INET6;
98 else if (persist && irc_in_addr_is_ipv4(&irc->addr)) {
99 struct sockaddr_in *v4 = (struct sockaddr_in*)v6;
100 v4->sin_family = AF_INET;
101 memcpy(&v4->sin_addr, &irc->addr.in6_16[6], sizeof(v4->sin_addr));
102 v4->sin_port = htons(irc->port);
106 v6->sin6_family = AF_INET6;
107 memcpy(&v6->sin6_addr, &irc->addr.in6_16[0], sizeof(v6->sin6_addr));
108 v6->sin6_port = htons(irc->port);
114 #define sockaddr_native sockaddr_in
115 #define sn_family sin_family
117 void sockaddr_to_irc(const struct sockaddr_in *v4, struct irc_sockaddr *irc)
119 assert(v4->sin_family == AF_INET);
120 memset(&irc->addr, 0, 6*sizeof(irc->addr.in6_16[0]));
121 memcpy(&irc->addr.in6_16[6], &v4->sin_addr, sizeof(v4->sin_addr));
122 irc->port = ntohs(v4->sin_port);
125 int sockaddr_from_irc(struct sockaddr_in *v4, const struct irc_sockaddr *irc, int persist)
127 v4->sin_family = AF_INET;
129 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));
130 memcpy(&v4->sin_addr, &irc->addr.in6_16[6], sizeof(v4->sin_addr));
131 v4->sin_port = htons(irc->port);
133 memset(&v4, 0, sizeof(v4));
142 * This is part of the STATS replies. There is no offical numeric for this
143 * since this isnt an official command, in much the same way as HASH isnt.
144 * It is also possible that some systems wont support this call or have
145 * different field names for "struct rusage".
148 int os_get_rusage(struct Client *cptr, int uptime, EnumFn enumerator)
150 #ifdef HAVE_GETRUSAGE
163 hzz = sysconf(_SC_CLK_TCK);
168 assert(0 != enumerator);
169 if (getrusage(RUSAGE_SELF, &rus) == -1)
172 secs = rus.ru_utime.tv_sec + rus.ru_stime.tv_sec;
176 sprintf(buf, "CPU Secs %ld:%ld User %ld:%ld System %ld:%ld",
177 (long)(secs / 60), (long)(secs % 60),
178 rus.ru_utime.tv_sec / 60, rus.ru_utime.tv_sec % 60,
179 rus.ru_stime.tv_sec / 60, rus.ru_stime.tv_sec % 60);
180 (*enumerator)(cptr, buf);
182 sprintf(buf, "RSS %ld ShMem %ld Data %ld Stack %ld",
184 rus.ru_ixrss / (uptime * hzz), rus.ru_idrss / (uptime * hzz),
185 rus.ru_isrss / (uptime * hzz));
186 (*enumerator)(cptr, buf);
188 sprintf(buf, "Swaps %ld Reclaims %ld Faults %ld",
189 rus.ru_nswap, rus.ru_minflt, rus.ru_majflt);
190 (*enumerator)(cptr, buf);
192 sprintf(buf, "Block in %ld out %ld", rus.ru_inblock, rus.ru_oublock);
193 (*enumerator)(cptr, buf);
195 sprintf(buf, "Msg Rcv %ld Send %ld", rus.ru_msgrcv, rus.ru_msgsnd);
196 (*enumerator)(cptr, buf);
198 sprintf(buf, "Signals %ld Context Vol. %ld Invol %ld",
199 rus.ru_nsignals, rus.ru_nvcsw, rus.ru_nivcsw);
200 (*enumerator)(cptr, buf);
202 #else /* HAVE_GETRUSAGE */
207 int hzz = 1, ticpermin;
208 int umin, smin, usec, ssec;
210 assert(0 != enumerator);
212 hzz = sysconf(_SC_CLK_TCK);
214 ticpermin = hzz * 60;
216 umin = tmsbuf.tms_utime / ticpermin;
217 usec = (tmsbuf.tms_utime % ticpermin) / (float)hzz;
218 smin = tmsbuf.tms_stime / ticpermin;
219 ssec = (tmsbuf.tms_stime % ticpermin) / (float)hzz;
221 mins = (secs / 60) + umin + smin;
224 if (times(&tmsbuf) == -1)
226 secs = tmsbuf.tms_utime + tmsbuf.tms_stime;
228 sprintf(buf, "CPU Secs %d:%d User %d:%d System %d:%d",
229 mins, secs, umin, usec, smin, ssec);
230 (*enumerator)(cptr, buf);
231 #endif /* HAVE_TIMES */
232 #endif /* HAVE_GETRUSAGE */
236 int os_get_sockerr(int fd)
239 #if defined(SO_ERROR)
240 unsigned int len = sizeof(err);
241 getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
249 * Set the client connection into non-blocking mode. If your
250 * system doesn't support this, you can make this a dummy
251 * function (and get all the old problems that plagued the
252 * blocking version of IRC--not a problem if you are a
253 * lightly loaded node...)
255 int os_set_nonblocking(int fd)
263 * NOTE: consult ALL your relevant manual pages *BEFORE* changing
264 * these ioctl's. There are quite a few variations on them,
265 * as can be seen by the PCS one. They are *NOT* all the same.
266 * Heed this well. - Avalon.
275 /* This portion of code might also apply to NeXT. -LynX */
278 if (ioctl(fd, FIONBIO, &res) == -1)
281 if ((res = fcntl(fd, F_GETFL, 0)) == -1)
283 else if (fcntl(fd, F_SETFL, res | nonb) == -1)
293 int os_set_reuseaddr(int fd)
295 unsigned int opt = 1;
296 return (0 == setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
297 (const char*) &opt, sizeof(opt)));
300 int os_set_sockbufs(int fd, unsigned int ssize, unsigned int rsize)
302 unsigned int sopt = ssize;
303 unsigned int ropt = rsize;
304 return (0 == setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
305 (const char*) &ropt, sizeof(ropt)) &&
306 0 == setsockopt(fd, SOL_SOCKET, SO_SNDBUF,
307 (const char*) &sopt, sizeof(sopt)));
310 int os_set_tos(int fd,int tos)
312 #if defined(IP_TOS) && defined(IPPROTO_IP)
313 unsigned int opt = tos;
314 return (0 == setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt)));
320 int os_disable_options(int fd)
322 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
323 return (0 == setsockopt(fd, IPPROTO_IP, IP_OPTIONS, NULL, 0));
330 * Try and find the correct name to use with getrlimit() for setting the max.
331 * number of files allowed to be open by this process.
334 #define RLIMIT_FD_MAX RLIMIT_FDMAX
337 #define RLIMIT_FD_MAX RLIMIT_NOFILE
339 #ifdef RLIMIT_OPEN_MAX
340 #define RLIMIT_FD_MAX RLIMIT_OPEN_MAX
347 int os_set_fdlimit(unsigned int max_descriptors)
349 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_FD_MAX)
352 if (!getrlimit(RLIMIT_FD_MAX, &limit)) {
353 if (limit.rlim_max < MAXCONNECTIONS)
354 return limit.rlim_max;
355 limit.rlim_cur = limit.rlim_max; /* make soft limit the max */
356 return setrlimit(RLIMIT_FD_MAX, &limit);
358 #endif /* defined(HAVE_SETRLIMIT) && defined(RLIMIT_FD_MAX) */
363 * os_recv_nonb - non blocking read of a connection
365 * 1 if data was read or socket is blocked (recoverable error)
366 * count_out > 0 if data was read
368 * 0 if socket closed from other end
369 * -1 if an unrecoverable error occurred
371 IOResult os_recv_nonb(int fd, char* buf, unsigned int length,
372 unsigned int* count_out)
376 assert(0 != count_out);
380 if (0 < (res = recv(fd, buf, length, 0))) {
381 *count_out = (unsigned) res;
385 if (EWOULDBLOCK == errno || EAGAIN == errno
398 * 0 == client closed the connection
404 IOResult os_recvfrom_nonb(int fd, char* buf, unsigned int length,
405 unsigned int* length_out,
406 struct irc_sockaddr* addr_out)
408 struct sockaddr_native addr;
409 unsigned int len = sizeof(addr);
412 assert(0 != length_out);
413 assert(0 != addr_out);
417 res = recvfrom(fd, buf, length, 0, (struct sockaddr*) &addr, &len);
419 if (EWOULDBLOCK == errno || ENOMEM == errno
430 sockaddr_to_irc(&addr, addr_out);
436 * os_sendto_nonb - non blocking send to a UDP socket
438 * 1 if data was written, *count_out contains number of bytes
439 * 0 if sendto call blocked
440 * -1 if an unrecoverable error occurred
442 IOResult os_sendto_nonb(int fd, const char* buf, unsigned int length,
443 unsigned int* count_out, unsigned int flags,
444 const struct irc_sockaddr* peer)
446 struct sockaddr_native addr;
453 size = sockaddr_from_irc(&addr, peer, 1);
454 if (-1 < (res = sendto(fd, buf, length, flags, (struct sockaddr*)&addr, size))) {
456 *count_out = (unsigned) res;
459 else if (EWOULDBLOCK == errno || EAGAIN == errno
472 * os_send_nonb - non blocking read of a connection
474 * 1 if data was written
475 * count_out contains amount written
477 * 0 if write call blocked, recoverable error
478 * -1 if an unrecoverable error occurred
480 IOResult os_send_nonb(int fd, const char* buf, unsigned int length,
481 unsigned int* count_out)
485 assert(0 != count_out);
489 if (-1 < (res = send(fd, buf, length, 0))) {
490 *count_out = (unsigned) res;
493 else if (EWOULDBLOCK == errno || EAGAIN == errno
505 IOResult os_sendv_nonb(int fd, struct MsgQ* buf, unsigned int* count_in,
506 unsigned int* count_out)
510 struct iovec iov[IOV_MAX];
513 assert(0 != count_in);
514 assert(0 != count_out);
520 count = msgq_mapiov(buf, iov, IOV_MAX, count_in);
522 if (-1 < (res = writev(fd, iov, count))) {
523 *count_out = (unsigned) res;
526 else if (EWOULDBLOCK == errno || EAGAIN == errno
539 int os_socket(const struct irc_sockaddr* local, int type, const char* port_name)
541 struct sockaddr_native addr;
544 size = sockaddr_from_irc(&addr, local, 1);
545 fd = socket(addr.sn_family, type, 0);
547 report_error(SOCKET_ERROR_MSG, port_name, errno);
550 if (fd > MAXCLIENTS - 1) {
551 report_error(CONNLIMIT_ERROR_MSG, port_name, 0);
555 if (!os_set_reuseaddr(fd)) {
556 report_error(REUSEADDR_ERROR_MSG, port_name, errno);
560 if (!os_set_nonblocking(fd)) {
561 report_error(NONB_ERROR_MSG, port_name, errno);
566 if (bind(fd, (struct sockaddr*)&addr, size)) {
567 report_error(BIND_ERROR_MSG, port_name, errno);
575 int os_accept(int fd, struct irc_sockaddr* peer)
577 struct sockaddr_native addr;
581 addrlen = sizeof(addr);
582 new_fd = accept(fd, (struct sockaddr*)&addr, &addrlen);
584 memset(peer, 0, sizeof(*peer));
586 sockaddr_to_irc(&addr, peer);
590 IOResult os_connect_nonb(int fd, const struct irc_sockaddr* sin)
592 struct sockaddr_native addr;
595 size = sockaddr_from_irc(&addr, sin, 1);
596 if (connect(fd, (struct sockaddr*) &addr, size))
597 return (errno == EINPROGRESS) ? IO_BLOCKED : IO_FAILURE;
601 int os_get_sockname(int fd, struct irc_sockaddr* sin_out)
603 struct sockaddr_native addr;
604 unsigned int len = sizeof(addr);
606 assert(0 != sin_out);
607 if (getsockname(fd, (struct sockaddr*) &addr, &len))
609 sockaddr_to_irc(&addr, sin_out);
613 int os_get_peername(int fd, struct irc_sockaddr* sin_out)
615 struct sockaddr_native addr;
616 unsigned int len = sizeof(addr);
618 assert(0 != sin_out);
619 if (getpeername(fd, (struct sockaddr*) &addr, &len))
621 sockaddr_to_irc(&addr, sin_out);
625 int os_set_listen(int fd, int backlog)
627 return (0 == listen(fd, backlog));