Set an IP address and routing in C

If you want to set an ipaddress and route in a C program, the following code should work for you.

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <sys/socket.h>
  5. #include <net/if.h>
  6. #include <net/if_arp.h>
  7. #include <sys/ioctl.h>
  8. #include <linux/sockios.h>
  9. #include <errno.h>
  10. #include <netinet/in.h>
  11. #include <net/route.h>
  12. #if defined(__GLIBC__) && __GLIBC__ >=2 && __GLIBC_MINOR__ >= 1
  13. #include <netpacket/packet.h>
  14. #include <net/ethernet.h>
  15. #else
  16. #include <sys/types.h>
  17. #include <netinet/if_ether.h>
  18. #endif
  19.  
  20. /**
  21. * Create socket function
  22. */
  23. int create_socket() {
  24.  
  25. int sockfd = 0;
  26.  
  27. sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  28. if(sockfd == -1){
  29. fprintf(stderr, "Could not get socket.\n");
  30. return -1;
  31. }
  32.  
  33. return sockfd;
  34.  
  35. }
  36.  
  37. /**
  38. * Generic ioctrlcall to reduce code size
  39. */
  40. int generic_ioctrlcall(int sockfd, u_long *flags, struct ifreq *ifr) {
  41.  
  42. if (ioctl(sockfd, (long unsigned int)flags, &ifr) < 0) {
  43. fprintf(stderr, "ioctl: %s\n", (char *)flags);
  44. return -1;
  45. }
  46. return 1;
  47. }
  48.  
  49. /**
  50. * Set route with metric 100
  51. */
  52. int set_route(int sockfd, char *gateway_addr, struct sockaddr_in *addr) {
  53.  
  54. struct rtentry route;
  55. int err = 0;
  56. memset(&route, 0, sizeof(route));
  57. addr = (struct sockaddr_in*) &route.rt_gateway;
  58. addr->sin_family = AF_INET;
  59. addr->sin_addr.s_addr = inet_addr(gateway_addr);
  60. addr = (struct sockaddr_in*) &route.rt_dst;
  61. addr->sin_family = AF_INET;
  62. addr->sin_addr.s_addr = inet_addr("0.0.0.0");
  63. addr = (struct sockaddr_in*) &route.rt_genmask;
  64. addr->sin_family = AF_INET;
  65. addr->sin_addr.s_addr = inet_addr("0.0.0.0");
  66. route.rt_flags = RTF_UP | RTF_GATEWAY;
  67. route.rt_metric = 100;
  68.  
  69. if ((err = ioctl(sockfd, SIOCADDRT, &route)) < 0) {
  70. fprintf(stderr, "ioctl: %s\n", (char *)flags);
  71. return -1;
  72. }
  73. return 1;
  74.  
  75. }
  76.  
  77. /**
  78. * Set ip function
  79. */
  80. int set_ip(char *iface_name, char *ip_addr, char *gateway_addr)
  81. {
  82. if(!iface_name)
  83. return -1;
  84.  
  85. struct ifreq ifr;
  86. struct sockaddr_in sin;
  87. int sockfd = create_socket();
  88.  
  89. sin.sin_family = AF_INET;
  90.  
  91. // Convert IP from numbers and dots to binary notation
  92. inet_aton(ip_addr,&sin.sin_addr.s_addr);
  93.  
  94. /* get interface name */
  95. strncpy(ifr.ifr_name, iface_name, IFNAMSIZ);
  96.  
  97. /* Read interface flags */
  98. generic_ioctrlcall(sockfd, (u_long *)"SIOCGIFFLAGS", &ifr);
  99.  
  100. /*
  101. * Expected in <net/if.h> according to
  102. * "UNIX Network Programming".
  103. */
  104. #ifdef ifr_flags
  105. # define IRFFLAGS ifr_flags
  106. #else /* Present on kFreeBSD */
  107. # define IRFFLAGS ifr_flagshigh
  108. #endif
  109.  
  110. // If interface is down, bring it up
  111. if (ifr.IRFFLAGS | ~(IFF_UP)) {
  112. ifr.IRFFLAGS |= IFF_UP;
  113. generic_ioctrlcall(sockfd, (u_long *)"SIOCSIFFLAGS", &ifr);
  114. }
  115.  
  116. // Set route
  117. set_route(sockfd, gateway_addr, &sin);
  118.  
  119. memcpy(&ifr.ifr_addr, &sin, sizeof(struct sockaddr));
  120.  
  121. // Set interface address
  122. if (ioctl(sockfd, SIOCSIFADDR, &ifr) < 0) {
  123. fprintf(stderr, "Cannot set IP address. ");
  124. perror(ifr.ifr_name);
  125. return -1;
  126. }
  127. #undef IRFFLAGS
  128.  
  129. return 0;
  130. }
  131.  
  132. void usage()
  133. {
  134. const char *usage = {
  135. "./set_ip [interface] [ip address] [gateway address]\n"
  136. };
  137. fprintf(stderr,"%s",usage);
  138. }
  139.  
  140. int main(int argc, char **argv)
  141. {
  142. if(argc < 3){
  143. usage();
  144. return -1;
  145. }else {
  146.  
  147. set_ip(argv[1],argv[2], argv[3]);
  148.  
  149. return 0;
  150. }
  151. }

Adapted and modified from http://www.lainoox.com/set-ip-address-c-linux/

Blog tags: 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.