Use thread-safe ntoa functions

NXStyle fixes

Apply suggestions from code review

Co-authored-by: Petro Karashchenko <petro.karashchenko@gmail.com>

API fixes and NXStyle fixes

Apply suggestions from code review

Co-authored-by: Petro Karashchenko <petro.karashchenko@gmail.com>

Use thread-safe ntoa functions

NXStyle fixes

Apply suggestions from code review

Co-authored-by: Petro Karashchenko <petro.karashchenko@gmail.com>

API fixes and NXStyle fixes

Apply suggestions from code review

Co-authored-by: Petro Karashchenko <petro.karashchenko@gmail.com>
This commit is contained in:
Peter van der Perk 2022-08-01 11:18:52 +02:00 committed by Xiang Xiao
parent c5102fe504
commit dcd60a2abe
11 changed files with 192 additions and 114 deletions

View File

@ -78,6 +78,7 @@ static int briget_net1_setup(void)
#ifdef CONFIG_EXAMPLES_BRIDGE_NET1_DHCPC
struct dhcpc_state ds;
void *handle;
char inetaddr[INET_ADDRSTRLEN];
#endif
printf("NET1: Configuring %s\n", CONFIG_EXAMPLES_BRIDGE_NET1_IFNAME);
@ -165,7 +166,8 @@ static int briget_net1_setup(void)
}
dhcpc_close(handle);
printf("NET1: Assigned IP: %s\n", inet_ntoa(ds.ipaddr));
printf("NET1: Assigned IP: %s\n",
net_ntoa_r(ds.ipaddr, inetaddr, sizeof(inetaddr)));
/* Save the IP address in network order */
@ -208,6 +210,7 @@ static int briget_net2_setup(void)
#ifdef CONFIG_EXAMPLES_BRIDGE_NET2_DHCPC
struct dhcpc_state ds;
void *handle;
char inetaddr[INET_ADDRSTRLEN];
#endif
printf("NET2: Configuring %s\n", CONFIG_EXAMPLES_BRIDGE_NET2_IFNAME);
@ -289,7 +292,8 @@ static int briget_net2_setup(void)
}
dhcpc_close(handle);
printf("NET1: Assigned IP: %s\n", inet_ntoa(ds.ipaddr));
printf("NET1: Assigned IP: %s\n",
inet_ntoa_r(ds.ipaddr, inetaddr, sizeof(inetaddr)));
/* Save the IP address in network order */

View File

@ -136,6 +136,8 @@ int main(int argc, FAR char *argv[])
if (handle)
{
struct dhcpc_state ds;
char inetaddr[INET_ADDRSTRLEN];
dhcpc_request(handle, &ds);
netlib_set_ipv4addr("eth0", &ds.ipaddr);
@ -155,7 +157,7 @@ int main(int argc, FAR char *argv[])
}
dhcpc_close(handle);
printf("IP: %s\n", inet_ntoa(ds.ipaddr));
printf("IP: %s\n", inet_ntoa_r(ds.ipaddr, inetaddr, sizeof(inetaddr)));
}
#endif /* CONFIG_EXAMPLES_DISCOVER_DHCPC */

View File

@ -282,7 +282,10 @@ static void ipcfg_dump_ipv4addr(FAR const char *variable, in_addr_t address)
address
};
printf("%s%s\n", variable, inet_ntoa(saddr));
char inetaddr[INET_ADDRSTRLEN];
printf("%s%s\n", variable,
inet_ntoa_r(saddr, inetaddr, sizeof(inetaddr)));
}
}
#endif

View File

