Date: Sun, 13 Feb 2000 14:46:37 -0800 From: nathan@khmere.com To: "freebsd-net@FreeBSD.ORG" <freebsd-net@FreeBSD.ORG>, "hackers@FreeBSD.ORG" <hackers@FreeBSD.ORG> Subject: Setting the default gw (with c) Message-ID: <38A7344C.71BA918C@khmere.com>
next in thread | raw e-mail | index | archive | help
Greetings ! I have a question, I need to write a small program that sets the default gw. It is written in C and I have looked at several way to do it. One way is using ioctl but that way seems a little "old" and not very well documented. Here is a simple snippet of my code, now when I run it I get an error "error 22" invalid arg. Does anyone have experience with this and can point out what I am doing wrong ? I did have it to where it would work but the kernel never added my netmask to the routing table. So the route would kinda "hang" their and not do anything. I did tell it that it was being sent but..... also the flags never where set proper on the route (when doing route -dvn flush ) Any help would be great ! (oh if this looks familiar, I learned this from reading both ruote.c and netstat.c. ) struct rt_data_msg{ struct rt_msghdr add_rtm; union{ struct sockaddr sa; struct sockaddr_in sin; struct sockaddr_dl sdl; }rt_dst, rt_gate, rt_mask; }; int set_droute(char *gw){ int sockfd, i, err = 0; int flags = 0, r_addrs = 0; u_long inits; char t_gw[32]; register int l; struct rt_data_msg add_rt; pid_t pid; /* check to see if we have a gateway address or quit */ if(!gw){ ERR("set_droute: FREEBSD no route sent ! %d"); return(BAD); } else{ /* convet this address if not valid quit */ if((inet_aton(gw, &so_gate.sin.sin_addr)) < 0){ ERR("set_droute: FREEBSD route not valid ! %d"); return(BAD); } } memset(&add_rt, 0, sizeof(struct rt_data_msg)); /* check to see if thier is an existing route or not make an entry from scratch */ if( (i = get_droute(t_gw)) == BAD){ /* make the address and netmasks */ if((inet_aton("0.0.0.0", &add_rt.rt_mask.sin.sin_addr)) < 0){ err++; } if((inet_aton("0.0.0.0", &add_rt.rt_dst.sin.sin_addr)) < 0) { err++; } if((inet_aton(gw, &add_rt.rt_gate.sin.sin_addr)) < 0){ err++; } if(err > 0){ ERR("get_droute: FREEBSD cannot set mask or dst or gw invalid addresses %d"); return(BAD); } inits = RTV_EXPIRE; inits |= RTV_RPIPE; inits |= RTV_SPIPE; inits |= RTV_SSTHRESH; r_addrs = RTAX_DST; r_addrs |= RTAX_GATEWAY; r_addrs |= RTAX_NETMASK; flags = RTF_UP; flags |= RTF_GATEWAY; flags |= RTF_STATIC; flags |= RTF_PRCLONING; /* add thier size */ #define ADD_SIZE(w) l = ROUNDUP(w.sa.sa_len) #define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)); ADD_SIZE(add_rt.rt_dst); ADD_SIZE(add_rt.rt_gate); ADD_SIZE(add_rt.rt_mask); /* add their address family */ #define ADD_FAM(x) x.sin.sin_family = AF_INET; ADD_FAM(add_rt.rt_dst); ADD_FAM(add_rt.rt_gate); ADD_FAM(add_rt.rt_mask); ADD_FAM(add_rt.rt_genmask); /* set up all of the flags and tables */ #define RD add_rt.add_rtm RD.rtm_msglen = l; RD.rtm_version = RTM_VERSION; RD.rtm_type = RTM_ADD; RD.rtm_flags = flags; RD.rtm_addrs = rtm_addrs; RD.rtm_inits = RTF_GATEWAY; RD.rtm_pid = pid = getpid(); RD.rtm_seq = ++SEQ; if( (sockfd = socket(PF_ROUTE, SOCK_RAW, 0)) < 0){ ERR("set_droute: FREEBSD cannot open socket %d"); } /* send it to the kernel */ i = write(sockfd, (char *)&add_rt, l); syslog(LOG_ERR, "errno = %d ", errno); } else{ /* get a buffer for the data */ ERR("get_droute returned ok %d "); } return(GOOD); } Thank you nathan To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-net" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?38A7344C.71BA918C>