📄 tables.c
字号:
if ((rt->rt_state & (RTS_INTERNAL | RTS_EXTERNAL)) == 0 && mPrivRouteEntryAdd (pDsin->sin_addr.s_addr, pGsin->sin_addr.s_addr, pNsin->sin_addr.s_addr, 0, 0, M2_ipRouteProto_rip, &rt->pKernelRt) == ERROR) { if (errno != EEXIST && gate->sa_family < af_max) { if (routedDebug) logMsg ("Error %x adding route to %s through gateway %s.\n", errno, (int)(*afswitch[dst->sa_family].af_format)(dst), (int)(*afswitch[gate->sa_family].af_format)(gate), 0, 0, 0); } if (routedDebug) logMsg ("Error %x adding new route.\n", errno, 0, 0, 0, 0, 0); if (errno == ENETUNREACH) { _remque((NODE *)rt); free((char *)rt); } } else { /* Store copy of metric in kernel routing table for IP group MIB. */ if (rt->pKernelRt) rt->pKernelRt->rt_rmx.rmx_hopcount = rt->rt_metric; ripState.ripGlobal.rip2GlobalRouteChanges++; } return (OK); }STATUS rtchange ( struct rt_entry *rt, struct sockaddr *gate, short metric, struct sockaddr *netmask ) { int add = 0, delete = 0, newgateway = 0; struct rt_entry oldroute; struct sockaddr_in *pDsin; struct sockaddr_in *pGsin; struct sockaddr_in *pNsin; char address[32]; IMPORT RIP ripState; FIXLEN(gate); FIXLEN(&(rt->rt_router)); FIXLEN(&(rt->rt_dst)); if (!equal(&rt->rt_router, gate)) { newgateway++; if (routedDebug > 1) logMsg ("Changing route gateway.\n", 0, 0, 0, 0, 0, 0); } else if (metric != rt->rt_metric) if (routedDebug > 1) logMsg ("Changing route metric.\n", 0, 0, 0, 0, 0, 0); if ((rt->rt_state & RTS_INTERNAL) == 0) { /* * If changing to different router, we need to add * new route and delete old one if in the kernel. * If the router is the same, we need to delete * the route if has become unreachable, or re-add * it if it had been unreachable. */ if (newgateway) { add++; if (rt->rt_metric != HOPCNT_INFINITY) delete++; } else if (metric == HOPCNT_INFINITY) delete++; else if (rt->rt_metric == HOPCNT_INFINITY) add++; } if (delete) oldroute = *rt; if ((rt->rt_state & RTS_INTERFACE) && delete) { rt->rt_state &= ~RTS_INTERFACE; rt->rt_flags |= RTF_GATEWAY; if (metric > rt->rt_metric && delete) if (routedDebug) logMsg ("Warning! %s route to interface %s (timed out).\n", (int)(add ? "Changing" : "Deleting"), (int)(rt->rt_ifp ? rt->rt_ifp->int_name : "?"), 0, 0, 0, 0); } if (add) { rt->rt_router = *gate; if (netmask != (struct sockaddr *)NULL) { rt->rt_netmask = *netmask; } else { /* Leave the netmask value as zero if a default route is used. */ bzero((char *)&rt->rt_netmask, sizeof(rt->rt_netmask)); rt->rt_netmask.sa_family = AF_INET; rt->rt_netmask.sa_len = 16; pNsin = (struct sockaddr_in *)&rt->rt_netmask; if ( ((struct sockaddr_in *)&rt->rt_dst)->sin_addr.s_addr) pNsin->sin_addr.s_addr = htonl(rt->rt_ifp->int_subnetmask); } rt->rt_ifp = ripIfWithDstAddr(&rt->rt_router); if (rt->rt_ifp == 0) rt->rt_ifp = ripIfWithNet(&rt->rt_router); } rt->rt_metric = metric; rt->rt_state |= RTS_CHANGED; if (newgateway) if (routedDebug > 1) logMsg ("Changing route gateway.\n", 0, 0, 0, 0, 0, 0); if (delete && !add) { ripRouteToAddrs(&oldroute, &pDsin, &pGsin, &pNsin); inet_ntoa_b(pDsin->sin_addr, (char *)&address); if (mPrivRouteEntryDelete (pDsin->sin_addr.s_addr, pGsin->sin_addr.s_addr, pNsin->sin_addr.s_addr, 0, 0, M2_ipRouteProto_rip, rt->pKernelRt) == ERROR) { if (routedDebug) logMsg ("Route change - error %x removing old route.\n", errno, 0, 0, 0, 0, 0); } else { rt->pKernelRt = NULL; ripState.ripGlobal.rip2GlobalRouteChanges++; } } else if (!delete && add) { ripRouteToAddrs(rt, &pDsin, &pGsin, &pNsin); if (mPrivRouteEntryAdd (pDsin->sin_addr.s_addr, pGsin->sin_addr.s_addr, pNsin->sin_addr.s_addr, 0, 0, M2_ipRouteProto_rip, &rt->pKernelRt) == ERROR) { if (routedDebug) logMsg ("Route change - error %x adding new route.\n", errno, 0, 0, 0, 0, 0); } else { /* Store metric in kernel routing table for IP group MIB. */ if (rt->pKernelRt) rt->pKernelRt->rt_rmx.rmx_hopcount = rt->rt_metric; ripState.ripGlobal.rip2GlobalRouteChanges++; } } else if (delete && add) { ripRouteToAddrs(rt, &pDsin, &pGsin, &pNsin); inet_ntoa_b(pDsin->sin_addr, (char *)&address); if (mPrivRouteEntryDelete (pDsin->sin_addr.s_addr, pGsin->sin_addr.s_addr, pNsin->sin_addr.s_addr, 0, 0, M2_ipRouteProto_rip, rt->pKernelRt) == ERROR) { if (routedDebug) logMsg ("Route change - error %x replacing old route.\n", errno, 0, 0, 0, 0, 0); } else { rt->pKernelRt = NULL; if (mPrivRouteEntryAdd (pDsin->sin_addr.s_addr, pGsin->sin_addr.s_addr, pNsin->sin_addr.s_addr, 0, 0, M2_ipRouteProto_rip, &rt->pKernelRt) == ERROR) { if (routedDebug) logMsg ("Route change - error %x creating new route.\n", errno, 0, 0, 0, 0, 0); } else { /* Store metric in kernel routing table for IP group MIB. */ if (rt->pKernelRt) rt->pKernelRt->rt_rmx.rmx_hopcount = rt->rt_metric; ripState.ripGlobal.rip2GlobalRouteChanges++; } } } return (OK); }STATUS rtdelete(rt) struct rt_entry *rt; { char address[32]; struct sockaddr_in *pDsin; struct sockaddr_in *pGsin; struct sockaddr_in *pNsin; IMPORT RIP ripState; if (routedDebug > 1) logMsg ("Removing route entry.\n", 0, 0, 0, 0, 0, 0); FIXLEN(&(rt->rt_router)); FIXLEN(&(rt->rt_dst)); if (rt->rt_metric < HOPCNT_INFINITY) { if ( (rt->rt_state & (RTS_INTERFACE|RTS_INTERNAL)) == RTS_INTERFACE) { if (routedDebug) logMsg ("Deleting route to interface %s? (timed out?).\n", (int)rt->rt_ifp->int_name, 0, 0, 0, 0, 0); } if ( (rt->rt_state & (RTS_INTERNAL | RTS_EXTERNAL)) == 0) { ripRouteToAddrs(rt, &pDsin, &pGsin, &pNsin); inet_ntoa_b(pDsin->sin_addr, (char *)&address); if (mPrivRouteEntryDelete (pDsin->sin_addr.s_addr, pGsin->sin_addr.s_addr, pNsin->sin_addr.s_addr, 0, 0, M2_ipRouteProto_rip, rt->pKernelRt) == ERROR) { if (routedDebug) logMsg ("Route delete - error %x removing route.\n", errno, 0, 0, 0, 0, 0); } else { rt->pKernelRt = NULL; ripState.ripGlobal.rip2GlobalRouteChanges++; } } } _remque((NODE *)rt); free((char *)rt); return (OK); }void rtdeleteall (void) { register struct rthash *rh; register struct rt_entry *rt; struct rthash *base = hosthash; int doinghost = 1; char address[32]; struct sockaddr_in *pDsin; struct sockaddr_in *pGsin; struct sockaddr_in *pNsin; IMPORT RIP ripState; again: for (rh = base; rh < &base[ROUTEHASHSIZ]; rh++) { rt = rh->rt_forw; for (; rt != (struct rt_entry *)rh; rt = ( (struct rt_entry *)rh)->rt_forw) { /* Remove the entry from the kernel routing table if present. */ if ( (rt->rt_state & (RTS_INTERNAL|RTS_EXTERNAL)) == 0) { ripRouteToAddrs (rt, &pDsin, &pGsin, &pNsin); inet_ntoa_b (pDsin->sin_addr, (char *)&address); /* * Expired route entries were already removed from the kernel * routing table, but may remain in the RIP routing table * if the garbage collection interval has not passed. * These entries are usually detectable by their infinite * metric, but all metrics are infinite in this case. * However, the pointer to the kernel route will be * NULL, so the following call will not attempt to * delete a (non-existent) expired entry. */ if (rt->pKernelRt) { if (mPrivRouteEntryDelete (pDsin->sin_addr.s_addr, pGsin->sin_addr.s_addr, pNsin->sin_addr.s_addr, 0, 0, M2_ipRouteProto_rip, rt->pKernelRt) == ERROR) { if (routedDebug) logMsg ("Error removing route from kernel table.\n", 0, 0, 0, 0, 0, 0); } else { rt->pKernelRt = NULL; ripState.ripGlobal.rip2GlobalRouteChanges++; } } } /* Remove the entry from the RIP routing table. */ ((struct rt_entry *)rh)->rt_forw = rt->rt_forw; free ( (char *)rt); } } if (doinghost) { doinghost = 0; base = nethash; goto again; } return; }/* * If we have an interface to the wide, wide world, * add an entry for an Internet default route (wildcard) to the internal * tables and advertise it. This route is not added to the kernel routes, * but this entry prevents us from listening to other people's defaults * and installing them in the kernel here. */void rtdefault() { extern struct sockaddr inet_default; rtadd(&inet_default, &inet_default, 1, RTS_CHANGED | RTS_PASSIVE | RTS_INTERNAL, NULL); }void routedTableInit() { register struct rthash *rh; for (rh = nethash; rh < &nethash[ROUTEHASHSIZ]; rh++) rh->rt_forw = rh->rt_back = (struct rt_entry *)rh; for (rh = hosthash; rh < &hosthash[ROUTEHASHSIZ]; rh++) rh->rt_forw = rh->rt_back = (struct rt_entry *)rh; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -