File: listifaces.c

package info (click to toggle)
minissdpd 1.2.20130907-3+deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 304 kB
  • ctags: 198
  • sloc: ansic: 3,891; makefile: 185; sh: 90
file content (79 lines) | stat: -rw-r--r-- 1,939 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* $Id: listifaces.c,v 1.4 2007/09/23 16:59:02 nanard Exp $ */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

void printhex(const unsigned char * p, int n)
{
	int i;
	while(n>0)
	{
		for(i=0; i<16; i++)
			printf("%02x ", p[i]);
		printf("| ");
		for(i=0; i<16; i++)
		{
			putchar((p[i]>=32 && p[i]<127)?p[i]:'.');
		}
		printf("\n");
		p+=16;
		n -= 16;
	}
}

void listifaces()
{
	struct ifconf ifc;
	char * buf = 0;
	int buflen = sizeof(struct ifreq)*20;
	/*[sizeof(struct ifreq)*8];*/
	int s, i;
	int j;
	char saddr[256/*INET_ADDRSTRLEN*/];
	/*s = socket(PF_INET, SOCK_DGRAM, 0);*/
	s = socket(AF_INET, SOCK_DGRAM, 0);
	do {
		buflen += buflen;
		buf = realloc(buf, buflen);
		ifc.ifc_len = buflen;
		ifc.ifc_buf = (caddr_t)buf;
		if(ioctl(s, SIOCGIFCONF, &ifc) < 0)
		{
			perror("ioctl");
			close(s);
			free(buf);
			return;
		}
		printf("%d - %d - %d\n", buflen, ifc.ifc_len, (int)sizeof(struct ifreq));
		printf(" %d\n", IFNAMSIZ);
		printf(" %d %d\n", (int)sizeof(struct sockaddr), (int)sizeof(struct sockaddr_in) );
	} while(buflen == ifc.ifc_len);
	printhex((const unsigned char *)ifc.ifc_buf, ifc.ifc_len);
	j = 0;
	for(i=0; i<ifc.ifc_len; /*i += sizeof(struct ifreq)*/)
	{
		//const struct ifreq * ifrp = &(ifc.ifc_req[j]);
		const struct ifreq * ifrp = (const struct ifreq *)(buf + i);
		i += sizeof(ifrp->ifr_name) + 16;//ifrp->ifr_addr.sa_len;
		/*inet_ntop(AF_INET, &(((struct sockaddr_in *)&(ifrp->ifr_addr))->sin_addr), saddr, sizeof(saddr));*/
		saddr[0] = '\0';
		inet_ntop(ifrp->ifr_addr.sa_family, &(ifrp->ifr_addr.sa_data[2]), saddr, sizeof(saddr));
		printf("%2d %d %d %s %s\n", j, ifrp->ifr_addr.sa_family, -1/*ifrp->ifr_addr.sa_len*/, ifrp->ifr_name, saddr);
		j++;
	}
	free(buf);
	close(s);
}

int main(int argc, char * * argv)
{
	listifaces();
	return 0;
}