Skip to content

Commit

Permalink
netutils/dhcpc: fix nxstyle warning
Browse files Browse the repository at this point in the history
Signed-off-by: chao.an <anchao@xiaomi.com>
  • Loading branch information
anchao authored and patacongo committed Apr 4, 2020
1 parent 70bb13c commit c6b678f
Showing 1 changed file with 61 additions and 46 deletions.
107 changes: 61 additions & 46 deletions netutils/dhcpc/dhcpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@

/* Configuration */


/* DHCP Definitions */

#define STATE_INITIAL 0
Expand Down Expand Up @@ -143,8 +142,15 @@ struct dhcpc_state_s
* Private Data
****************************************************************************/

static const uint8_t xid[4] = {0xad, 0xde, 0x12, 0x23};
static const uint8_t magic_cookie[4] = {99, 130, 83, 99};
static const uint8_t xid[4] =
{
0xad, 0xde, 0x12, 0x23
};

static const uint8_t magic_cookie[4] =
{
99, 130, 83, 99
};

/****************************************************************************
* Private Functions
Expand Down Expand Up @@ -226,7 +232,8 @@ static int dhcpc_sendmsg(FAR struct dhcpc_state_s *pdhcpc,
pdhcpc->packet.hlen = pdhcpc->ds_maclen;
memcpy(pdhcpc->packet.xid, xid, 4);
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));

/* Add the common header options */
Expand Down Expand Up @@ -278,7 +285,7 @@ static int dhcpc_sendmsg(FAR struct dhcpc_state_s *pdhcpc,
}

pend = dhcpc_addend(pend);
len = pend - (uint8_t*)&pdhcpc->packet;
len = pend - (uint8_t *)&pdhcpc->packet;

/* Send the request */

Expand All @@ -287,7 +294,7 @@ static int dhcpc_sendmsg(FAR struct dhcpc_state_s *pdhcpc,
addr.sin_addr.s_addr = serverid;

return sendto(pdhcpc->sockfd, &pdhcpc->packet, len, 0,
(struct sockaddr*)&addr, sizeof(struct sockaddr_in));
(struct sockaddr *)&addr, sizeof(struct sockaddr_in));
}

/****************************************************************************
Expand All @@ -302,33 +309,38 @@ static uint8_t dhcpc_parseoptions(FAR struct dhcpc_state *presult,

while (optptr < end)
{
switch(*optptr)
switch (*optptr)
{
case DHCP_OPTION_SUBNET_MASK:

/* Get subnet mask in network order */

memcpy(&presult->netmask.s_addr, optptr + 2, 4);
break;

case DHCP_OPTION_ROUTER:

/* Get the default router address in network order */

memcpy(&presult->default_router.s_addr, optptr + 2, 4);
break;

case DHCP_OPTION_DNS_SERVER:

/* Get the DNS server address in network order */

memcpy(&presult->dnsaddr.s_addr, optptr + 2, 4);
break;

case DHCP_OPTION_MSG_TYPE:

/* Get message type */

type = *(optptr + 2);
break;

case DHCP_OPTION_SERVER_ID:

/* Get server address in network order */

