Blame view

RIOT/pkg/lwip/contrib/sock/tcp/lwip_sock_tcp.c 10.6 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
  /*
   * Copyright (C) 2016 Freie Universitรคt Berlin
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @{
   *
   * @file
   * @author  Martine Lenders <m.lenders@fu-berlin.de>
   */
  
  #include "mutex.h"
  
  #include "net/sock/tcp.h"
  #include "timex.h"
  
  #include "lwip/sock_internal.h"
  #include "lwip/api.h"
  #include "lwip/opt.h"
  
  static inline void _tcp_sock_init(sock_tcp_t *sock, struct netconn *conn,
                                    sock_tcp_queue_t *queue)
  {
      mutex_init(&sock->mutex);
      mutex_lock(&sock->mutex);
      sock->conn = conn;
      sock->queue = queue;
      sock->last_buf = NULL;
      sock->last_offset = 0;
      mutex_unlock(&sock->mutex);
  }
  
  int sock_tcp_connect(sock_tcp_t *sock, const sock_tcp_ep_t *remote,
                       uint16_t local_port, uint16_t flags)
  {
      assert(sock != NULL);
      assert((remote != NULL) && (remote->port != 0));
  
      int res;
      struct netconn *tmp = NULL;
      struct _sock_tl_ep local = { .family = remote->family,
                                   .netif = remote->netif,
                                   .port = local_port };
  
      if ((res = lwip_sock_create(&tmp, &local, (struct _sock_tl_ep *)remote, 0,
                                  flags, NETCONN_TCP)) == 0) {
          _tcp_sock_init(sock, tmp, NULL);
      }
      return res;
  }
  
  int sock_tcp_listen(sock_tcp_queue_t *queue, const sock_tcp_ep_t *local,
                      sock_tcp_t *queue_array, unsigned queue_len,
                      uint16_t flags)
  {
      assert(queue != NULL);
      assert((local != NULL) && (local->port != 0));
      assert((queue_array != NULL) && (queue_len != 0));
  
      int res;
      struct netconn *tmp = NULL;
  
      if (queue_len > USHRT_MAX) {
          return -EFAULT;
      }
      if ((res = lwip_sock_create(&tmp, (struct _sock_tl_ep *)local, NULL, 0,
                                  flags, NETCONN_TCP)) < 0) {
          return res;
      }
      assert(tmp != NULL); /* just in case lwIP is trolling */
      mutex_init(&queue->mutex);
      mutex_lock(&queue->mutex);
      queue->conn = tmp;
      queue->array = queue_array;
      queue->len = queue_len;
      queue->used = 0;
      memset(queue->array, 0, sizeof(sock_tcp_t) * queue_len);
      mutex_unlock(&queue->mutex);
      switch (netconn_listen_with_backlog(queue->conn, queue->len)) {
          case ERR_OK:
              break;
          case ERR_MEM:
              return -ENOMEM;
          case ERR_USE:
              return -EADDRINUSE;
          case ERR_VAL:
              return -EINVAL;
          default:
              assert(false); /* should not happen since queue->conn is not closed
                              * and we have a TCP conn */
              break;
      }
      return 0;
  }
  
  void sock_tcp_disconnect(sock_tcp_t *sock)
  {
      assert(sock != NULL);
      mutex_lock(&sock->mutex);
      if (sock->conn != NULL) {
          netconn_close(sock->conn);
          netconn_delete(sock->conn);
          sock->conn = NULL;
          /* if sock came from a sock_tcp_queue_t: since sock is a pointer in it's
           * array it is also deleted from there, but we need to decrement the used
           * counter */
          if (sock->queue != NULL) {
              assert(sock->queue->used > 0);
              sock->queue->used--;
              sock->queue = NULL;
          }
      }
      mutex_unlock(&sock->mutex);
      memset(&sock->mutex, 0, sizeof(mutex_t));
  }
  
  void sock_tcp_stop_listen(sock_tcp_queue_t *queue)
  {
      assert(queue != NULL);
      mutex_lock(&queue->mutex);
      if (queue->conn != NULL) {
          netconn_close(queue->conn);
          netconn_delete(queue->conn);
          queue->conn = NULL;
          /* sever connections established through this queue */
          for (unsigned i = 0; i < queue->len; i++) {
              sock_tcp_disconnect(&queue->array[i]);
          }
          queue->array = NULL;
          queue->len = 0;
          queue->used = 0;
      }
      mutex_unlock(&queue->mutex);
      memset(&queue->mutex, 0, sizeof(mutex_t));
  }
  
  int sock_tcp_get_local(sock_tcp_t *sock, sock_tcp_ep_t *ep)
  {
      int res = 0;
      assert(sock != NULL);
      mutex_lock(&sock->mutex);
      if ((sock->conn == NULL) || lwip_sock_get_addr(sock->conn,
                                                     (struct _sock_tl_ep *)ep,
                                                     1)) {
          res = -EADDRNOTAVAIL;
      }
      mutex_unlock(&sock->mutex);
      return res;
  }
  
  int sock_tcp_get_remote(sock_tcp_t *sock, sock_tcp_ep_t *ep)
  {
      int res = 0;
      assert(sock != NULL);
      mutex_lock(&sock->mutex);
      if ((sock->conn == NULL) || lwip_sock_get_addr(sock->conn,
                                                     (struct _sock_tl_ep *)ep,
                                                     0)) {
          res = -ENOTCONN;
      }
      mutex_unlock(&sock->mutex);
      return res;
  }
  
  int sock_tcp_queue_get_local(sock_tcp_queue_t *queue, sock_tcp_ep_t *ep)
  {
      int res = 0;
  
      assert(queue != NULL);
      mutex_lock(&queue->mutex);
      if ((queue->conn == NULL) || lwip_sock_get_addr(queue->conn,
                                                      (struct _sock_tl_ep *)ep,
                                                      1)) {
          res = -EADDRNOTAVAIL;
      }
      mutex_unlock(&queue->mutex);
      return res;
  }
  
  int sock_tcp_accept(sock_tcp_queue_t *queue, sock_tcp_t **sock,
                      uint32_t timeout)
  {
      struct netconn *tmp = NULL;
      int res = 0;
  
      assert((queue != NULL) && (sock != NULL));
      if (queue->conn == NULL) {
          return -EINVAL;
      }
      if (timeout == 0) {
          if (!mutex_trylock(&queue->mutex)) {
              return -EAGAIN;
          }
      }
      else if (timeout != 0) {
          mutex_lock(&queue->mutex);
      }
      if (queue->used < queue->len) {
  #if LWIP_SO_RCVTIMEO
          if ((timeout != 0) && (timeout != SOCK_NO_TIMEOUT)) {
              netconn_set_recvtimeout(queue->conn, timeout / US_PER_MS);
          }
          else
  #endif
          if ((timeout == 0) && !cib_avail(&queue->conn->acceptmbox.mbox.cib)) {
              mutex_unlock(&queue->mutex);
              return -EAGAIN;
          }
          switch (netconn_accept(queue->conn, &tmp)) {
              case ERR_OK:
                  for (unsigned short i = 0; i < queue->len; i++) {
                      sock_tcp_t *s = &queue->array[i];
                      if (s->conn == NULL) {
                          _tcp_sock_init(s, tmp, queue);
                          queue->used++;
                          assert(queue->used > 0);
                          *sock = s;
                          break;
                      }
                  }
                  break;
              case ERR_ABRT:
                  res = -ECONNABORTED;
                  break;
              case ERR_MEM:
                  res = -ENOMEM;
                  break;
  #if LWIP_SO_RCVTIMEO
              case ERR_TIMEOUT:
                  res = -ETIMEDOUT;
                  break;
  #endif
              default:
                  assert(false);
                  res = -1;
                  break;
          }
      }
      else {
          res = -ENOMEM;
      }
  #if LWIP_SO_RCVTIMEO
      netconn_set_recvtimeout(queue->conn, 0);
  #endif
      mutex_unlock(&queue->mutex);
      return res;
  }
  
  ssize_t sock_tcp_read(sock_tcp_t *sock, void *data, size_t max_len,
                        uint32_t timeout)
  {
      uint8_t *data_ptr = data;
      struct pbuf *buf;
      ssize_t offset = 0, res = 0;
      bool done = false;
  
      assert((sock != NULL) && (data != NULL) && (max_len > 0));
      if (sock->conn == NULL) {
          return -ENOTCONN;
      }
      if (timeout == 0) {
          if (!mutex_trylock(&sock->mutex)) {
              return -EAGAIN;
          }
      }
      else {
          mutex_lock(&sock->mutex);
      }
  #if LWIP_SO_RCVTIMEO
      if ((timeout != 0) && (timeout != SOCK_NO_TIMEOUT)) {
          netconn_set_recvtimeout(sock->conn, timeout / US_PER_MS);
      }
      else
  #endif
      if ((timeout == 0) && !cib_avail(&sock->conn->recvmbox.mbox.cib)) {
          mutex_unlock(&sock->mutex);
          return -EAGAIN;
      }
      while (!done) {
          uint16_t copylen, buf_len;
          if (sock->last_buf != NULL) {
              buf = sock->last_buf;
          }
          else {
              err_t err;
              if ((err = netconn_recv_tcp_pbuf(sock->conn, &buf)) < 0) {
                  switch (err) {
                      case ERR_ABRT:
                          res = -ECONNABORTED;
                          break;
                      case ERR_CONN:
                          res = -EADDRNOTAVAIL;
                          break;
                      case ERR_RST:
                      case ERR_CLSD:
                          res = -ECONNRESET;
                          break;
                      case ERR_MEM:
                          res = -ENOMEM;
                          break;
  #if LWIP_SO_RCVTIMEO
                      case ERR_TIMEOUT:
                          res = -ETIMEDOUT;
                          break;
  #endif
                      default:
                          /* no applicable error */
                          res = -1;
                          break;
                  }
                  break;
              }
              sock->last_buf = buf;
          }
          buf_len = buf->tot_len - sock->last_offset;
          copylen = (buf_len > max_len) ? (uint16_t)max_len : buf_len;
          pbuf_copy_partial(buf, data_ptr + offset, copylen, sock->last_offset);
          offset += copylen;
          max_len -= copylen; /* should be 0 at minimum due to copylen setting above */
          if (max_len == 0) {
              done = true;
              res = offset;   /* in case offset == 0 */
          }
          /* post-process buf */
          if (buf_len > copylen) {
              /* there is still data in the buffer */
              sock->last_buf = buf;
              sock->last_offset = copylen;
          }
          else {
              sock->last_buf = NULL;
              sock->last_offset = 0;
              pbuf_free(buf);
              break;
          }
      }
      if (offset > 0) {
          res = offset;   /* we received data so return it */
      }
      /* unset flags */
  #if LWIP_SO_RCVTIMEO
      netconn_set_recvtimeout(sock->conn, 0);
  #endif
      netconn_set_nonblocking(sock->conn, false);
      mutex_unlock(&sock->mutex);
      return res;
  }
  
  ssize_t sock_tcp_write(sock_tcp_t *sock, const void *data, size_t len)
  {
      struct netconn *conn;
      int res = 0;
  
      assert(sock != NULL);
      assert((len == 0) || (data != NULL)); /* (len != 0) => (data != NULL) */
      mutex_lock(&sock->mutex);
      if (sock->conn == NULL) {
          mutex_unlock(&sock->mutex);
          return -ENOTCONN;
      }
      conn = sock->conn;
      mutex_unlock(&sock->mutex); /* we won't change anything to sock here
                                     (lwip_sock_send neither, since it remote is
                                     NULL) so we can leave the mutex */
      res = lwip_sock_send(&conn, data, len, 0, NULL, NETCONN_TCP);
      return res;
  }
  
  /** @} */