Annotation of embedaddon/miniupnpd/minissdpd/listifaces.c, revision 1.1
1.1 ! misho 1: /* $Id: listifaces.c,v 1.7 2015/02/08 08:51:54 nanard Exp $ */
! 2: /* (c) 2006-2015 Thomas BERNARD
! 3: * http://miniupnp.free.fr/ http://miniupnp.tuxfamily.org/
! 4: */
! 5: #include <sys/types.h>
! 6: #include <sys/socket.h>
! 7: #include <sys/ioctl.h>
! 8: #include <net/if.h>
! 9: #include <arpa/inet.h>
! 10: #include <netinet/in.h>
! 11: #include <unistd.h>
! 12: #include <stdio.h>
! 13: #include <stdlib.h>
! 14: #include "upnputils.h"
! 15:
! 16: /* hexdump */
! 17: void printhex(const unsigned char * p, int n)
! 18: {
! 19: int i;
! 20: while(n>0)
! 21: {
! 22: for(i=0; i<16; i++)
! 23: printf("%02x ", p[i]);
! 24: printf("| ");
! 25: for(i=0; i<16; i++)
! 26: {
! 27: putchar((p[i]>=32 && p[i]<127)?p[i]:'.');
! 28: }
! 29: printf("\n");
! 30: p+=16;
! 31: n -= 16;
! 32: }
! 33: }
! 34:
! 35: /* List network interfaces */
! 36: void listifaces(void)
! 37: {
! 38: struct ifconf ifc;
! 39: char * buf = NULL;
! 40: int buflen;
! 41: int s, i;
! 42: int j;
! 43: char saddr[256/*INET_ADDRSTRLEN*/];
! 44: #ifdef __linux__
! 45: buflen = sizeof(struct ifreq)*10;
! 46: #else
! 47: buflen = 0;
! 48: #endif
! 49: /*s = socket(PF_INET, SOCK_DGRAM, 0);*/
! 50: s = socket(AF_INET, SOCK_DGRAM, 0);
! 51: do {
! 52: char * tmp;
! 53: #ifdef __linux__
! 54: buflen += buflen;
! 55: #endif
! 56: if(buflen > 0) {
! 57: tmp = realloc(buf, buflen);
! 58: if(!tmp) {
! 59: fprintf(stderr, "error allocating %d bytes.\n", buflen);
! 60: close(f);
! 61: free(buf);
! 62: return;
! 63: }
! 64: buf = tmp;
! 65: }
! 66: ifc.ifc_len = buflen;
! 67: ifc.ifc_buf = (caddr_t)buf;
! 68: if(ioctl(s, SIOCGIFCONF, &ifc) < 0)
! 69: {
! 70: perror("ioctl");
! 71: close(s);
! 72: free(buf);
! 73: return;
! 74: }
! 75: printf("buffer length=%d - buffer used=%d - sizeof(struct ifreq)=%d\n",
! 76: buflen, ifc.ifc_len, (int)sizeof(struct ifreq));
! 77: printf("IFNAMSIZ=%d ", IFNAMSIZ);
! 78: printf("sizeof(struct sockaddr)=%d sizeof(struct sockaddr_in)=%d\n",
! 79: (int)sizeof(struct sockaddr), (int)sizeof(struct sockaddr_in) );
! 80: #ifndef __linux__
! 81: if(buflen == 0)
! 82: buflen = ifc.ifc_len;
! 83: else
! 84: break;
! 85: } while(1);
! 86: #else
! 87: } while(buflen <= ifc.ifc_len);
! 88: #endif
! 89: printhex((const unsigned char *)ifc.ifc_buf, ifc.ifc_len);
! 90: printf("off index fam name address\n");
! 91: for(i = 0, j = 0; i<ifc.ifc_len; j++)
! 92: {
! 93: /*const struct ifreq * ifrp = &(ifc.ifc_req[j]);*/
! 94: const struct ifreq * ifrp = (const struct ifreq *)(buf + i);
! 95: /*inet_ntop(AF_INET, &(((struct sockaddr_in *)&(ifrp->ifr_addr))->sin_addr), saddr, sizeof(saddr));*/
! 96: saddr[0] = '\0';
! 97: /* inet_ntop(ifrp->ifr_addr.sa_family, &(ifrp->ifr_addr.sa_data[2]), saddr, sizeof(saddr)); */
! 98: sockaddr_to_string(&ifrp->ifr_addr, saddr, sizeof(saddr));
! 99: printf("0x%03x %2d %2d %-16s %s\n", i, j, ifrp->ifr_addr.sa_family, ifrp->ifr_name, saddr);
! 100: /*ifrp->ifr_addr.sa_len is only available on BSD */
! 101: #ifdef __linux__
! 102: i += sizeof(struct ifreq);
! 103: #else
! 104: if(ifrp->ifr_addr.sa_len == 0)
! 105: break;
! 106: i += IFNAMSIZ + ifrp->ifr_addr.sa_len;
! 107: #endif
! 108: }
! 109: free(buf);
! 110: close(s);
! 111: }
! 112:
! 113: int main(int argc, char * * argv)
! 114: {
! 115: (void)argc;
! 116: (void)argv;
! 117: listifaces();
! 118: return 0;
! 119: }
! 120:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>