memcpy(&presult->serverid.s_addr, optptr + 2, 4);
Expand Down Expand Up @@ -364,7 +376,8 @@ static uint8_t dhcpc_parsemsg(FAR struct dhcpc_state_s *pdhcpc, int buflen,
{
if (pdhcpc->packet.op == DHCP_REPLY &&
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);
return dhcpc_parseoptions(presult, &pdhcpc->packet.options[4], buflen);
Expand All @@ -390,8 +403,9 @@ FAR void *dhcpc_open(FAR const char *interface, FAR const void *macaddr,
int ret;

ninfo("MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
((uint8_t*)macaddr)[0], ((uint8_t*)macaddr)[1], ((uint8_t*)macaddr)[2],
((uint8_t*)macaddr)[3], ((uint8_t*)macaddr)[4], ((uint8_t*)macaddr)[5]);
((uint8_t *)macaddr)[0], ((uint8_t *)macaddr)[1],
((uint8_t *)macaddr)[2], ((uint8_t *)macaddr)[3],
((uint8_t *)macaddr)[4], ((uint8_t *)macaddr)[5]);

/* Allocate an internal DHCP structure */

Expand All @@ -410,7 +424,7 @@ FAR void *dhcpc_open(FAR const char *interface, FAR const void *macaddr,
pdhcpc->sockfd = socket(PF_INET, SOCK_DGRAM, 0);
if (pdhcpc->sockfd < 0)
{
ninfo("socket handle %d\n",ret);
ninfo("socket handle %d\n", ret);
free(pdhcpc);
return NULL;
}
Expand All @@ -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_addr.s_addr = INADDR_ANY;

ret = bind(pdhcpc->sockfd, (struct sockaddr*)&addr,
ret = bind(pdhcpc->sockfd, (struct sockaddr *)&addr,
sizeof(struct sockaddr_in));
if (ret < 0)
{
ninfo("bind status %d\n",ret);
ninfo("bind status %d\n", ret);
close(pdhcpc->sockfd);
free(pdhcpc);
return NULL;
Expand All @@ -440,7 +454,7 @@ FAR void *dhcpc_open(FAR const char *interface, FAR const void *macaddr,
sizeof(struct timeval));
if (ret < 0)
{
ninfo("setsockopt(RCVTIMEO) status %d\n",ret);
ninfo("setsockopt(RCVTIMEO) status %d\n", ret);
close(pdhcpc->sockfd);
free(pdhcpc);
return NULL;
Expand All @@ -457,13 +471,12 @@ FAR void *dhcpc_open(FAR const char *interface, FAR const void *macaddr,
pdhcpc->interface, strlen(pdhcpc->interface));
if (ret < 0)
{
ninfo("setsockopt(BINDTODEVICE) status %d\n",ret);
ninfo("setsockopt(BINDTODEVICE) status %d\n", ret);
close(pdhcpc->sockfd);
free(pdhcpc);
return NULL;
}
#endif

}

return (FAR void *)pdhcpc;
Expand Down Expand Up @@ -557,8 +570,8 @@ int dhcpc_request(FAR void *handle, FAR struct dhcpc_state *presult)
pdhcpc->ipaddr.s_addr = presult->ipaddr.s_addr;
pdhcpc->serverid.s_addr = presult->serverid.s_addr;

/* Temporarily use the address offered by the server and break
* out of the loop.
/* Temporarily use the address offered by the server
* and break out of the loop.
*/

netlib_set_ipv4addr(pdhcpc->interface,
Expand All @@ -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
* nothing was received on this socket for a long period of time).
* Then loop and send the DISCOVER command again.
/* An error has occurred. If this was a timeout error (meaning
* that nothing was received on this socket for a long period
* of time). Then loop and send the DISCOVER command again.
*/

else if (errno != EAGAIN)
Expand All @@ -579,7 +592,8 @@ int dhcpc_request(FAR void *handle, FAR struct dhcpc_state *presult)
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 */

Expand Down Expand Up @@ -617,8 +631,8 @@ int dhcpc_request(FAR void *handle, FAR struct dhcpc_state *presult)

msgtype = dhcpc_parsemsg(pdhcpc, result, presult);

/* The ACK response means that the server has accepted our request
* and we have the lease.
/* The ACK response means that the server has accepted
* our request and we have the lease.
*/

if (msgtype == DHCPACK)
Expand All @@ -638,9 +652,9 @@ int dhcpc_request(FAR void *handle, FAR struct dhcpc_state *presult)
break;
}

/* If we get any OFFERs from other servers, then decline them now
* and continue waiting for the ACK from the server that we
* requested from.
/* If we get any OFFERs from other servers, then decline
* them now and continue waiting for the ACK from the server
* that we requested from.
*/

else if (msgtype == DHCPOFFER)
Expand All @@ -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
* that nothing was received on this socket for a long period of
* time). Then break out and send the DISCOVER command again (at most
* 3 times).
* time). Then break out and send the DISCOVER command again
* (at most 3 times).
*/

else if (errno != EAGAIN)
Expand All @@ -671,30 +685,31 @@ int dhcpc_request(FAR void *handle, FAR struct dhcpc_state *presult)
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);

ninfo("Got IP address %d.%d.%d.%d\n",
(presult->ipaddr.s_addr ) & 0xff,
(presult->ipaddr.s_addr >> 8 ) & 0xff,
(presult->ipaddr.s_addr >> 16 ) & 0xff,
(presult->ipaddr.s_addr >> 24 ) & 0xff);
(presult->ipaddr.s_addr) & 0xff,
(presult->ipaddr.s_addr >> 8) & 0xff,
(presult->ipaddr.s_addr >> 16) & 0xff,
(presult->ipaddr.s_addr >> 24) & 0xff);
ninfo("Got netmask %d.%d.%d.%d\n",
(presult->netmask.s_addr ) & 0xff,
(presult->netmask.s_addr >> 8 ) & 0xff,
(presult->netmask.s_addr >> 16 ) & 0xff,
(presult->netmask.s_addr >> 24 ) & 0xff);
(presult->netmask.s_addr) & 0xff,
(presult->netmask.s_addr >> 8) & 0xff,
(presult->netmask.s_addr >> 16) & 0xff,
(presult->netmask.s_addr >> 24) & 0xff);
ninfo("Got DNS server %d.%d.%d.%d\n",
(presult->dnsaddr.s_addr ) & 0xff,
(presult->dnsaddr.s_addr >> 8 ) & 0xff,
(presult->dnsaddr.s_addr >> 16 ) & 0xff,
(presult->dnsaddr.s_addr >> 24 ) & 0xff);
(presult->dnsaddr.s_addr) & 0xff,
(presult->dnsaddr.s_addr >> 8) & 0xff,
(presult->dnsaddr.s_addr >> 16) & 0xff,
(presult->dnsaddr.s_addr >> 24) & 0xff);
ninfo("Got default router %d.%d.%d.%d\n",
(presult->default_router.s_addr ) & 0xff,
(presult->default_router.s_addr >> 8 ) & 0xff,
(presult->default_router.s_addr >> 16 ) & 0xff,
(presult->default_router.s_addr >> 24 ) & 0xff);
(presult->default_router.s_addr) & 0xff,
(presult->default_router.s_addr >> 8) & 0xff,
(presult->default_router.s_addr >> 16) & 0xff,
(presult->default_router.s_addr >> 24) & 0xff);
ninfo("Lease expires in %d seconds\n", presult->lease_time);
return OK;
}

0 comments on commit c6b678f

Please sign in to comment.