1
/* $Id: upnputils.c,v 1.3 2011/05/20 09:42:23 nanard Exp $ */
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 */
11
#include <sys/types.h>
12
#include <sys/socket.h>
13
#include <netinet/in.h>
14
#include <arpa/inet.h>
16
#include <net/if_dl.h>
19
#include "upnputils.h"
22
sockaddr_to_string(const struct sockaddr * addr, char * str, size_t size)
25
unsigned short port = 0;
28
switch(addr->sa_family)
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);
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);
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,
55
n = snprintf(str, size, "unknown address family %d", addr->sa_family);
57
n = snprintf(str, size, "unknown address family %d "
58
"%02x %02x %02x %02x %02x %02x %02x %02x",
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]);