|
| 1 | +/** |
| 2 | + * ipaddr - Show the IP addresses associated with given devices. |
| 3 | + * |
| 4 | + * Usage: |
| 5 | + * ips [options] |
| 6 | + * |
| 7 | + * Options: |
| 8 | + * |
| 9 | + * --all Show all familes. |
| 10 | + * --ipv4 Show IPv4. |
| 11 | + * --ipv6 Show IPv6. |
| 12 | + * --device Show only the given device. |
| 13 | + * |
| 14 | + * Examples: |
| 15 | + * |
| 16 | + * $ ips -d eth0 -4 |
| 17 | + * eth0 192.168.0.3 |
| 18 | + * |
| 19 | + * $ ips -a |
| 20 | + * lo 127.0.0.1 |
| 21 | + * eth0 192.168.0.3 |
| 22 | + * lo ::1 |
| 23 | + * eth0 fe80::62a4:4cff:fe2d:fab9 |
| 24 | + * teredo 2001:0:53aa:64c:301f:13f3:4fe4:3a0b |
| 25 | + * teredo fe80::ffff:ffff:ffff |
| 26 | + * |
| 27 | + */ |
| 28 | + |
| 29 | +#include <stdio.h> |
| 30 | +#include <sys/types.h> |
| 31 | +#include <ifaddrs.h> |
| 32 | +#include <netinet/in.h> |
| 33 | +#include <string.h> |
| 34 | +#include <arpa/inet.h> |
| 35 | +#include <stdlib.h> |
| 36 | +#include <getopt.h> |
| 37 | + |
| 38 | + |
| 39 | +int main (int argc, char * argv[]) |
| 40 | +{ |
| 41 | + int c; |
| 42 | + |
| 43 | + /** |
| 44 | + * Flags. |
| 45 | + */ |
| 46 | + char *dev = NULL; |
| 47 | + int show_ipv4 = 0; |
| 48 | + int show_ipv6 = 0; |
| 49 | + |
| 50 | + while (1) |
| 51 | + { |
| 52 | + static struct option long_options[] = |
| 53 | + { |
| 54 | + {"device", required_argument, 0, 'd'}, |
| 55 | + {"all", no_argument, 0, 'a'}, |
| 56 | + {"ipv4", no_argument, 0, '4'}, |
| 57 | + {"ipv6", no_argument, 0, '6'}, |
| 58 | + {0, 0, 0, 0} |
| 59 | + }; |
| 60 | + |
| 61 | + /* getopt_long stores the option index here. */ |
| 62 | + int option_index = 0; |
| 63 | + |
| 64 | + c = getopt_long(argc, argv, "d:a46", long_options, &option_index); |
| 65 | + |
| 66 | + /* Detect the end of the options. */ |
| 67 | + if (c == -1) |
| 68 | + break; |
| 69 | + |
| 70 | + switch (c) |
| 71 | + { |
| 72 | + case '4': |
| 73 | + show_ipv4 = 1; |
| 74 | + break; |
| 75 | + case '6': |
| 76 | + show_ipv6 = 1; |
| 77 | + break; |
| 78 | + case 'a': |
| 79 | + show_ipv4 = 1; |
| 80 | + show_ipv6 = 1; |
| 81 | + break; |
| 82 | + case 'd': |
| 83 | + dev = strdup( optarg ); |
| 84 | + break; |
| 85 | + case '?': |
| 86 | + /* getopt_long already printed an error message. */ |
| 87 | + exit(1); |
| 88 | + break; |
| 89 | + |
| 90 | + default: |
| 91 | + fprintf( stderr, "Unknown argument" ); |
| 92 | + exit (1); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + struct ifaddrs * ifAddrStruct=NULL; |
| 97 | + struct ifaddrs * ifa=NULL; |
| 98 | + void * tmpAddrPtr=NULL; |
| 99 | + |
| 100 | + getifaddrs(&ifAddrStruct); |
| 101 | + |
| 102 | + for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) |
| 103 | + { |
| 104 | + /** |
| 105 | + * If we have a non-null entry. |
| 106 | + */ |
| 107 | + if ( ifa && ifa->ifa_addr ) |
| 108 | + { |
| 109 | + |
| 110 | + /** |
| 111 | + * And it is IPv4 and we're showing IPv4 .. |
| 112 | + */ |
| 113 | + if (ifa ->ifa_addr->sa_family==AF_INET && show_ipv4 ) |
| 114 | + { |
| 115 | + |
| 116 | + tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr; |
| 117 | + char addressBuffer[INET_ADDRSTRLEN]; |
| 118 | + inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN); |
| 119 | + |
| 120 | + if ( ( dev == NULL ) || |
| 121 | + ( strcmp( dev, ifa->ifa_name ) == 0 ) ) |
| 122 | + printf("%s %s\n", ifa->ifa_name, addressBuffer); |
| 123 | + } |
| 124 | + else if (ifa->ifa_addr->sa_family==AF_INET6 && show_ipv6 ) |
| 125 | + { |
| 126 | + /** |
| 127 | + * IPv6 address, and we're showing them. |
| 128 | + */ |
| 129 | + tmpAddrPtr=&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr; |
| 130 | + char addressBuffer[INET6_ADDRSTRLEN]; |
| 131 | + inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN); |
| 132 | + if ( ( dev == NULL ) || |
| 133 | + ( strcmp( dev, ifa->ifa_name ) == 0 ) ) |
| 134 | + printf("%s %s\n", ifa->ifa_name, addressBuffer); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + if (ifAddrStruct!=NULL) |
| 140 | + freeifaddrs(ifAddrStruct); |
| 141 | + |
| 142 | + return 0; |
| 143 | +} |
0 commit comments