@ -93,6 +93,7 @@ static int tcpecho_netsetup()
#ifdef CONFIG_EXAMPLES_TCPECHO_DHCPC
struct dhcpc_state ds;
void *handle;
char inetaddr[INET_ADDRSTRLEN];
#endif
/* Many embedded network interfaces must have a software assigned MAC */
@ -174,7 +175,7 @@ static int tcpecho_netsetup()
}
dhcpc_close(handle);
printf("IP: %s\n", inet_ntoa(ds.ipaddr));
printf("IP: %s\n", inet_ntoa_r(ds.ipaddr, inetaddr, sizeof(inetaddr)));
#endif /* CONFIG_EXAMPLES_TCPECHO_DHCPC */
#endif /* CONFIG_NSH_NETINIT */
@ -243,12 +244,15 @@ static int tcpecho_server(void)
if (client[0].revents & POLLRDNORM)
{
char inetaddr[INET_ADDRSTRLEN];
/* new client connection */
clilen = sizeof(cliaddr);
connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen);
ninfo("new client: %s\n", inet_ntoa(cliaddr.sin_addr));
ninfo("new client: %s\n",
inet_ntoa_r(cliaddr.sin_addr, inetaddr, sizeof(inetaddr)));
for (i = 1; i < CONFIG_EXAMPLES_TCPECHO_NCONN; i++)
{

View File

@ -165,6 +165,8 @@ int main(int argc, FAR char *argv[])
if (handle)
{
struct dhcpc_state ds;
char inetaddr[INET_ADDRSTRLEN];
dhcpc_request(handle, &ds);
netlib_set_ipv4addr("eth0", &ds.ipaddr);
@ -184,7 +186,7 @@ int main(int argc, FAR char *argv[])
}
dhcpc_close(handle);
printf("IP: %s\n", inet_ntoa(ds.ipaddr));
printf("IP: %s\n", inet_ntoa_r(ds.ipaddr, inetaddr, sizeof(inetaddr)));
}
#endif
#endif /* CONFIG_NSH_NETINIT */

View File

