net/ip: print ip addresses using ip4_addrN macro

Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
This commit is contained in:
Petro Karashchenko 2023-08-18 22:57:42 +03:00 committed by Xiang Xiao
parent 2b58cc6578
commit 189d0c803f
7 changed files with 111 additions and 126 deletions

View File

@ -33,6 +33,7 @@
#include <net/if.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <nuttx/net/ip.h>
#include "netutils/netlib.h"
@ -327,7 +328,6 @@ static int bridge_net1_worker(int argc, char *argv[])
struct sockaddr_in fromaddr;
struct sockaddr_in toaddr;
socklen_t addrlen;
in_addr_t tmpaddr;
ssize_t nrecvd;
ssize_t nsent;
int optval;
@ -338,13 +338,16 @@ static int bridge_net1_worker(int argc, char *argv[])
/* Create a UDP receive socket on network 1 */
tmpaddr = ntohl(g_net1_ipaddr);
printf("NET1: Create receive socket: %d.%d.%d.%d:%d\n",
(int)(tmpaddr >> 24),
(int)((tmpaddr >> 16) & 0xff),
(int)((tmpaddr >> 8) & 0xff),
(int)(tmpaddr & 0xff),
CONFIG_EXAMPLES_BRIDGE_NET1_RECVPORT);
receiver.sin_family = AF_INET;
receiver.sin_port = htons(CONFIG_EXAMPLES_BRIDGE_NET1_RECVPORT);
receiver.sin_addr.s_addr = g_net1_ipaddr;
printf("NET1: Create receive socket: %u.%u.%u.%u:%u\n",
ip4_addr1(receiver.sin_addr.s_addr),
ip4_addr2(receiver.sin_addr.s_addr),
ip4_addr3(receiver.sin_addr.s_addr),
ip4_addr4(receiver.sin_addr.s_addr),
htons(receiver.sin_port));
recvsd = socket(PF_INET, SOCK_DGRAM, 0);
if (recvsd < 0)
@ -367,10 +370,6 @@ static int bridge_net1_worker(int argc, char *argv[])
/* Bind the socket to a local address */
receiver.sin_family = AF_INET;
receiver.sin_port = HTONS(CONFIG_EXAMPLES_BRIDGE_NET1_RECVPORT);
receiver.sin_addr.s_addr = g_net1_ipaddr;
if (bind(recvsd, (struct sockaddr *)&receiver,
sizeof(struct sockaddr_in)) < 0)
{
@ -380,12 +379,9 @@ static int bridge_net1_worker(int argc, char *argv[])
/* Create a UDP send socket on network 2 */
tmpaddr = ntohl(g_net2_ipaddr);
printf("NET1: Create send socket: %d.%d.%d.%d:INPORT_ANY\n",
(int)(tmpaddr >> 24),
(int)((tmpaddr >> 16) & 0xff),
(int)((tmpaddr >> 8) & 0xff),
(int)(tmpaddr & 0xff));
printf("NET1: Create send socket: %u.%u.%u.%u:INPORT_ANY\n",
ip4_addr1(g_net2_ipaddr), ip4_addr2(g_net2_ipaddr),
ip4_addr3(g_net2_ipaddr), ip4_addr4(g_net2_ipaddr));
sndsd = socket(PF_INET, SOCK_DGRAM, 0);
if (sndsd < 0)
@ -433,13 +429,12 @@ static int bridge_net1_worker(int argc, char *argv[])
CONFIG_EXAMPLES_BRIDGE_NET1_IOBUFIZE, 0,
(FAR struct sockaddr *)&fromaddr, &addrlen);
tmpaddr = ntohl(fromaddr.sin_addr.s_addr);
printf("NET1: Received %ld bytes from %d.%d.%d.%d:%d\n",
printf("NET1: Received %ld bytes from %u.%u.%u.%u:%u\n",
(long)nrecvd,
(int)(tmpaddr >> 24),
(int)((tmpaddr >> 16) & 0xff),
(int)((tmpaddr >> 8) & 0xff),
(int)(tmpaddr & 0xff),
ip4_addr1(fromaddr.sin_addr.s_addr),
ip4_addr2(fromaddr.sin_addr.s_addr),
ip4_addr3(fromaddr.sin_addr.s_addr),
ip4_addr4(fromaddr.sin_addr.s_addr),
ntohs(fromaddr.sin_port));
/* Check for a receive error or zero bytes received. The negative
@ -465,18 +460,18 @@ static int bridge_net1_worker(int argc, char *argv[])
/* Send the newly received packet out network 2 */
printf("NET1: Sending %ld bytes on network 2: %d.%d.%d.%d:%d\n",
(long)nrecvd,
CONFIG_EXAMPLES_BRIDGE_NET2_IPHOST >> 24,
(CONFIG_EXAMPLES_BRIDGE_NET2_IPHOST >> 16) & 0xff,
(CONFIG_EXAMPLES_BRIDGE_NET2_IPHOST >> 8) & 0xff,
CONFIG_EXAMPLES_BRIDGE_NET2_IPHOST & 0xff,
CONFIG_EXAMPLES_BRIDGE_NET2_HOSTPORT);
toaddr.sin_family = AF_INET;
toaddr.sin_port = htons(CONFIG_EXAMPLES_BRIDGE_NET2_HOSTPORT);
toaddr.sin_addr.s_addr = htonl(CONFIG_EXAMPLES_BRIDGE_NET2_IPHOST);
printf("NET1: Sending %ld bytes on network 2: %u.%u.%u.%u:%u\n",
(long)nrecvd,
ip4_addr1(toaddr.sin_addr.s_addr),
ip4_addr2(toaddr.sin_addr.s_addr),
ip4_addr3(toaddr.sin_addr.s_addr),
ip4_addr4(toaddr.sin_addr.s_addr),
htons(toaddr.sin_port));
nsent = sendto(sndsd, g_net1_buffer, nrecvd, 0,
(struct sockaddr *)&toaddr,
sizeof(struct sockaddr_in));
@ -518,7 +513,6 @@ static int bridge_net2_worker(int argc, char *argv[])
struct sockaddr_in fromaddr;
struct sockaddr_in toaddr;
socklen_t addrlen;
in_addr_t tmpaddr;
ssize_t nrecvd;
ssize_t nsent;
int optval;
@ -529,13 +523,16 @@ static int bridge_net2_worker(int argc, char *argv[])
/* Create a UDP receive socket on network 2 */
tmpaddr = ntohl(g_net2_ipaddr);
printf("NET2: Create receive socket: %d.%d.%d.%d:%d\n",
(int)(tmpaddr >> 24),
(int)((tmpaddr >> 16) & 0xff),
(int)((tmpaddr >> 8) & 0xff),
(int)(tmpaddr & 0xff),
CONFIG_EXAMPLES_BRIDGE_NET2_RECVPORT);
receiver.sin_family = AF_INET;
receiver.sin_port = htons(CONFIG_EXAMPLES_BRIDGE_NET2_RECVPORT);
receiver.sin_addr.s_addr = g_net2_ipaddr;
printf("NET2: Create receive socket: %u.%u.%u,%u:%u\n",
ip4_addr1(receiver.sin_addr.s_addr),
ip4_addr2(receiver.sin_addr.s_addr),
ip4_addr3(receiver.sin_addr.s_addr),
ip4_addr4(receiver.sin_addr.s_addr),
htons(receiver.sin_port));
recvsd = socket(PF_INET, SOCK_DGRAM, 0);
if (recvsd < 0)
@ -558,10 +555,6 @@ static int bridge_net2_worker(int argc, char *argv[])
/* Bind the socket to a local address */
receiver.sin_family = AF_INET;
receiver.sin_port = HTONS(CONFIG_EXAMPLES_BRIDGE_NET2_RECVPORT);
receiver.sin_addr.s_addr = g_net2_ipaddr;
if (bind(recvsd, (struct sockaddr *)&receiver,
sizeof(struct sockaddr_in)) < 0)
{
@ -571,12 +564,9 @@ static int bridge_net2_worker(int argc, char *argv[])
/* Create a UDP send socket on network 1 */
tmpaddr = ntohl(g_net1_ipaddr);
printf("NET2: Create send socket: %d.%d.%d.%d:INPORT_ANY\n",
(int)(tmpaddr >> 24),
(int)((tmpaddr >> 16) & 0xff),
(int)((tmpaddr >> 8) & 0xff),
(int)(tmpaddr & 0xff));
printf("NET2: Create send socket: %u.%u.%u.%u:INPORT_ANY\n",
ip4_addr1(g_net1_ipaddr), ip4_addr2(g_net1_ipaddr),
ip4_addr3(g_net1_ipaddr), ip4_addr4(g_net1_ipaddr));
sndsd = socket(PF_INET, SOCK_DGRAM, 0);
if (sndsd < 0)
@ -624,13 +614,12 @@ static int bridge_net2_worker(int argc, char *argv[])
CONFIG_EXAMPLES_BRIDGE_NET2_IOBUFIZE, 0,
(FAR struct sockaddr *)&fromaddr, &addrlen);
tmpaddr = ntohl(fromaddr.sin_addr.s_addr);
printf("NET2: Received %ld bytes from %d.%d.%d.%d:%d\n",
printf("NET2: Received %ld bytes from %u.%u.%u.%u:%u\n",
(long)nrecvd,
(int)(tmpaddr >> 24),
(int)((tmpaddr >> 16) & 0xff),
(int)((tmpaddr >> 8) & 0xff),
(int)(tmpaddr & 0xff),
ip4_addr1(fromaddr.sin_addr.s_addr),
ip4_addr2(fromaddr.sin_addr.s_addr),
ip4_addr3(fromaddr.sin_addr.s_addr),
ip4_addr4(fromaddr.sin_addr.s_addr),
ntohs(fromaddr.sin_port));
/* Check for a receive error or zero bytes received. The negative
@ -656,18 +645,18 @@ static int bridge_net2_worker(int argc, char *argv[])
/* Send the newly received packet out network 1 */
printf("NET2: Sending %ld bytes on network 1: %d.%d.%d.%d:%d\n",
(long)nrecvd,
CONFIG_EXAMPLES_BRIDGE_NET1_IPHOST >> 24,
(CONFIG_EXAMPLES_BRIDGE_NET1_IPHOST >> 16) & 0xff,
(CONFIG_EXAMPLES_BRIDGE_NET1_IPHOST >> 8) & 0xff,
CONFIG_EXAMPLES_BRIDGE_NET1_IPHOST & 0xff,
CONFIG_EXAMPLES_BRIDGE_NET1_HOSTPORT);
toaddr.sin_family = AF_INET;
toaddr.sin_port = htons(CONFIG_EXAMPLES_BRIDGE_NET1_HOSTPORT);
toaddr.sin_addr.s_addr = htonl(CONFIG_EXAMPLES_BRIDGE_NET1_IPHOST);
printf("NET2: Sending %ld bytes on network 1: %u.%u.%u.%u:%u\n",
(long)nrecvd,
ip4_addr1(toaddr.sin_addr.s_addr),
ip4_addr2(toaddr.sin_addr.s_addr),
ip4_addr3(toaddr.sin_addr.s_addr),
ip4_addr4(toaddr.sin_addr.s_addr),
htons(toaddr.sin_port));
nsent = sendto(sndsd, g_net2_buffer, nrecvd, 0,
(struct sockaddr *)&toaddr, sizeof(struct sockaddr_in));

View File

@ -63,7 +63,6 @@ int main(int argc, char *argv[])
struct sockaddr_in fromaddr;
struct sockaddr_in toaddr;
socklen_t addrlen;
in_addr_t tmpaddr;
ssize_t nrecvd;
ssize_t nsent;
int optval;
@ -175,17 +174,15 @@ int main(int argc, char *argv[])
/* Read a packet */
printf(LABEL "Receiving up to %d bytes\n", EXAMPLES_BRIDGE_SEND_IOBUFIZE);
printf(LABEL "Receiving up to %d bytes\n", EXAMPLES_BRIDGE_SEND_IOBUFIZE);
addrlen = sizeof(struct sockaddr_in);
nrecvd = recvfrom(recvsd, g_rdbuffer, EXAMPLES_BRIDGE_SEND_IOBUFIZE, 0,
(struct sockaddr *)&fromaddr, &addrlen);
tmpaddr = ntohl(fromaddr.sin_addr.s_addr);
printf(LABEL "Received %ld bytes from %d.%d.%d.%d:%d\n",
printf(LABEL "Received %ld bytes from %s:%u\n",
(long)nrecvd,
tmpaddr >> 24, (tmpaddr >> 16) & 0xff,
(tmpaddr >> 8) & 0xff, tmpaddr & 0xff,
inet_ntoa(fromaddr.sin_addr),
ntohs(fromaddr.sin_port));
/* Check for a receive error or zero bytes received. The negative

View File

@ -79,7 +79,7 @@ void udp_server(void)
#else
struct sockaddr_in server;
struct sockaddr_in client;
in_addr_t tmpaddr;
char ip_str[INET_ADDRSTRLEN];
#endif
unsigned char inbuf[1024];
socklen_t addrlen;
@ -167,11 +167,10 @@ void udp_server(void)
client.sin6_addr.s6_addr[14], client.sin6_addr.s6_addr[15],
ntohs(client.sin6_port));
#else
tmpaddr = ntohl(client.sin_addr.s_addr);
printf("server: %d. Received %d bytes from %u.%u.%u.%u:%d\n",
printf("server: %d. Received %d bytes from %s:%u\n",
offset, nbytes,
(uint8_t)(tmpaddr >> 24), (uint8_t)((tmpaddr >> 16) & 0xff),
(uint8_t)((tmpaddr >> 8) & 0xff), (uint8_t)(tmpaddr & 0xff),
inet_ntop(AF_INET, &client.sin_addr.s_addr,
ip_str, sizeof(ip_str)),
ntohs(client.sin_port));
#endif
if (nbytes < 0)

View File

@ -57,6 +57,7 @@
#include <arpa/inet.h>
#include <netinet/udp.h>
#include <nuttx/net/ip.h>
#include "netutils/dhcpc.h"
#include "netutils/netlib.h"
@ -907,26 +908,26 @@ int dhcpc_request(FAR void *handle, FAR struct dhcpc_state *presult)
return ERROR;
}
ninfo("Got IP address %d.%d.%d.%d\n",
(int)((presult->ipaddr.s_addr) & 0xff),
(int)((presult->ipaddr.s_addr >> 8) & 0xff),
(int)((presult->ipaddr.s_addr >> 16) & 0xff),
(int)((presult->ipaddr.s_addr >> 24) & 0xff));
ninfo("Got netmask %d.%d.%d.%d\n",
(int)((presult->netmask.s_addr) & 0xff),
(int)((presult->netmask.s_addr >> 8) & 0xff),
(int)((presult->netmask.s_addr >> 16) & 0xff),
(int)((presult->netmask.s_addr >> 24) & 0xff));
ninfo("Got DNS server %d.%d.%d.%d\n",
(int)((presult->dnsaddr.s_addr) & 0xff),
(int)((presult->dnsaddr.s_addr >> 8) & 0xff),
(int)((presult->dnsaddr.s_addr >> 16) & 0xff),
(int)((presult->dnsaddr.s_addr >> 24) & 0xff));
ninfo("Got default router %d.%d.%d.%d\n",
(int)((presult->default_router.s_addr) & 0xff),
(int)((presult->default_router.s_addr >> 8) & 0xff),
(int)((presult->default_router.s_addr >> 16) & 0xff),
(int)((presult->default_router.s_addr >> 24) & 0xff));
ninfo("Got IP address %u.%u.%u.%u\n",
ip4_addr1(presult->ipaddr.s_addr),
ip4_addr2(presult->ipaddr.s_addr),
ip4_addr3(presult->ipaddr.s_addr),
ip4_addr4(presult->ipaddr.s_addr));
ninfo("Got netmask %u.%u.%u.%u\n",
ip4_addr1(presult->netmask.s_addr),
ip4_addr2(presult->netmask.s_addr),
ip4_addr3(presult->netmask.s_addr),
ip4_addr4(presult->netmask.s_addr));
ninfo("Got DNS server %u.%u.%u.%u\n",
ip4_addr1(presult->dnsaddr.s_addr),
ip4_addr2(presult->dnsaddr.s_addr),
ip4_addr3(presult->dnsaddr.s_addr),
ip4_addr4(presult->dnsaddr.s_addr));
ninfo("Got default router %u.%u.%u.%u\n",
ip4_addr1(presult->default_router.s_addr),
ip4_addr2(presult->default_router.s_addr),
ip4_addr3(presult->default_router.s_addr),
ip4_addr4(presult->default_router.s_addr));
ninfo("Lease expires in %" PRId32 " seconds\n", presult->lease_time);
return OK;
}

View File

@ -42,6 +42,7 @@
#include <netdb.h>
#include <arpa/inet.h>
#include <nuttx/net/ip.h>
#include "netutils/esp8266.h"
@ -54,15 +55,15 @@
#define NETAPP_IPCONFIG_MAC_OFFSET (20)
#ifndef CONFIG_NETUTILS_ESP8266_MAXTXLEN
# define CONFIG_NETUTILS_ESP8266_MAXTXLEN 256
# define CONFIG_NETUTILS_ESP8266_MAXTXLEN 256
#endif
#ifndef CONFIG_NETUTILS_ESP8266_MAXRXLEN
# define CONFIG_NETUTILS_ESP8266_MAXRXLEN 256
# define CONFIG_NETUTILS_ESP8266_MAXRXLEN 256
#endif
#if (CONFIG_NETUTILS_ESP8266_MAXRXLEN < CONFIG_NETUTILS_ESP8266_WORKER_BUF_LEN)
# error "CONFIG_NETUTILS_ESP8266_WORKER_BUF_LEN would be bigger than CONFIG_NETUTILS_ESP8266_MAXRXLEN"
# error "CONFIG_NETUTILS_ESP8266_WORKER_BUF_LEN would be bigger than CONFIG_NETUTILS_ESP8266_MAXRXLEN"
#endif
#define BUF_CMD_LEN CONFIG_NETUTILS_ESP8266_MAXTXLEN
@ -1836,19 +1837,15 @@ int lesp_set_net(lesp_mode_t mode, in_addr_t ip,
if (ret >= 0)
{
ret = lesp_ask_ans_ok(LESP_TIMEOUT_MS, "AT+CIP%s_CUR=\"%d.%d.%d.%d\","
"\"%d.%d.%d.%d\",\"%d.%d.%d.%d\"\r\n",
ret = lesp_ask_ans_ok(LESP_TIMEOUT_MS, "AT+CIP%s_CUR=\"%u.%u.%u.%u\","
"\"%u.%u.%u.%u\",\"%u.%u.%u.%u\"\r\n",
(mode == LESP_MODE_STATION) ? "STA" : "AP",
*((uint8_t *)&(ip)+0), *((uint8_t *)&(ip)+1),
*((uint8_t *)&(ip)+2), *((uint8_t *)&(ip)+3),
*((uint8_t *)&(gateway)+0),
*((uint8_t *)&(gateway)+1),
*((uint8_t *)&(gateway)+2),
*((uint8_t *)&(gateway)+3),
*((uint8_t *)&(mask)+0),
*((uint8_t *)&(mask)+1),
*((uint8_t *)&(mask)+2),
*((uint8_t *)&(mask)+3));
ip4_addr1(ip), ip4_addr2(ip),
ip4_addr3(ip), ip4_addr4(ip),
ip4_addr1(gateway), ip4_addr2(gateway),
ip4_addr3(gateway), ip4_addr4(gateway),
ip4_addr1(mask), ip4_addr2(mask),
ip4_addr3(mask), ip4_addr4(mask));
}
pthread_mutex_unlock(&g_lesp_state.mutex);
@ -2337,9 +2334,9 @@ int lesp_connect(int sockfd, FAR const struct sockaddr *addr,
if (ret >= 0)
{
ret = lesp_ask_ans_ok(LESP_TIMEOUT_MS, "AT+CIPSTART=%d,\"%s\","
"\"%d.%d.%d.%d\",%d\r\n", sockfd, proto_str,
*((uint8_t *)&ip + 0), *((uint8_t *)&ip +1),
*((uint8_t *)&ip + 2), *((uint8_t *)&ip + 3),
"\"%u.%u.%u.%u\",%d\r\n", sockfd, proto_str,
ip4_addr1(ip), ip4_addr2(ip),
ip4_addr3(ip), ip4_addr4(ip),
port);
if (ret < 0)
{

View File

@ -88,7 +88,7 @@ static const uint8_t g_ipcplist[] =
void printip(struct in_addr ip2)
{
FAR unt8_t *ip = (FAR uint8_t *)&ip2.s_addr;
DEBUG1((" %d.%d.%d.%d ", ip[0], ip[1], ip[2], ip[3]));
DEBUG1((" %u.%u.%u.%u ", ip[0], ip[1], ip[2], ip[3]));
}
#else
# define printip(x)

View File

@ -33,6 +33,8 @@
#include <limits.h>
#include <fixedmath.h>
#include <nuttx/net/ip.h>
#include "netutils/icmp_ping.h"
/****************************************************************************
@ -130,10 +132,10 @@ static void ping_result(FAR const struct ping_result_s *result)
case ICMP_I_BEGIN:
printf("PING %u.%u.%u.%u %u bytes of data\n",
(unsigned int)(result->dest.s_addr) & 0xff,
(unsigned int)(result->dest.s_addr >> 8) & 0xff,
(unsigned int)(result->dest.s_addr >> 16) & 0xff,
(unsigned int)(result->dest.s_addr >> 24) & 0xff,
ip4_addr1(result->dest.s_addr),
ip4_addr2(result->dest.s_addr),
ip4_addr3(result->dest.s_addr),
ip4_addr4(result->dest.s_addr),
result->info->datalen);
break;
@ -153,10 +155,10 @@ static void ping_result(FAR const struct ping_result_s *result)
case ICMP_W_TIMEOUT:
printf("No response from %u.%u.%u.%u: icmp_seq=%u time=%ld ms\n",
(unsigned int)(result->dest.s_addr) & 0xff,
(unsigned int)(result->dest.s_addr >> 8) & 0xff,
(unsigned int)(result->dest.s_addr >> 16) & 0xff,
(unsigned int)(result->dest.s_addr >> 24) & 0xff,
ip4_addr1(result->dest.s_addr),
ip4_addr2(result->dest.s_addr),
ip4_addr3(result->dest.s_addr),
ip4_addr4(result->dest.s_addr),
result->seqno, result->extra);
break;
@ -201,10 +203,10 @@ static void ping_result(FAR const struct ping_result_s *result)
printf("%u bytes from %u.%u.%u.%u: icmp_seq=%u time=%ld.%ld ms\n",
result->info->datalen,
(unsigned int)(result->dest.s_addr) & 0xff,
(unsigned int)(result->dest.s_addr >> 8) & 0xff,
(unsigned int)(result->dest.s_addr >> 16) & 0xff,
(unsigned int)(result->dest.s_addr >> 24) & 0xff,
ip4_addr1(result->dest.s_addr),
ip4_addr2(result->dest.s_addr),
ip4_addr3(result->dest.s_addr),
ip4_addr4(result->dest.s_addr),
result->seqno, result->extra / USEC_PER_MSEC,
result->extra % USEC_PER_MSEC / MSEC_PER_DSEC);
break;