Gateway acquisition and setting (Linux C)

Source: Internet
Author: User

# Include <sys/types. h> <br/> # include <sys/socket. h> <br/> # include <netinet/in. h> <br/> # include <net/if. h> <br/> # include <sys/IOCTL. h> <br/> # include <errno. h> <br/> # include <string. h> <br/> # include <net/route. h> <br/> # include <unistd. h> <br/> # include <stdio. h> </P> <p> int set_gateway (unsigned long GW) <br/>{< br/> int skfd; <br/> struct rtentry RT; <br/> int err; </P> <p> skfd = socket (pf_inet, sock_d Gram, 0); <br/> If (skfd <0) <br/> return-1; </P> <p>/* Delete existing defalt gateway */<br/> memset (& RT, 0, sizeof (RT )); </P> <p> RT. rt_dst.sa_family = af_inet; <br/> (struct sockaddr_in *) & RT. rt_dst)-> sin_addr.s_addr = 0; </P> <p> RT. rt_genmask.sa_family = af_inet; <br/> (struct sockaddr_in *) & RT. rt_genmask)-> sin_addr.s_addr = 0; </P> <p> RT. rt_flags = rtf_up; </P> <p> err = IOCTL (skfd, siocde LRT, & RT); </P> <p> If (ERR = 0 | errno = esrch) & GW) {<br/>/* Set Default Gateway */<br/> memset (& RT, 0, sizeof (RT); </P> <p> RT. rt_dst.sa_family = af_inet; <br/> (struct sockaddr_in *) & RT. rt_dst)-> sin_addr.s_addr = 0; </P> <p> RT. rt_gateway.sa_family = af_inet; <br/> (struct sockaddr_in *) & RT. rt_gateway)-> sin_addr.s_addr = GW; </P> <p> RT. rt_genmask.sa_family = af_inet; <br/> (struct so Ckaddr_in *) & RT. rt_genmask)-> sin_addr.s_addr = 0; </P> <p> RT. rt_flags = rtf_up | rtf_gateway; </P> <p> err = IOCTL (skfd, siocaddrt, & RT ); <br/>}</P> <p> close (skfd); </P> <p> return err; <br/>}</P> <p> int get_gateway (unsigned long * P) <br/>{< br/> file * FP; <br/> char Buf [256]; // 128 is enough for Linux <br/> char iface [16]; <br/> unsigned long dest_addr, gate_addr; <br/> * P = inaddr_none; <br/> Fp = fopen ("/proc/NET/route", "R"); <br/> If (FP = NULL) <br/> return-1; <br/>/* Skip title line */<br/> fgets (BUF, sizeof (BUF), FP); <br/> while (fgets (BUF, sizeof (BUF), FP) {<br/> If (sscanf (BUF, "% S/T % LX/T % lx", iface, & dest_addr, & gate_addr )! = 3 | <br/> dest_addr! = 0) <br/> continue; <br/> * P = gate_addr; <br/> break; <br/>}</P> <p> fclose (FP); <br/> return 0; <br/>}</P> <p>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.