📄 dhcp_client.c
字号:
/*pDhcp->options[0] = 99;
pDhcp->options[1] = 130;
pDhcp->options[2] = 83;
pDhcp->options[3] = 99;*/
offset = MAGIC_LEN;
DEBUG_UT_DHCP("magic\n",pDhcp,offset);
/* insert message type */
pDhcp->options[offset++] = _DHCP_MSGTYPE_TAG;
pDhcp->options[offset++] = 1;
pDhcp->options[offset++] = DHCPDISCOVER;
DEBUG_UT_DHCP("_DHCP_MSGTYPE_TAG\n",pDhcp,offset);
/* insert client identifier */
/*
Code Len Type Client-Identifier
+-----+-----+-----+-----+-----+---
| 61 | n | t1 | i1 | i2 | ...
+-----+-----+-----+-----+-----+---
*/
optLen = pBinding->cid.idlen;
if (gDhcpClientGlobalConfig.cidFlag && optLen > 0) {
if (dhcp_client_have_option_room(offset,optLen)) {
pDhcp->options[offset++] = _DHCP_CLIENT_ID_TAG;
pDhcp->options[offset++] = optLen+1;
pDhcp->options[offset++] = pBinding->cid.idtype;
bcopy(pBinding->cid.id,&pDhcp->options[offset], optLen);
offset += optLen;
} else {
return ERROR;
}
} else if(gDhcpClientGlobalConfig.haddrFlag && pBinding->haddr.hlen) {
if (dhcp_client_have_option_room(offset,pBinding->haddr.hlen)) {
pDhcp->options[offset++] = _DHCP_CLIENT_ID_TAG;
pDhcp->options[offset++] = pBinding->haddr.hlen+1;
pDhcp->options[offset++] = pBinding->haddr.htype;
bcopy(pBinding->haddr.haddr,&pDhcp->options[offset], pBinding->haddr.hlen);
offset += pBinding->haddr.hlen;
} else {
return ERROR;
}
} else {
/* don't insert _DHCP_CLIENT_ID_TAG*/
}
DEBUG_UT_DHCP("_DHCP_CLIENT_ID_TAG\n",pDhcp,offset);
/* insert vendor class identifier */
if (gDhcpClientGlobalConfig.vendor_id.len != 0) {
if (dhcp_client_have_option_room(offset,gDhcpClientGlobalConfig.vendor_id.len)) {
pDhcp->options[offset++] = _DHCP_VENDOR_CLASS_ID_TAG;
pDhcp->options[offset++] = gDhcpClientGlobalConfig.vendor_id.len;
bcopy(gDhcpClientGlobalConfig.vendor_id.id,&pDhcp->options[offset],
gDhcpClientGlobalConfig.vendor_id.len);
offset += gDhcpClientGlobalConfig.vendor_id.len;
} else {
return ERROR;
}
}
DEBUG_UT_DHCP("_DHCP_VENDOR_CLASS_ID_TAG\n",pDhcp,offset);
/* UTStarcom-specific operator-domain-name option */
if (gDhcpClientGlobalConfig.domain.name != NULL)
{
optLen = strlen(gDhcpClientGlobalConfig.domain.name);
if (dhcp_client_have_option_room(offset, 2 + optLen))
{
pDhcp->options[offset++] = _DHCP_VENDOR_SPEC_TAG;
pDhcp->options[offset++] = 2 + optLen;
pDhcp->options[offset++] = UTS_OPERATOR_DOMAIN;
pDhcp->options[offset++] = optLen;
bcopy(gDhcpClientGlobalConfig.domain.name,&pDhcp->options[offset], optLen);
offset += optLen;
} else {
return ERROR;
}
}
DEBUG_UT_DHCP("_DHCP_VENDOR_SPEC_TAG\n",pDhcp,offset);
/* user class id (refer to RFC 3004)
*
* Code Len Value
* +-----+-----+--------------------- . . . --+
* | 77 | N | User Class Data ('Len' octets) |
* +-----+-----+--------------------- . . . --+
* / \
* UC_Len_i User_Class_Data_i
* +--------+------------------------ . . . --+
* | L_i | Opaque-Data ('UC_Len_i' octets) |
* +--------+------------------------ . . . --+
*
*/
if(gDhcpClientGlobalConfig.user_id.len != 0)
{
optLen = gDhcpClientGlobalConfig.user_id.len;
if (dhcp_client_have_option_room(offset, optLen + 1))
{
pDhcp->options[offset++] = _DHCP_USER_CLASS_TAG;
pDhcp->options[offset++] = optLen + 1;
pDhcp->options[offset++] = optLen;
bcopy((char *)gDhcpClientGlobalConfig.user_id.id,&pDhcp->options[offset], optLen);
offset += optLen;
} else {
return ERROR;
}
}
DEBUG_UT_DHCP("_DHCP_USER_CLASS_TAG\n",pDhcp,offset);
/* subnet selection option */
if (gDhcpClientGlobalConfig.subnet != 0)
{
optLen = 4; /* IP address length */
if (dhcp_client_have_option_room(offset, optLen))
{
pDhcp->options[offset++] = _DHCP_SUBNET_SELECTION_TAG;
pDhcp->options[offset++] = optLen;
bcopy((char *)&gDhcpClientGlobalConfig.subnet,&pDhcp->options[offset], optLen);
offset += optLen;
} else {
return ERROR;
}
}
DEBUG_UT_DHCP("_DHCP_SUBNET_SELECTION_TAG\n",pDhcp,offset);
/* insert requesting lease */
if (gDhcpClientGlobalConfig.reqLease != 0 )
{
if(dhcp_client_have_option_room(offset,4)) {
pDhcp->options[offset++] = _DHCP_LEASE_TIME_TAG;
pDhcp->options[offset++] = 4;
ulTemp = htonl(gDhcpClientGlobalConfig.reqLease);
bcopy((char *)&ulTemp,&pDhcp->options[offset], 4);
offset += 4;
} else {
return ERROR;
}
}
DEBUG_UT_DHCP("_DHCP_LEASE_TIME_TAG\n",pDhcp,offset);
/* insert Maximum DHCP message size */
if (dhcp_client_have_option_room(offset,2))
{
pDhcp->options[offset++] = _DHCP_MAXMSGSIZE_TAG;
pDhcp->options[offset++] = 2;
usTemp = htons(DFLTDHCPLEN);
bcopy((char *)&usTemp,&pDhcp->options[offset], 2);
offset += 2;
} else{
return OK;
}
DEBUG_UT_DHCP("_DHCP_MAXMSGSIZE_TAG\n",pDhcp,offset);
/* if necessary, insert request list */
optLen = gDhcpClientGlobalConfig.reqListLen;
if (optLen!= 0)
{
if (dhcp_client_have_option_room(offset,optLen))
{
pDhcp->options[offset++] = _DHCP_REQ_LIST_TAG;
pDhcp->options[offset++] = optLen;
bcopy((char *)gDhcpClientGlobalConfig.reqList,&pDhcp->options[offset],optLen);
offset += optLen;
} else {
return ERROR;
}
}
DEBUG_UT_DHCP("_DHCP_REQ_LIST_TAG\n",pDhcp,offset);
if(dhcp_client_have_option_room(offset,1)) {
pDhcp->options[offset] = _DHCP_END_TAG;
offset ++;
}
DEBUG_UT_DHCP("end\n",pDhcp,offset);
pBinding->msgLen = DFLTDHCPLEN - DFLTOPTLEN + offset;
TRACE0("msgLen %d, offset %d\n",pBinding->msgLen,offset);
pBinding->state = SELECTING;
return OK;
}
int make_request(St_DhcpBinding *pBinding)
{
int offset = 0; /* offset in options field */
int optLen;
unsigned long ulTemp;
unsigned short usTemp;
struct dhcp *pDhcp = pDhcpcOut;
DHCP_ASSERT(DHCP_BINDING_GOOD(pBinding));
bzero((char *)pDhcp,sizeof(struct dhcp));
DEBUG_UT_DHCP("begin\n",pDhcp,offset);
pDhcp->op = BOOTREQUEST;
pDhcp->htype = pBinding->haddr.htype;
pDhcp->hlen = pBinding->haddr.hlen;
pDhcp->xid = *((unsigned int*)&pBinding->cid.id[2]);
pDhcp->hops = 0;
pDhcp->secs = 0;
SETBRDCST(pDhcp->flags);
/*pDhcp->ciaddr.s_addr = 0;*/
pDhcp->yiaddr.s_addr = 0;
pDhcp->siaddr.s_addr = 0;
pDhcp->giaddr.s_addr = 0;
bcopy(pBinding->haddr.haddr,(char *)pDhcp->chaddr,pBinding->haddr.hlen);
bzero((char *)pDhcp->sname,MAX_SNAME);
bzero((char *)pDhcp->file,MAX_FILE);
/* insert magic cookie */
bcopy ((char *)dhcpCookie, pDhcp->options, MAGIC_LEN);
/*pDhcp->options[0] = 99;
pDhcp->options[1] = 130;
pDhcp->options[2] = 83;
pDhcp->options[3] = 99;*/
offset = MAGIC_LEN;
DEBUG_UT_DHCP("magic\n",pDhcp,offset);
/* insert message type */
pDhcp->options[offset++] = _DHCP_MSGTYPE_TAG;
pDhcp->options[offset++] = 1;
pDhcp->options[offset++] = DHCPREQUEST;
DEBUG_UT_DHCP("_DHCP_MSGTYPE_TAG\n",pDhcp,offset);
if (pBinding->state == BOUND || pBinding->state == RENEWING || pBinding->state == REBINDING ||
pBinding->state == RENEW_RETRY || pBinding->state == REBIND_RETRY)
{
pDhcp->ciaddr.s_addr = htonl(pBinding->pRes->ip_addr.s_addr);
} else {
pDhcp->ciaddr.s_addr = 0;
}
/* insert client identifier */
/*
Code Len Type Client-Identifier
+-----+-----+-----+-----+-----+---
| 61 | n | t1 | i1 | i2 | ...
+-----+-----+-----+-----+-----+---
*/
optLen = pBinding->cid.idlen;
if (gDhcpClientGlobalConfig.cidFlag && optLen > 0) {
if (dhcp_client_have_option_room(offset,optLen)) {
pDhcp->options[offset++] = _DHCP_CLIENT_ID_TAG;
pDhcp->options[offset++] = optLen+1;
pDhcp->options[offset++] = pBinding->cid.idtype;
bcopy(pBinding->cid.id,&pDhcp->options[offset], optLen);
offset += optLen;
} else {
return ERROR;
}
} else if(gDhcpClientGlobalConfig.haddrFlag && pBinding->haddr.hlen) {
if (dhcp_client_have_option_room(offset,pBinding->haddr.hlen)) {
pDhcp->options[offset++] = _DHCP_CLIENT_ID_TAG;
pDhcp->options[offset++] = pBinding->haddr.hlen+1;
pDhcp->options[offset++] = pBinding->haddr.htype;
bcopy(pBinding->haddr.haddr,&pDhcp->options[offset], pBinding->haddr.hlen);
offset += pBinding->haddr.hlen;
} else {
return ERROR;
}
} else {
/* don't insert _DHCP_CLIENT_ID_TAG*/
}
DEBUG_UT_DHCP("_DHCP_CLIENT_ID_TAG\n",pDhcp,offset);
/* insert vendor class identifier */
if (gDhcpClientGlobalConfig.vendor_id.len != 0) {
if (dhcp_client_have_option_room(offset,gDhcpClientGlobalConfig.vendor_id.len)) {
pDhcp->options[offset++] = _DHCP_VENDOR_CLASS_ID_TAG;
pDhcp->options[offset++] = gDhcpClientGlobalConfig.vendor_id.len;
bcopy(gDhcpClientGlobalConfig.vendor_id.id,&pDhcp->options[offset],
gDhcpClientGlobalConfig.vendor_id.len);
offset += gDhcpClientGlobalConfig.vendor_id.len;
} else {
return ERROR;
}
}
DEBUG_UT_DHCP("_DHCP_VENDOR_CLASS_ID_TAG\n",pDhcp,offset);
/* UTStarcom-specific operator-domain-name option */
if (gDhcpClientGlobalConfig.domain.name != NULL)
{
optLen = strlen(gDhcpClientGlobalConfig.domain.name);
if (dhcp_client_have_option_room(offset, 2 + optLen))
{
pDhcp->options[offset++] = _DHCP_VENDOR_SPEC_TAG;
pDhcp->options[offset++] = 2 + optLen;
pDhcp->options[offset++] = UTS_OPERATOR_DOMAIN;
pDhcp->options[offset++] = optLen;
bcopy(gDhcpClientGlobalConfig.domain.name,&pDhcp->options[offset], optLen);
offset += optLen;
} else {
return ERROR;
}
}
DEBUG_UT_DHCP("_DHCP_VENDOR_SPEC_TAG\n",pDhcp,offset);
/* user class id (refer to RFC 3004)
*
* Code Len Value
* +-----+-----+--------------------- . . . --+
* | 77 | N | User Class Data ('Len' octets) |
* +-----+-----+--------------------- . . . --+
* / \
* UC_Len_i User_Class_Data_i
* +--------+------------------------ . . . --+
* | L_i | Opaque-Data ('UC_Len_i' octets) |
* +--------+------------------------ . . . --+
*
*/
if(gDhcpClientGlobalConfig.user_id.len != 0)
{
optLen = gDhcpClientGlobalConfig.user_id.len;
if (dhcp_client_have_option_room(offset, optLen + 1))
{
pDhcp->options[offset++] = _DHCP_USER_CLASS_TAG;
pDhcp->options[offset++] = optLen + 1;
pDhcp->options[offset++] = optLen;
bcopy((char *)gDhcpClientGlobalConfig.user_id.id,&pDhcp->options[offset], optLen);
offset += optLen;
} else {
return ERROR;
}
}
DEBUG_UT_DHCP("_DHCP_USER_CLASS_TAG\n",pDhcp,offset);
/* subnet selection option */
if (gDhcpClientGlobalConfig.subnet != 0)
{
optLen = 4; /* IP address length */
if (dhcp_client_have_option_room(offset, optLen))
{
pDhcp->options[offset++] = _DHCP_SUBNET_SELECTION_TAG;
pDhcp->options[offset++] = optLen;
bcopy((char *)&gDhcpClientGlobalConfig.subnet,&pDhcp->options[offset], optLen);
offset += optLen;
} else {
return ERROR;
}
}
DEBUG_UT_DHCP("_DHCP_SUBNET_SELECTION_TAG\n",pDhcp,offset);
/* insert server identifier -- only allowed before BOUND state */
if (pBinding->state == SELECTING || pBinding->state == REQUESTING)
{
if (pBinding->pRes->siaddr.s_addr == 0) {
return ERROR;
}
if (dhcp_client_have_option_room(offset,4))
{
pDhcp->options[offset++] = _DHCP_SERVER_ID_TAG;
pDhcp->options[offset++] = 4;
bcopy((char *)&pBinding->pRes->siaddr.s_addr,&pDhcp->options[offset], 4);
offset += 4;
}
else
{
return ERROR;
}
}
DEBUG_UT_DHCP("_DHCP_SERVER_ID_TAG\n",pDhcp,offset);
/* insert requesting lease */
if (gDhcpClientGlobalConfig.reqLease != 0 )
{
if(dhcp_client_have_option_room(offset,4)) {
pDhcp->options[offset++] = _DHCP_LEASE_TIME_TAG;
pDhcp->options[offset++] = 4;
ulTemp = htonl(gDhcpClientGlobalConfig.reqLease);
bcopy((char *)&ulTemp,&pDhcp->options[offset], 4);
offset += 4;
} else {
return ERROR;
}
}
DEBUG_UT_DHCP("_DHCP_LEASE_TIME_TAG\n",pDhcp,offset);
/* insert requested ipaddr -- only allowed before BOUND state */
if (pBinding->state == SELECTING || pBinding->state == REQUESTING)
{
if (pBinding->pRes->ip_addr.s_addr != 0 && dhcp_client_have_option_room(offset,4))
{
pDhcp->options[offset++] = _DHCP_REQUEST_IPADDR_TAG;
pDhcp->options[offset++] = 4;
bcopy((char *)&pBinding->pRes->ip_addr.s_addr,&pDhcp->options[offset], 4);
offset +=4;
}
else
{
return ERROR;
}
}
DEBUG_UT_DHCP("_DHCP_REQUEST_IPADDR_TAG\n",pDhcp,offset);
/* insert Maximum DHCP message size */
if (dhcp_client_have_option_room(offset,2))
{
pDhcp->options[offset++] = _DHCP_MAXMSGSIZE_TAG;
pDhcp->options[offset++] = 2;
usTemp = htons(DFLTDHCPLEN);
bcopy((char *)&usTemp,&pDhcp->options[offset], 2);
offset += 2;
} else{
return OK;
}
DEBUG_UT_DHCP("_DHCP_MAXMSGSIZE_TAG\n",pDhcp,offset);
/* if necessary, insert request list */
optLen = gDhcpClientGlobalConfig.reqListLen;
if (optLen!= 0)
{
if (dhcp_client_have_option_room(offset,optLen))
{
pDhcp->options[offset++] = _DHCP_REQ_LIST_TAG;
pDhcp->options[offset++] = optLen;
bcopy((char *)gDhcpClientGlobalConfig.reqList,&pDhcp->options[offset],optLen);
offset += optLen;
} else {
return ERROR;
}
}
DEBUG_UT_DHCP("_DHCP_REQ_LIST_TAG\n",pDhcp,offset);
if(dhcp_client_have_option_room(offset,1)) {
pDhcp->options[offset] = _DHCP_END_TAG;
offset ++;
}
DEBUG_UT_DHCP("end\n",pDhcp,offset);
pBinding->msgLen = DFLTDHCPLEN - DFLTOPTLEN + offset;
TRACE0("msgLen %d, offset %d\n",pBinding->msgLen,offset);
return OK;
}
int make_decline(St_DhcpBinding *pBinding)
{
int offset = 0; /* offset in options field */
int optLen;
struct dhcp *pDhcp = pDhcpcOut;
DHCP_ASSERT(DHCP_BINDING_GOOD(pBinding));
bzero((char *)pDhcp,sizeof(struct dhcp));
DEBUG_UT_DHCP("begin\n",pDhcp,offset);
pDhcp->op = BOOTREQUEST;
pDhcp->htype = pBinding->haddr.htype;
pDhcp->hlen = pBinding->haddr.hlen;
pDhcp->xid = *((unsigned int*)&pBinding->cid.id[2]);
pDhcp->hops = 0;
pDhcp->secs = 0;
SETBRDCST(pDhcp->flags);
pDhcp->ciaddr.s_addr = 0;
pDhcp->yiaddr.s_addr = 0;
pDhcp
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -