Skip to content

Commit 0b826d9

Browse files
authored
Merge pull request #5 from LinuxJedi/stm32-lwip
STM32 LWIP
2 parents bf43566 + 93acf75 commit 0b826d9

File tree

18 files changed

+1242
-68
lines changed

18 files changed

+1242
-68
lines changed

examples/Linux-LWIP/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,3 @@ sudo docker-compose -f docker-compose.yml down
9696
## Notes
9797

9898
* The `lwip-include/arch` directory is a copy of the lwIP directory from `contrib/ports/unix/port/include/arch`.
99-
* `fflush()` is used after `printf()` because docker-compose buffers `stdout`.

examples/Linux-LWIP/echo.c

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
/* Called by echo_msgrecv() when it effectively gets an EOF */
1919
static void echo_msgclose(struct tcp_pcb *pcb)
2020
{
21-
printf("Closing connection from: %s\n", ipaddr_ntoa(&(pcb->remote_ip)));
22-
fflush(stdout);
21+
fprintf(stderr, "Closing connection from: %s\n", ipaddr_ntoa(&(pcb->remote_ip)));
2322
/* Tell sentry_action() that this is a disconnect event which decrements
2423
* the connection count */
2524
sentry_action(&pcb->local_ip, &pcb->remote_ip, pcb->local_port, pcb->remote_port, SENTRY_ACTION_DISCONNECT);
@@ -42,8 +41,7 @@ static err_t echo_msgrecv(void *arg, struct tcp_pcb *pcb, struct pbuf *p,
4241

4342
for (q = p; q != NULL; q = q->next)
4443
{
45-
printf("Got: %.*s\n", q->len, q->payload);
46-
fflush(stdout);
44+
fprintf(stderr, "Got: %.*s\n", q->len, q->payload);
4745
}
4846
}
4947
else if (err == ERR_OK && p == NULL)
@@ -58,8 +56,7 @@ static err_t echo_msgrecv(void *arg, struct tcp_pcb *pcb, struct pbuf *p,
5856
static void echo_msgerr(void *arg, err_t err)
5957
{
6058
LWIP_DEBUGF(ECHO_DEBUG, ("echo_msgerr: %s (%i)\n", lwip_strerr(err), err));
61-
printf("Err: %s\n", lwip_strerr(err));
62-
fflush(stdout);
59+
fprintf(stderr, "Err: %s\n", lwip_strerr(err));
6360
}
6461

6562
/* TCP accept connection callback handler */
@@ -68,17 +65,15 @@ static err_t echo_msgaccept(void *arg, struct tcp_pcb *pcb, err_t err)
6865
/* Accepted new connection */
6966
LWIP_PLATFORM_DIAG(("echo_msgaccept called\n"));
7067

71-
printf("Connect from: %s port: %d\n", ipaddr_ntoa(&(pcb->remote_ip)), pcb->remote_port);
72-
fflush(stdout);
68+
fprintf(stderr, "Connect from: %s port: %d\n", ipaddr_ntoa(&(pcb->remote_ip)), pcb->remote_port);
7369

7470
/* The below is an alternative hook to check for incoming connections. The
7571
* down side of this is that it will only trigger after the initial SYN/ACK
7672
*/
7773
/*
7874
if (sentry_action(pcb, SENTRY_ACTION_CONNECT) != 0)
7975
{
80-
printf("Sentry rejected connection\n");
81-
fflush(stdout);
76+
fprintf(stderr, "Sentry rejected connection\n");
8277
tcp_abort(pcb);
8378
return ERR_ABRT;
8479
}
@@ -129,15 +124,13 @@ int sentry_tcp_inpkt(struct tcp_pcb *pcb, struct tcp_hdr *hdr, uint16_t optlen,
129124
* yet, that happens immediately after this callback, so we get these
130125
* details from other sources. The same sources that are about to fill
131126
* in the details into the sruct */
132-
printf("Incomming connection from: %s\n",
127+
fprintf(stderr, "Incomming connection from: %s\n",
133128
ipaddr_ntoa(ip_current_src_addr()));
134-
fflush(stdout);
135129
if (sentry_action(ip_current_dest_addr(), ip_current_src_addr(),
136130
pcb->local_port, hdr->src , SENTRY_ACTION_CONNECT) != 0)
137131
{
138-
printf("Sentry rejected connection from: %s\n",
132+
fprintf(stderr, "Sentry rejected connection from: %s\n",
139133
ipaddr_ntoa(ip_current_src_addr()));
140-
fflush(stdout);
141134
return ERR_ABRT;
142135
}
143136
}

examples/Linux-LWIP/main.c

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,15 @@
2323
static err_t pcap_output(struct netif *netif, struct pbuf *p)
2424
{
2525
pcap_t *pcap = netif->state;
26-
//printf("Sending packet with length %d\n", p->tot_len);
26+
//fprintf(stderr, "Sending packet with length %d\n", p->tot_len);
2727

2828
/* Just fire the raw packet data down PCAP */
2929
int r = pcap_sendpacket(pcap, (uint8_t *)p->payload, p->tot_len);
3030

3131
if (r != 0)
3232
{
33-
printf("Error sending packet\n");
34-
printf("Error: %s\n", pcap_geterr(pcap));
35-
fflush(stdout);
33+
fprintf(stderr, "Error sending packet\n");
34+
fprintf(stderr, "Error: %s\n", pcap_geterr(pcap));
3635
return ERR_IF;
3736
}
3837

@@ -49,17 +48,15 @@ static err_t filter_input(struct pbuf *p, struct netif *inp)
4948
/* "src" contains the source hardware address from the packet */
5049
if (sentry_action_mac(ethaddr) != 0)
5150
{
52-
printf("Sentry rejected MAC address %02X:%02X:%02X:%02X:%02X:%02X\n",
51+
fprintf(stderr, "Sentry rejected MAC address %02X:%02X:%02X:%02X:%02X:%02X\n",
5352
ethaddr->addr[0], ethaddr->addr[1], ethaddr->addr[2],
5453
ethaddr->addr[3], ethaddr->addr[4], ethaddr->addr[5]);
55-
fflush(stdout);
5654
/* Basically drop the packet */
5755
return ERR_ABRT;
5856
}
59-
printf("Sentry accepted MAC address %02X:%02X:%02X:%02X:%02X:%02X\n",
57+
fprintf(stderr, "Sentry accepted MAC address %02X:%02X:%02X:%02X:%02X:%02X\n",
6058
ethaddr->addr[0], ethaddr->addr[1], ethaddr->addr[2],
6159
ethaddr->addr[3], ethaddr->addr[4], ethaddr->addr[5]);
62-
fflush(stdout);
6360
/* We passed the MAC filter, so pass the packet to the regular internal
6461
* lwIP input callback */
6562
return netif_input(p, inp);
@@ -113,24 +110,21 @@ int main(size_t argc, char **argv)
113110
/* Initialize wolfSentry */
114111
if (sentry_init() != 0)
115112
{
116-
printf("Sentry init failure\n");
117-
fflush(stdout);
113+
fprintf(stderr, "Sentry init failure\n");
118114
return -1;
119115
}
120116

121117
/* Initialize TCP listener */
122118
if (echo_init() != 0)
123119
{
124-
printf("TCP init failure\n");
125-
fflush(stdout);
120+
fprintf(stderr, "TCP init failure\n");
126121
return -1;
127122
}
128123

129124
/* Initialize ICMP listener */
130125
if (ping_init() != 0)
131126
{
132-
printf("ICMP init failure\n");
133-
fflush(stdout);
127+
fprintf(stderr, "ICMP init failure\n");
134128
return -1;
135129
}
136130

@@ -149,22 +143,20 @@ int main(size_t argc, char **argv)
149143
continue;
150144

151145
case -1:
152-
printf("Error: %s\n", pcap_geterr(pcap));
153-
fflush(stdout);
146+
fprintf(stderr, "Error: %s\n", pcap_geterr(pcap));
154147
continue;
155148

156149
case 1:
157150
break;
158151

159152
default:
160-
printf("Unknown result: %d\n", r);
161-
fflush(stdout);
153+
fprintf(stderr, "Unknown result: %d\n", r);
162154
continue;
163155
}
164156

165157
/* Copy the packet to lwIP's packet buffer and trigger a lwIP packet
166158
* input */
167-
//printf("Packet length: %d / %d\n", hdr->len, hdr->caplen);
159+
//fprintf(stderr, "Packet length: %d / %d\n", hdr->len, hdr->caplen);
168160
struct pbuf *pbuf = pbuf_alloc(PBUF_RAW, hdr->len, PBUF_RAM);
169161
memcpy(pbuf->payload, data, hdr->len);
170162
netif.input(pbuf, &netif);

examples/Linux-LWIP/ping.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,12 @@ static u8_t ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p, const ip_a
2525
{
2626
/* RAW recv needs to free if not returning 0 */
2727
pbuf_free(p);
28-
printf("Ping rejected from %s\n", ipaddr_ntoa(addr));
29-
fflush(stdout);
28+
fprintf(stderr, "Ping rejected from %s\n", ipaddr_ntoa(addr));
3029
return 1;
3130
}
3231
}
3332

34-
printf("Ping accepted from %s\n", ipaddr_ntoa(addr));
35-
fflush(stdout);
33+
fprintf(stderr, "Ping accepted from %s\n", ipaddr_ntoa(addr));
3634
return 0;
3735
}
3836

examples/Linux-LWIP/sentry.c

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static wolfsentry_errcode_t test_action(
3030
(void)route_table;
3131
(void)action_results;
3232

33-
/*printf("action callback: a=\"%s\" parent_event=\"%s\" trigger=\"%s\" t=%u r_id=%u caller_arg=%p\n",
33+
/*fprintf(stderr, "action callback: a=\"%s\" parent_event=\"%s\" trigger=\"%s\" t=%u r_id=%u caller_arg=%p\n",
3434
wolfsentry_action_get_label(action),
3535
wolfsentry_event_get_label(parent_event),
3636
wolfsentry_event_get_label(trigger_event),
@@ -52,8 +52,7 @@ int sentry_init()
5252
if (ret < 0) {
5353
fprintf(stderr, "wolfsentry_init() returned " WOLFSENTRY_ERROR_FMT "\n",
5454
WOLFSENTRY_ERROR_FMT_ARGS(ret));
55-
printf("unable to initialize wolfSentry");
56-
fflush(stdout);
55+
fprintf(stderr, "unable to initialize wolfSentry");
5756
}
5857

5958
/* Insert the possible actions into wolfSentry */
@@ -119,8 +118,7 @@ int sentry_init()
119118

120119
if (f == NULL) {
121120
fprintf(stderr, "fopen(%s): %s\n",wolfsentry_config_path,strerror(errno));
122-
printf("unable to open wolfSentry config file");
123-
fflush(stdout);
121+
fprintf(stderr, "unable to open wolfSentry config file");
124122
}
125123

126124
/* Initalize the wolfSentry JSON parser */
@@ -131,26 +129,23 @@ int sentry_init()
131129
fprintf(stderr, "wolfsentry_config_json_init() returned "
132130
WOLFSENTRY_ERROR_FMT "\n",
133131
WOLFSENTRY_ERROR_FMT_ARGS(ret));
134-
printf("error while initializing wolfSentry config parser");
135-
fflush(stdout);
132+
fprintf(stderr, "error while initializing wolfSentry config parser");
136133
}
137134

138135
/* wolfSentry uses a streaming reader/parser for the config file */
139136
for (;;) {
140137
/* Read some data from the config file */
141138
size_t n = fread(buf, 1, sizeof buf, f);
142139
if ((n < sizeof buf) && ferror(f)) {
143-
fprintf(stderr,"fread(%s): %s\n",wolfsentry_config_path, strerror(errno));
144-
printf("error while reading wolfSentry config file");
145-
fflush(stdout);
140+
fprintf(stderr, "fread(%s): %s\n",wolfsentry_config_path, strerror(errno));
141+
fprintf(stderr, "error while reading wolfSentry config file");
146142
}
147143

148144
/* Send the read data into the JSON parser */
149145
ret = wolfsentry_config_json_feed(jps, buf, n, err_buf, sizeof err_buf);
150146
if (ret < 0) {
151147
fprintf(stderr, "%.*s\n", (int)sizeof err_buf, err_buf);
152-
printf("error while loading wolfSentry config file");
153-
fflush(stdout);
148+
fprintf(stderr, "error while loading wolfSentry config file");
154149
}
155150
if ((n < sizeof buf) && feof(f))
156151
break;
@@ -160,8 +155,7 @@ int sentry_init()
160155
/* Clean up the JSON parser */
161156
if ((ret = wolfsentry_config_json_fini(&jps, err_buf, sizeof err_buf)) < 0) {
162157
fprintf(stderr, "%.*s\n", (int)sizeof err_buf, err_buf);
163-
printf("error while loading wolfSentry config file");
164-
fflush(stdout);
158+
fprintf(stderr, "error while loading wolfSentry config file");
165159
}
166160

167161
return 0;
@@ -225,9 +219,8 @@ int sentry_action(ip_addr_t *local_ip, ip_addr_t *remote_ip, in_port_t local_por
225219
NULL,
226220
&action_results);
227221

228-
printf("TCP Sentry action returned " WOLFSENTRY_ERROR_FMT "\n",
222+
fprintf(stderr, "TCP Sentry action returned " WOLFSENTRY_ERROR_FMT "\n",
229223
WOLFSENTRY_ERROR_FMT_ARGS(ret));
230-
fflush(stdout);
231224

232225
/* Check the result, if it contains "reject" then notify the caller */
233226
if (WOLFSENTRY_ERROR_DECODE_ERROR_CODE(ret) >= 0) {
@@ -270,9 +263,8 @@ int sentry_action_ping(const ip_addr_t *addr, u8_t type)
270263
NULL,
271264
NULL,
272265
&action_results);
273-
printf("PING Sentry action returned " WOLFSENTRY_ERROR_FMT "\n",
266+
fprintf(stderr, "PING Sentry action returned " WOLFSENTRY_ERROR_FMT "\n",
274267
WOLFSENTRY_ERROR_FMT_ARGS(ret));
275-
fflush(stdout);
276268
if (WOLFSENTRY_ERROR_DECODE_ERROR_CODE(ret) >= 0) {
277269
if (WOLFSENTRY_MASKIN_BITS(action_results, WOLFSENTRY_ACTION_RES_REJECT)) {
278270
return -1;
@@ -312,9 +304,8 @@ int sentry_action_mac(struct eth_addr *addr)
312304
NULL,
313305
&action_results);
314306

315-
printf("MAC Sentry action returned " WOLFSENTRY_ERROR_FMT "\n",
307+
fprintf(stderr, "MAC Sentry action returned " WOLFSENTRY_ERROR_FMT "\n",
316308
WOLFSENTRY_ERROR_FMT_ARGS(ret));
317-
fflush(stdout);
318309
if (WOLFSENTRY_ERROR_DECODE_ERROR_CODE(ret) >= 0) {
319310
if (WOLFSENTRY_MASKIN_BITS(action_results, WOLFSENTRY_ACTION_RES_REJECT)) {
320311
return -1;

examples/STM32/Inc/echo.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
3+
#ifndef __ECHO_H__
4+
#define __ECHO_H__
5+
6+
#include <stdint.h>
7+
8+
struct pbuf;
9+
struct tcp_pcb;
10+
struct tcp_hdr;
11+
12+
int echo_init(void);
13+
int sentry_tcp_inpkt(struct tcp_pcb *pcb, struct tcp_hdr *hdr, uint16_t optlen, uint16_t opt1len, uint8_t *opt2, struct pbuf *p);
14+
15+
#endif

examples/STM32/Inc/ping.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
3+
#ifndef __PING_H__
4+
#define __PING_H__
5+
6+
int ping_init(void);
7+
8+
#endif

examples/STM32/Inc/sentry.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
3+
#ifndef __SENTRY_H__
4+
#define __SENTRY_H__
5+
#include "lwip/tcp.h"
6+
#include "lwip/sockets.h"
7+
#include <netif/etharp.h>
8+
#include <stdbool.h>
9+
10+
typedef enum {
11+
SENTRY_ACTION_NONE,
12+
SENTRY_ACTION_CONNECT,
13+
SENTRY_ACTION_DISCONNECT
14+
} sentry_action_type;
15+
16+
int sentry_init(void);
17+
int sentry_action(ip_addr_t *local_ip, ip_addr_t *remote_ip, in_port_t local_port, in_port_t remote_port, sentry_action_type action);
18+
int sentry_action_ping(const ip_addr_t *addr, u8_t type);
19+
int sentry_action_mac(struct eth_addr *addr);
20+
#endif

0 commit comments

Comments
 (0)