Blame view

RIOT/sys/net/gnrc/conn/udp/gnrc_conn_udp.c 3.9 KB
fb11e647   vrobic   reseau statique a...
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
  /*
   * Copyright (C) 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
   *
   * 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
   * @brief       GNRC implementation of the udp interface defined by net/gnrc/udp.h
   *
   * @author  Martine Lenders <mlenders@inf.fu-berlin.de>
   */
  
  #include <errno.h>
  #include "net/af.h"
  #include "net/gnrc/conn.h"
  #include "net/gnrc/ipv6.h"
  #include "net/gnrc/udp.h"
  
  #include "net/conn/udp.h"
  
  int conn_udp_create(conn_udp_t *conn, const void *addr, size_t addr_len,
                      int family, uint16_t port)
  {
      conn->l4_type = GNRC_NETTYPE_UDP;
      switch (family) {
  #ifdef MODULE_GNRC_IPV6
          case AF_INET6:
              if (addr_len != sizeof(ipv6_addr_t)) {
                  return -EINVAL;
              }
              if (gnrc_conn6_set_local_addr(conn->local_addr, addr)) {
                  conn->l3_type = GNRC_NETTYPE_IPV6;
                  conn->local_addr_len = addr_len;
                  conn_udp_close(conn);       /* unregister possibly registered netreg entry */
                  gnrc_conn_reg(&conn->netreg_entry, conn->l4_type, (uint32_t)port);
              }
              else {
                  return -EADDRNOTAVAIL;
              }
              break;
  #endif
          default:
              (void)addr;
              (void)addr_len;
              (void)port;
              return -EAFNOSUPPORT;
      }
      return 0;
  }
  
  void conn_udp_close(conn_udp_t *conn)
  {
      assert(conn->l4_type == GNRC_NETTYPE_UDP);
      if (conn->netreg_entry.target.pid != KERNEL_PID_UNDEF) {
          gnrc_netreg_unregister(GNRC_NETTYPE_UDP, &conn->netreg_entry);
          conn->netreg_entry.target.pid = KERNEL_PID_UNDEF;
      }
  }
  
  int conn_udp_getlocaladdr(conn_udp_t *conn, void *addr, uint16_t *port)
  {
      assert(conn->l4_type == GNRC_NETTYPE_UDP);
      memcpy(addr, &conn->local_addr, conn->local_addr_len);
      *port = (uint16_t)conn->netreg_entry.demux_ctx;
      return conn->local_addr_len;
  }
  
  int conn_udp_recvfrom(conn_udp_t *conn, void *data, size_t max_len, void *addr, size_t *addr_len,
                        uint16_t *port)
  {
      assert(conn->l4_type == GNRC_NETTYPE_UDP);
      switch (conn->l3_type) {
  #ifdef MODULE_GNRC_IPV6
          case GNRC_NETTYPE_IPV6:
              return gnrc_conn_recvfrom((conn_t *)conn, data, max_len, addr, addr_len, port);
  #endif
          default:
              (void)data;
              (void)max_len;
              (void)addr;
              (void)addr_len;
              (void)port;
              return -EBADF;
      }
  }
  
  int conn_udp_sendto(const void *data, size_t len, const void *src, size_t src_len,
                      const void *dst, size_t dst_len, int family, uint16_t sport,
                      uint16_t dport)
  {
      gnrc_pktsnip_t *pkt, *hdr = NULL;
  
      pkt = gnrc_pktbuf_add(NULL, (void *)data, len, GNRC_NETTYPE_UNDEF); /* data will only be copied */
      hdr = gnrc_udp_hdr_build(pkt, sport, dport);
      if (hdr == NULL) {
          gnrc_pktbuf_release(pkt);
          return -ENOMEM;
      }
      pkt = hdr;
      switch (family) {
  #ifdef MODULE_GNRC_IPV6
          case AF_INET6:
              if (((src != NULL) && (src_len != sizeof(ipv6_addr_t))) ||
                  (dst_len != sizeof(ipv6_addr_t))) {
                  gnrc_pktbuf_release(pkt);
                  return -EINVAL;
              }
              /* addr will only be copied */
              hdr = gnrc_ipv6_hdr_build(pkt, src, dst);
              if (hdr == NULL) {
                  gnrc_pktbuf_release(pkt);
                  return -ENOMEM;
              }
              pkt = hdr;
              break;
  #endif /* MODULE_GNRC_IPV6 */
          default:
              (void)hdr;
              (void)src;
              (void)src_len;
              (void)dst;
              (void)dst_len;
              gnrc_pktbuf_release(pkt);
              return -EAFNOSUPPORT;
      }
  
      gnrc_netapi_dispatch_send(GNRC_NETTYPE_UDP, GNRC_NETREG_DEMUX_CTX_ALL, pkt);
  
      return len;
  }
  
  /** @} */