3

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

  1. C code to get the IP address

  2. Find IP address of the machine in C?

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

  • 1
    show what you tried. Did you see the answer [*The equivalent on Windows is to use the Win32 `GetAdaptersInfo()` function on XP and earlier, or the `GetAdaptersAddresses()` function on Vista and later*](https://stackoverflow.com/a/41151132/995714) in your linked question? – phuclv Mar 11 '18 at 02:25
  • https://msdn.microsoft.com/en-us/library/windows/desktop/aa365915(v=vs.85).aspx – selbie Mar 11 '18 at 02:25
  • probably want to start here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms738545(v=vs.85).aspx – yano Mar 11 '18 at 02:25
  • since you are using `gcc`, you should have setup your environment for that compiler. I.E. also download the rest of the tool accessories, like the header files and the libraries – user3629249 Mar 13 '18 at 00:02

2 Answers2

4

You could use the IpHlpApi Module. Microsoft provides Ipconfig sample code which demonstrates how to use the IP Helper APIs GetNetworkParams() and GetAdaptersInfo(). Here is a to the Windows-classic-samples Ipconfig Solution.

Alternatively you could use getaddrinfo.

Both links provide full source code example to accomplish your goals.

Paul T
  • 336
  • 2
  • 8
-1

use this code to find ip address for your system

#include<stdlib.h>

int main()
{
   system("C:\\Windows\\System32\\ipconfig");

   return 0;
}

I think it will help you.

Brahma
  • 58
  • 8