diff options
Diffstat (limited to 'scripts/ip.c')
| -rw-r--r-- | scripts/ip.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/scripts/ip.c b/scripts/ip.c new file mode 100644 index 0000000..3276696 --- /dev/null +++ b/scripts/ip.c @@ -0,0 +1,32 @@ +#include <stdio.h> +#include <unistd.h> +#include <string.h> /* for strncpy */ + +#include <sys/types.h> +#include <sys/socket.h> +#include <sys/ioctl.h> +#include <netinet/in.h> +#include <net/if.h> +#include <arpa/inet.h> + +int +main() +{ + int fd; + struct ifreq ifr; + + fd = socket(AF_INET, SOCK_DGRAM, 0); + + /* I want to get an IPv4 IP address */ + ifr.ifr_addr.sa_family = AF_INET; + strncpy(ifr.ifr_name, "wlan0", IFNAMSIZ-1); + + ioctl(fd, SIOCGIFADDR, &ifr); + + close(fd); + + /* display result */ + printf("%s\n", inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr)); + + return 0; +} |