Linux set ip address using ioctl


/ Published in: C
Save to your folder(s)



Copy this code and paste it in your HTML
  1. static int set_ip_using(const char *name, int c, unsigned long ip)
  2. {
  3. struct ifreq ifr;
  4. struct sockaddr_in sin;
  5.  
  6. safe_strncpy(ifr.ifr_name, name, IFNAMSIZ);
  7. memset(&sin, 0, sizeof(struct sockaddr));
  8. sin.sin_family = AF_INET;
  9. sin.sin_addr.s_addr = ip;
  10. memcpy(&ifr.ifr_addr, &sin, sizeof(struct sockaddr));
  11. if (ioctl(skfd, c, &ifr) < 0)
  12. return -1;
  13. return 0;
  14. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.