@ -41,8 +41,7 @@
* POSSIBILITY OF SUCH DAMAGE.
****************************************************************************/
/*
* Lightweight Embedded XML-RPC Server main
/* Lightweight Embedded XML-RPC Server main
*
* mtj@cogitollc.com
*
@ -88,8 +87,8 @@
* Private Data
****************************************************************************/
static const char *notimplemented = { "HTTP/1.1 501 Not Implemented\n\n" };
static const char *separator = { "\015\012\015\012" };
static const FAR char *notimplemented = "HTTP/1.1 501 Not Implemented\n\n";
static const FAR char *separator = "\015\012\015\012";
/****************************************************************************
* External Function Prototypes
@ -133,9 +132,10 @@ static char *xmlrpc_findbody(char *buf)
*
****************************************************************************/
static int xmlrpc_getheader(char *buffer, char *header, char *value, int size)
static int xmlrpc_getheader(FAR char *buffer, FAR char *header,
FAR char *value, int size)
{
char *temp;
FAR char *temp;
int i = 0;
temp = strstr(buffer, header);
@ -178,10 +178,17 @@ static void xmlrpc_handler(int fd)
{
fd_set rfds;
struct timeval tv;
int ret, len, max = 0, loadlen = -1;
char buffer[CONFIG_EXAMPLES_XMLRPC_BUFFERSIZE] = { 0 };
int ret;
int len;
int max = 0;
int loadlen = -1;
char buffer[CONFIG_EXAMPLES_XMLRPC_BUFFERSIZE] =
{
0
};
char value[CONFIG_XMLRPC_STRINGSIZE + 1];
char *temp;
FAR char *temp;
/* Read in the Request Header */
@ -237,7 +244,6 @@ static void xmlrpc_handler(int fd)
if (strlen(temp) - 4 == loadlen)
break;
}
}
while (1);
@ -264,8 +270,8 @@ static void xmlrpc_handler(int fd)
static int xmlrpc_netinit(void)
{
/* If this task is excecutated as an NSH built-in function, then the network
* has already been configured by NSH's start-up logic.
/* If this task is excecutated as an NSH built-in function,
* then the network has already been configured by NSH's start-up logic.
*/
#ifndef CONFIG_NSH_NETINIT
@ -323,15 +329,17 @@ static int xmlrpc_netinit(void)
handle = dhcpc_open("eth0", &mac, IFHWADDRLEN);
/* Get an IP address. Note: there is no logic here for renewing the address
* in this example. The address should be renewed in ds.lease_time/2
* seconds.
/* Get an IP address. Note: there is no logic here for renewing the
* address in this example. The address should be renewed in
* ds.lease_time/2 seconds.
*/
printf("Getting IP address\n");
if (handle)
{
struct dhcpc_state ds;
char inetaddr[INET_ADDRSTRLEN];
dhcpc_request(handle, &ds);
netlib_set_ipv4addr("eth0", &ds.ipaddr);
@ -351,7 +359,7 @@ static int xmlrpc_netinit(void)
}
dhcpc_close(handle);
printf("IP: %s\n", inet_ntoa(ds.ipaddr));
printf("IP: %s\n", inet_ntoa_r(ds.ipaddr, inetaddr, sizeof(inetaddr)));
}
#endif /* CONFIG_EXAMPLES_XMLRPC_DHCPC */
@ -374,9 +382,12 @@ static int xmlrpc_netinit(void)
int main(int argc, FAR char *argv[])
{
int listenfd, connfd, on = 1;
int listenfd;
int connfd;
int on = 1;
socklen_t clilen;
struct sockaddr_in cliaddr, servaddr;
struct sockaddr_in cliaddr;
struct sockaddr_in servaddr;
if (xmlrpc_netinit() < 0)
{
@ -409,6 +420,7 @@ int main(int argc, FAR char *argv[])
{
break;
}
ninfo("Connection accepted: %d\n", connfd);
xmlrpc_handler(connfd);

View File

@ -383,7 +383,11 @@ static int iperf_run_tcp_server(void)
}
else
{
printf("accept: %s,%d\n", inet_ntoa(remote_addr.sin_addr),
char inetaddr[INET_ADDRSTRLEN];
printf("accept: %s,%d\n",
inet_ntoa_r(remote_addr.sin_addr, inetaddr,
sizeof(inetaddr)),
htons(remote_addr.sin_port));
iperf_start_report();
@ -397,8 +401,10 @@ static int iperf_run_tcp_server(void)
actual_recv = recv(sockfd, buffer, want_recv, 0);
if (actual_recv == 0)
{
char inetaddr[INET_ADDRSTRLEN];
printf("closed by the peer: %s,%d\n",
inet_ntoa(remote_addr.sin_addr),
inet_ntoa_r(remote_addr.sin_addr, inetaddr,
sizeof(inetaddr)),
htons(remote_addr.sin_port));
/* Note: unlike the original iperf, this implementation

View File

@ -101,6 +101,7 @@ int main(int argc, FAR char *argv[])
struct iperf_cfg_t cfg;
struct in_addr addr;
int nerrors;
char inetaddr[INET_ADDRSTRLEN];
bzero(&addr, sizeof(struct in_addr));
bzero(&cfg, sizeof(cfg));
@ -157,7 +158,7 @@ int main(int argc, FAR char *argv[])
return -1;
}
printf(" IP: %s\n", inet_ntoa(addr));
printf(" IP: %s\n", inet_ntoa_r(addr, inetaddr, sizeof(inetaddr)));
cfg.sip = addr.s_addr;

View File

@ -409,6 +409,7 @@ static void send_mime(httpd_conn *hc, int status, const char *title, const char
{
add_response(hc, extraheads);
}
add_response(hc, "\r\n");
}
}
@ -487,12 +488,13 @@ static void defang(const char *str, char *dfstr, int dfsize)
break;
}
}
*cp2 = '\0';
}
#ifdef CONFIG_THTTPD_ERROR_DIRECTORY
static int send_err_file(httpd_conn *hc, int status, char *title, char *extraheads,
char *filename)
static int send_err_file(httpd_conn *hc, int status, char *title,
char *extraheads, char *filename)
{
FILE *fp;
char buf[1000];
@ -514,6 +516,7 @@ static int send_err_file(httpd_conn *hc, int status, char *title, char *extrahea
buf[nread] = '\0';
add_response(hc, buf);
}
fclose(fp);
#ifdef ERR_APPEND_SERVER_INFO
@ -588,7 +591,6 @@ static inline b64_charmap(char *ch)
}
}
}
}
/* Do base-64 decoding on a string. Ignore any non-base64 bytes.
@ -636,9 +638,11 @@ static int b64_decode(const char *str, unsigned char *space, int size)
phase = 0;
break;
}
prev_decoded = decoded;
}
}
return ndx;
}
@ -808,6 +812,7 @@ static int auth_check2(httpd_conn *hc, char *dirname)
{
continue;
}
*cryp++ = '\0';
/* Is this the right user? */
@ -1014,6 +1019,7 @@ static int vhost_map(httpd_conn *hc)
nerr("ERROR: getsockname: %d\n", errno);
return 0;
}
hc->vhostname = httpd_ntoa(&sa);
}
@ -1075,6 +1081,7 @@ static int vhost_map(httpd_conn *hc)
*cp2++ = '/';
}
strcpy(cp2, hc->vhostname);
#else /* VHOST_DIRLEVELS */
@ -1326,9 +1333,11 @@ static char *bufgets(httpd_conn *hc)
hc->read_buf[hc->checked_idx] = '\0';
++hc->checked_idx;
}
return &(hc->read_buf[i]);
}
}
return NULL;
}
@ -1473,6 +1482,7 @@ static void figure_mime(httpd_conn *hc)
me_indexes[n_me_indexes] = i;
++n_me_indexes;
}
goto next;
}
}
@ -1514,6 +1524,7 @@ static void figure_mime(httpd_conn *hc)
goto done;
}
}
hc->type = default_type;
done:
@ -1767,6 +1778,7 @@ static void ls_child(int argc, char **argv)
timestr[10] = timestr[14];
timestr[11] = timestr[15];
}
timestr[12] = '\0';
/* The ls -F file class. */
@ -1840,6 +1852,7 @@ static int ls(httpd_conn *hc)
hc->encodedurl);
return -1;
}
#endif
++hc->hs->cgi_count;
@ -1980,6 +1993,7 @@ static int really_check_referer(httpd_conn *hc)
httpd_realloc_str(&refhost, &refhost_size, cp2 - cp1);
for (cp3 = refhost; cp1 < cp2; ++cp1, ++cp3)
{
if (isupper(*cp1))
{
*cp3 = tolower(*cp1);
@ -1988,6 +2002,8 @@ static int really_check_referer(httpd_conn *hc)
{
*cp3 = *cp1;
}
}
*cp3 = '\0';
/* Local pattern? */
@ -2075,6 +2091,7 @@ static size_t sockaddr_len(httpd_sockaddr *saP)
default:
break;
}
return 0;
}
@ -2584,6 +2601,7 @@ int httpd_got_request(httpd_conn *hc)
return GR_BAD_REQUEST;
}
}
return GR_NO_REQUEST;
}
@ -2640,6 +2658,7 @@ int httpd_parse_request(httpd_conn *hc)
}
}
}
hc->protocol = protocol;
/* Check for HTTP/1.1 absolute URL. */
@ -2661,6 +2680,7 @@ int httpd_parse_request(httpd_conn *hc)
httpd_send_err(hc, 400, httpd_err400title, "", httpd_err400form, "");
return -1;
}
*url = '\0';
if (strchr(reqhost, '/') != NULL || reqhost[0] == '.')
@ -2746,6 +2766,7 @@ int httpd_parse_request(httpd_conn *hc)
if (hc->mime_flag)
{
/* Read the MIME headers. */
while ((buf = bufgets(hc)) != NULL)
{
if (buf[0] == '\0')
@ -2795,6 +2816,7 @@ int httpd_parse_request(httpd_conn *hc)
httpd_ntoa(&hc->client_addr));
continue;
}
httpd_realloc_str(&hc->accept, &hc->maxaccept, strlen(hc->accept) + 2 + strlen(cp));
strcat(hc->accept, ", ");
}
@ -2802,6 +2824,7 @@ int httpd_parse_request(httpd_conn *hc)
{
httpd_realloc_str(&hc->accept, &hc->maxaccept, strlen(cp));
}
strcat(hc->accept, cp);
}
else if (strncasecmp(buf, "Accept-Encoding:", 16) == 0)
@ -2816,6 +2839,7 @@ int httpd_parse_request(httpd_conn *hc)
httpd_ntoa(&hc->client_addr));
continue;
}
httpd_realloc_str(&hc->accepte, &hc->maxaccepte, strlen(hc->accepte) + 2 + strlen(cp));
strcat(hc->accepte, ", ");
}
@ -2823,6 +2847,7 @@ int httpd_parse_request(httpd_conn *hc)
{
httpd_realloc_str(&hc->accepte, &hc->maxaccepte, strlen(cp));
}
strcpy(hc->accepte, cp);
}
else if (strncasecmp(buf, "Accept-Language:", 16) == 0)
@ -2849,6 +2874,7 @@ int httpd_parse_request(httpd_conn *hc)
else if (strncasecmp(buf, "Range:", 6) == 0)
{
/* Only support %d- and %d-%d, not %d-%d,%d-%d or -%d. */
if (strchr(buf, ',') == NULL)
{
char *cp_dash;
@ -2995,6 +3021,7 @@ int httpd_parse_request(httpd_conn *hc)
httpd_send_err(hc, 404, err404title, "", err404form, hc->encodedurl);
return -1;
}
#endif
#ifdef CONFIG_THTTPD_TILDE_MAP2
if (!httpd_tilde_map2(hc))
@ -3002,6 +3029,7 @@ int httpd_parse_request(httpd_conn *hc)
httpd_send_err(hc, 404, err404title, "", err404form, hc->encodedurl);
return -1;
}
#endif
}
@ -3123,7 +3151,8 @@ int httpd_start_request(httpd_conn *hc, struct timeval *nowP)
static char *dirname;
static size_t maxdirname = 0;
#endif /* CONFIG_THTTPD_AUTH_FILE */
size_t expnlen, indxlen;
size_t expnlen;
size_t indxlen;
char *cp;
char *pi;
int i;
@ -3231,6 +3260,7 @@ int httpd_start_request(httpd_conn *hc, struct timeval *nowP)
hc->encodedurl);
return -1;
}
# ifdef CONFIG_THTTPD_AUTH_FILE
/* Check authorization for this directory. */
@ -3347,7 +3377,9 @@ int httpd_start_request(httpd_conn *hc, struct timeval *nowP)
/* Referer check. */
if (!check_referer(hc))
{
return -1;
}
/* Is it in the CGI area? */
@ -3403,8 +3435,8 @@ int httpd_start_request(httpd_conn *hc, struct timeval *nowP)
else if (hc->if_modified_since != (time_t) - 1 &&
hc->if_modified_since >= hc->sb.st_mtime)
{
send_mime(hc, 304, err304title, hc->encodings, "", hc->type, (off_t) - 1,
hc->sb.st_mtime);
send_mime(hc, 304, err304title, hc->encodings, "",
hc->type, (off_t) - 1, hc->sb.st_mtime);
}
else
{
@ -3412,9 +3444,11 @@ int httpd_start_request(httpd_conn *hc, struct timeval *nowP)
if (hc->file_fd < 0)
{
INTERNALERROR(hc->expnfilename);
httpd_send_err(hc, 500, err500title, "", err500form, hc->encodedurl);
httpd_send_err(hc, 500, err500title, "", err500form,
hc->encodedurl);
return -1;
}
send_mime(hc, 200, ok200title, hc->encodings, "", hc->type,
hc->sb.st_size, hc->sb.st_mtime);
}
@ -3426,7 +3460,11 @@ char *httpd_ntoa(httpd_sockaddr *saP)
{
#ifdef CONFIG_NET_IPv6
static char str[200];
#else
static char str[INET_ADDRSTRLEN];
#endif
#ifdef CONFIG_NET_IPv6
if (getnameinfo
(&saP->sa, sockaddr_len(saP), str, sizeof(str), 0, 0,
NI_NUMERICHOST) != 0)
@ -3446,7 +3484,7 @@ char *httpd_ntoa(httpd_sockaddr *saP)
#else /* CONFIG_NET_IPv6 */
return inet_ntoa(saP->sin_addr);
return inet_ntoa_r(saP->sin_addr, str, sizeof(str));
#endif /* CONFIG_NET_IPv6 */
}

