Make default virtual host work for .12, and make IPv4-only
[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  * $Id$
20  *
21  */
22 #include "config.h"
23
24 #define _XOPEN_SOURCE   500 /* make limits.h #define IOV_MAX */
25 #define __EXTENSIONS__  1   /* make Solaris netinet/in.h know IPv6 */
26
27 #include "ircd_osdep.h"
28 #include "msgq.h"
29 #include "res.h"
30 #include "s_bsd.h"
31 #include "sys.h"
32
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
37  * netinet/in.h.
38  */
39 #include <assert.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <limits.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <sys/ioctl.h>
46 #include <sys/types.h>
47 #include <sys/time.h>
48 #include <netinet/in.h>
49 #include <sys/resource.h>
50 #include <sys/socket.h>
51 #include <sys/uio.h>
52
53 #if HAVE_SYS_PARAM_H
54 #include <sys/param.h>
55 #endif
56
57 #if HAVE_UNISTD_H
58 #include <unistd.h>
59 #endif
60
61 #ifndef IOV_MAX
62 #define IOV_MAX 16      /* minimum required */
63 #endif
64
65 #ifdef HPUX
66 #include <sys/syscall.h>
67 #define getrusage(a,b) syscall(SYS_GETRUSAGE, a, b)
68 #endif
69
70 #ifdef IPV6
71 #define sockaddr_native sockaddr_in6
72 #define sn_family sin6_family
73
74 void sockaddr_to_irc(const struct sockaddr_in6 *v6, struct irc_sockaddr *irc)
75 {
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);
79     }
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);
86     }
87     else assert(0 && "Unhandled native address family");
88 }
89
90 int sockaddr_from_irc(struct sockaddr_in6 *v6, const struct irc_sockaddr *irc, int persist)
91 {
92     memset(v6, 0, sizeof(*v6));
93     if (!irc) {
94         memset(v6, 0, sizeof(v6));
95         v6->sin6_family = AF_INET6;
96         return sizeof(*v6);
97     }
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);
103         return sizeof(*v4);
104     }
105     else {
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);
109         return sizeof(*v6);
110     }
111 }
112
113 #else
114 #define sockaddr_native sockaddr_in
115 #define sn_family sin_family
116
117 void sockaddr_to_irc(const struct sockaddr_in *v4, struct irc_sockaddr *irc)
118 {
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);
123 }
124
125 int sockaddr_from_irc(struct sockaddr_in *v4, const struct irc_sockaddr *irc, int persist)
126 {
127     v4->sin_family = AF_INET;
128     if (irc) {
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);
132     } else{
133         memset(&v4, 0, sizeof(v4));
134     }
135     (void)persist;
136     return sizeof(*v4);
137 }
138
139 #endif
140
141 /*
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".
146  * -avalon
147  */
148 int os_get_rusage(struct Client *cptr, int uptime, EnumFn enumerator)
149 {
150 #ifdef HAVE_GETRUSAGE
151   char buf[256];
152   struct rusage rus;
153   time_t secs;
154
155 #ifdef  hz
156 #  define hzz hz
157 #else
158 #  ifdef HZ
159 #    define hzz HZ
160 #  else
161   int hzz = 1;
162 #  ifdef HPUX
163   hzz = sysconf(_SC_CLK_TCK);
164 #  endif
165 #endif
166 #endif
167
168   assert(0 != enumerator);
169   if (getrusage(RUSAGE_SELF, &rus) == -1)
170     return 0;
171
172   secs = rus.ru_utime.tv_sec + rus.ru_stime.tv_sec;
173   if (secs == 0)
174     secs = 1;
175
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);
181
182   sprintf(buf, "RSS %ld ShMem %ld Data %ld Stack %ld",
183           rus.ru_maxrss,
184           rus.ru_ixrss / (uptime * hzz), rus.ru_idrss / (uptime * hzz),
185           rus.ru_isrss / (uptime * hzz));
186   (*enumerator)(cptr, buf);
187
188   sprintf(buf, "Swaps %ld Reclaims %ld Faults %ld",
189           rus.ru_nswap, rus.ru_minflt, rus.ru_majflt);
190   (*enumerator)(cptr, buf);
191
192   sprintf(buf, "Block in %ld out %ld", rus.ru_inblock, rus.ru_oublock);
193   (*enumerator)(cptr, buf);
194
195   sprintf(buf, "Msg Rcv %ld Send %ld", rus.ru_msgrcv, rus.ru_msgsnd);
196   (*enumerator)(cptr, buf);
197
198   sprintf(buf, "Signals %ld Context Vol. %ld Invol %ld",
199           rus.ru_nsignals, rus.ru_nvcsw, rus.ru_nivcsw);
200   (*enumerator)(cptr, buf);
201
202 #else /* HAVE_GETRUSAGE */
203 #if HAVE_TIMES
204   char buf[256];
205   struct tms tmsbuf;
206   time_t secs, mins;
207   int hzz = 1, ticpermin;
208   int umin, smin, usec, ssec;
209
210   assert(0 != enumerator);
211 #ifdef HPUX
212   hzz = sysconf(_SC_CLK_TCK);
213 #endif
214   ticpermin = hzz * 60;
215
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;
220   secs = usec + ssec;
221   mins = (secs / 60) + umin + smin;
222   secs %= hzz;
223
224   if (times(&tmsbuf) == -1)
225     return 0;
226   secs = tmsbuf.tms_utime + tmsbuf.tms_stime;
227
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 */
233   return 1;
234 }
235
236 int os_get_sockerr(int fd)
237 {
238   int    err = 0;
239 #if defined(SO_ERROR)
240   unsigned int len = sizeof(err);
241   getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
242 #endif
243   return err;
244 }
245
246 /*
247  * set_non_blocking
248  *
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...)
254  */
255 int os_set_nonblocking(int fd)
256 {
257   int res;
258 #ifndef NBLOCK_SYSV
259   int nonb = 0;
260 #endif
261
262   /*
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.
267    */
268 #ifdef  NBLOCK_POSIX
269   nonb |= O_NONBLOCK;
270 #endif
271 #ifdef  NBLOCK_BSD
272   nonb |= O_NDELAY;
273 #endif
274 #ifdef  NBLOCK_SYSV
275   /* This portion of code might also apply to NeXT. -LynX */
276   res = 1;
277
278   if (ioctl(fd, FIONBIO, &res) == -1)
279     return 0;
280 #else
281   if ((res = fcntl(fd, F_GETFL, 0)) == -1)
282     return 0;
283   else if (fcntl(fd, F_SETFL, res | nonb) == -1)
284     return 0;
285 #endif
286   return 1;
287 }
288
289
290 /*
291  *  set_sock_opts
292  */
293 int os_set_reuseaddr(int fd)
294 {
295   unsigned int opt = 1;
296   return (0 == setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
297                           (const char*) &opt, sizeof(opt)));
298 }
299
300 int os_set_sockbufs(int fd, unsigned int ssize, unsigned int rsize)
301 {
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)));
308 }
309
310 int os_set_tos(int fd,int tos)
311 {
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)));
315 #else
316   return 1;
317 #endif
318 }
319
320 int os_disable_options(int fd)
321 {
322 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
323   return (0 == setsockopt(fd, IPPROTO_IP, IP_OPTIONS, NULL, 0));
324 #else
325   return 1;
326 #endif
327 }
328
329 /*
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.
332  */
333 #ifdef RLIMIT_FDMAX
334 #define RLIMIT_FD_MAX   RLIMIT_FDMAX
335 #else
336 #ifdef RLIMIT_NOFILE
337 #define RLIMIT_FD_MAX RLIMIT_NOFILE
338 #else
339 #ifdef RLIMIT_OPEN_MAX
340 #define RLIMIT_FD_MAX RLIMIT_OPEN_MAX
341 #else
342 #undef RLIMIT_FD_MAX
343 #endif
344 #endif
345 #endif
346
347 int os_set_fdlimit(unsigned int max_descriptors)
348 {
349 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_FD_MAX)
350   struct rlimit limit;
351
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);
357   }
358 #endif /* defined(HAVE_SETRLIMIT) && defined(RLIMIT_FD_MAX) */
359   return 0;
360 }
361
362 /*
363  * os_recv_nonb - non blocking read of a connection
364  * returns:
365  *  1  if data was read or socket is blocked (recoverable error)
366  *    count_out > 0 if data was read
367  *
368  *  0  if socket closed from other end
369  *  -1 if an unrecoverable error occurred
370  */
371 IOResult os_recv_nonb(int fd, char* buf, unsigned int length,
372                  unsigned int* count_out)
373 {
374   int res;
375   assert(0 != buf);
376   assert(0 != count_out);
377   *count_out = 0;
378   errno = 0;
379
380   if (0 < (res = recv(fd, buf, length, 0))) {
381     *count_out = (unsigned) res;
382     return IO_SUCCESS;
383   }
384   else if (res < 0) {
385     if (EWOULDBLOCK == errno || EAGAIN == errno
386 #ifdef ENOMEM
387         || ENOMEM == errno
388 #endif
389 #ifdef ENOBUFS
390         || ENOBUFS == errno
391 #endif
392         )
393       return IO_BLOCKED;
394     else
395       return IO_FAILURE;
396   }
397   /*
398    * 0   == client closed the connection
399    * < 1 == error
400    */
401   return IO_FAILURE;
402 }
403
404 IOResult os_recvfrom_nonb(int fd, char* buf, unsigned int length,
405                           unsigned int* length_out,
406                           struct irc_sockaddr* addr_out)
407 {
408   struct sockaddr_native addr;
409   unsigned int len = sizeof(addr);
410   int    res;
411   assert(0 != buf);
412   assert(0 != length_out);
413   assert(0 != addr_out);
414   errno = 0;
415   *length_out = 0;
416
417   res = recvfrom(fd, buf, length, 0, (struct sockaddr*) &addr, &len);
418   if (-1 == res) {
419     if (EWOULDBLOCK == errno || ENOMEM == errno
420 #ifdef ENOMEM
421         || ENOMEM == errno
422 #endif
423 #ifdef ENOBUFS
424         || ENOBUFS == errno
425 #endif
426         )
427       return IO_BLOCKED;
428     return IO_FAILURE;
429   }
430   sockaddr_to_irc(&addr, addr_out);
431   *length_out = res;
432   return IO_SUCCESS;
433 }
434
435 /*
436  * os_sendto_nonb - non blocking send to a UDP socket
437  * returns:
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
441  */
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)
445 {
446   struct sockaddr_native addr;
447   int res, size;
448   assert(0 != buf);
449   if (count_out)
450     *count_out = 0;
451   errno = 0;
452
453   size = sockaddr_from_irc(&addr, peer, 1);
454   if (-1 < (res = sendto(fd, buf, length, flags, (struct sockaddr*)&addr, size))) {
455     if (count_out)
456       *count_out = (unsigned) res;
457     return IO_SUCCESS;
458   }
459   else if (EWOULDBLOCK == errno || EAGAIN == errno
460 #ifdef ENOMEM
461            || ENOMEM == errno
462 #endif
463 #ifdef ENOBUFS
464            || ENOBUFS == errno
465 #endif
466       )
467     return IO_BLOCKED;
468   return IO_FAILURE;
469 }
470
471 /*
472  * os_send_nonb - non blocking read of a connection
473  * returns:
474  *  1  if data was written
475  *    count_out contains amount written
476  *
477  *  0  if write call blocked, recoverable error
478  *  -1 if an unrecoverable error occurred
479  */
480 IOResult os_send_nonb(int fd, const char* buf, unsigned int length, 
481                  unsigned int* count_out)
482 {
483   int res;
484   assert(0 != buf);
485   assert(0 != count_out);
486   *count_out = 0;
487   errno = 0;
488
489   if (-1 < (res = send(fd, buf, length, 0))) {
490     *count_out = (unsigned) res;
491     return IO_SUCCESS;
492   }
493   else if (EWOULDBLOCK == errno || EAGAIN == errno
494 #ifdef ENOMEM
495            || ENOMEM == errno
496 #endif
497 #ifdef ENOBUFS
498            || ENOBUFS == errno
499 #endif
500       )
501     return IO_BLOCKED;
502   return IO_FAILURE;
503 }
504
505 IOResult os_sendv_nonb(int fd, struct MsgQ* buf, unsigned int* count_in,
506                        unsigned int* count_out)
507 {
508   int res;
509   int count;
510   struct iovec iov[IOV_MAX];
511
512   assert(0 != buf);
513   assert(0 != count_in);
514   assert(0 != count_out);
515
516   *count_in = 0;
517   *count_out = 0;
518   errno = 0;
519
520   count = msgq_mapiov(buf, iov, IOV_MAX, count_in);
521
522   if (-1 < (res = writev(fd, iov, count))) {
523     *count_out = (unsigned) res;
524     return IO_SUCCESS;
525   }
526   else if (EWOULDBLOCK == errno || EAGAIN == errno
527 #ifdef ENOMEM
528            || ENOMEM == errno
529 #endif
530 #ifdef ENOBUFS
531            || ENOBUFS == errno
532 #endif
533       )
534     return IO_BLOCKED;
535
536   return IO_FAILURE;
537 }
538
539 int os_socket(const struct irc_sockaddr* local, int type, const char* port_name)
540 {
541   struct sockaddr_native addr;
542   int size, fd;
543
544   size = sockaddr_from_irc(&addr, local, 1);
545   fd = socket(addr.sn_family, type, 0);
546   if (fd < 0) {
547     report_error(SOCKET_ERROR_MSG, port_name, errno);
548     return -1;
549   }
550   if (fd > MAXCLIENTS - 1) {
551     report_error(CONNLIMIT_ERROR_MSG, port_name, 0);
552     close(fd);
553     return -1;
554   }
555   if (!os_set_reuseaddr(fd)) {
556     report_error(REUSEADDR_ERROR_MSG, port_name, errno);
557     close(fd);
558     return -1;
559   }
560   if (!os_set_nonblocking(fd)) {
561     report_error(NONB_ERROR_MSG, port_name, errno);
562     close(fd);
563     return -1;
564   }
565   if (local) {
566     if (bind(fd, (struct sockaddr*)&addr, size)) {
567       report_error(BIND_ERROR_MSG, port_name, errno);
568       close(fd);
569       return -1;
570     }
571   }
572   return fd;
573 }
574
575 int os_accept(int fd, struct irc_sockaddr* peer)
576 {
577   struct sockaddr_native addr;
578   socklen_t addrlen;
579   int new_fd;
580
581   addrlen = sizeof(addr);
582   new_fd = accept(fd, (struct sockaddr*)&addr, &addrlen);
583   if (new_fd < 0)
584     memset(peer, 0, sizeof(*peer));
585   else
586     sockaddr_to_irc(&addr, peer);
587   return new_fd;
588 }
589
590 IOResult os_connect_nonb(int fd, const struct irc_sockaddr* sin)
591 {
592   struct sockaddr_native addr;
593   int size;
594
595   size = sockaddr_from_irc(&addr, sin, 1);
596   if (connect(fd, (struct sockaddr*) &addr, size))
597     return (errno == EINPROGRESS) ? IO_BLOCKED : IO_FAILURE;
598   return IO_SUCCESS;
599 }
600
601 int os_get_sockname(int fd, struct irc_sockaddr* sin_out)
602 {
603   struct sockaddr_native addr;
604   unsigned int len = sizeof(addr);
605
606   assert(0 != sin_out);
607   if (getsockname(fd, (struct sockaddr*) &addr, &len))
608     return 0;
609   sockaddr_to_irc(&addr, sin_out);
610   return 1;
611 }
612
613 int os_get_peername(int fd, struct irc_sockaddr* sin_out)
614 {
615   struct sockaddr_native addr;
616   unsigned int len = sizeof(addr);
617
618   assert(0 != sin_out);
619   if (getpeername(fd, (struct sockaddr*) &addr, &len))
620     return 0;
621   sockaddr_to_irc(&addr, sin_out);
622   return 1;
623 }
624
625 int os_set_listen(int fd, int backlog)
626 {
627   return (0 == listen(fd, backlog));
628 }