netutils/dhcpc: fix nxstyle warning
Signed-off-by: chao.an <anchao@xiaomi.com>
This commit is contained in:
parent
70bb13ce3b
commit
c6b678fa60
@ -64,7 +64,6 @@
|
|||||||
|
|
||||||
/* Configuration */
|
/* Configuration */
|
||||||
|
|
||||||
|
|
||||||
/* DHCP Definitions */
|
/* DHCP Definitions */
|
||||||
|
|
||||||
#define STATE_INITIAL 0
|
#define STATE_INITIAL 0
|
||||||
@ -143,8 +142,15 @@ struct dhcpc_state_s
|
|||||||
* Private Data
|
* Private Data
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
static const uint8_t xid[4] = {0xad, 0xde, 0x12, 0x23};
|
static const uint8_t xid[4] =
|
||||||
static const uint8_t magic_cookie[4] = {99, 130, 83, 99};
|
{
|
||||||
|
0xad, 0xde, 0x12, 0x23
|
||||||
|
};
|
||||||
|
|
||||||
|
static const uint8_t magic_cookie[4] =
|
||||||
|
{
|
||||||
|
99, 130, 83, 99
|
||||||
|
};
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Private Functions
|
* Private Functions
|
||||||
@ -226,7 +232,8 @@ static int dhcpc_sendmsg(FAR struct dhcpc_state_s *pdhcpc,
|
|||||||
pdhcpc->packet.hlen = pdhcpc->ds_maclen;
|
pdhcpc->packet.hlen = pdhcpc->ds_maclen;
|
||||||
memcpy(pdhcpc->packet.xid, xid, 4);
|
memcpy(pdhcpc->packet.xid, xid, 4);
|
||||||
memcpy(pdhcpc->packet.chaddr, pdhcpc->ds_macaddr, pdhcpc->ds_maclen);
|
memcpy(pdhcpc->packet.chaddr, pdhcpc->ds_macaddr, pdhcpc->ds_maclen);
|
||||||
memset(&pdhcpc->packet.chaddr[pdhcpc->ds_maclen], 0, 16 - pdhcpc->ds_maclen);
|
memset(&pdhcpc->packet.chaddr[pdhcpc->ds_maclen],
|
||||||
|
0, 16 - pdhcpc->ds_maclen);
|
||||||
memcpy(pdhcpc->packet.options, magic_cookie, sizeof(magic_cookie));
|
memcpy(pdhcpc->packet.options, magic_cookie, sizeof(magic_cookie));
|
||||||
|
|
||||||
/* Add the common header options */
|
/* Add the common header options */
|
||||||
@ -278,7 +285,7 @@ static int dhcpc_sendmsg(FAR struct dhcpc_state_s *pdhcpc,
|
|||||||
}
|
}
|
||||||
|
|
||||||
pend = dhcpc_addend(pend);
|
pend = dhcpc_addend(pend);
|
||||||
len = pend - (uint8_t*)&pdhcpc->packet;
|
len = pend - (uint8_t *)&pdhcpc->packet;
|
||||||
|
|
||||||
/* Send the request */
|
/* Send the request */
|
||||||
|
|
||||||
@ -287,7 +294,7 @@ static int dhcpc_sendmsg(FAR struct dhcpc_state_s *pdhcpc,
|
|||||||
addr.sin_addr.s_addr = serverid;
|
addr.sin_addr.s_addr = serverid;
|
||||||
|
|
||||||
return sendto(pdhcpc->sockfd, &pdhcpc->packet, len, 0,
|
return sendto(pdhcpc->sockfd, &pdhcpc->packet, len, 0,
|
||||||
(struct sockaddr*)&addr, sizeof(struct sockaddr_in));
|
(struct sockaddr *)&addr, sizeof(struct sockaddr_in));
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
@ -302,33 +309,38 @@ static uint8_t dhcpc_parseoptions(FAR struct dhcpc_state *presult,
|
|||||||
|
|
||||||
while (optptr < end)
|
while (optptr < end)
|
||||||
{
|
{
|
||||||
switch(*optptr)
|
switch (*optptr)
|
||||||
{
|
{
|
||||||
case DHCP_OPTION_SUBNET_MASK:
|
case DHCP_OPTION_SUBNET_MASK:
|
||||||
|
|
||||||
/* Get subnet mask in network order */
|
/* Get subnet mask in network order */
|
||||||
|
|
||||||
memcpy(&presult->netmask.s_addr, optptr + 2, 4);
|
memcpy(&presult->netmask.s_addr, optptr + 2, 4);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case DHCP_OPTION_ROUTER:
|
case DHCP_OPTION_ROUTER:
|
||||||
|
|
||||||
/* Get the default router address in network order */
|
/* Get the default router address in network order */
|
||||||
|
|
||||||
memcpy(&presult->default_router.s_addr, optptr + 2, 4);
|
memcpy(&presult->default_router.s_addr, optptr + 2, 4);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case DHCP_OPTION_DNS_SERVER:
|
case DHCP_OPTION_DNS_SERVER:
|
||||||
|
|
||||||
/* Get the DNS server address in network order */
|
/* Get the DNS server address in network order */
|
||||||
|
|
||||||
memcpy(&presult->dnsaddr.s_addr, optptr + 2, 4);
|
memcpy(&presult->dnsaddr.s_addr, optptr + 2, 4);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case DHCP_OPTION_MSG_TYPE:
|
case DHCP_OPTION_MSG_TYPE:
|
||||||
|
|
||||||
/* Get message type */
|
/* Get message type */
|
||||||
|
|
||||||
type = *(optptr + 2);
|
type = *(optptr + 2);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case DHCP_OPTION_SERVER_ID:
|
case DHCP_OPTION_SERVER_ID:
|
||||||
|
|
||||||
/* Get server address in network order */
|
/* Get server address in network order */
|
||||||
|
|
||||||
memcpy(&presult->serverid.s_addr, optptr + 2, 4);
|
memcpy(&presult->serverid.s_addr, optptr + 2, 4);
|
||||||
@ -364,7 +376,8 @@ static uint8_t dhcpc_parsemsg(FAR struct dhcpc_state_s *pdhcpc, int buflen,
|
|||||||
{
|
{
|
||||||
if (pdhcpc->packet.op == DHCP_REPLY &&
|
if (pdhcpc->packet.op == DHCP_REPLY &&
|
||||||
memcmp(pdhcpc->packet.xid, xid, sizeof(xid)) == 0 &&
|
memcmp(pdhcpc->packet.xid, xid, sizeof(xid)) == 0 &&
|
||||||
memcmp(pdhcpc->packet.chaddr, pdhcpc->ds_macaddr, pdhcpc->ds_maclen) == 0)
|
memcmp(pdhcpc->packet.chaddr,
|
||||||
|
pdhcpc->ds_macaddr, pdhcpc->ds_maclen) == 0)
|
||||||
{
|
{
|
||||||
memcpy(&presult->ipaddr.s_addr, pdhcpc->packet.yiaddr, 4);
|
memcpy(&presult->ipaddr.s_addr, pdhcpc->packet.yiaddr, 4);
|
||||||
return dhcpc_parseoptions(presult, &pdhcpc->packet.options[4], buflen);
|
return dhcpc_parseoptions(presult, &pdhcpc->packet.options[4], buflen);
|
||||||
@ -390,8 +403,9 @@ FAR void *dhcpc_open(FAR const char *interface, FAR const void *macaddr,
|
|||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
ninfo("MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
|
ninfo("MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
|
||||||
((uint8_t*)macaddr)[0], ((uint8_t*)macaddr)[1], ((uint8_t*)macaddr)[2],
|
((uint8_t *)macaddr)[0], ((uint8_t *)macaddr)[1],
|
||||||
((uint8_t*)macaddr)[3], ((uint8_t*)macaddr)[4], ((uint8_t*)macaddr)[5]);
|
((uint8_t *)macaddr)[2], ((uint8_t *)macaddr)[3],
|
||||||
|
((uint8_t *)macaddr)[4], ((uint8_t *)macaddr)[5]);
|
||||||
|
|
||||||
/* Allocate an internal DHCP structure */
|
/* Allocate an internal DHCP structure */
|
||||||
|
|
||||||
@ -410,7 +424,7 @@ FAR void *dhcpc_open(FAR const char *interface, FAR const void *macaddr,
|
|||||||
pdhcpc->sockfd = socket(PF_INET, SOCK_DGRAM, 0);
|
pdhcpc->sockfd = socket(PF_INET, SOCK_DGRAM, 0);
|
||||||
if (pdhcpc->sockfd < 0)
|
if (pdhcpc->sockfd < 0)
|
||||||
{
|
{
|
||||||
ninfo("socket handle %d\n",ret);
|
ninfo("socket handle %d\n", ret);
|
||||||
free(pdhcpc);
|
free(pdhcpc);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -421,11 +435,11 @@ FAR void *dhcpc_open(FAR const char *interface, FAR const void *macaddr,
|
|||||||
addr.sin_port = HTONS(DHCPC_CLIENT_PORT);
|
addr.sin_port = HTONS(DHCPC_CLIENT_PORT);
|
||||||
addr.sin_addr.s_addr = INADDR_ANY;
|
addr.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
|
||||||
ret = bind(pdhcpc->sockfd, (struct sockaddr*)&addr,
|
ret = bind(pdhcpc->sockfd, (struct sockaddr *)&addr,
|
||||||
sizeof(struct sockaddr_in));
|
sizeof(struct sockaddr_in));
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
{
|
{
|
||||||
ninfo("bind status %d\n",ret);
|
ninfo("bind status %d\n", ret);
|
||||||
close(pdhcpc->sockfd);
|
close(pdhcpc->sockfd);
|
||||||
free(pdhcpc);
|
free(pdhcpc);
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -440,7 +454,7 @@ FAR void *dhcpc_open(FAR const char *interface, FAR const void *macaddr,
|
|||||||
sizeof(struct timeval));
|
sizeof(struct timeval));
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
{
|
{
|
||||||
ninfo("setsockopt(RCVTIMEO) status %d\n",ret);
|
ninfo("setsockopt(RCVTIMEO) status %d\n", ret);
|
||||||
close(pdhcpc->sockfd);
|
close(pdhcpc->sockfd);
|
||||||
free(pdhcpc);
|
free(pdhcpc);
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -457,13 +471,12 @@ FAR void *dhcpc_open(FAR const char *interface, FAR const void *macaddr,
|
|||||||
pdhcpc->interface, strlen(pdhcpc->interface));
|
pdhcpc->interface, strlen(pdhcpc->interface));
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
{
|
{
|
||||||
ninfo("setsockopt(BINDTODEVICE) status %d\n",ret);
|
ninfo("setsockopt(BINDTODEVICE) status %d\n", ret);
|
||||||
close(pdhcpc->sockfd);
|
close(pdhcpc->sockfd);
|
||||||
free(pdhcpc);
|
free(pdhcpc);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (FAR void *)pdhcpc;
|
return (FAR void *)pdhcpc;
|
||||||
@ -557,8 +570,8 @@ int dhcpc_request(FAR void *handle, FAR struct dhcpc_state *presult)
|
|||||||
pdhcpc->ipaddr.s_addr = presult->ipaddr.s_addr;
|
pdhcpc->ipaddr.s_addr = presult->ipaddr.s_addr;
|
||||||
pdhcpc->serverid.s_addr = presult->serverid.s_addr;
|
pdhcpc->serverid.s_addr = presult->serverid.s_addr;
|
||||||
|
|
||||||
/* Temporarily use the address offered by the server and break
|
/* Temporarily use the address offered by the server
|
||||||
* out of the loop.
|
* and break out of the loop.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
netlib_set_ipv4addr(pdhcpc->interface,
|
netlib_set_ipv4addr(pdhcpc->interface,
|
||||||
@ -567,9 +580,9 @@ int dhcpc_request(FAR void *handle, FAR struct dhcpc_state *presult)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* An error has occurred. If this was a timeout error (meaning that
|
/* An error has occurred. If this was a timeout error (meaning
|
||||||
* nothing was received on this socket for a long period of time).
|
* that nothing was received on this socket for a long period
|
||||||
* Then loop and send the DISCOVER command again.
|
* of time). Then loop and send the DISCOVER command again.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
else if (errno != EAGAIN)
|
else if (errno != EAGAIN)
|
||||||
@ -579,7 +592,8 @@ int dhcpc_request(FAR void *handle, FAR struct dhcpc_state *presult)
|
|||||||
return ERROR;
|
return ERROR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
while (state == STATE_INITIAL && retries < CONFIG_NETUTILS_DHCPC_RETRIES);
|
while (state == STATE_INITIAL &&
|
||||||
|
retries < CONFIG_NETUTILS_DHCPC_RETRIES);
|
||||||
|
|
||||||
/* If no DHCPOFFER recveived here, error out */
|
/* If no DHCPOFFER recveived here, error out */
|
||||||
|
|
||||||
@ -617,8 +631,8 @@ int dhcpc_request(FAR void *handle, FAR struct dhcpc_state *presult)
|
|||||||
|
|
||||||
msgtype = dhcpc_parsemsg(pdhcpc, result, presult);
|
msgtype = dhcpc_parsemsg(pdhcpc, result, presult);
|
||||||
|
|
||||||
/* The ACK response means that the server has accepted our request
|
/* The ACK response means that the server has accepted
|
||||||
* and we have the lease.
|
* our request and we have the lease.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if (msgtype == DHCPACK)
|
if (msgtype == DHCPACK)
|
||||||
@ -638,9 +652,9 @@ int dhcpc_request(FAR void *handle, FAR struct dhcpc_state *presult)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If we get any OFFERs from other servers, then decline them now
|
/* If we get any OFFERs from other servers, then decline
|
||||||
* and continue waiting for the ACK from the server that we
|
* them now and continue waiting for the ACK from the server
|
||||||
* requested from.
|
* that we requested from.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
else if (msgtype == DHCPOFFER)
|
else if (msgtype == DHCPOFFER)
|
||||||
@ -659,8 +673,8 @@ int dhcpc_request(FAR void *handle, FAR struct dhcpc_state *presult)
|
|||||||
|
|
||||||
/* An error has occurred. If this was a timeout error (meaning
|
/* An error has occurred. If this was a timeout error (meaning
|
||||||
* that nothing was received on this socket for a long period of
|
* that nothing was received on this socket for a long period of
|
||||||
* time). Then break out and send the DISCOVER command again (at most
|
* time). Then break out and send the DISCOVER command again
|
||||||
* 3 times).
|
* (at most 3 times).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
else if (errno != EAGAIN)
|
else if (errno != EAGAIN)
|
||||||
@ -671,30 +685,31 @@ int dhcpc_request(FAR void *handle, FAR struct dhcpc_state *presult)
|
|||||||
return ERROR;
|
return ERROR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
while (state == STATE_HAVE_OFFER && retries < CONFIG_NETUTILS_DHCPC_RETRIES);
|
while (state == STATE_HAVE_OFFER &&
|
||||||
|
retries < CONFIG_NETUTILS_DHCPC_RETRIES);
|
||||||
}
|
}
|
||||||
while (state != STATE_HAVE_LEASE);
|
while (state != STATE_HAVE_LEASE);
|
||||||
|
|
||||||
ninfo("Got IP address %d.%d.%d.%d\n",
|
ninfo("Got IP address %d.%d.%d.%d\n",
|
||||||
(presult->ipaddr.s_addr ) & 0xff,
|
(presult->ipaddr.s_addr) & 0xff,
|
||||||
(presult->ipaddr.s_addr >> 8 ) & 0xff,
|
(presult->ipaddr.s_addr >> 8) & 0xff,
|
||||||
(presult->ipaddr.s_addr >> 16 ) & 0xff,
|
(presult->ipaddr.s_addr >> 16) & 0xff,
|
||||||
(presult->ipaddr.s_addr >> 24 ) & 0xff);
|
(presult->ipaddr.s_addr >> 24) & 0xff);
|
||||||
ninfo("Got netmask %d.%d.%d.%d\n",
|
ninfo("Got netmask %d.%d.%d.%d\n",
|
||||||
(presult->netmask.s_addr ) & 0xff,
|
(presult->netmask.s_addr) & 0xff,
|
||||||
(presult->netmask.s_addr >> 8 ) & 0xff,
|
(presult->netmask.s_addr >> 8) & 0xff,
|
||||||
(presult->netmask.s_addr >> 16 ) & 0xff,
|
(presult->netmask.s_addr >> 16) & 0xff,
|
||||||
(presult->netmask.s_addr >> 24 ) & 0xff);
|
(presult->netmask.s_addr >> 24) & 0xff);
|
||||||
ninfo("Got DNS server %d.%d.%d.%d\n",
|
ninfo("Got DNS server %d.%d.%d.%d\n",
|
||||||
(presult->dnsaddr.s_addr ) & 0xff,
|
(presult->dnsaddr.s_addr) & 0xff,
|
||||||
(presult->dnsaddr.s_addr >> 8 ) & 0xff,
|
(presult->dnsaddr.s_addr >> 8) & 0xff,
|
||||||
(presult->dnsaddr.s_addr >> 16 ) & 0xff,
|
(presult->dnsaddr.s_addr >> 16) & 0xff,
|
||||||
(presult->dnsaddr.s_addr >> 24 ) & 0xff);
|
(presult->dnsaddr.s_addr >> 24) & 0xff);
|
||||||
ninfo("Got default router %d.%d.%d.%d\n",
|
ninfo("Got default router %d.%d.%d.%d\n",
|
||||||
(presult->default_router.s_addr ) & 0xff,
|
(presult->default_router.s_addr) & 0xff,
|
||||||
(presult->default_router.s_addr >> 8 ) & 0xff,
|
(presult->default_router.s_addr >> 8) & 0xff,
|
||||||
(presult->default_router.s_addr >> 16 ) & 0xff,
|
(presult->default_router.s_addr >> 16) & 0xff,
|
||||||
(presult->default_router.s_addr >> 24 ) & 0xff);
|
(presult->default_router.s_addr >> 24) & 0xff);
|
||||||
ninfo("Lease expires in %d seconds\n", presult->lease_time);
|
ninfo("Lease expires in %d seconds\n", presult->lease_time);
|
||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user