ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/s_bsd.c
Revision: 8393
Committed: Sun Mar 18 12:54:22 2018 UTC (8 years, 6 months ago) by michael
Content type: text/x-csrc
File size: 18213 byte(s)
Log Message:
- Move close_connection() from s_bsd.c to client.c

File Contents

# User Rev Content
1 adx 30 /*
2 michael 2916 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 adx 30 *
4 michael 8279 * Copyright (c) 1997-2018 ircd-hybrid development team
5 adx 30 *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18 michael 4565 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 adx 30 * USA
20     */
21    
22 michael 2916 /*! \file s_bsd.c
23     * \brief Network functions.
24     * \version $Id$
25     */
26    
27 adx 30 #include "stdinc.h"
28     #include <netinet/in_systm.h>
29     #include <netinet/ip.h>
30     #include <netinet/tcp.h>
31 michael 1011 #include "list.h"
32 adx 30 #include "fdlist.h"
33     #include "s_bsd.h"
34     #include "client.h"
35     #include "dbuf.h"
36     #include "event.h"
37     #include "irc_string.h"
38     #include "ircd.h"
39     #include "listener.h"
40     #include "numeric.h"
41     #include "packet.h"
42 michael 3322 #include "res.h"
43 adx 30 #include "restart.h"
44 michael 1309 #include "conf.h"
45     #include "log.h"
46 michael 3347 #include "server.h"
47 adx 30 #include "send.h"
48     #include "memory.h"
49 michael 3347 #include "user.h"
50 adx 30
51 michael 3274
52 michael 5887 static const char *const comm_err_str[] =
53     {
54     [COMM_OK] = "Comm OK",
55     [COMM_ERR_BIND] = "Error during bind()",
56     [COMM_ERR_DNS] = "Error during DNS lookup",
57     [COMM_ERR_TIMEOUT] = "connect timeout",
58     [COMM_ERR_CONNECT] = "Error during connect()",
59     [COMM_ERROR] = "Comm Error"
60     };
61 adx 30
62 michael 1013 static void comm_connect_callback(fde_t *, int);
63 michael 4461 static void comm_connect_timeout(fde_t *, void *);
64 michael 4408 static void comm_connect_dns_callback(void *, const struct irc_ssaddr *, const char *, size_t);
65 michael 4461 static void comm_connect_tryconnect(fde_t *, void *);
66 adx 30
67    
68     /* get_sockerr - get the error value from the socket or the current errno
69     *
70     * Get the *real* error from the socket (well try to anyway..).
71     * This may only work when SO_DEBUG is enabled but its worth the
72     * gamble anyway.
73     */
74     int
75     get_sockerr(int fd)
76     {
77     int errtmp = errno;
78     #ifdef SO_ERROR
79     int err = 0;
80     socklen_t len = sizeof(err);
81    
82 michael 967 if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len))
83 adx 30 {
84     if (err)
85     errtmp = err;
86     }
87     errno = errtmp;
88     #endif
89     return errtmp;
90     }
91    
92     /*
93 michael 2916 * report_error - report an error from an errno.
94 adx 30 * Record error to log and also send a copy to all *LOCAL* opers online.
95     *
96     * text is a *format* string for outputing error. It must
97     * contain only two '%s', the first will be replaced
98     * by the sockhost from the client_p, and the latter will
99     * be taken from sys_errlist[errno].
100     *
101     * client_p if not NULL, is the *LOCAL* client associated with
102     * the error.
103     *
104     * Cannot use perror() within daemon. stderr is closed in
105     * ircd and cannot be used. And, worse yet, it might have
106     * been reassigned to a normal connection...
107 michael 2916 *
108 adx 30 * Actually stderr is still there IFF ircd was run with -s --Rodder
109     */
110    
111     void
112 michael 2916 report_error(int level, const char* text, const char* who, int error)
113 adx 30 {
114     who = (who) ? who : "";
115    
116 michael 1618 sendto_realops_flags(UMODE_DEBUG, level, SEND_NOTICE,
117     text, who, strerror(error));
118 michael 1247 ilog(LOG_TYPE_IRCD, text, who, strerror(error));
119 adx 30 }
120    
121     /*
122     * setup_socket()
123     *
124     * Set the socket non-blocking, and other wonderful bits.
125     */
126 michael 2632 static void
127     setup_socket(int fd)
128 adx 30 {
129     int opt = 1;
130    
131 michael 967 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
132 adx 30
133     #ifdef IPTOS_LOWDELAY
134     opt = IPTOS_LOWDELAY;
135 michael 967 setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt));
136 adx 30 #endif
137    
138     fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
139     }
140    
141     /*
142     * ssl_handshake - let OpenSSL initialize the protocol. Register for
143     * read/write events if necessary.
144     */
145     static void
146 michael 8339 ssl_handshake(fde_t *F, void *data)
147 adx 30 {
148 michael 4461 struct Client *client_p = data;
149 adx 30
150 michael 8339 assert(client_p);
151     assert(client_p->connection);
152     assert(client_p->connection->fd);
153     assert(client_p->connection->fd == F);
154    
155     tls_handshake_status_t ret = tls_handshake(&F->ssl, TLS_ROLE_SERVER, NULL);
156 michael 7105 if (ret != TLS_HANDSHAKE_DONE)
157 michael 2463 {
158 michael 4737 if ((CurrentTime - client_p->connection->firsttime) > CONNECTTIMEOUT)
159 michael 2725 {
160 michael 7105 exit_client(client_p, "Timeout during TLS handshake");
161 michael 2725 return;
162     }
163    
164 michael 7105 switch (ret)
165 michael 2463 {
166 michael 7105 case TLS_HANDSHAKE_WANT_WRITE:
167 michael 8339 comm_setselect(client_p->connection->fd, COMM_SELECT_WRITE,
168 michael 4737 ssl_handshake, client_p, CONNECTTIMEOUT);
169 michael 2463 return;
170 michael 7105 case TLS_HANDSHAKE_WANT_READ:
171 michael 8339 comm_setselect(client_p->connection->fd, COMM_SELECT_READ,
172 michael 4737 ssl_handshake, client_p, CONNECTTIMEOUT);
173 michael 2463 return;
174     default:
175 michael 7105 exit_client(client_p, "Error during TLS handshake");
176 michael 2916 return;
177 michael 2463 }
178     }
179    
180 michael 8339 comm_settimeout(F, 0, NULL, NULL);
181 michael 2733
182 michael 8339 if (!tls_verify_cert(&F->ssl, ConfigServerInfo.message_digest_algorithm, &client_p->certfp))
183 michael 7142 ilog(LOG_TYPE_IRCD, "Client %s!%s@%s gave bad TLS client certificate",
184     client_p->name, client_p->username, client_p->host);
185 michael 2228
186 michael 7955 auth_start(client_p);
187 adx 30 }
188    
189     /*
190 michael 2916 * add_connection - creates a client which has just connected to us on
191 adx 30 * the given fd. The sockhost field is initialized with the ip# of the host.
192     * An unique id is calculated now, in case it is needed for auth.
193     * The client is sent to the auth module for verification, and not put in
194     * any client list yet.
195     */
196     void
197 michael 549 add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd)
198 adx 30 {
199 michael 7957 struct Client *client_p = client_make(NULL);
200 michael 549
201 michael 8339 client_p->connection->fd = fd_open(fd, 1, (listener->flags & LISTENER_SSL) ?
202     "Incoming SSL connection" : "Incoming connection");
203 adx 30
204 michael 1123 /*
205 adx 30 * copy address to 'sockhost' as a string, copy it to host too
206     * so we have something valid to put into error messages...
207     */
208 michael 8310 memcpy(&client_p->connection->ip, irn, sizeof(client_p->connection->ip));
209 adx 30
210 michael 5051 getnameinfo((const struct sockaddr *)&client_p->connection->ip,
211 michael 4588 client_p->connection->ip.ss_len, client_p->sockhost,
212 michael 3171 sizeof(client_p->sockhost), NULL, 0, NI_NUMERICHOST);
213 michael 4588 client_p->connection->aftype = client_p->connection->ip.ss.ss_family;
214 michael 1072
215 michael 8265 if (client_p->sockhost[0] == ':')
216 adx 30 {
217 michael 8265 client_p->sockhost[0] = '0';
218 michael 7431 memmove(client_p->sockhost + 1, client_p->sockhost, sizeof(client_p->sockhost) - 1);
219 michael 549 }
220 adx 30
221 michael 8265 strlcpy(client_p->host, client_p->sockhost, sizeof(client_p->host));
222    
223 michael 4588 client_p->connection->listener = listener;
224 adx 30 ++listener->ref_count;
225    
226 michael 7274 if (listener->flags & LISTENER_SSL)
227 adx 30 {
228 michael 8339 if (!tls_new(&client_p->connection->fd->ssl, fd, TLS_ROLE_SERVER))
229 adx 30 {
230 michael 3171 SetDead(client_p);
231 michael 7105 exit_client(client_p, "TLS context initialization failed");
232 adx 30 return;
233     }
234    
235 michael 3171 AddFlag(client_p, FLAGS_SSL);
236 michael 8339 ssl_handshake(client_p->connection->fd, client_p);
237 adx 30 }
238     else
239 michael 7955 auth_start(client_p);
240 adx 30 }
241    
242     /*
243     * stolen from squid - its a neat (but overused! :) routine which we
244     * can use to see whether we can ignore this errno or not. It is
245     * generally useful for non-blocking network IO related errnos.
246     * -- adrian
247     */
248     int
249     ignoreErrno(int ierrno)
250     {
251     switch (ierrno)
252     {
253     case EINPROGRESS:
254     case EWOULDBLOCK:
255     #if EAGAIN != EWOULDBLOCK
256     case EAGAIN:
257     #endif
258     case EALREADY:
259     case EINTR:
260     #ifdef ERESTART
261     case ERESTART:
262     #endif
263     return 1;
264     default:
265     return 0;
266     }
267     }
268    
269     /*
270     * comm_settimeout() - set the socket timeout
271     *
272     * Set the timeout for the fd
273     */
274     void
275 michael 8339 comm_settimeout(fde_t *F, uintmax_t timeout, void (*callback)(fde_t *, void *), void *cbdata)
276 adx 30 {
277 michael 8339 assert(F->flags.open);
278 adx 30
279 michael 8339 F->timeout = CurrentTime + (timeout / 1000);
280     F->timeout_handler = callback;
281     F->timeout_data = cbdata;
282 adx 30 }
283    
284     /*
285     * comm_setflush() - set a flush function
286     *
287     * A flush function is simply a function called if found during
288     * comm_timeouts(). Its basically a second timeout, except in this case
289     * I'm too lazy to implement multiple timeout functions! :-)
290     * its kinda nice to have it separate, since this is designed for
291     * flush functions, and when comm_close() is implemented correctly
292     * with close functions, we _actually_ don't call comm_close() here ..
293     * -- originally Adrian's notes
294 michael 2916 * comm_close() is replaced with fd_close() in fdlist.c
295 adx 30 */
296     void
297 michael 8339 comm_setflush(fde_t *F, uintmax_t timeout, void (*callback)(fde_t *, void *), void *cbdata)
298 adx 30 {
299 michael 8339 assert(F->flags.open);
300 adx 30
301 michael 8339 F->flush_timeout = CurrentTime + (timeout / 1000);
302     F->flush_handler = callback;
303     F->flush_data = cbdata;
304 adx 30 }
305    
306     /*
307     * comm_checktimeouts() - check the socket timeouts
308     *
309     * All this routine does is call the given callback/cbdata, without closing
310     * down the file descriptor. When close handlers have been implemented,
311     * this will happen.
312     */
313     void
314 michael 4439 comm_checktimeouts(void *unused)
315 adx 30 {
316 michael 4461 void (*hdl)(fde_t *, void *);
317 adx 30 void *data;
318    
319 michael 8339 for (int fd = 0; fd <= highest_fd; ++fd)
320 michael 7431 {
321 michael 8339 fde_t *F = &fd_table[fd];
322 adx 30
323 michael 8339 if (F->flags.open == 0)
324     continue;
325 adx 30
326 michael 8339 /* check flush functions */
327     if (F->flush_handler && F->flush_timeout > 0 &&
328     F->flush_timeout < CurrentTime)
329     {
330     hdl = F->flush_handler;
331     data = F->flush_data;
332 michael 8349
333 michael 8339 comm_setflush(F, 0, NULL, NULL);
334     hdl(F, data);
335 adx 30 }
336 michael 8339
337     /* check timeouts */
338     if (F->timeout_handler && F->timeout > 0 &&
339     F->timeout < CurrentTime)
340     {
341     /* Call timeout handler */
342     hdl = F->timeout_handler;
343     data = F->timeout_data;
344 michael 8349
345 michael 8339 comm_settimeout(F, 0, NULL, NULL);
346     hdl(F, data);
347     }
348 michael 7431 }
349 adx 30 }
350    
351     /*
352     * void comm_connect_tcp(int fd, const char *host, unsigned short port,
353     * struct sockaddr *clocal, int socklen,
354     * CNCB *callback, void *data, int aftype, int timeout)
355     * Input: An fd to connect with, a host and port to connect to,
356     * a local sockaddr to connect from + length(or NULL to use the
357     * default), a callback, the data to pass into the callback, the
358     * address family.
359     * Output: None.
360     * Side-effects: A non-blocking connection to the host is started, and
361     * if necessary, set up for selection. The callback given
362     * may be called now, or it may be called later.
363     */
364     void
365 michael 8339 comm_connect_tcp(fde_t *F, const char *host, unsigned short port, struct sockaddr *clocal,
366 michael 4464 int socklen, void (*callback)(fde_t *, int, void *), void *data,
367 michael 7330 int aftype, uintmax_t timeout)
368 adx 30 {
369     struct addrinfo hints, *res;
370 michael 992 char portname[PORTNAMELEN + 1];
371 adx 30
372     assert(callback);
373 michael 8339 F->connect.callback = callback;
374     F->connect.data = data;
375 adx 30
376 michael 8339 F->connect.hostaddr.ss.ss_family = aftype;
377     F->connect.hostaddr.ss_port = htons(port);
378 adx 30
379     /* Note that we're using a passed sockaddr here. This is because
380     * generally you'll be bind()ing to a sockaddr grabbed from
381     * getsockname(), so this makes things easier.
382     * XXX If NULL is passed as local, we should later on bind() to the
383     * virtual host IP, for completeness.
384     * -- adrian
385     */
386 michael 8339 if (clocal && bind(F->fd, clocal, socklen) < 0)
387 michael 2916 {
388 adx 30 /* Failure, call the callback with COMM_ERR_BIND */
389 michael 8339 comm_connect_callback(F, COMM_ERR_BIND);
390 michael 6948 return; /* ... and quit */
391 adx 30 }
392    
393     memset(&hints, 0, sizeof(hints));
394 michael 7431
395 adx 30 hints.ai_family = AF_UNSPEC;
396     hints.ai_socktype = SOCK_STREAM;
397     hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
398    
399 michael 992 snprintf(portname, sizeof(portname), "%d", port);
400 adx 30
401 michael 6948 /*
402     * Next, if we have been given an IP address, get the address and skip the
403     * DNS check (and head direct to comm_connect_tryconnect()).
404     */
405 michael 1123 if (getaddrinfo(host, portname, &hints, &res))
406 adx 30 {
407     /* Send the DNS request, for the next level */
408 db 871 if (aftype == AF_INET6)
409 michael 8339 gethost_byname_type(comm_connect_dns_callback, F, host, T_AAAA);
410 db 871 else
411 michael 8339 gethost_byname_type(comm_connect_dns_callback, F, host, T_A);
412 adx 30 }
413     else
414     {
415     /* We have a valid IP, so we just call tryconnect */
416     /* Make sure we actually set the timeout here .. */
417 michael 7431 assert(res);
418    
419 michael 8339 memcpy(&F->connect.hostaddr, res->ai_addr, res->ai_addrlen);
420     F->connect.hostaddr.ss_len = res->ai_addrlen;
421     F->connect.hostaddr.ss.ss_family = res->ai_family;
422 michael 1123 freeaddrinfo(res);
423 michael 7431
424 michael 8339 comm_settimeout(F, timeout * 1000, comm_connect_timeout, NULL);
425     comm_connect_tryconnect(F, NULL);
426 adx 30 }
427     }
428    
429     /*
430     * comm_connect_callback() - call the callback, and continue with life
431     */
432     static void
433 michael 8339 comm_connect_callback(fde_t *F, int status)
434 adx 30 {
435 michael 4464 void (*hdl)(fde_t *, int, void *);
436 adx 30
437     /* This check is gross..but probably necessary */
438 michael 8339 if (F->connect.callback == NULL)
439 adx 30 return;
440    
441     /* Clear the connect flag + handler */
442 michael 8339 hdl = F->connect.callback;
443     F->connect.callback = NULL;
444 adx 30
445     /* Clear the timeout handler */
446 michael 8339 comm_settimeout(F, 0, NULL, NULL);
447 adx 30
448     /* Call the handler */
449 michael 8339 hdl(F, status, F->connect.data);
450 adx 30 }
451    
452     /*
453     * comm_connect_timeout() - this gets called when the socket connection
454     * times out. This *only* can be called once connect() is initially
455     * called ..
456     */
457     static void
458 michael 8339 comm_connect_timeout(fde_t *F, void *unused)
459 adx 30 {
460     /* error! */
461 michael 8339 comm_connect_callback(F, COMM_ERR_TIMEOUT);
462 adx 30 }
463    
464     /*
465     * comm_connect_dns_callback() - called at the completion of the DNS request
466     *
467     * The DNS request has completed, so if we've got an error, return it,
468     * otherwise we initiate the connect()
469     */
470     static void
471 michael 4408 comm_connect_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name, size_t namelength)
472 adx 30 {
473 michael 7431 fde_t *const F = vptr;
474 adx 30
475 michael 4411 if (!addr)
476 adx 30 {
477     comm_connect_callback(F, COMM_ERR_DNS);
478     return;
479     }
480    
481 michael 8339 /* No error, set a 30 second timeout */
482     comm_settimeout(F, 30 * 1000, comm_connect_timeout, NULL);
483 adx 30
484     /* Copy over the DNS reply info so we can use it in the connect() */
485     /*
486     * Note we don't fudge the refcount here, because we aren't keeping
487 michael 2916 * the DNS record around, and the DNS cache is gone anyway..
488 adx 30 * -- adrian
489     */
490 michael 992 memcpy(&F->connect.hostaddr, addr, addr->ss_len);
491 michael 7431
492 adx 30 /* The cast is hacky, but safe - port offset is same on v4 and v6 */
493 michael 7431 ((struct sockaddr_in *)&F->connect.hostaddr)->sin_port = F->connect.hostaddr.ss_port;
494 michael 992 F->connect.hostaddr.ss_len = addr->ss_len;
495 adx 30
496     /* Now, call the tryconnect() routine to try a connect() */
497     comm_connect_tryconnect(F, NULL);
498     }
499    
500 michael 8339 /* static void comm_connect_tryconnect(fde_t *fd, void *unused)
501 adx 30 * Input: The fd, the handler data(unused).
502     * Output: None.
503     * Side-effects: Try and connect with pending connect data for the FD. If
504     * we succeed or get a fatal error, call the callback.
505     * Otherwise, it is still blocking or something, so register
506     * to select for a write event on this FD.
507     */
508     static void
509 michael 8339 comm_connect_tryconnect(fde_t *F, void *unused)
510 adx 30 {
511     /* This check is needed or re-entrant s_bsd_* like sigio break it. */
512 michael 8339 if (F->connect.callback == NULL)
513 adx 30 return;
514    
515     /* Try the connect() */
516 michael 8339 int retval = connect(F->fd, (struct sockaddr *)&F->connect.hostaddr, F->connect.hostaddr.ss_len);
517 adx 30
518     /* Error? */
519     if (retval < 0)
520     {
521     /*
522     * If we get EISCONN, then we've already connect()ed the socket,
523     * which is a good thing.
524     * -- adrian
525     */
526     if (errno == EISCONN)
527 michael 8339 comm_connect_callback(F, COMM_OK);
528 adx 30 else if (ignoreErrno(errno))
529     /* Ignore error? Reschedule */
530 michael 8339 comm_setselect(F, COMM_SELECT_WRITE, comm_connect_tryconnect, NULL, 0);
531 adx 30 else
532     /* Error? Fail with COMM_ERR_CONNECT */
533 michael 8339 comm_connect_callback(F, COMM_ERR_CONNECT);
534 adx 30 return;
535     }
536    
537     /* If we get here, we've suceeded, so call with COMM_OK */
538 michael 8339 comm_connect_callback(F, COMM_OK);
539 adx 30 }
540    
541     /*
542     * comm_errorstr() - return an error string for the given error condition
543     */
544     const char *
545     comm_errstr(int error)
546     {
547     if (error < 0 || error >= COMM_ERR_MAX)
548     return "Invalid error number!";
549     return comm_err_str[error];
550     }
551    
552     /*
553     * comm_open() - open a socket
554     *
555     * This is a highly highly cut down version of squid's comm_open() which
556     * for the most part emulates socket(), *EXCEPT* it fails if we're about
557     * to run out of file descriptors.
558     */
559     int
560 michael 8339 comm_socket(int family, int sock_type, int proto)
561 adx 30 {
562     /* First, make sure we aren't going to run out of file descriptors */
563     if (number_fd >= hard_fdlimit)
564     {
565     errno = ENFILE;
566     return -1;
567     }
568    
569     /*
570     * Next, we try to open the socket. We *should* drop the reserved FD
571     * limit if/when we get an error, but we can deal with that later.
572     * XXX !!! -- adrian
573     */
574 michael 7431 int fd = socket(family, sock_type, proto);
575 adx 30 if (fd < 0)
576     return -1; /* errno will be passed through, yay.. */
577    
578 michael 2632 setup_socket(fd);
579 adx 30
580 michael 8339 return fd;
581 adx 30 }
582    
583     /*
584     * comm_accept() - accept an incoming connection
585     *
586     * This is a simple wrapper for accept() which enforces FD limits like
587     * comm_open() does. Returned fd must be either closed or tagged with
588     * fd_open (this function no longer does it).
589     */
590     int
591 michael 8339 comm_accept(int fd, struct irc_ssaddr *addr)
592 adx 30 {
593     socklen_t addrlen = sizeof(struct irc_ssaddr);
594    
595     if (number_fd >= hard_fdlimit)
596     {
597     errno = ENFILE;
598     return -1;
599     }
600    
601 michael 4413 memset(addr, 0, sizeof(struct irc_ssaddr));
602 michael 4407
603 adx 30 /*
604     * Next, do the accept(). if we get an error, we should drop the
605     * reserved fd limit, but we can deal with that when comm_open()
606     * also does it. XXX -- adrian
607     */
608 michael 8339 int new_fd = accept(fd, (struct sockaddr *)addr, &addrlen);
609     if (new_fd < 0)
610 adx 30 return -1;
611    
612 michael 4407 remove_ipv6_mapping(addr);
613 adx 30
614 michael 8339 setup_socket(new_fd);
615 adx 30
616     /* .. and return */
617 michael 8339 return new_fd;
618 adx 30 }
619    
620 michael 2916 /*
621 adx 30 * remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address
622 michael 1122 * OSes with IPv6 mapping listening on both
623 adx 30 * AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures
624 michael 2916 *
625 adx 30 */
626     void
627     remove_ipv6_mapping(struct irc_ssaddr *addr)
628     {
629     if (addr->ss.ss_family == AF_INET6)
630     {
631 michael 1122 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr))
632     {
633 michael 2916 struct sockaddr_in6 v6;
634 michael 1122 struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
635 adx 30
636 michael 1122 memcpy(&v6, addr, sizeof(v6));
637     memset(v4, 0, sizeof(struct sockaddr_in));
638     memcpy(&v4->sin_addr, &v6.sin6_addr.s6_addr[12], sizeof(v4->sin_addr));
639    
640 adx 30 addr->ss.ss_family = AF_INET;
641     addr->ss_len = sizeof(struct sockaddr_in);
642     }
643 michael 2916 else
644 adx 30 addr->ss_len = sizeof(struct sockaddr_in6);
645     }
646     else
647     addr->ss_len = sizeof(struct sockaddr_in);
648 michael 2916 }

Properties

Name Value
svn:eol-style native
svn:keywords Id