Annotation of embedaddon/miniupnpd/upnputils.c, revision 1.1.1.1
1.1 misho 1: /* $Id: upnputils.c,v 1.3 2011/05/20 09:42:23 nanard Exp $ */
2: /* MiniUPnP project
3: * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
4: * (c) 2006-2011 Thomas Bernard
5: * This software is subject to the conditions detailed
6: * in the LICENCE file provided within the distribution */
7:
8: #include "config.h"
9:
10: #include <stdio.h>
11: #include <sys/types.h>
12: #include <sys/socket.h>
13: #include <netinet/in.h>
14: #include <arpa/inet.h>
15: #ifdef AF_LINK
16: #include <net/if_dl.h>
17: #endif
18:
19: #include "upnputils.h"
20:
21: int
22: sockaddr_to_string(const struct sockaddr * addr, char * str, size_t size)
23: {
24: char buffer[64];
25: unsigned short port = 0;
26: int n = -1;
27:
28: switch(addr->sa_family)
29: {
30: case AF_INET6:
31: inet_ntop(addr->sa_family,
32: &((struct sockaddr_in6 *)addr)->sin6_addr,
33: buffer, sizeof(buffer));
34: port = ntohs(((struct sockaddr_in6 *)addr)->sin6_port);
35: n = snprintf(str, size, "[%s]:%hu", buffer, port);
36: break;
37: case AF_INET:
38: inet_ntop(addr->sa_family,
39: &((struct sockaddr_in *)addr)->sin_addr,
40: buffer, sizeof(buffer));
41: port = ntohs(((struct sockaddr_in *)addr)->sin_port);
42: n = snprintf(str, size, "%s:%hu", buffer, port);
43: break;
44: #ifdef AF_LINK
45: case AF_LINK:
46: {
47: struct sockaddr_dl * sdl = (struct sockaddr_dl *)addr;
48: n = snprintf(str, size, "index=%hu type=%d %s",
49: sdl->sdl_index, sdl->sdl_type,
50: link_ntoa(sdl));
51: }
52: break;
53: #endif
54: default:
55: n = snprintf(str, size, "unknown address family %d", addr->sa_family);
56: #if 0
57: n = snprintf(str, size, "unknown address family %d "
58: "%02x %02x %02x %02x %02x %02x %02x %02x",
59: addr->sa_family,
60: addr->sa_data[0], addr->sa_data[1], (unsigned)addr->sa_data[2], addr->sa_data[3],
61: addr->sa_data[4], addr->sa_data[5], (unsigned)addr->sa_data[6], addr->sa_data[7]);
62: #endif
63: }
64: return n;
65: }
66:
67:
68:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>