View File

@ -1130,6 +1130,8 @@ int cmd_arp(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
#endif
if (strcmp(argv[1], "-a") == 0)
{
char hwaddr[20];
if (argc != 3)
{
goto errout_toomany;
@ -1147,7 +1149,7 @@ int cmd_arp(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
goto errout_cmdfaild;
}
nsh_output(vtbl, "HWaddr: %s\n", ether_ntoa(&mac));
nsh_output(vtbl, "HWaddr: %s\n", ether_ntoa_r(&mac, hwaddr));
}
else if (strcmp(argv[1], "-d") == 0)
{

View File

@ -267,6 +267,7 @@ static int wapi_show_cmd(int sock, int argc, FAR char **argv)
int sense;
int chan;
int ret;
char inetaddr[INET_ADDRSTRLEN];
printf("%s Configuration:\n", ifname);
@ -281,7 +282,8 @@ static int wapi_show_cmd(int sock, int argc, FAR char **argv)
}
else
{
printf(" IP: %s\n", inet_ntoa(addr));
printf(" IP: %s\n", inet_ntoa_r(addr, inetaddr,
sizeof(inetaddr)));
}
/* Get netmask */
@ -294,7 +296,8 @@ static int wapi_show_cmd(int sock, int argc, FAR char **argv)
}
else
{
printf(" NetMask: %s\n", inet_ntoa(addr));
printf(" NetMask: %s\n", inet_ntoa_r(addr, inetaddr,
sizeof(inetaddr)));
}
/* Get frequency */
@ -913,6 +916,7 @@ static int wapi_reconnect_cmd(int sock, int argc, FAR char **argv)
static int wapi_save_config_cmd(int sock, int argc, FAR char **argv)
{
char essid[WAPI_ESSID_MAX_SIZE + 1];
char bssid[20];
enum wapi_essid_flag_e essid_flag;
struct wpa_wconfig_s conf;
struct ether_addr ap;
@ -958,7 +962,7 @@ static int wapi_save_config_cmd(int sock, int argc, FAR char **argv)
return ret;
}
conf.bssid = ether_ntoa(&ap);
conf.bssid = ether_ntoa_r(&ap, bssid);
memset(psk, 0, sizeof(psk));
ret = wpa_driver_wext_get_key_ext(sock,