Changeset 17691 in vbox
- Timestamp:
- Mar 11, 2009 12:56:27 PM (16 years ago)
- svn:sync-xref-src-repo-rev:
- 44193
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/NetworkServices/DHCP/VBoxNetDHCP.cpp
r17678 r17691 52 52 /** The requested address. */ 53 53 #define RTNET_DHCP_OPT_REQUESTED_ADDRESS 50 54 55 /** The normal size of RTNETBOOTP::bp_vend::Dhcp::dhcp_opts. */ 56 #define RTNET_DHCP_OPT_SIZE (312 - 4) 54 57 55 58 … … 245 248 void updateLeaseConfig(VBoxNetDhcpLease *pLease); 246 249 247 static uint8_t 250 static uint8_t const *findOption(uint8_t uOption, PCRTNETBOOTP pDhcpMsg, size_t cb, size_t *pcbMaxOpt); 248 251 static bool findOptionIPv4Addr(uint8_t uOption, PCRTNETBOOTP pDhcpMsg, size_t cb, PRTNETADDRIPV4 pIPv4Addr); 249 252 … … 292 295 /** Pointer to the DHCP server. */ 293 296 static VBoxNetDhcp *g_pDhcp; 297 298 299 /** 300 * Offer this lease to a client. 301 * 302 * @param xid The transaction ID. 303 */ 304 void VBoxNetDhcpLease::offer(uint32_t xid) 305 { 306 m_enmState = kState_Offer; 307 RTTimeNow(&m_ExpireTime); 308 RTTimeSpecAddSeconds(&m_ExpireTime, 60); 309 } 310 311 312 /** 313 * Activate this lease (i.e. a client is now using it). 314 */ 315 void VBoxNetDhcpLease::activate(void) 316 { 317 m_enmState = kState_Active; 318 RTTimeNow(&m_ExpireTime); 319 RTTimeSpecAddSeconds(&m_ExpireTime, m_pCfg ? m_pCfg->m_cSecLease : 60); /* m_pCfg can be NULL right now... */ 320 } 321 322 323 /** 324 * Release a lease either upon client request or because it didn't quite match a 325 * DHCP_REQUEST. 326 */ 327 void VBoxNetDhcpLease::release(void) 328 { 329 m_enmState = kState_Free; 330 RTTimeNow(&m_ExpireTime); 331 RTTimeSpecAddSeconds(&m_ExpireTime, 5); 332 } 294 333 295 334 … … 912 951 913 952 953 /** 954 * Look up a lease by MAC address. 955 * 956 * @returns Pointer to the lease if found, NULL if not found. 957 * @param pMacAddress The mac address. 958 * @param fEnsureUpToDateConfig Whether to update the configuration. 959 */ 914 960 VBoxNetDhcpLease *VBoxNetDhcp::findLeaseByMacAddress(PCRTMAC pMacAddress, bool fEnsureUpToDateConfig) 915 961 { 962 size_t iLease = m_Leases.size(); 963 while (iLease-- > 0) 964 { 965 VBoxNetDhcpLease *pLease = &m_Leases[iLease]; 966 if ( pLease 967 && pLease->m_MacAddress.au16[0] == pMacAddress->au16[0] 968 && pLease->m_MacAddress.au16[1] == pMacAddress->au16[1] 969 && pLease->m_MacAddress.au16[2] == pMacAddress->au16[2]) 970 { 971 if (fEnsureUpToDateConfig) 972 updateLeaseConfig(pLease); 973 return pLease; 974 } 975 } 976 916 977 return NULL; 917 978 } 918 979 919 980 981 /** 982 * Look up a lease by IPv4 and MAC addresses. 983 * 984 * @returns Pointer to the lease if found, NULL if not found. 985 * @param IPv4Addr The IPv4 address. 986 * @param pMacAddress The mac address. 987 */ 920 988 VBoxNetDhcpLease *VBoxNetDhcp::findLeaseByIpv4AndMacAddresses(RTNETADDRIPV4 IPv4Addr, PCRTMAC pMacAddress) 921 989 { 990 size_t iLease = m_Leases.size(); 991 while (iLease-- > 0) 992 { 993 VBoxNetDhcpLease *pLease = &m_Leases[iLease]; 994 if ( pLease 995 && pLease->m_IPv4Address.u == IPv4Addr.u 996 && pLease->m_MacAddress.au16[0] == pMacAddress->au16[0] 997 && pLease->m_MacAddress.au16[1] == pMacAddress->au16[1] 998 && pLease->m_MacAddress.au16[2] == pMacAddress->au16[2]) 999 return pLease; 1000 } 1001 922 1002 return NULL; 923 924 } 925 1003 } 1004 1005 1006 /** 1007 * Creates a new lease for the client specified in the DHCP message. 1008 * 1009 * The caller has already made sure it doesn't already have a lease. 1010 * 1011 * @returns Pointer to the lease if found, NULL+log if not found. 1012 * @param IPv4Addr The IPv4 address. 1013 * @param pMacAddress The MAC address. 1014 */ 926 1015 VBoxNetDhcpLease *VBoxNetDhcp::newLease(PCRTNETBOOTP pDhcpMsg, size_t cb) 927 1016 { … … 934 1023 * Finds an option. 935 1024 * 936 * @returns On success, a pointer to the option number within the DHCP message937 * and *pcbMaxOpt set to the maximum number of bytes the option may938 * contain.939 * If not found NULL is returned and *pcbMaxOpt is notchanged.1025 * @returns On success, a pointer to the first byte in the option data (no none 1026 * then it'll be the byte following the 0 size field) and *pcbOpt set 1027 * to the option length. 1028 * On failure, NULL is returned and *pcbOpt unchanged. 940 1029 * 941 1030 * @param uOption The option to search for. 942 1031 * @param pDhcpMsg The DHCP message. 943 1032 * @param cb The size of the message. 944 * @param pcbMaxOpt Where to store the max option size. Optional. 945 */ 946 /* static */ uint8_t * 947 VBoxNetDhcp::findOption(uint8_t uOption, PCRTNETBOOTP pDhcpMsg, size_t cb, size_t *pcbMaxOpt) 948 { 1033 * @param pcbOpt Where to store the option size size. Optional. Note 1034 * that this is adjusted if the option length is larger 1035 * than the message buffer. 1036 */ 1037 /* static */ const uint8_t * 1038 VBoxNetDhcp::findOption(uint8_t uOption, PCRTNETBOOTP pDhcpMsg, size_t cb, size_t *pcbOpt) 1039 { 1040 Assert(uOption != RTNET_DHCP_OPT_PAD); 1041 1042 /* 1043 * Validate the DHCP bits and figure the max size of the options in the vendor field. 1044 */ 1045 if (cb <= RT_UOFFSETOF(RTNETBOOTP, bp_vend.Dhcp.dhcp_opts)) 1046 return NULL; 1047 if (pDhcpMsg->bp_vend.Dhcp.dhcp_cookie != RTNET_DHCP_COOKIE) 1048 return NULL; 1049 size_t cbLeft = cb - RT_UOFFSETOF(RTNETBOOTP, bp_vend.Dhcp.dhcp_opts); 1050 if (cbLeft > RTNET_DHCP_OPT_SIZE) 1051 cbLeft = RTNET_DHCP_OPT_SIZE; 1052 1053 /* 1054 * Search the vendor field. 1055 */ 1056 bool fExtended = false; 1057 uint8_t const *pb = &pDhcpMsg->bp_vend.Dhcp.dhcp_opts[0]; 1058 while (pb && cbLeft > 0) 1059 { 1060 uint8_t uCur = *pb; 1061 if (uCur == RTNET_DHCP_OPT_PAD) 1062 { 1063 cbLeft--; 1064 pb++; 1065 } 1066 else if (cbLeft <= 1) 1067 break; 1068 else 1069 { 1070 size_t cbCur = pb[1]; 1071 if (cbCur > cbLeft - 2) 1072 cbCur = cbLeft - 2; 1073 if (uCur == uOption) 1074 { 1075 if (pcbOpt) 1076 *pcbOpt = cbCur; 1077 return pb+2; 1078 } 1079 pb += cbCur + 2; 1080 cbLeft -= cbCur - 2; 1081 } 1082 } 1083 1084 /** @todo search extended dhcp option field(s) when present */ 1085 949 1086 return NULL; 950 1087 } … … 964 1101 VBoxNetDhcp::findOptionIPv4Addr(uint8_t uOption, PCRTNETBOOTP pDhcpMsg, size_t cb, PRTNETADDRIPV4 pIPv4Addr) 965 1102 { 966 size_t cbMaxOpt;967 uint8_t *pbOpt = findOption(uOption, pDhcpMsg, cb, &cbMaxOpt);1103 size_t cbOpt; 1104 uint8_t const *pbOpt = findOption(uOption, pDhcpMsg, cb, &cbOpt); 968 1105 if (pbOpt) 969 1106 { 970 1107 if (cbOpt >= sizeof(RTNETADDRIPV4)) 1108 { 1109 *pIPv4Addr = *(PCRTNETADDRIPV4)pbOpt; 1110 return true; 1111 } 971 1112 } 972 1113 return false;
Note:
See TracChangeset
for help on using the changeset viewer.