ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/s_bsd.c
Revision: 1649
Committed: Sat Nov 10 19:27:13 2012 UTC (11 years, 5 months ago) by michael
Content type: text/x-csrc
File size: 20815 byte(s)
Log Message:
- minor MaskItem structure cleanup

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 * s_bsd.c: Network functions.
4 *
5 * Copyright (C) 2002 by the past and present ircd coders, and others.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 * USA
21 *
22 * $Id$
23 */
24
25 #include "stdinc.h"
26 #include <netinet/in_systm.h>
27 #include <netinet/ip.h>
28 #include <netinet/tcp.h>
29 #include "list.h"
30 #include "fdlist.h"
31 #include "s_bsd.h"
32 #include "client.h"
33 #include "dbuf.h"
34 #include "event.h"
35 #include "irc_string.h"
36 #include "ircd.h"
37 #include "listener.h"
38 #include "numeric.h"
39 #include "packet.h"
40 #include "irc_res.h"
41 #include "restart.h"
42 #include "s_auth.h"
43 #include "conf.h"
44 #include "log.h"
45 #include "s_serv.h"
46 #include "send.h"
47 #include "memory.h"
48 #include "s_user.h"
49 #include "hook.h"
50
51 static const char *comm_err_str[] = { "Comm OK", "Error during bind()",
52 "Error during DNS lookup", "connect timeout", "Error during connect()",
53 "Comm Error" };
54
55 struct Callback *setup_socket_cb = NULL;
56
57 static void comm_connect_callback(fde_t *, int);
58 static PF comm_connect_timeout;
59 static void comm_connect_dns_callback(void *, const struct irc_ssaddr *, const char *);
60 static PF comm_connect_tryconnect;
61
62
63 /* check_can_use_v6()
64 * Check if the system can open AF_INET6 sockets
65 */
66 void
67 check_can_use_v6(void)
68 {
69 #ifdef IPV6
70 int v6;
71
72 if ((v6 = socket(AF_INET6, SOCK_STREAM, 0)) < 0)
73 ServerInfo.can_use_v6 = 0;
74 else
75 {
76 ServerInfo.can_use_v6 = 1;
77 close(v6);
78 }
79 #else
80 ServerInfo.can_use_v6 = 0;
81 #endif
82 }
83
84 /* get_sockerr - get the error value from the socket or the current errno
85 *
86 * Get the *real* error from the socket (well try to anyway..).
87 * This may only work when SO_DEBUG is enabled but its worth the
88 * gamble anyway.
89 */
90 int
91 get_sockerr(int fd)
92 {
93 int errtmp = errno;
94 #ifdef SO_ERROR
95 int err = 0;
96 socklen_t len = sizeof(err);
97
98 if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len))
99 {
100 if (err)
101 errtmp = err;
102 }
103 errno = errtmp;
104 #endif
105 return errtmp;
106 }
107
108 /*
109 * report_error - report an error from an errno.
110 * Record error to log and also send a copy to all *LOCAL* opers online.
111 *
112 * text is a *format* string for outputing error. It must
113 * contain only two '%s', the first will be replaced
114 * by the sockhost from the client_p, and the latter will
115 * be taken from sys_errlist[errno].
116 *
117 * client_p if not NULL, is the *LOCAL* client associated with
118 * the error.
119 *
120 * Cannot use perror() within daemon. stderr is closed in
121 * ircd and cannot be used. And, worse yet, it might have
122 * been reassigned to a normal connection...
123 *
124 * Actually stderr is still there IFF ircd was run with -s --Rodder
125 */
126
127 void
128 report_error(int level, const char* text, const char* who, int error)
129 {
130 who = (who) ? who : "";
131
132 sendto_realops_flags(UMODE_DEBUG, level, SEND_NOTICE,
133 text, who, strerror(error));
134 ilog(LOG_TYPE_IRCD, text, who, strerror(error));
135 }
136
137 /*
138 * setup_socket()
139 *
140 * Set the socket non-blocking, and other wonderful bits.
141 */
142 static void *
143 setup_socket(va_list args)
144 {
145 int fd = va_arg(args, int);
146 int opt = 1;
147
148 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
149
150 #ifdef IPTOS_LOWDELAY
151 opt = IPTOS_LOWDELAY;
152 setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt));
153 #endif
154
155 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
156
157 return NULL;
158 }
159
160 /*
161 * init_comm()
162 *
163 * Initializes comm subsystem.
164 */
165 void
166 init_comm(void)
167 {
168 setup_socket_cb = register_callback("setup_socket", setup_socket);
169 init_netio();
170 }
171
172 /*
173 * close_connection
174 * Close the physical connection. This function must make
175 * MyConnect(client_p) == FALSE, and set client_p->from == NULL.
176 */
177 void
178 close_connection(struct Client *client_p)
179 {
180 dlink_node *ptr = NULL;
181
182 assert(client_p);
183
184 if (!IsDead(client_p))
185 {
186 /* attempt to flush any pending dbufs. Evil, but .. -- adrian */
187 /* there is still a chance that we might send data to this socket
188 * even if it is marked as blocked (COMM_SELECT_READ handler is called
189 * before COMM_SELECT_WRITE). Let's try, nothing to lose.. -adx
190 */
191 ClearSendqBlocked(client_p);
192 send_queued_write(client_p);
193 }
194
195 if (IsClient(client_p))
196 {
197 ++ServerStats.is_cl;
198 ServerStats.is_cbs += client_p->localClient->send.bytes;
199 ServerStats.is_cbr += client_p->localClient->recv.bytes;
200 ServerStats.is_cti += CurrentTime - client_p->localClient->firsttime;
201 }
202 else if (IsServer(client_p))
203 {
204 ++ServerStats.is_sv;
205 ServerStats.is_sbs += client_p->localClient->send.bytes;
206 ServerStats.is_sbr += client_p->localClient->recv.bytes;
207 ServerStats.is_sti += CurrentTime - client_p->localClient->firsttime;
208
209 DLINK_FOREACH(ptr, server_items.head)
210 {
211 struct MaskItem *conf = ptr->data;
212
213 if (irccmp(conf->name, client_p->name))
214 continue;
215
216 /*
217 * Reset next-connect cycle of all connect{} blocks that match
218 * this servername.
219 */
220 conf->until = CurrentTime + conf->class->con_freq;
221 }
222 }
223 else
224 ++ServerStats.is_ni;
225
226 #ifdef HAVE_LIBCRYPTO
227 if (client_p->localClient->fd.ssl)
228 {
229 SSL_set_shutdown(client_p->localClient->fd.ssl, SSL_RECEIVED_SHUTDOWN);
230
231 if (!SSL_shutdown(client_p->localClient->fd.ssl))
232 SSL_shutdown(client_p->localClient->fd.ssl);
233 }
234 #endif
235 if (client_p->localClient->fd.flags.open)
236 fd_close(&client_p->localClient->fd);
237
238 dbuf_clear(&client_p->localClient->buf_sendq);
239 dbuf_clear(&client_p->localClient->buf_recvq);
240
241 MyFree(client_p->localClient->passwd);
242 detach_conf(client_p, CONF_CLIENT|CONF_OPER|CONF_SERVER);
243 client_p->from = NULL; /* ...this should catch them! >:) --msa */
244 }
245
246 #ifdef HAVE_LIBCRYPTO
247 /*
248 * ssl_handshake - let OpenSSL initialize the protocol. Register for
249 * read/write events if necessary.
250 */
251 static void
252 ssl_handshake(int fd, struct Client *client_p)
253 {
254 int ret = SSL_accept(client_p->localClient->fd.ssl);
255
256 if (ret <= 0)
257 switch (SSL_get_error(client_p->localClient->fd.ssl, ret))
258 {
259 case SSL_ERROR_WANT_WRITE:
260 comm_setselect(&client_p->localClient->fd, COMM_SELECT_WRITE,
261 (PF *) ssl_handshake, client_p, 0);
262 return;
263
264 case SSL_ERROR_WANT_READ:
265 comm_setselect(&client_p->localClient->fd, COMM_SELECT_READ,
266 (PF *) ssl_handshake, client_p, 0);
267 return;
268
269 default:
270 exit_client(client_p, client_p, "Error during SSL handshake");
271 return;
272 }
273
274 execute_callback(auth_cb, client_p);
275 }
276 #endif
277
278 /*
279 * add_connection - creates a client which has just connected to us on
280 * the given fd. The sockhost field is initialized with the ip# of the host.
281 * An unique id is calculated now, in case it is needed for auth.
282 * The client is sent to the auth module for verification, and not put in
283 * any client list yet.
284 */
285 void
286 add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd)
287 {
288 struct Client *new_client = make_client(NULL);
289
290 fd_open(&new_client->localClient->fd, fd, 1,
291 (listener->flags & LISTENER_SSL) ?
292 "Incoming SSL connection" : "Incoming connection");
293
294 /*
295 * copy address to 'sockhost' as a string, copy it to host too
296 * so we have something valid to put into error messages...
297 */
298 memcpy(&new_client->localClient->ip, irn, sizeof(struct irc_ssaddr));
299
300 getnameinfo((struct sockaddr *)&new_client->localClient->ip,
301 new_client->localClient->ip.ss_len, new_client->sockhost,
302 sizeof(new_client->sockhost), NULL, 0, NI_NUMERICHOST);
303 new_client->localClient->aftype = new_client->localClient->ip.ss.ss_family;
304
305 if (new_client->sockhost[0] == ':' && new_client->sockhost[1] == ':')
306 {
307 strlcpy(new_client->host, "0", sizeof(new_client->host));
308 strlcpy(new_client->host+1, new_client->sockhost, sizeof(new_client->host)-1);
309 memmove(new_client->sockhost+1, new_client->sockhost, sizeof(new_client->sockhost)-1);
310 new_client->sockhost[0] = '0';
311 }
312 else
313 strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host));
314
315 new_client->localClient->listener = listener;
316 ++listener->ref_count;
317
318 #ifdef HAVE_LIBCRYPTO
319 if (listener->flags & LISTENER_SSL)
320 {
321 if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.server_ctx)) == NULL)
322 {
323 ilog(LOG_TYPE_IRCD, "SSL_new() ERROR! -- %s",
324 ERR_error_string(ERR_get_error(), NULL));
325
326 SetDead(new_client);
327 exit_client(new_client, new_client, "SSL_new failed");
328 return;
329 }
330
331 SSL_set_fd(new_client->localClient->fd.ssl, fd);
332 ssl_handshake(0, new_client);
333 }
334 else
335 #endif
336 execute_callback(auth_cb, new_client);
337 }
338
339 /*
340 * stolen from squid - its a neat (but overused! :) routine which we
341 * can use to see whether we can ignore this errno or not. It is
342 * generally useful for non-blocking network IO related errnos.
343 * -- adrian
344 */
345 int
346 ignoreErrno(int ierrno)
347 {
348 switch (ierrno)
349 {
350 case EINPROGRESS:
351 case EWOULDBLOCK:
352 #if EAGAIN != EWOULDBLOCK
353 case EAGAIN:
354 #endif
355 case EALREADY:
356 case EINTR:
357 #ifdef ERESTART
358 case ERESTART:
359 #endif
360 return 1;
361 default:
362 return 0;
363 }
364 }
365
366 /*
367 * comm_settimeout() - set the socket timeout
368 *
369 * Set the timeout for the fd
370 */
371 void
372 comm_settimeout(fde_t *fd, time_t timeout, PF *callback, void *cbdata)
373 {
374 assert(fd->flags.open);
375
376 fd->timeout = CurrentTime + (timeout / 1000);
377 fd->timeout_handler = callback;
378 fd->timeout_data = cbdata;
379 }
380
381 /*
382 * comm_setflush() - set a flush function
383 *
384 * A flush function is simply a function called if found during
385 * comm_timeouts(). Its basically a second timeout, except in this case
386 * I'm too lazy to implement multiple timeout functions! :-)
387 * its kinda nice to have it separate, since this is designed for
388 * flush functions, and when comm_close() is implemented correctly
389 * with close functions, we _actually_ don't call comm_close() here ..
390 * -- originally Adrian's notes
391 * comm_close() is replaced with fd_close() in fdlist.c
392 */
393 void
394 comm_setflush(fde_t *fd, time_t timeout, PF *callback, void *cbdata)
395 {
396 assert(fd->flags.open);
397
398 fd->flush_timeout = CurrentTime + (timeout / 1000);
399 fd->flush_handler = callback;
400 fd->flush_data = cbdata;
401 }
402
403 /*
404 * comm_checktimeouts() - check the socket timeouts
405 *
406 * All this routine does is call the given callback/cbdata, without closing
407 * down the file descriptor. When close handlers have been implemented,
408 * this will happen.
409 */
410 void
411 comm_checktimeouts(void *notused)
412 {
413 int i;
414 fde_t *F;
415 PF *hdl;
416 void *data;
417
418 for (i = 0; i < FD_HASH_SIZE; i++)
419 for (F = fd_hash[i]; F != NULL; F = fd_next_in_loop)
420 {
421 assert(F->flags.open);
422 fd_next_in_loop = F->hnext;
423
424 /* check flush functions */
425 if (F->flush_handler && F->flush_timeout > 0 &&
426 F->flush_timeout < CurrentTime)
427 {
428 hdl = F->flush_handler;
429 data = F->flush_data;
430 comm_setflush(F, 0, NULL, NULL);
431 hdl(F, data);
432 }
433
434 /* check timeouts */
435 if (F->timeout_handler && F->timeout > 0 &&
436 F->timeout < CurrentTime)
437 {
438 /* Call timeout handler */
439 hdl = F->timeout_handler;
440 data = F->timeout_data;
441 comm_settimeout(F, 0, NULL, NULL);
442 hdl(F, data);
443 }
444 }
445 }
446
447 /*
448 * void comm_connect_tcp(int fd, const char *host, unsigned short port,
449 * struct sockaddr *clocal, int socklen,
450 * CNCB *callback, void *data, int aftype, int timeout)
451 * Input: An fd to connect with, a host and port to connect to,
452 * a local sockaddr to connect from + length(or NULL to use the
453 * default), a callback, the data to pass into the callback, the
454 * address family.
455 * Output: None.
456 * Side-effects: A non-blocking connection to the host is started, and
457 * if necessary, set up for selection. The callback given
458 * may be called now, or it may be called later.
459 */
460 void
461 comm_connect_tcp(fde_t *fd, const char *host, unsigned short port,
462 struct sockaddr *clocal, int socklen, CNCB *callback,
463 void *data, int aftype, int timeout)
464 {
465 struct addrinfo hints, *res;
466 char portname[PORTNAMELEN + 1];
467
468 assert(callback);
469 fd->connect.callback = callback;
470 fd->connect.data = data;
471
472 fd->connect.hostaddr.ss.ss_family = aftype;
473 fd->connect.hostaddr.ss_port = htons(port);
474
475 /* Note that we're using a passed sockaddr here. This is because
476 * generally you'll be bind()ing to a sockaddr grabbed from
477 * getsockname(), so this makes things easier.
478 * XXX If NULL is passed as local, we should later on bind() to the
479 * virtual host IP, for completeness.
480 * -- adrian
481 */
482 if ((clocal != NULL) && (bind(fd->fd, clocal, socklen) < 0))
483 {
484 /* Failure, call the callback with COMM_ERR_BIND */
485 comm_connect_callback(fd, COMM_ERR_BIND);
486 /* ... and quit */
487 return;
488 }
489
490 /* Next, if we have been given an IP, get the addr and skip the
491 * DNS check (and head direct to comm_connect_tryconnect().
492 */
493 memset(&hints, 0, sizeof(hints));
494 hints.ai_family = AF_UNSPEC;
495 hints.ai_socktype = SOCK_STREAM;
496 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
497
498 snprintf(portname, sizeof(portname), "%d", port);
499
500 if (getaddrinfo(host, portname, &hints, &res))
501 {
502 /* Send the DNS request, for the next level */
503 if (aftype == AF_INET6)
504 gethost_byname_type(comm_connect_dns_callback, fd, host, T_AAAA);
505 else
506 gethost_byname_type(comm_connect_dns_callback, fd, host, T_A);
507 }
508 else
509 {
510 /* We have a valid IP, so we just call tryconnect */
511 /* Make sure we actually set the timeout here .. */
512 assert(res != NULL);
513 memcpy(&fd->connect.hostaddr, res->ai_addr, res->ai_addrlen);
514 fd->connect.hostaddr.ss_len = res->ai_addrlen;
515 fd->connect.hostaddr.ss.ss_family = res->ai_family;
516 freeaddrinfo(res);
517 comm_settimeout(fd, timeout*1000, comm_connect_timeout, NULL);
518 comm_connect_tryconnect(fd, NULL);
519 }
520 }
521
522 /*
523 * comm_connect_callback() - call the callback, and continue with life
524 */
525 static void
526 comm_connect_callback(fde_t *fd, int status)
527 {
528 CNCB *hdl;
529
530 /* This check is gross..but probably necessary */
531 if (fd->connect.callback == NULL)
532 return;
533
534 /* Clear the connect flag + handler */
535 hdl = fd->connect.callback;
536 fd->connect.callback = NULL;
537
538 /* Clear the timeout handler */
539 comm_settimeout(fd, 0, NULL, NULL);
540
541 /* Call the handler */
542 hdl(fd, status, fd->connect.data);
543 }
544
545 /*
546 * comm_connect_timeout() - this gets called when the socket connection
547 * times out. This *only* can be called once connect() is initially
548 * called ..
549 */
550 static void
551 comm_connect_timeout(fde_t *fd, void *notused)
552 {
553 /* error! */
554 comm_connect_callback(fd, COMM_ERR_TIMEOUT);
555 }
556
557 /*
558 * comm_connect_dns_callback() - called at the completion of the DNS request
559 *
560 * The DNS request has completed, so if we've got an error, return it,
561 * otherwise we initiate the connect()
562 */
563 static void
564 comm_connect_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name)
565 {
566 fde_t *F = vptr;
567
568 if (name == NULL)
569 {
570 comm_connect_callback(F, COMM_ERR_DNS);
571 return;
572 }
573
574 /* No error, set a 10 second timeout */
575 comm_settimeout(F, 30*1000, comm_connect_timeout, NULL);
576
577 /* Copy over the DNS reply info so we can use it in the connect() */
578 /*
579 * Note we don't fudge the refcount here, because we aren't keeping
580 * the DNS record around, and the DNS cache is gone anyway..
581 * -- adrian
582 */
583 memcpy(&F->connect.hostaddr, addr, addr->ss_len);
584 /* The cast is hacky, but safe - port offset is same on v4 and v6 */
585 ((struct sockaddr_in *) &F->connect.hostaddr)->sin_port =
586 F->connect.hostaddr.ss_port;
587 F->connect.hostaddr.ss_len = addr->ss_len;
588
589 /* Now, call the tryconnect() routine to try a connect() */
590 comm_connect_tryconnect(F, NULL);
591 }
592
593 /* static void comm_connect_tryconnect(int fd, void *notused)
594 * Input: The fd, the handler data(unused).
595 * Output: None.
596 * Side-effects: Try and connect with pending connect data for the FD. If
597 * we succeed or get a fatal error, call the callback.
598 * Otherwise, it is still blocking or something, so register
599 * to select for a write event on this FD.
600 */
601 static void
602 comm_connect_tryconnect(fde_t *fd, void *notused)
603 {
604 int retval;
605
606 /* This check is needed or re-entrant s_bsd_* like sigio break it. */
607 if (fd->connect.callback == NULL)
608 return;
609
610 /* Try the connect() */
611 retval = connect(fd->fd, (struct sockaddr *) &fd->connect.hostaddr,
612 fd->connect.hostaddr.ss_len);
613
614 /* Error? */
615 if (retval < 0)
616 {
617 /*
618 * If we get EISCONN, then we've already connect()ed the socket,
619 * which is a good thing.
620 * -- adrian
621 */
622 if (errno == EISCONN)
623 comm_connect_callback(fd, COMM_OK);
624 else if (ignoreErrno(errno))
625 /* Ignore error? Reschedule */
626 comm_setselect(fd, COMM_SELECT_WRITE, comm_connect_tryconnect,
627 NULL, 0);
628 else
629 /* Error? Fail with COMM_ERR_CONNECT */
630 comm_connect_callback(fd, COMM_ERR_CONNECT);
631 return;
632 }
633
634 /* If we get here, we've suceeded, so call with COMM_OK */
635 comm_connect_callback(fd, COMM_OK);
636 }
637
638 /*
639 * comm_errorstr() - return an error string for the given error condition
640 */
641 const char *
642 comm_errstr(int error)
643 {
644 if (error < 0 || error >= COMM_ERR_MAX)
645 return "Invalid error number!";
646 return comm_err_str[error];
647 }
648
649 /*
650 * comm_open() - open a socket
651 *
652 * This is a highly highly cut down version of squid's comm_open() which
653 * for the most part emulates socket(), *EXCEPT* it fails if we're about
654 * to run out of file descriptors.
655 */
656 int
657 comm_open(fde_t *F, int family, int sock_type, int proto, const char *note)
658 {
659 int fd;
660
661 /* First, make sure we aren't going to run out of file descriptors */
662 if (number_fd >= hard_fdlimit)
663 {
664 errno = ENFILE;
665 return -1;
666 }
667
668 /*
669 * Next, we try to open the socket. We *should* drop the reserved FD
670 * limit if/when we get an error, but we can deal with that later.
671 * XXX !!! -- adrian
672 */
673 fd = socket(family, sock_type, proto);
674 if (fd < 0)
675 return -1; /* errno will be passed through, yay.. */
676
677 execute_callback(setup_socket_cb, fd);
678
679 /* update things in our fd tracking */
680 fd_open(F, fd, 1, note);
681 return 0;
682 }
683
684 /*
685 * comm_accept() - accept an incoming connection
686 *
687 * This is a simple wrapper for accept() which enforces FD limits like
688 * comm_open() does. Returned fd must be either closed or tagged with
689 * fd_open (this function no longer does it).
690 */
691 int
692 comm_accept(struct Listener *lptr, struct irc_ssaddr *pn)
693 {
694 int newfd;
695 socklen_t addrlen = sizeof(struct irc_ssaddr);
696
697 if (number_fd >= hard_fdlimit)
698 {
699 errno = ENFILE;
700 return -1;
701 }
702
703 /*
704 * Next, do the accept(). if we get an error, we should drop the
705 * reserved fd limit, but we can deal with that when comm_open()
706 * also does it. XXX -- adrian
707 */
708 newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, &addrlen);
709 if (newfd < 0)
710 return -1;
711
712 #ifdef IPV6
713 remove_ipv6_mapping(pn);
714 #else
715 pn->ss_len = addrlen;
716 #endif
717
718 execute_callback(setup_socket_cb, newfd);
719
720 /* .. and return */
721 return newfd;
722 }
723
724 /*
725 * remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address
726 * OSes with IPv6 mapping listening on both
727 * AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures
728 *
729 */
730 #ifdef IPV6
731 void
732 remove_ipv6_mapping(struct irc_ssaddr *addr)
733 {
734 if (addr->ss.ss_family == AF_INET6)
735 {
736 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr))
737 {
738 struct sockaddr_in6 v6;
739 struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
740
741 memcpy(&v6, addr, sizeof(v6));
742 memset(v4, 0, sizeof(struct sockaddr_in));
743 memcpy(&v4->sin_addr, &v6.sin6_addr.s6_addr[12], sizeof(v4->sin_addr));
744
745 addr->ss.ss_family = AF_INET;
746 addr->ss_len = sizeof(struct sockaddr_in);
747 }
748 else
749 addr->ss_len = sizeof(struct sockaddr_in6);
750 }
751 else
752 addr->ss_len = sizeof(struct sockaddr_in);
753 }
754 #endif

Properties

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