I'm new to C programming and trying to write my own code to get an IP Address on Microsoft Windows.
I've been reading about this, unfortunately all examples are meant for Linux and not Windows.
Reference
I've tried to replace <sys/socket.h>
with Winsock2.h
on all sample codes but still can't compile it under windows.
These are a few errors when I compile it under windows ...
C:\Windows C Programming> gcc get_ip_address.c -o get_ip_address
get_ip_address.c:2:24: fatal error: sys/socket.h: No such file or directory
compilation terminated.
C:\Windows C Programming>
Other errors
fatal error: arpa/inet.h: No such file or directory
fatal error: netdb.h: No such file or directory
fatal error: netdb.h: No such file or directory
fatal error: ifaddrs.h: No such file or directory
Btw, I've been able to get an ip using system()
function. But according to manlio
system() (or popen()) can be a security risk since certain environment variables (like $IFS / $PATH) can be modified so that your program will execute external programs you never intended it to (i.e. a command is specified without a path name and the command processor path name resolution mechanism is accessible to an attacker).
Also the system() function can result in exploitable vulnerabilities:
- when passing an unsanitized or improperly sanitized command string originating from a tainted source;
- if a relative path to an executable is specified and control over the current working directory is accessible to an attacker;
- if the specified executable program can be spoofed by an attacker.
This is my code using system()
C:\Windows C Programming> more ip_address_test.c
#include<stdlib.h>
int main()
{
system("ipconfig | findstr IPv4");
}
C:\Windows C Programming>
C:\Windows C Programming> gcc ip_address_test.c -o ip_address_test
C:\Windows C Programming>
C:\Windows C Programming> ip_address_test
IPv4 Address. . . . . . . . . . . : 192.168.1.1
IPv4 Address. . . . . . . . . . . : 10.1.1.1
C:\Windows C Programming>
Please let me know how to get an IP Address in Windows using C Programming. Thanks