1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| #include <errno.h> #include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/ioctl.h> #include <net/route.h> #include <fcntl.h> #include <sys/socket.h> #include <net/if.h> #include <unistd.h> int main() { int sockfd; struct rtentry rt; sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd == -1) { perror("socket creation failed\n"); return -1; } struct sockaddr_in *sockinfo = (struct sockaddr_in *)&rt.rt_gateway; sockinfo->sin_family = AF_INET; sockinfo->sin_addr.s_addr = inet_addr("192.168.0.1"); sockinfo = (struct sockaddr_in *)&rt.rt_dst; sockinfo->sin_family = AF_INET; sockinfo->sin_addr.s_addr = INADDR_ANY; sockinfo = (struct sockaddr_in *)&rt.rt_genmask; sockinfo->sin_family = AF_INET; sockinfo->sin_addr.s_addr = INADDR_ANY; rt.rt_flags = RTF_UP | RTF_GATEWAY; rt.rt_dev = "eth0"; if(ioctl(sockfd, SIOCADDRT, &rt) < 0 ) perror("ioctl"); return 0; }
|