求助在linux下用c语言取得双网卡的网关地址
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了求助在linux下用c语言取得双网卡的网关地址相关的知识,希望对你有一定的参考价值。
参考技术A 多给我来点财富值哦:[code=C/C++]
#define RTF_UP 0x0001
#define RTF_GATEWAY 0x0002
#define RTF_HOST 0x0004
#define _PATH_PROCNET_ROUTE "/proc/net/route"
struct addr
struct sockaddr_in addr;
char *name;
int host;
struct addr *next;
;
static struct addr *INET_nn = NULL; /* addr-to-name cache */
struct aftype
char *name;
char *title;
int af;
int alen;
char *(*print) (unsigned char *);
char *(*sprint) (struct sockaddr *, int numeric);
int (*input) (int type, char *bufp, struct sockaddr *);
void (*herror) (char *text);
int (*rprint) (int options);
int (*rinput) (int typ, int ext, char **argv);
/* may modify src */
int (*getmask) (char *src, struct sockaddr * mask, char *name);
int fd;
char *flag_file;
;
struct aftype inet_aftype;
struct sockaddr snet_gateway;
char *proc_gen_fmt(char *name, int more, FILE * fh,...)
char buf[512], format[512] = "";
char *title, *head, *hdr;
va_list ap;
if (!fgets(buf, (sizeof buf) - 1, fh))
return NULL;
strcat(buf, " ");
va_start(ap, fh);
title = va_arg(ap, char *);
for (hdr = buf; hdr;)
while (isspace(*hdr) || *hdr == '|')
hdr++;
head = hdr;
hdr = strpbrk(hdr, "| \t\n");
if (hdr)
*hdr++ = 0;
if (!strcmp(title, head))
strcat(format, va_arg(ap, char *));
title = va_arg(ap, char *);
if (!title || !head)
break;
else
strcat(format, "%*s"); /* XXX */
strcat(format, " ");
va_end(ap);
if (!more && title)
fprintf(stderr, "warning: %s does not contain required field %s\n",
name, title);
return NULL;
return strdup(format);
static int INET_getsock(char *bufp, struct sockaddr *sap)
char *sp = bufp, *bp;
unsigned int i;
unsigned val;
struct sockaddr_in *sin;
sin = (struct sockaddr_in *) sap;
sin->sin_family = AF_INET;
sin->sin_port = 0;
val = 0;
bp = (char *) &val;
for (i = 0; i < sizeof(sin->sin_addr.s_addr); i++)
*sp = toupper(*sp);
if ((*sp >= 'A') && (*sp <= 'F'))
bp[i] |= (int) (*sp - 'A') + 10;
else if ((*sp >= '0') && (*sp <= '9'))
bp[i] |= (int) (*sp - '0');
else
return (-1);
bp[i] <<= 4;
sp++;
*sp = toupper(*sp);
if ((*sp >= 'A') && (*sp <= 'F'))
bp[i] |= (int) (*sp - 'A') + 10;
else if ((*sp >= '0') && (*sp <= '9'))
bp[i] |= (int) (*sp - '0');
else
return (-1);
sp++;
sin->sin_addr.s_addr = htonl(val);
return (sp - bufp);
static int INET_resolve(char *name, struct sockaddr_in *sin, int hostfirst)
struct hostent *hp;
struct netent *np;
/* Grmpf. -FvK */
sin->sin_family = AF_INET;
sin->sin_port = 0;
/* Default is special, meaning 0.0.0.0. */
if (!strcmp(name, "default"))
sin->sin_addr.s_addr = INADDR_ANY;
return (1);
/* Look to see if it's a dotted quad. */
if (inet_aton(name, &sin->sin_addr))
return 0;
#ifdef EMBED
return(-1);
#else
/* If we expect this to be a hostname, try hostname database first */
#ifdef DEBUG
if (hostfirst) fprintf (stderr, "gethostbyname (%s)\n", name);
#endif
if (hostfirst &&
(hp = gethostbyname(name)) != (struct hostent *) NULL)
memcpy((char *) &sin->sin_addr, (char *) hp->h_addr_list[0],
sizeof(struct in_addr));
return 0;
/* Try the NETWORKS database to see if this is a known network. */
#ifdef DEBUG
fprintf (stderr, "getnetbyname (%s)\n", name);
#endif
if ((np = getnetbyname(name)) != (struct netent *) NULL)
sin->sin_addr.s_addr = htonl(np->n_net);
return 1;
if (hostfirst)
/* Don't try again */
errno = h_errno;
return -1;
#ifdef DEBUG
res_init();
_res.options |= RES_DEBUG;
#endif
#ifdef DEBUG
fprintf (stderr, "gethostbyname (%s)\n", name);
#endif
if ((hp = gethostbyname(name)) == (struct hostent *) NULL)
errno = h_errno;
return -1;
memcpy((char *) &sin->sin_addr, (char *) hp->h_addr_list[0],
sizeof(struct in_addr));
return 0;
#endif
static int INET_input(int type, char *bufp, struct sockaddr *sap)
switch (type)
case 1:
return (INET_getsock(bufp, sap));
case 256:
return (INET_resolve(bufp, (struct sockaddr_in *) sap, 1));
default:
return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0));
char *safe_strncpy(char *dst, const char *src, size_t size)
dst[size-1] = '\0';
return strncpy(dst,src,size-1);
static int INET_rresolve(char *name, size_t len, struct sockaddr_in *sin,
int numeric, unsigned int netmask)
struct hostent *ent;
struct netent *np;
struct addr *pn;
unsigned long ad, host_ad;
int host = 0;
/* Grmpf. -FvK */
if (sin->sin_family != AF_INET)
errno = EAFNOSUPPORT;
return (-1);
ad = (unsigned long) sin->sin_addr.s_addr;
if (ad == INADDR_ANY)
if ((numeric & 0x0FFF) == 0)
if (numeric & 0x8000)
safe_strncpy(name, "default", len);
else
safe_strncpy(name, "*", len);
return (0);
if (numeric & 0x0FFF)
safe_strncpy(name, inet_ntoa(sin->sin_addr), len);
return (0);
if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
host = 1;
pn = INET_nn;
while (pn != NULL)
if (pn->addr.sin_addr.s_addr == ad && pn->host == host)
safe_strncpy(name, pn->name, len);
return (0);
pn = pn->next;
host_ad = ntohl(ad);
np = NULL;
ent = NULL;
#ifndef EMBED
if (host)
ent = gethostbyaddr((char *) &ad, 4, AF_INET);
if (ent != NULL)
safe_strncpy(name, ent->h_name, len);
#ifndef __UC_LIBC__
else
np = getnetbyaddr(host_ad, AF_INET);
if (np != NULL)
safe_strncpy(name, np->n_name, len);
#endif
#endif
if ((ent == NULL) && (np == NULL))
safe_strncpy(name, inet_ntoa(sin->sin_addr), len);
pn = (struct addr *) malloc(sizeof(struct addr));
pn->addr = *sin;
pn->next = INET_nn;
pn->host = host;
pn->name = (char *) malloc(strlen(name) + 1);
strcpy(pn->name, name);
INET_nn = pn;
return (0);
static char *INET_sprint(struct sockaddr *sap, int numeric)
static char buff[128];
if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
return safe_strncpy(buff, "[NONE SET]", sizeof(buff));
if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
numeric, 0xffffff00) != 0)
return (NULL);
return (buff);
int main()
int ext=1,numeric=0;
char buff[1024], iface[16], flags[64];
char gate_addr[128], net_addr[128];
char mask_addr[128];
int num, iflags, metric, refcnt, use, mss, window, irtt;
FILE *fp = fopen(_PATH_PROCNET_ROUTE, "r");
char *fmt;
if (!fp)
perror(_PATH_PROCNET_ROUTE);
printf("INET (IPv4) not configured in this system.\n");
return 1;
irtt = 0;
window = 0;
mss = 0;
fmt = proc_gen_fmt(_PATH_PROCNET_ROUTE, 0, fp,
"Iface", "%16s",
"Destination", "%128s",
"Gateway", "%128s",
"Flags", "%X",
"RefCnt", "%d",
"Use", "%d",
"Metric", "%d",
"Mask", "%128s",
"MTU", "%d",
"Window", "%d",
"IRTT", "%d",
NULL);
/* "%16s %128s %128s %X %d %d %d %128s %d %d %d\n" */
if (!fmt)
return 1;
while (fgets(buff, 1023, fp))
num = sscanf(buff, fmt,
iface, net_addr, gate_addr,
&iflags, &refcnt, &use, &metric, mask_addr,
&mss, &window, &irtt);
if (num < 10 || !(iflags & RTF_UP))
continue;
INET_input(1, gate_addr, &snet_gateway);
strcpy(gate_addr, INET_sprint(&snet_gateway, numeric | 0x4000));
gate_addr[15] = '\0';
if (iflags & RTF_GATEWAY)
printf("==gateway=======%s===%s=====\n",gate_addr,iface);
free(fmt);
(void) fclose(fp);
return (0);
[/code] 参考技术B ifconfig吗
求助:双网卡route命令设置不能用
电脑双网卡,外网连接服务器,IP192.168.30.105,默认子网255.255.225.0,网关192.168.30.1。另一个连接内网,IP10.33.42.190,子网255.255.255.252,网关10.33.42.189。设置先删除全部路由,重新配置双网卡网关,后运行命令route add -p 10.0.0.0 mask 255.255.255.252 10.33.42.189route add -p 0.0.0.0 mask 0.0.0.0 192.168.30.1设置完成后,内网IP可以访问,外网不通,禁用内网网卡,外网可通。问题在哪里,请教大神原因和解决办法。
参考技术A 最后一条命令应该改为route -p add 135.11.0.0 mask 255.255.0.0 135.11.66.1 metric 3 还有,你加的那个内网网段为什么是10段呢?这条也有问题,应该是135.11.0.0那个 ! 参考技术B 我在局域网,一个IP能上外网但是不能上数据库,另一个能上数据库但不能上外网,怎样用route命令写个批处理能同时使用?已经装好双网卡都连到交换机上了,谢谢内10.80.1 ... 【求助】高手请进,谁懂route命令设置双网卡双IP路由的问题 ,POPPUR爱以上是关于求助在linux下用c语言取得双网卡的网关地址的主要内容,如果未能解决你的问题,请参考以下文章
ROS 2.9.27设置第一块网卡内网IP设置为192。168。1。1/24那网关设置啥呢?谁知道的说详细一点,先谢谢!