Blame view

RIOT/pkg/emb6/contrib/sock/udp/emb6_sock_udp.c 10 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
  /*
   * 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 <assert.h>
  #include <errno.h>
  #include <stdbool.h>
  
  #include "byteorder.h"
  #include "evproc.h"
  #include "msg.h"
  #include "mutex.h"
  #include "net/af.h"
  #include "net/sock/udp.h"
  #include "net/ipv6/hdr.h"
  #include "sched.h"
  #include "uip.h"
  #include "xtimer.h"
  
  #define _MSG_TYPE_CLOSE     (0x4123)
  #define _MSG_TYPE_TIMEOUT   (0x4124)
  #define _MSG_TYPE_RCV       (0x4125)
  
  /* struct to describe a sendto command for emb6 thread */
  typedef struct {
      mutex_t block;
      struct udp_socket *sock;
      const sock_udp_ep_t *remote;
      int res;
      const void *data;
      size_t len;
  } _send_cmd_t;
  
  extern uint16_t uip_slen;
  
  static bool send_registered = false;
  
  static void _timeout_callback(void *arg);
  static void _input_callback(struct udp_socket *c, void *ptr,
                              const uip_ipaddr_t *src_addr, uint16_t src_port,
                              const uip_ipaddr_t *dst_addr, uint16_t dst_port,
                              const uint8_t *data, uint16_t datalen);
  static void _output_callback(c_event_t c_event, p_data_t p_data);
  
  static int _reg(struct udp_socket *c, void *ptr, udp_socket_input_callback_t cb,
                  const sock_udp_ep_t *local, const sock_udp_ep_t *remote)
  {
      if (((local != NULL) && (local->family != AF_INET6)) ||
          ((remote != NULL) && (remote->family != AF_INET6))) {
          return -EAFNOSUPPORT;
      }
      if (udp_socket_register(c, ptr, cb) < 0) {
          return -ENOMEM;
      }
      if (local != NULL) {
          if (udp_socket_bind(c, local->port) < 0) {
              udp_socket_close(c);
              return -EADDRINUSE;
          }
      }
      if (remote != NULL) {
          /* check of return value not necessary, since neither c nor
           * c->udp_conn is NULL (only error case) at this point */
          udp_socket_connect(c, (uip_ipaddr_t *)&remote->addr, remote->port);
      }
      return 0;
  }
  
  int sock_udp_create(sock_udp_t *sock, const sock_udp_ep_t *local,
                      const sock_udp_ep_t *remote, uint16_t flags)
  {
      int res;
  
      (void)flags;
      assert((sock != NULL));
      assert((local == NULL) || (local->port != 0));
      assert((remote == NULL) || (remote->port != 0));
      if (sock->sock.input_callback != NULL) {
          sock_udp_close(sock);
      }
      mutex_init(&sock->mutex);
      mutex_lock(&sock->mutex);
      mbox_init(&sock->mbox, sock->mbox_queue, SOCK_MBOX_SIZE);
      atomic_flag_clear(&sock->receivers);
      if ((res = _reg(&sock->sock, sock, _input_callback, local, remote)) < 0) {
          sock->sock.input_callback = NULL;
      }
      mutex_unlock(&sock->mutex);
      return res;
  }
  
  void sock_udp_close(sock_udp_t *sock)
  {
      assert(sock != NULL);
      if (sock->sock.input_callback != NULL) {
          while (atomic_fetch_sub(&sock->receivers, 1) > 0) {
              msg_t msg = { .type = _MSG_TYPE_CLOSE };
              mbox_put(&sock->mbox, &msg);
          }
          mutex_lock(&sock->mutex);
          udp_socket_close(&sock->sock);
          sock->sock.input_callback = NULL;
          mutex_unlock(&sock->mutex);
      }
  }
  
  int sock_udp_get_local(sock_udp_t *sock, sock_udp_ep_t *ep)
  {
      assert((sock != NULL) && (ep != NULL));
      if ((sock->sock.input_callback != NULL) &&
          (sock->sock.udp_conn->lport != 0)) {
          mutex_lock(&sock->mutex);
          /* local UDP endpoints do not have addresses in emb6 */
          memset(&ep->addr, 0, sizeof(ipv6_addr_t));
          ep->port = ntohs(sock->sock.udp_conn->lport);
          mutex_unlock(&sock->mutex);
          return sizeof(ipv6_addr_t);
      }
      return -EADDRNOTAVAIL;
  }
  
  int sock_udp_get_remote(sock_udp_t *sock, sock_udp_ep_t *ep)
  {
      assert((sock != NULL) && (ep != NULL));
      if ((sock->sock.input_callback != NULL) &&
          (sock->sock.udp_conn->rport != 0)) {
          mutex_lock(&sock->mutex);
          memcpy(&ep->addr, &sock->sock.udp_conn->ripaddr, sizeof(ipv6_addr_t));
          ep->port = ntohs(sock->sock.udp_conn->rport);
          mutex_unlock(&sock->mutex);
          return sizeof(ipv6_addr_t);
      }
      return -ENOTCONN;
  }
  
  int sock_udp_recv(sock_udp_t *sock, void *data, size_t max_len,
                    uint32_t timeout, sock_udp_ep_t *remote)
  {
      xtimer_t timeout_timer;
      int blocking = BLOCKING;
      int res = -EIO;
      msg_t msg;
  
      assert((sock != NULL) && (data != NULL) && (max_len > 0));
      if (sock->sock.input_callback == NULL) {
          return -EADDRNOTAVAIL;
      }
      if (timeout == 0) {
          blocking = NON_BLOCKING;
      }
      else if (timeout != SOCK_NO_TIMEOUT) {
          timeout_timer.callback = _timeout_callback;
          timeout_timer.arg = &sock->mbox;
          xtimer_set(&timeout_timer, timeout);
      }
      atomic_fetch_add(&sock->receivers, 1);
      if (_mbox_get(&sock->mbox, &msg, blocking) == 0) {
          /* do not need to remove xtimer, since we only get here in non-blocking
           * mode (timeout > 0) */
          return -EAGAIN;
      }
      switch (msg.type) {
          case _MSG_TYPE_CLOSE:
              res = -EADDRNOTAVAIL;
              break;
          case _MSG_TYPE_TIMEOUT:
              res = -ETIMEDOUT;
              break;
          case _MSG_TYPE_RCV:
              mutex_lock(&sock->mutex);
              if (max_len < sock->recv_info.datalen) {
                  res = -ENOBUFS;
                  mutex_unlock(&sock->mutex);
                  break;
              }
              memcpy(data, sock->recv_info.data, sock->recv_info.datalen);
              if (remote != NULL) {
                  remote->family = AF_INET6;
                  remote->netif = SOCK_ADDR_ANY_NETIF;
                  memcpy(&remote->addr, &sock->recv_info.src, sizeof(ipv6_addr_t));
                  remote->port = sock->recv_info.src_port;
              }
              res = (int)sock->recv_info.datalen;
              mutex_unlock(&sock->mutex);
              break;
      }
      atomic_fetch_sub(&sock->receivers, 1);
      return res;
  }
  
  int sock_udp_send(sock_udp_t *sock, const void *data, size_t len,
                    const sock_udp_ep_t *remote)
  {
      struct udp_socket tmp;
      _send_cmd_t send_cmd = { .block = MUTEX_INIT,
                               .remote = remote,
                               .data = data,
                               .len = len };
  
      assert((sock != NULL) || (remote != NULL));
      assert((len == 0) || (data != NULL));   /* (len != 0) => (data != NULL) */
      /* we want the send in the uip thread (which udp_socket_send does not offer)
       * so we need to do it manually */
      if (!send_registered) {
          if (evproc_regCallback(EVENT_TYPE_SOCK_SEND, _output_callback) != E_SUCCESS) {
              return -ENOMEM;
          }
          else {
              send_registered = true;
          }
      }
      if ((len > (UIP_BUFSIZE - (UIP_LLH_LEN + UIP_IPUDPH_LEN))) ||
          (len > UINT16_MAX)) {
          return -ENOMEM;
      }
      if (remote != NULL) {
          if (remote->family != AF_INET6) {
              return -EAFNOSUPPORT;
          }
          if (remote->port == 0) {
              return -EINVAL;
          }
          send_cmd.remote = remote;
      }
      else if (sock->sock.udp_conn->rport == 0) {
          return -ENOTCONN;
      }
      /* cppcheck-supress nullPointerRedundantCheck
       * remote == NULL implies that sock != NULL (see assert at start of
       * function) * that's why it is okay in the if-statement above to check
       * sock->... without checking (sock != NULL) first => this check afterwards
       * isn't redundant */
      if (sock == NULL) {
          int res;
          if ((res = _reg(&tmp, NULL, NULL, NULL, NULL)) < 0) {
              return res;
          }
          send_cmd.sock = &tmp;
      }
      else {
          send_cmd.sock = &sock->sock;
      }
      mutex_lock(&send_cmd.block);
      /* change to emb6 thread context */
      if (evproc_putEvent(E_EVPROC_TAIL, EVENT_TYPE_SOCK_SEND, &send_cmd) == E_SUCCESS) {
          /* block thread until data was sent */
          mutex_lock(&send_cmd.block);
      }
      else {
          /* most likely error: event queue was full */
          send_cmd.res = -ENOMEM;
      }
      if (send_cmd.sock == &tmp) {
          udp_socket_close(&tmp);
      }
      mutex_unlock(&send_cmd.block);
  
      return send_cmd.res;
  }
  
  static void _timeout_callback(void *arg)
  {
      msg_t msg = { .type = _MSG_TYPE_TIMEOUT };
      mbox_t *mbox = arg;
  
      /* should be safe, because otherwise if mbox were filled this callback is
       * senseless */
      mbox_try_put(mbox, &msg);
  }
  
  static void _input_callback(struct udp_socket *c, void *ptr,
                              const uip_ipaddr_t *src_addr, uint16_t src_port,
                              const uip_ipaddr_t *dst_addr, uint16_t dst_port,
                              const uint8_t *data, uint16_t datalen)
  {
      msg_t msg = { .type = _MSG_TYPE_RCV };
      sock_udp_t *sock = ptr;
  
      (void)dst_addr;
      (void)dst_port;
      mutex_lock(&sock->mutex);
      sock->recv_info.src_port = src_port;
      sock->recv_info.src = (const ipv6_addr_t *)src_addr;
      sock->recv_info.data = data;
      sock->recv_info.datalen = datalen - sizeof(ipv6_hdr_t);
      mutex_unlock(&sock->mutex);
      mbox_put(&sock->mbox, &msg);
  }
  
  static void _output_callback(c_event_t c_event, p_data_t p_data)
  {
  
      if ((c_event != EVENT_TYPE_SOCK_SEND) || (p_data == NULL)) {
          return;
      }
  
      _send_cmd_t *send_cmd = (_send_cmd_t *)p_data;
  
      if (send_cmd->remote != NULL) {
          /* send_cmd->len was previously checked */
          send_cmd->res = udp_socket_sendto(send_cmd->sock, send_cmd->data,
                                            (uint16_t)send_cmd->len,
                                            (uip_ipaddr_t *)&send_cmd->remote->addr,
                                            send_cmd->remote->port);
      }
      else {
          /* send_cmd->len was previously checked */
          send_cmd->res = udp_socket_send(send_cmd->sock, send_cmd->data,
                                          (uint16_t)send_cmd->len);
      }
      send_cmd->res = (send_cmd->res < 0) ? -EHOSTUNREACH : send_cmd->res;
      /* notify notify waiting thread */
      mutex_unlock(&send_cmd->block);
  }
  /** @} */