Skip to content

Commit d4b4452

Browse files
committed
cleanup: use inline iptoa, and clean up formatting.
1 parent 1f530da commit d4b4452

File tree

3 files changed

+75
-67
lines changed

3 files changed

+75
-67
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ LDFLAGS+=-pthread
77
# CFLAGS+=-DDEBUG_TAP
88
# print ethernet headers
99
# CFLAGS+=-DDEBUG_ETH
10-
# print tcp headers
10+
# print ip headers
1111
# CFLAGS+=-DDEBUG_IP
12-
# print tcp headers
12+
# print tcp headers
1313
# CFLAGS+=-DDEBUG_TCP
1414

1515
CPPCHECK=cppcheck

src/wolfip.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@
2424
#include "wolfip.h"
2525
#include "config.h"
2626

27-
#ifdef DEBUG_TCP
28-
#include <inttypes.h>
29-
#include <arpa/inet.h>
30-
#endif
31-
3227
/* Fixed size binary heap: each element is a timer. */
3328
#define MAX_TIMERS MAX_TCPSOCKETS * 3
3429

@@ -804,10 +799,10 @@ static void wolfIP_print_tcp(struct wolfIP_tcp_seg * tcp)
804799
printf("| %8d | %8d | (sport, dport)\n",
805800
ee16(tcp->src_port), ee16(tcp->dst_port));
806801
printf("+-----------------------------+\n");
807-
printf("| %10" PRIu32 " | %10" PRIu32 " | (raw seq, raw ack)\n",
802+
printf("| %10u | %10u | (raw seq, raw ack)\n",
808803
ee32(tcp->seq), ee32(tcp->ack));
809804
printf("+-----------------------------+\n");
810-
printf("| %2" PRIu32 " | %d %d %d %d %d %d | %5d | (hdrlen, flags, win)\n",
805+
printf("| %2u | %d %d %d %d %d %d | %5d | (hdrlen, flags, win)\n",
811806
tcp->hlen >> 2,
812807
tcp->flags >> 5 & 1, tcp->flags >> 4 & 1, tcp->flags >> 3 & 1,
813808
tcp->flags >> 2 & 1, tcp->flags >> 1 & 1, tcp->flags >> 0 & 1,
@@ -2009,6 +2004,13 @@ void wolfIP_init_static(struct wolfIP **s)
20092004
#ifdef DEBUG_IP
20102005
static void wolfIP_print_ip(struct wolfIP_ip_packet * ip)
20112006
{
2007+
char src[32];
2008+
char dst[32];
2009+
memset(src, 0, sizeof(src));
2010+
memset(dst, 0, sizeof(dst));
2011+
iptoa(ee32(ip->src), src);
2012+
iptoa(ee32(ip->dst), dst);
2013+
20122014
printf("ip hdr:\n");
20132015
printf("+-----------------------------+\n");
20142016
printf("| 0x%02x | 0x%02x | 0x%02x | %4d | (ipv, hdr_len, tos, ip_len)\n",
@@ -2020,13 +2022,9 @@ static void wolfIP_print_ip(struct wolfIP_ip_packet * ip)
20202022
printf("| %3d | 0x%02x | 0x%04x | (ttl, proto, chksum)\n",
20212023
ip->ttl, ip->proto, ee16(ip->csum));
20222024
printf("+-----------------------------+\n");
2023-
printf("| %3d.%3d.%3d.%3d | (src)\n",
2024-
ee32(ip->src) >> (3 * 8) & 0xff, ee32(ip->src) >> (2 * 8) & 0xff,
2025-
ee32(ip->src) >> (1 * 8) & 0xff, ee32(ip->src) & 0xff);
2025+
printf("| %15s | (src)\n", src);
20262026
printf("+-----------------------------+\n");
2027-
printf("| %3d.%3d.%3d.%3d | (dst)\n",
2028-
ee32(ip->dst) >> (3 * 8) & 0xff, ee32(ip->dst) >> (2 * 8) & 0xff,
2029-
ee32(ip->dst) >> (1 * 8) & 0xff, ee32(ip->dst) & 0xff);
2027+
printf("| %15s | (dst)\n", dst);
20302028
printf("+-----------------------------+\n");
20312029
printf("\n");
20322030
}

wolfip.h

Lines changed: 62 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ typedef uint32_t ip4;
1414

1515

1616
#ifdef DEBUG
17-
#include <stdio.h>
18-
#define LOG(fmt, ...) printf(fmt, ##__VA_ARGS__)
19-
#else
20-
#define LOG(fmt, ...) do{}while(0)
21-
#endif
17+
#include <stdio.h>
18+
#define LOG(fmt, ...) printf(fmt, ##__VA_ARGS__)
19+
#else
20+
#define LOG(fmt, ...) do{}while(0)
21+
#endif /* DEBUG */
2222

2323
/* Device driver interface */
2424
/* Struct to contain a hw device description */
@@ -43,55 +43,63 @@ struct ipconf {
4343
#define MARK_TCP_SOCKET 0x100 /* Mark a socket as TCP */
4444
#define MARK_UDP_SOCKET 0x200 /* Mark a socket as UDP */
4545
#if (MARK_TCP_SOCKET >= MARK_UDP_SOCKET)
46-
#error "MARK_TCP_SOCKET must be less than MARK_UDP_SOCKET"
47-
#endif
46+
#error "MARK_TCP_SOCKET must be less than MARK_UDP_SOCKET"
47+
#endif /* MARK_TCP_SOCKET >= MARK_UDP_SOCKET */
4848

4949

5050
#ifndef WOLF_POSIX
51-
#define IPSTACK_SOCK_STREAM 1
52-
#define IPSTACK_SOCK_DGRAM 2
53-
54-
55-
struct wolfIP_sockaddr_in {
56-
uint16_t sin_family;
57-
uint16_t sin_port;
58-
struct sin_addr { uint32_t s_addr; } sin_addr;
59-
};
60-
struct wolfIP_sockaddr { uint16_t sa_family; };
61-
typedef uint32_t socklen_t;
62-
#ifndef AF_INET
63-
#define AF_INET 2
64-
#endif
51+
#define IPSTACK_SOCK_STREAM 1
52+
#define IPSTACK_SOCK_DGRAM 2
53+
54+
struct wolfIP_sockaddr_in {
55+
uint16_t sin_family;
56+
uint16_t sin_port;
57+
struct sin_addr { uint32_t s_addr; } sin_addr;
58+
};
59+
struct wolfIP_sockaddr { uint16_t sa_family; };
60+
typedef uint32_t socklen_t;
61+
#ifndef AF_INET
62+
#define AF_INET 2
63+
#endif /* !AF_INET */
6564
#else
66-
#include <sys/socket.h>
67-
#include <netinet/in.h>
68-
#include <arpa/inet.h>
69-
#include <unistd.h>
70-
#define wolfIP_sockaddr_in sockaddr_in
71-
#define wolfIP_sockaddr sockaddr
72-
#endif
65+
#include <sys/socket.h>
66+
#include <netinet/in.h>
67+
#include <arpa/inet.h>
68+
#include <unistd.h>
69+
#define wolfIP_sockaddr_in sockaddr_in
70+
#define wolfIP_sockaddr sockaddr
71+
#endif /* !WOLF_POSIX */
7372

7473
int wolfIP_sock_socket(struct wolfIP *s, int domain, int type, int protocol);
75-
int wolfIP_sock_bind(struct wolfIP *s, int sockfd, const struct wolfIP_sockaddr *addr, socklen_t addrlen);
74+
int wolfIP_sock_bind(struct wolfIP *s, int sockfd,
75+
const struct wolfIP_sockaddr *addr, socklen_t addrlen);
7676
int wolfIP_sock_listen(struct wolfIP *s, int sockfd, int backlog);
77-
int wolfIP_sock_accept(struct wolfIP *s, int sockfd, struct wolfIP_sockaddr *addr, socklen_t *addrlen);
78-
int wolfIP_sock_connect(struct wolfIP *s, int sockfd, const struct wolfIP_sockaddr *addr, socklen_t addrlen);
79-
int wolfIP_sock_sendto(struct wolfIP *s, int sockfd, const void *buf, size_t len, int flags, const struct wolfIP_sockaddr *dest_addr, socklen_t addrlen);
80-
int wolfIP_sock_send(struct wolfIP *s, int sockfd, const void *buf, size_t len, int flags);
77+
int wolfIP_sock_accept(struct wolfIP *s, int sockfd,
78+
struct wolfIP_sockaddr *addr, socklen_t *addrlen);
79+
int wolfIP_sock_connect(struct wolfIP *s, int sockfd,
80+
const struct wolfIP_sockaddr *addr, socklen_t addrlen);
81+
int wolfIP_sock_sendto(struct wolfIP *s, int sockfd, const void *buf,
82+
size_t len, int flags, const struct wolfIP_sockaddr *dest_addr,
83+
socklen_t addrlen);
84+
int wolfIP_sock_send(struct wolfIP *s, int sockfd, const void *buf, size_t len,
85+
int flags);
8186
int wolfIP_sock_write(struct wolfIP *s, int sockfd, const void *buf, size_t len);
82-
int wolfIP_sock_recvfrom(struct wolfIP *s, int sockfd, void *buf, size_t len, int flags, struct wolfIP_sockaddr *src_addr, socklen_t *addrlen);
87+
int wolfIP_sock_recvfrom(struct wolfIP *s, int sockfd, void *buf, size_t len,
88+
int flags, struct wolfIP_sockaddr *src_addr, socklen_t *addrlen);
8389
int wolfIP_sock_recv(struct wolfIP *s, int sockfd, void *buf, size_t len, int flags);
8490
int wolfIP_sock_read(struct wolfIP *s, int sockfd, void *buf, size_t len);
8591
int wolfIP_sock_close(struct wolfIP *s, int sockfd);
86-
int wolfIP_sock_getpeername(struct wolfIP *s, int sockfd, struct wolfIP_sockaddr *addr, const socklen_t *addrlen);
87-
int wolfIP_sock_getsockname(struct wolfIP *s, int sockfd, struct wolfIP_sockaddr *addr, const socklen_t *addrlen);
92+
int wolfIP_sock_getpeername(struct wolfIP *s, int sockfd, struct wolfIP_sockaddr *addr,
93+
const socklen_t *addrlen);
94+
int wolfIP_sock_getsockname(struct wolfIP *s, int sockfd, struct wolfIP_sockaddr *addr,
95+
const socklen_t *addrlen);
8896

8997
int dhcp_client_init(struct wolfIP *s);
9098
int dhcp_bound(struct wolfIP *s);
9199

92100
/* DNS client */
93-
94-
int nslookup(struct wolfIP *s, const char *name, uint16_t *id, void (*lookup_cb)(uint32_t ip));
101+
int nslookup(struct wolfIP *s, const char *name, uint16_t *id,
102+
void (*lookup_cb)(uint32_t ip));
95103

96104
/* IP stack interface */
97105
void wolfIP_init(struct wolfIP *s);
@@ -108,7 +116,9 @@ struct ll *wolfIP_getdev(struct wolfIP *s);
108116
#define CB_EVENT_TIMEOUT 0x02 /* Timeout */
109117
#define CB_EVENT_WRITABLE 0x04 /* Connected or space available to send */
110118
#define CB_EVENT_CLOSED 0x10 /* Connection closed by peer */
111-
void wolfIP_register_callback(struct wolfIP *s, int sock_fd, void (*cb)(int sock_fd, uint16_t events, void *arg), void *arg);
119+
void wolfIP_register_callback(struct wolfIP *s, int sock_fd,
120+
void (*cb)(int sock_fd, uint16_t events, void *arg),
121+
void *arg);
112122

113123
/* External requirements */
114124
uint32_t wolfIP_getrandom(void);
@@ -153,16 +163,16 @@ static inline void iptoa(ip4 ip, char *buf)
153163
}
154164

155165
#ifdef WOLFSSL_WOLFIP
156-
#ifdef WOLFSSL_USER_SETTINGS
157-
#include "user_settings.h"
158-
#else
159-
#include <wolfssl/options.h>
160-
#endif
161-
#include <wolfssl/wolfcrypt/settings.h>
162-
#include <wolfssl/ssl.h>
163-
/* Defined in wolfssl_io.c */
164-
int wolfSSL_SetIO_FT(WOLFSSL* ssl, int fd);
165-
int wolfSSL_SetIO_FT_CTX(WOLFSSL_CTX *ctx, struct wolfIP *s);
166-
#endif
167-
168-
#endif
166+
#ifdef WOLFSSL_USER_SETTINGS
167+
#include "user_settings.h"
168+
#else
169+
#include <wolfssl/options.h>
170+
#endif /* WOLFSSL_USER_SETTINGS */
171+
#include <wolfssl/wolfcrypt/settings.h>
172+
#include <wolfssl/ssl.h>
173+
/* Defined in wolfssl_io.c */
174+
int wolfSSL_SetIO_FT(WOLFSSL* ssl, int fd);
175+
int wolfSSL_SetIO_FT_CTX(WOLFSSL_CTX *ctx, struct wolfIP *s);
176+
#endif /* WOLFSSL_WOLFIP */
177+
178+
#endif /* !WOLFIP_H */

0 commit comments

Comments
 (0)