📄 m2iplib.c
字号:
#ifdef ROUTER_STACK /* Inform Fastpath about the change */ FFL_CALL (ffLibIpConfigFlagsChanged, (_ipCfgFlags));#endif /* ROUTER_STACK */ } /* * Set the new time. The calling routine is expected to have verified that * the new time is a valid value. */ if (varToSet & M2_IPDEFAULTTTL) ipTimeToLive = pIpInfo->ipDefaultTTL; return (OK); } /******************************************************************************** m2IpAddrTblEntryGet - get an IP MIB-II address entry** This routine traverses the IP address table and does an M2_EXACT_VALUE or* a M2_NEXT_VALUE search based on the <search> parameter. The calling * routine is responsible for supplying a valid MIB-II entry index in the* input structure <pIpAddrTblEntry>. The index is the local IP* address. The first entry in the table is retrieved by doing a NEXT search* with the index field set to zero. ** RETURNS: * OK, ERROR if the input parameter is not specified, or a match is not found.** ERRNO:* S_m2Lib_INVALID_PARAMETER* S_m2Lib_ENTRY_NOT_FOUND** SEE ALSO:* m2Lib, m2IpInit(), m2IpGroupInfoGet(), m2IpGroupInfoSet(), * m2IpAtransTblEntrySet(), m2IpRouteTblEntryGet(), m2IpRouteTblEntrySet(),* m2IpDelete()*/STATUS m2IpAddrTblEntryGet ( int search, /* M2_EXACT_VALUE or M2_NEXT_VALUE */ M2_IPADDRTBL * pIpAddrTblEntry /* ptr to requested IP address entry */ ) { struct in_ifaddr * pIfAddr; /* Pointer to IP list of internet addr */ struct in_ifaddr * pIfAddrSaved; /* Pointer to IP entry */ unsigned long ipAddrSaved; /* IP address selected */ unsigned long ipAddr; /* Caller IP address Table index req */ unsigned long currIpAddr; /* Current IP address Table index req */ int netLock; /* Used to secure the Network Code Access */ /* Validate the input pointer */ if (pIpAddrTblEntry == NULL) { errnoSet (S_m2Lib_INVALID_PARAMETER); return (ERROR); } /* Setup variables to be used in the table search */ pIfAddrSaved = NULL; /* Nothing found yeat */ ipAddrSaved = -1; /* Largest IP address */ /* Requested IP address */ ipAddr = pIpAddrTblEntry->ipAdEntAddr; netLock = splnet (); /* Get exclusive access to Network Code */ /* Search the IP list of internet addresses to satisfy the request */ #ifdef VIRTUAL_STACK for (pIfAddr = _in_ifaddr; pIfAddr != NULL; pIfAddr = pIfAddr->ia_next)#else for (pIfAddr = in_ifaddr; pIfAddr != NULL; pIfAddr = pIfAddr->ia_next)#endif /* VIRTUAL_STACK */ { currIpAddr = ntohl((IA_SIN(pIfAddr)->sin_addr.s_addr)); if (search == M2_EXACT_VALUE) { if (ipAddr == currIpAddr) { /* * Match found. Save a pointer to the entry and the ip address */ pIfAddrSaved = pIfAddr; ipAddrSaved = currIpAddr; break; /* Found EXACT Match */ } } else { /* * A NEXT search is satisfied by an IP address lexicographicaly * greater than the input IP address. Because IP addresses are not * in order in the IP list, the list must be traverse complety * before a selection is made. */ if (currIpAddr >= ipAddr && currIpAddr < ipAddrSaved) { /* Save possible IP address selection */ pIfAddrSaved = pIfAddr; ipAddrSaved = currIpAddr; } } } /* Entry not found */ if (pIfAddrSaved == NULL) { splx (netLock); /* Give up exclusive access to Network Code */ errnoSet (S_m2Lib_ENTRY_NOT_FOUND); return (ERROR); } /* Fill the request structure with the found entry */ pIpAddrTblEntry->ipAdEntIfIndex = pIfAddrSaved->ia_ifp->if_index; pIpAddrTblEntry->ipAdEntNetMask = pIfAddrSaved->ia_subnetmask; if (pIfAddrSaved->ia_ifp->if_flags & IFF_BROADCAST) pIpAddrTblEntry->ipAdEntBcastAddr = (ntohl(pIfAddrSaved->ia_netbroadcast.s_addr)) & 1; else pIpAddrTblEntry->ipAdEntBcastAddr = 1; splx (netLock); /* Give up exclusive access to Network Code */ pIpAddrTblEntry->ipAdEntAddr = ipAddrSaved; pIpAddrTblEntry->ipAdEntReasmMaxSize = IP_MAXPACKET; /* Fromp ip.h */ return (OK); }/******************************************************************************** m2IpAtransTblEntryGet - get a MIB-II ARP table entry** This routine traverses the ARP table and does an M2_EXACT_VALUE or a* M2_NEXT_VALUE search based on the <search> parameter. The calling * routine is responsible for supplying a valid MIB-II entry index in the * input structure <pReqIpatEntry>. The index is made up of the network* interface index and the IP address corresponding to the physical address.* The first entry in the table is retrieved by doing a NEXT search with the* index fields set to zero. ** RETURNS: * OK, ERROR if the input parameter is not specified, or a match is not found.** ERRNO:* S_m2Lib_INVALID_PARAMETER* S_m2Lib_ENTRY_NOT_FOUND** SEE ALSO:* m2Lib, m2IpInit(), m2IpGroupInfoGet(), m2IpGroupInfoSet(), * m2IpAtransTblEntrySet(), m2IpRouteTblEntryGet(), m2IpRouteTblEntrySet(),* m2IpDelete()*/STATUS m2IpAtransTblEntryGet ( int search, /* M2_EXACT_VALUE or M2_NEXT_VALUE */ M2_IPATRANSTBL * pReqIpAtEntry /* ptr to the requested ARP entry */ ) { unsigned long ipAddrSaved; /* Used for a NEXT search */ unsigned long currIpAddr; /* Current IP address Table index req */ int ix; /* All purpose loop Index */ int netLock; /* Use to secure the Network Code Access */ int maxIndex; /* max. possible index value */ int currIndex; /* current interface index required */ struct llinfo_arp * pLnkInfo; struct rtentry * pRtEnt; struct rtentry * pRtEntMatch; /* Validate Pointer to ARP request structure */ if (pReqIpAtEntry == NULL) { errnoSet (S_m2Lib_INVALID_PARAMETER); return (ERROR); } /* Initialize all local variable before the Network Semaphore is taken */ ix = 0; pRtEntMatch = NULL; /* initialized to NULL */ maxIndex = ipMaxUnits + 1; /* largest possible value */ ipAddrSaved = -1; /* Largest IP address */ /* Convert IP address to network byte order */ netLock = splnet (); /* Get exclusive access to Network Code */ /* * Traverse the ARP Table. The whole table is searched */#ifdef VIRTUAL_STACK pLnkInfo = _llinfo_arp.la_next; while (pLnkInfo != &_llinfo_arp) #else pLnkInfo = llinfo_arp.la_next; while (pLnkInfo != &llinfo_arp) #endif /* VIRTUAL_STACK */ { pRtEnt = pLnkInfo->la_rt; pLnkInfo = pLnkInfo->la_next; currIpAddr = ntohl(((struct sockaddr_in *) rt_key(pRtEnt))->sin_addr.s_addr); currIndex = pRtEnt->rt_ifp->if_index; if (search == M2_NEXT_VALUE) { /* * A NEXT search is satisfied by an IP address lexicographicaly * equal to or greater than the input IP address. Because IP * addresses are not in order in the ARP table, the list must * be traverse completely before a selection is made. */ if (((currIndex > pReqIpAtEntry->ipNetToMediaIfIndex) || ((currIndex == pReqIpAtEntry->ipNetToMediaIfIndex) && (currIpAddr >= pReqIpAtEntry->ipNetToMediaNetAddress))) && ((currIndex < maxIndex) || ((currIndex == maxIndex) && (currIpAddr < ipAddrSaved)))) { pRtEntMatch = pRtEnt; /* Found a Candidate */ ipAddrSaved = currIpAddr; } } else { /* Search for an EXACT match in the ARP Table */ if (pReqIpAtEntry->ipNetToMediaNetAddress == currIpAddr) { pRtEntMatch = pRtEnt; /* Found Requested Entry */ ipAddrSaved = currIpAddr; break; } } } /* pRtEntMatch is pointer to the route for the IP address found * The interface pointer is obtained from pRtEntMatch. This pointer is * used as a key to search the MIB-II interface table, * once the key is matched the interface number is found. */ if (pRtEntMatch != NULL) { /* Search for Interface number in the MIB-II interface table */ pReqIpAtEntry->ipNetToMediaIfIndex = pRtEntMatch->rt_ifp->if_index; /* Fill requested ARP entry */ pReqIpAtEntry->ipNetToMediaNetAddress = ipAddrSaved; pReqIpAtEntry->ipNetToMediaType = (pRtEntMatch->rt_rmx.rmx_expire == 0) ? M2_ipNetToMediaType_static : M2_ipNetToMediaType_dynamic; bcopy (LLADDR((struct sockaddr_dl *)pRtEntMatch->rt_gateway), (char *) pReqIpAtEntry->ipNetToMediaPhysAddress.phyAddress, ETHERADDRLEN); splx (netLock); /* Give up exclusive access to Network Code */ pReqIpAtEntry->ipNetToMediaPhysAddress.addrLength = ETHERADDRLEN; return (OK); } splx (netLock); /* Give up exclusive access to Network Code */ errnoSet (S_m2Lib_ENTRY_NOT_FOUND); return (ERROR); }/******************************************************************************** m2IpAtransTblEntrySet - add, modify, or delete a MIB-II ARP entry** This routine traverses the ARP table for the entry specified in the parameter* <pReqIpAtEntry>. An ARP entry can be added, modified, or deleted. A MIB-II* entry index is specified by the destination IP address and the physical media* address. A new ARP entry can be added by specifying all the fields in the* parameter <pReqIpAtEntry>. An entry can be modified by specifying the MIB-II* index and the field that is to be modified. An entry is deleted by * specifying the index and setting the type field in the input parameter * <pReqIpAtEntry> to the MIB-II value "invalid" (2).** RETURNS: * OK, or ERROR if the input parameter is not specified, the physical address* is not specified for an add/modify request, or the ioctl() request to the ARP* module fails.** ERRNO:* S_m2Lib_INVALID_PARAMETER* S_m2Lib_ARP_PHYSADDR_NOT_SPECIFIED** SEE ALSO:* m2IpInit(), m2IpGroupInfoGet(), m2IpGroupInfoSet(), * m2IpAddrTblEntryGet(), m2IpRouteTblEntryGet(), m2IpRouteTblEntrySet(),* m2IpDelete()*/STATUS m2IpAtransTblEntrySet ( M2_IPATRANSTBL * pReqIpAtEntry /* pointer to MIB-II ARP entry */ ) { int ioctlCmd; /* ARP module IOCTL command value */ struct arpreq arpCmd; /* ARP module IOCTL command structure */ struct sockaddr_in * pIpAddr; /* Pointer to an IP address */ struct sockaddr * pPhyAddr; /* Pointer to an ethernet address */ /* Validate Pointer to ARP request structure */ if (pReqIpAtEntry == NULL) { errnoSet (S_m2Lib_INVALID_PARAMETER); return (ERROR); } /* Initialize ARP module IOCTL variables */ bzero ((char *)&arpCmd, sizeof(struct arpreq)); pIpAddr = (struct sockaddr_in *) &arpCmd.arp_pa; /* Protocol address */ pIpAddr->sin_family = AF_INET; pIpAddr->sin_len = sizeof(struct sockaddr_in); pIpAddr->sin_addr.s_addr = htonl(pReqIpAtEntry->ipNetToMediaNetAddress); /* Copy the Ethernet address in the IOCTL structure */ if (pReqIpAtEntry->ipNetToMediaPhysAddress.addrLength > 0) { pPhyAddr = &arpCmd.arp_ha; pPhyAddr->sa_family = AF_UNSPEC; bcopy ((char *) pReqIpAtEntry->ipNetToMediaPhysAddress.phyAddress, pPhyAddr->sa_data, pReqIpAtEntry->ipNetToMediaPhysAddress.addrLength ); } arpCmd.arp_flags = 0; /* Check if the ARP entry is to be deleted, Added or Modified */ if (pReqIpAtEntry->ipNetToMediaType == M2_ipNetToMediaType_invalid) { /* * Request to DELETE the specified ARP entry. The hardware address * is optional. However, if the IP address is not found in the ARP * table the request to delete the ARP entry can fail. */ ioctlCmd = SIOCDARP; } else { /* * Request to ADD or MODIFY the specified ARP entry. The hardware * address must be specified. If this is not the case then fail. */ if (pReqIpAtEntry->ipNetToMediaPhysAddress.addrLength <= 0) { errnoSet (S_m2Lib_ARP_PHYSADDR_NOT_SPECIFIED); return (ERROR); } ioctlCmd = SIOCSARP; if (pReqIpAtEntry->ipNetToMediaType == M2_ipNetToMediaType_dynamic) arpCmd.arp_flags &= ~ATF_PERM; if (pReqIpAtEntry->ipNetToMediaType == M2_ipNetToMediaType_static) arpCmd.arp_flags |= ATF_PERM; } /* Issue IOCTL command to the ARP module */ if (arpioctl (ioctlCmd, (caddr_t)&arpCmd) != 0) return (ERROR); return (OK); }/******************************************************************************** m2IpRouteTblEntryGet - get a MIB-2 routing table entry ** This routine retrieves MIB-II information about an entry in * the network routing table and returns it in the caller-supplied structure * <pIpRouteTblEntry>. ** The routine compares routing table entries to the address specified by the* `ipRouteDest' member of the <pIpRouteTblEntry> structure, and retrieves an* entry chosen by the <search> type (M2_EXACT_VALUE or M2_NEXT_VALUE, as* described in the manual entry for m2Lib).* * RETURNS: OK if successful, otherwise ERROR.** ERRNO:* S_m2Lib_INVALID_PARAMETER * S_m2Lib_ENTRY_NOT_FOUND* * SEE ALSO: m2Lib, m2IpInit(), m2IpGroupInfoGet(), m2IpGroupInfoSet(), * m2IpAddrTblEntryGet(), m2IpRouteTblEntryGet(), m2IpRouteTblEntrySet(),* m2IpDelete()*/STATUS m2IpRouteTblEntryGet ( int search, /* M2_EXACT_VALUE or M2_NEXT_VALUE */ M2_IPROUTETBL * pIpRouteTblEntry /* route table entry */ ) { int ix; int index; unsigned long nextLarger; unsigned long tableDest; unsigned long dstIpAddr; /* Validate the arguments */ if ((pIpRouteTblEntry == NULL) || ((search != M2_EXACT_VALUE) && (search != M2_NEXT_VALUE))) { errnoSet(S_m2Lib_INVALID_PARAMETER); return (ERROR); /* invalid argument */ }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -