📄 routelib.c
字号:
if (((destIp = (long) inet_addr (pDest)) == ERROR) || ((gateIp = (long) inet_addr (pGate)) == ERROR)) { return (ERROR); } mask = htonl (mask); return (mRouteEntryAdd (destIp, gateIp, mask, tos, flags, M2_ipRouteProto_other)); } /********************************************************************************* mPrivRouteEntryAdd - add a route to the routing table ** A route to the destination <pDest> with gateway <pGate>, destination mask* <mask>, and the type of service <tos> is added to the routing table. <flags>* specifies any flags associated with this entry. Valid values for <flags> * are 0, RTF_HOST and RTF_CLONING as defined in net/route.h. The <proto> * parameter identifies the protocol that generated this route. Values for * <proto> may be found in m2Lib.h and <tos> is one of following values defined * in netinet/ip.h:* * IPTOS_LOWDELAY* IPTOS_THROUGHPUT* IPTOS_RELIABILITY* IPTOS_MINCOST** This routine is the internal version of mRouteEntryAdd() and allows* you to get a ptr to the routing entry added, which is not available* with mRouteEntryAdd().** RETURNS: OK or ERROR.** NOMANUAL*/STATUS mPrivRouteEntryAdd ( long destIp, /* destination address, network order */ long gateIp, /* gateway address, network order */ long mask, /* mask for destination, network order */ int tos, /* type of service */ int flags, /* route flags */ int proto, /* routing protocol */ struct rtentry ** ppRtEntry /* for returning ptr to new entry added */ ) { struct sockaddr_in destSock; /* destination socket */ struct sockaddr_in gateSock; /* gateway socket */ struct sockaddr_in maskSock; /* destination mask socket */ struct rtentry * pRtOld; int s; struct radix_node_head * pRnh; if ((mask != 0) && (flags & RTF_HOST)) { return (ERROR); } bzero ((caddr_t) & destSock, sizeof (destSock)); bzero ((caddr_t) & gateSock, sizeof (gateSock)); bzero ((caddr_t) & maskSock, sizeof (maskSock)); destSock.sin_addr.s_addr = (mask != 0) ? (destIp & mask) : destIp; gateSock.sin_addr.s_addr = gateIp; destSock.sin_family = gateSock.sin_family = maskSock.sin_family = AF_INET; destSock.sin_len = gateSock.sin_len = maskSock.sin_len = sizeof (destSock); /* set gateway flag if not a direct route */ if (ifa_ifwithaddr ((struct sockaddr *) & gateSock) == NULL) flags |= RTF_GATEWAY; if (mask == 0xffffffff) flags |= RTF_GATEWAY; maskSock.sin_addr.s_addr = mask; TOS_SET (&maskSock, 0x1f); in_socktrim (&maskSock); TOS_SET (&destSock, tos); RT_PROTO_SET (&destSock, proto); /* check if route exists and is owned by a proto of higher preference * if yes then we return , else we delete the route to make way * for the new one */ pRnh = rt_tables [AF_INET]; s = splnet (); pRtOld = (struct rtentry *) pRnh->rnh_lookup (& destSock, & maskSock, pRnh); if (pRtOld != NULL) { /* do not delete route owned by higher priority proto */ if (routePref [RT_PROTO_GET(rt_key(pRtOld))] > routePref [proto]) { splx (s); return (OK); } else { splx (s); if (rtrequest (RTM_DELETE, (struct sockaddr *) & destSock, (struct sockaddr *) 0, (struct sockaddr *) & maskSock, flags, NULL) != OK) { return (ERROR); } } } else { splx (s); } if (rtrequest (RTM_ADD, (struct sockaddr *) & destSock, (struct sockaddr *) & gateSock, (struct sockaddr *) & maskSock, flags, ppRtEntry) != OK) { return (ERROR); } return (OK); }/********************************************************************************* mRouteEntryAdd - add a protocol-specific route to the routing table ** For a single destination <destIp>, this routine can add additional * routes <gateIp> to the routing table. The different routes are * distinguished by a destination mask <mask>, the type of service <tos>, * and associated flag values <flags>. Valid values for <flags> are 0,* RTF_HOST, RTF_CLONING (defined in net/route.h). The <proto> parameter * identifies the protocol that generated this route. Values for <proto> may * be found in m2Lib.h. The <tos> parameter takes one of following values * (defined in netinet/ip.h): * * IPTOS_LOWDELAY* IPTOS_THROUGHPUT* IPTOS_RELIABILITY* IPTOS_MINCOST** RETURNS: OK or ERROR.** SEE ALSO: m2Lib.h, mRouteAdd(), mRouteDelete()*/STATUS mRouteEntryAdd ( long destIp, /* destination address, network order */ long gateIp, /* gateway address, network order */ long mask, /* mask for destination, network order */ int tos, /* type of service */ int flags, /* route flags */ int proto /* routing protocol */ ) { return (mPrivRouteEntryAdd (destIp, gateIp, mask, tos, flags, proto, NULL)); }/********************************************************************************* mRouteEntryDelete - delete route from the routing table ** This routine deletes a protocol-specific route from the routing * table. Specify the route using a destination <pDest>, a gateway <pGate>, * a destination mask <mask>, the type of service <tos>, a <flags> value, * and a <proto> value that identifies the routing protocol that added the * route. The valid values for <flags> are 0 and RTF_HOST (defined * in net/route.h). Values for <proto> may be found in m2Lib.h and <tos> * is one of the following values defined in netinet/ip.h:** IPTOS_LOWDELA* IPTOS_THROUGHPU* IPTOS_RELIABILIT* IPTOS_MINCOST** An existing route is deleted only if it is owned by the protocol specified * by <proto>.** RETURNS: OK or ERROR.**/STATUS mRouteEntryDelete ( long destIp, /* destination address, network order */ long gateIp, /* gateway address, network order */ long mask, /* mask for destination, network order */ int tos, /* type of service */ int flags, /* route flags */ int proto /* routing protocol */ ) { struct sockaddr_in destSock; /* destination socket */ struct sockaddr_in gateSock; /* gateway socket */ struct sockaddr_in maskSock; /* destination mask socket */ struct rtentry * pRtOld; int s; struct radix_node_head * pRnh; if ((mask != 0) && (flags & RTF_HOST)) { return (ERROR); } bzero ((caddr_t) & destSock, sizeof (destSock)); bzero ((caddr_t) & gateSock, sizeof (gateSock)); bzero ((caddr_t) & maskSock, sizeof (maskSock)); destSock.sin_addr.s_addr = (mask != 0) ? (destIp & mask) : destIp; gateSock.sin_addr.s_addr = gateIp; destSock.sin_family = gateSock.sin_family = maskSock.sin_family = AF_INET; destSock.sin_len = gateSock.sin_len = maskSock.sin_len = sizeof (destSock); /* set gateway flag if not a direct route */ if (ifa_ifwithaddr ((struct sockaddr *) & gateSock) == NULL) flags |= RTF_GATEWAY; maskSock.sin_addr.s_addr = mask; TOS_SET (&maskSock, 0x1f); in_socktrim (&maskSock); if (mask == 0xffffffff) flags |= RTF_GATEWAY; TOS_SET (&destSock, tos); pRnh = rt_tables [AF_INET]; s = splnet (); pRtOld = (struct rtentry *) pRnh->rnh_lookup (& destSock, & maskSock, pRnh); /* return if route is owned by other protocol */ if ((pRtOld == NULL) || (RT_PROTO_GET (rt_key (pRtOld)) != proto)) { splx (s); return (OK); } else { splx (s); return ((rtrequest (RTM_DELETE, (struct sockaddr *) & destSock, (struct sockaddr *) & gateSock, (struct sockaddr *) & maskSock, flags, 0) == OK) ? OK : ERROR); } return (ERROR); }/********************************************************************************* mPrivRouteEntryDelete - delete a held route from the routing table ** This routine deletes a protocol-specific route from the routing * table when mPrivRouteEntryAdd retained a copy of the new route* with the <pRoute> pointer. The routine calls rtfree to adjust the* reference count then passes the remaining arguments to the * mRouteEntryDelete routine to remove the route. ** RETURNS: OK or ERROR.** NOMANUAL*/STATUS mPrivRouteEntryDelete ( long destIp, /* destination address, network order */ long gateIp, /* gateway address, network order */ long mask, /* mask for destination, network order */ int tos, /* type of service */ int flags, /* route flags */ int proto, /* routing protocol */ struct rtentry * pRoute ) { int s; STATUS result; s = splnet (); if (pRoute) rtfree (pRoute); result = mRouteEntryDelete (destIp, gateIp, mask, tos, flags, proto); splx (s); return (result); }/********************************************************************************* mRouteDelete - delete a route from the routing table ** This routine deletes a routing table entry as specified by the* destination, <pDest>, the destination mask, <mask>, and type of service,* <tos>. The <tos> values are as defined in the reference entry for* mRouteAdd().** EXAMPLE* Consider the case of a route added in the following manner:* .CS* -> mRouteAdd ("90.0.0.0", "91.0.0.3", 0xffffff00, 0, 0);* .CE* To delete a route that was added in the above manner, call mRouteDelete()* as follows:* .CS* -> mRouteDelete("90.0.0.0", 0xffffff00, 0);* .CE** If the netmask and or type of service do not match, the route* is not deleted.** The value of <flags> should be RTF_HOST for host routes, RTF_CLONING for* routes which need to be cloned, and 0 in all other cases.** RETURNS: OK or ERROR. ** SEE ALSO: mRouteAdd()*/STATUS mRouteDelete ( char * pDest, /* destination address */ long mask, /* mask for destination */ int tos, /* type of service */ int flags /* either 0 or RTF_HOST */ ) { struct sockaddr_in destSock; /* destination socket */ struct sockaddr_in maskSock; /* destination mask socket */ if ((mask != 0) && (flags & RTF_HOST)) { return (ERROR); } bzero ((caddr_t) & destSock, sizeof (destSock)); bzero ((caddr_t) & maskSock, sizeof (maskSock)); if ((destSock.sin_addr.s_addr = (long) inet_addr (pDest)) == ERROR) { return (ERROR); } mask = htonl (mask); if (mask != 0) destSock.sin_addr.s_addr &= mask; destSock.sin_family = maskSock.sin_family = AF_INET; destSock.sin_len = maskSock.sin_len = sizeof (destSock); maskSock.sin_addr.s_addr = mask; in_socktrim (&maskSock); TOS_SET (&destSock, tos); return ((rtrequest (RTM_DELETE, (struct sockaddr *) & destSock, 0, (struct sockaddr *) & maskSock, flags, NULL) != OK) ? ERROR : OK); }/********************************************************************************* routeProtoPrioritySet - set the priority of routes added by the routing protocol ** This routine assigns a priority to a routing protocol. A route generated * by the <proto> protocol is added to the routing table only if a protocol * of higher priority does not already have that route installed in the table. * Use <proto> to identify the protocol. See m2Lib.h for a listing of valid* values for <proto>. Use <prio> to specify the priority level you want to * assign to <proto>. The <prio> parameter may be any integer value greater * or equal to 0 and less than or equal to 200. The higher values indicate * higher priority. If you do not want VxWorks to prioritize protocols, do * not call this routine. ** Routes that are added with the routeAdd() or mRouteAdd() call are of* type `M2_ipRouteProto_other'. These are static routes that are not* affected by routing protocols such as RIP and OSPF. To change the* priority of routes added in this way pass the value* `M2_ipRoute_Proto_other' in the first argument of this routine.** RETURNS: OK if priority set successfully else ERROR.**/STATUS routeProtoPrioritySet ( int proto, /* protocol no, from m2Lib.h */ int prio /* priority, >= 0 , <= 200 */ ) { int s; /* for splx */ if ((proto < M2_ipRouteProto_other) || (proto > M2_ipRouteProto_bgp) || (prio < 0) || (prio > 200)) { return (ERROR); } s = splnet (); routePref [proto] = prio; splx (s); return (OK); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -