Skip to content

Commit f2a60a4

Browse files
jmnlcruzAnas Nashif
authored andcommitted
sample: net: mbedtls ssl: SSL client sample is adapted to TCP API
Jira: ZEP-1798 Change-Id: I790cd49cb7cc5ed207141ca6634999d77d6a19bb Signed-off-by: Juan Manuel Cruz Alcaraz <[email protected]> (cherry picked from commit c200676d9f7090b22580286b5413ff07473fa4fe)
1 parent 41de428 commit f2a60a4

File tree

4 files changed

+159
-86
lines changed

4 files changed

+159
-86
lines changed

samples/net/mbedtls_sslclient/src/mini_client.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ void tls_client(void)
163163
int ret = exit_ok;
164164
struct tcp_context ctx;
165165

166+
ctx.timeout = MBEDTLS_NETWORK_TIMEOUT;
167+
166168
#if defined(MBEDTLS_X509_CRT_PARSE_C)
167169
mbedtls_x509_crt ca;
168170
#endif
@@ -258,11 +260,14 @@ void tls_client(void)
258260
* 1. Start the connection
259261
*/
260262

261-
mbedtls_printf(" . Connecting to tcp %d.%d.%d.%d:%d...",
262-
SERVER_IPADDR0, SERVER_IPADDR1, SERVER_IPADDR2,
263-
SERVER_IPADDR3, SERVER_PORT);
263+
mbedtls_printf(" . Connecting to tcp %s...", SERVER_ADDR);
264+
265+
if (tcp_set_local_addr(&ctx, LOCAL_ADDR) != 0) {
266+
printk("tcp set_local_addr error\n");
267+
goto exit;
268+
}
264269

265-
if (tcp_init(&ctx) != 0) {
270+
if (tcp_init(&ctx, SERVER_ADDR, SERVER_PORT) != 0) {
266271
ret = connect_failed;
267272
mbedtls_printf(" failed\n ! tcp_init returned -0x%x\n\n",
268273
-ret);

samples/net/mbedtls_sslclient/src/tcp.c

Lines changed: 121 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,12 @@
2424

2525
#include "mbedtls/ssl.h"
2626

27-
#define CLIENT_IP_ADDR { { { CLIENT_IPADDR0, CLIENT_IPADDR1, \
28-
CLIENT_IPADDR2, CLIENT_IPADDR3 } } }
29-
30-
#define SERVER_IP_ADDR { { { SERVER_IPADDR0, SERVER_IPADDR1, \
31-
SERVER_IPADDR2, SERVER_IPADDR3 } } }
32-
3327
#define INET_FAMILY AF_INET
3428

35-
static struct in_addr client_addr = CLIENT_IP_ADDR;
36-
static struct in_addr server_addr = SERVER_IP_ADDR;
29+
#define TCP_BUF_CTR 5
30+
#define TCP_BUF_SIZE 1024
31+
32+
NET_BUF_POOL_DEFINE(tcp_msg_pool, TCP_BUF_CTR, TCP_BUF_SIZE, 0, NULL);
3733

3834
static void tcp_received(struct net_context *context,
3935
struct net_buf *buf, int status, void *user_data)
@@ -49,7 +45,6 @@ int tcp_tx(void *context, const unsigned char *buf, size_t size)
4945
struct tcp_context *ctx = context;
5046
struct net_context *tcp_ctx;
5147
struct net_buf *send_buf;
52-
struct sockaddr_in dst_addr;
5348
int rc, len;
5449

5550
tcp_ctx = ctx->net_ctx;
@@ -66,16 +61,9 @@ int tcp_tx(void *context, const unsigned char *buf, size_t size)
6661
return -EIO;
6762
}
6863

69-
net_ipaddr_copy(&dst_addr.sin_addr, &server_addr);
70-
dst_addr.sin_family = AF_INET;
71-
dst_addr.sin_port = htons(SERVER_PORT);
72-
7364
len = net_buf_frags_len(send_buf);
74-
k_sleep(TCP_TX_TIMEOUT);
7565

76-
rc = net_context_sendto(send_buf, (struct sockaddr *)&dst_addr,
77-
sizeof(struct sockaddr_in),
78-
NULL, K_FOREVER, NULL, NULL);
66+
rc = net_context_send(send_buf, NULL, ctx->timeout, NULL, NULL);
7967

8068
if (rc < 0) {
8169
printk("Cannot send IPv4 data to peer (%d)\n", rc);
@@ -88,74 +76,146 @@ int tcp_tx(void *context, const unsigned char *buf, size_t size)
8876

8977
int tcp_rx(void *context, unsigned char *buf, size_t size)
9078
{
79+
struct net_buf *data = NULL;
9180
struct tcp_context *ctx = context;
81+
int rc;
9282
uint8_t *ptr;
93-
int pos = 0;
94-
int len;
95-
struct net_buf *rx_buf;
96-
int rc, read_bytes;
83+
int read_bytes;
84+
uint16_t offset;
9785

9886
rc = net_context_recv(ctx->net_ctx, tcp_received, K_FOREVER, ctx);
9987
if (rc != 0) {
88+
printk("net_context_recv failed with code:%d\n", rc);
10089
return 0;
10190
}
10291
read_bytes = net_nbuf_appdatalen(ctx->rx_nbuf);
10392

104-
ptr = net_nbuf_appdata(ctx->rx_nbuf);
105-
rx_buf = ctx->rx_nbuf->frags;
106-
len = rx_buf->len - (ptr - rx_buf->data);
107-
108-
while (rx_buf) {
109-
memcpy(buf + pos, ptr, len);
110-
pos += len;
111-
rx_buf = rx_buf->frags;
112-
if (!rx_buf) {
113-
break;
114-
}
115-
ptr = rx_buf->data;
116-
len = rx_buf->len;
117-
}
118-
119-
if (read_bytes != pos) {
93+
data = net_buf_alloc(&tcp_msg_pool, APP_SLEEP_MSECS);
94+
if (data == NULL) {
95+
net_nbuf_unref(ctx->rx_nbuf);
96+
printk("net_buf_alloc failed\n");
12097
return -EIO;
12198
}
122-
rc = read_bytes;
99+
100+
offset = net_buf_frags_len(ctx->rx_nbuf) - read_bytes;
101+
rc = net_nbuf_linear_copy(data, ctx->rx_nbuf, offset, read_bytes);
102+
ptr = net_nbuf_appdata(data);
103+
memcpy(buf, ptr, read_bytes);
104+
123105
net_nbuf_unref(ctx->rx_nbuf);
124-
ctx->remaining = 0;
125-
ctx->rx_nbuf = NULL;
106+
net_nbuf_unref(data);
107+
108+
return read_bytes;
109+
}
110+
111+
static int set_addr(struct sockaddr *sock_addr, const char *addr,
112+
uint16_t server_port)
113+
{
114+
void *ptr = NULL;
115+
int rc;
116+
117+
#ifdef CONFIG_NET_IPV6
118+
net_sin6(sock_addr)->sin6_port = htons(server_port);
119+
sock_addr->family = AF_INET6;
120+
ptr = &(net_sin6(sock_addr)->sin6_addr);
121+
rc = net_addr_pton(AF_INET6, addr, ptr);
122+
#else
123+
net_sin(sock_addr)->sin_port = htons(server_port);
124+
sock_addr->family = AF_INET;
125+
ptr = &(net_sin(sock_addr)->sin_addr);
126+
rc = net_addr_pton(AF_INET, addr, ptr);
127+
#endif
128+
129+
if (rc) {
130+
printk("Invalid IP address: %s\n", addr);
131+
}
132+
133+
return rc;
134+
}
135+
136+
static int if_addr_add(struct sockaddr *local_sock)
137+
{
138+
void *p = NULL;
139+
140+
#ifdef CONFIG_NET_IPV6
141+
p = net_if_ipv6_addr_add(net_if_get_default(),
142+
&net_sin6(local_sock)->sin6_addr,
143+
NET_ADDR_MANUAL, 0);
144+
#else
145+
p = net_if_ipv4_addr_add(net_if_get_default(),
146+
&net_sin(local_sock)->sin_addr,
147+
NET_ADDR_MANUAL, 0);
148+
#endif
149+
150+
if (p) {
151+
return 0;
152+
}
153+
154+
return -EINVAL;
155+
}
156+
157+
int tcp_set_local_addr(struct tcp_context *ctx, const char *local_addr)
158+
{
159+
int rc;
160+
161+
rc = set_addr(&ctx->local_sock, local_addr, 0);
162+
if (rc) {
163+
printk("set_addr (local) error\n");
164+
goto lb_exit;
165+
}
166+
167+
rc = if_addr_add(&ctx->local_sock);
168+
if (rc) {
169+
printk("if_addr_add error\n");
170+
}
171+
172+
lb_exit:
126173
return rc;
127174
}
128175

129-
int tcp_init(struct tcp_context *ctx)
176+
int tcp_init(struct tcp_context *ctx, const char *server_addr,
177+
uint16_t server_port)
130178
{
131-
struct net_context *tcp_ctx = { 0 };
132-
struct sockaddr_in my_addr4 = { 0 };
179+
struct sockaddr server_sock;
133180
int rc;
134181

135-
net_ipaddr_copy(&my_addr4.sin_addr, &client_addr);
136-
my_addr4.sin_family = AF_INET;
137-
my_addr4.sin_port = htons(CLIENT_PORT);
182+
#ifdef CONFIG_NET_IPV6
183+
socklen_t addr_len = sizeof(struct sockaddr_in6);
184+
sa_family_t family = AF_INET6;
185+
#else
186+
socklen_t addr_len = sizeof(struct sockaddr_in);
187+
sa_family_t family = AF_INET;
188+
#endif
138189

139-
rc = net_context_get(AF_INET, SOCK_DGRAM, IPPROTO_TCP, &tcp_ctx);
140-
if (rc < 0) {
141-
printk("Cannot get network context for IPv4 TCP (%d)", rc);
142-
return -EIO;
190+
rc = net_context_get(family, SOCK_STREAM, IPPROTO_TCP, &ctx->net_ctx);
191+
if (rc) {
192+
printk("net_context_get error\n");
193+
return rc;
143194
}
144195

145-
rc = net_context_bind(tcp_ctx, (struct sockaddr *)&my_addr4,
146-
sizeof(struct sockaddr_in));
147-
if (rc < 0) {
148-
printk("Cannot bind IPv4 TCP port %d (%d)", CLIENT_PORT, rc);
149-
goto error;
196+
rc = net_context_bind(ctx->net_ctx, &ctx->local_sock, addr_len);
197+
if (rc) {
198+
printk("net_context_bind error\n");
199+
goto lb_exit;
150200
}
151201

152-
ctx->rx_nbuf = NULL;
153-
ctx->remaining = 0;
154-
ctx->net_ctx = tcp_ctx;
202+
rc = set_addr(&server_sock, server_addr, server_port);
203+
if (rc) {
204+
printk("set_addr (server) error\n");
205+
goto lb_exit;
206+
}
207+
208+
rc = net_context_connect(ctx->net_ctx, &server_sock, addr_len, NULL,
209+
ctx->timeout, NULL);
210+
if (rc) {
211+
printk("net_context_connect error\n");
212+
goto lb_exit;
213+
}
155214

156215
return 0;
157216

158-
error:
159-
net_context_put(tcp_ctx);
160-
return -EINVAL;
217+
lb_exit:
218+
net_context_put(ctx->net_ctx);
219+
220+
return rc;
161221
}

samples/net/mbedtls_sslclient/src/tcp.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,22 @@
77
#ifndef _TCP_H_
88
#define _TCP_H_
99

10-
#include <net/net_core.h>
10+
#include <net/net_context.h>
11+
#include <net/net_ip.h>
1112
#include <net/buf.h>
1213

1314
struct tcp_context {
1415
struct net_context *net_ctx;
16+
struct sockaddr local_sock;
1517
struct net_buf *rx_nbuf;
16-
int remaining;
18+
int32_t timeout;
1719
};
1820

19-
int tcp_init(struct tcp_context *ctx);
21+
int tcp_init(struct tcp_context *ctx, const char *server_addr,
22+
uint16_t server_port);
23+
2024
int tcp_tx(void *ctx, const unsigned char *buf, size_t size);
2125
int tcp_rx(void *ctx, unsigned char *buf, size_t size);
26+
int tcp_set_local_addr(struct tcp_context *ctx, const char *local_addr);
2227

2328
#endif

samples/net/mbedtls_sslclient/src/tcp_cfg.h

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,29 @@
77
#ifndef TCP_CONFIG_H_
88
#define TCP_CONFIG_H_
99

10-
#define NETMASK0 255
11-
#define NETMASK1 255
12-
#define NETMASK2 255
13-
#define NETMASK3 0
14-
15-
#define CLIENT_IPADDR0 192
16-
#define CLIENT_IPADDR1 168
17-
#define CLIENT_IPADDR2 1
18-
#define CLIENT_IPADDR3 100
19-
20-
#define SERVER_IPADDR0 192
21-
#define SERVER_IPADDR1 168
22-
#define SERVER_IPADDR2 1
23-
#define SERVER_IPADDR3 1
10+
#ifdef CONFIG_NET_SAMPLES_IP_ADDRESSES
11+
#ifdef CONFIG_NET_IPV6
12+
#define LOCAL_ADDR CONFIG_NET_SAMPLES_MY_IPV6_ADDR
13+
#define SERVER_ADDR CONFIG_NET_SAMPLES_PEER_IPV6_ADDR
14+
#else
15+
#define LOCAL_ADDR CONFIG_NET_SAMPLES_MY_IPV4_ADDR
16+
#define SERVER_ADDR CONFIG_NET_SAMPLES_PEER_IPV4_ADDR
17+
#endif
18+
#else
19+
#ifdef CONFIG_NET_IPV6
20+
#define LOCAL_ADDR "2001:db8::1"
21+
#define SERVER_ADDR "2001:db8::2"
22+
#else
23+
#define LOCAL_ADDR "192.0.2.1"
24+
#define SERVER_ADDR "192.0.2.2"
25+
#endif
26+
#endif /* CONFIG */
2427

2528
#define SERVER_PORT 4433
2629

27-
#define CLIENT_PORT 8484
28-
29-
#define TCP_RX_TIMEOUT 500
30-
#define TCP_TX_TIMEOUT 500
30+
#define MBEDTLS_NETWORK_TIMEOUT 30000
31+
#define TCP_RX_TIMEOUT 5000
32+
#define TCP_TX_TIMEOUT 5000
33+
#define APP_SLEEP_MSECS 500
3134

3235
#endif

0 commit comments

Comments
 (0)