• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * coap_io.h -- Default network I/O functions for libcoap
3  *
4  * Copyright (C) 2012-2013 Olaf Bergmann <bergmann@tzi.org>
5  *
6  * SPDX-License-Identifier: BSD-2-Clause
7  *
8  * This file is part of the CoAP library libcoap. Please see README for terms
9  * of use.
10  */
11 
12 #ifndef COAP_IO_INTERNAL_H_
13 #define COAP_IO_INTERNAL_H_
14 
15 #include <sys/types.h>
16 
17 #include "address.h"
18 
19 #ifdef RIOT_VERSION
20 #include "net/gnrc.h"
21 #endif /* RIOT_VERSION */
22 
23 struct coap_socket_t {
24 #if defined(WITH_LWIP)
25   struct udp_pcb *pcb;
26 #elif defined(WITH_CONTIKI)
27   void *conn;
28 #else
29   coap_fd_t fd;
30 #endif /* WITH_LWIP */
31 #if defined(RIOT_VERSION)
32   gnrc_pktsnip_t *pkt; /* pointer to received packet for processing */
33 #endif /* RIOT_VERSION */
34   coap_socket_flags_t flags;
35   coap_session_t *session; /* Used by the epoll logic for an active session. */
36   coap_endpoint_t *endpoint; /* Used by the epoll logic for a listening
37                                 endpoint. */
38 };
39 
40 /**
41  * coap_socket_flags_t values
42  */
43 #define COAP_SOCKET_EMPTY        0x0000  /**< the socket is not used */
44 #define COAP_SOCKET_NOT_EMPTY    0x0001  /**< the socket is not empty */
45 #define COAP_SOCKET_BOUND        0x0002  /**< the socket is bound */
46 #define COAP_SOCKET_CONNECTED    0x0004  /**< the socket is connected */
47 #define COAP_SOCKET_WANT_READ    0x0010  /**< non blocking socket is waiting for reading */
48 #define COAP_SOCKET_WANT_WRITE   0x0020  /**< non blocking socket is waiting for writing */
49 #define COAP_SOCKET_WANT_ACCEPT  0x0040  /**< non blocking server socket is waiting for accept */
50 #define COAP_SOCKET_WANT_CONNECT 0x0080  /**< non blocking client socket is waiting for connect */
51 #define COAP_SOCKET_CAN_READ     0x0100  /**< non blocking socket can now read without blocking */
52 #define COAP_SOCKET_CAN_WRITE    0x0200  /**< non blocking socket can now write without blocking */
53 #define COAP_SOCKET_CAN_ACCEPT   0x0400  /**< non blocking server socket can now accept without blocking */
54 #define COAP_SOCKET_CAN_CONNECT  0x0800  /**< non blocking client socket can now connect without blocking */
55 #define COAP_SOCKET_MULTICAST    0x1000  /**< socket is used for multicast communication */
56 #define COAP_SOCKET_BROADCAST    0x2000  /**< socket is used for broadcast communication */
57 
58 coap_endpoint_t *coap_malloc_endpoint( void );
59 void coap_mfree_endpoint( coap_endpoint_t *ep );
60 
61 const char *coap_socket_format_errno(int error);
62 
63 int
64 coap_socket_connect_udp(coap_socket_t *sock,
65                         const coap_address_t *local_if,
66                         const coap_address_t *server,
67                         int default_port,
68                         coap_address_t *local_addr,
69                         coap_address_t *remote_addr);
70 
71 int
72 coap_socket_bind_udp(coap_socket_t *sock,
73                      const coap_address_t *listen_addr,
74                      coap_address_t *bound_addr );
75 
76 void coap_socket_close(coap_socket_t *sock);
77 
78 ssize_t
79 coap_socket_send( coap_socket_t *sock, coap_session_t *session,
80                   const uint8_t *data, size_t data_len );
81 
82 ssize_t
83 coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len);
84 
85 ssize_t
86 coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len);
87 
88 void
89 coap_epoll_ctl_mod(coap_socket_t *sock, uint32_t events, const char *func);
90 
91 #ifdef WITH_LWIP
92 ssize_t
93 coap_socket_send_pdu( coap_socket_t *sock, coap_session_t *session,
94                       coap_pdu_t *pdu );
95 #endif
96 
97 /**
98  * Function interface for data transmission. This function returns the number of
99  * bytes that have been transmitted, or a value less than zero on error.
100  *
101  * @param sock             Socket to send data with
102  * @param session          Addressing information for unconnected sockets, or NULL
103  * @param data             The data to send.
104  * @param datalen          The actual length of @p data.
105  *
106  * @return                 The number of bytes written on success, or a value
107  *                         less than zero on error.
108  */
109 ssize_t coap_network_send( coap_socket_t *sock, const coap_session_t *session, const uint8_t *data, size_t datalen );
110 
111 /**
112  * Function interface for reading data. This function returns the number of
113  * bytes that have been read, or a value less than zero on error. In case of an
114  * error, @p *packet is set to NULL.
115  *
116  * @param sock   Socket to read data from
117  * @param packet Received packet metadata and payload. src and dst should be preset.
118  *
119  * @return       The number of bytes received on success, or a value less than
120  *               zero on error.
121  */
122 ssize_t coap_network_read( coap_socket_t *sock, coap_packet_t *packet );
123 
124 #ifndef coap_mcast_interface
125 # define coap_mcast_interface(Local) 0
126 #endif
127 
128 /**
129  * Given a packet, set msg and msg_len to an address and length of the packet's
130  * data in memory.
131  * */
132 void coap_packet_get_memmapped(coap_packet_t *packet,
133                                unsigned char **address,
134                                size_t *length);
135 
136 #ifdef WITH_LWIP
137 /**
138  * Get the pbuf of a packet. The caller takes over responsibility for freeing
139  * the pbuf.
140  */
141 struct pbuf *coap_packet_extract_pbuf(coap_packet_t *packet);
142 #endif
143 
144 #if defined(WITH_LWIP)
145 /*
146  * This is only included in coap_io.h instead of .c in order to be available for
147  * sizeof in lwippools.h.
148  * Simple carry-over of the incoming pbuf that is later turned into a node.
149  *
150  * Source address data is currently side-banded via ip_current_dest_addr & co
151  * as the packets have limited lifetime anyway.
152  */
153 struct coap_packet_t {
154   struct pbuf *pbuf;
155   const coap_endpoint_t *local_interface;
156   coap_addr_tuple_t addr_info; /**< local and remote addresses */
157   int ifindex;                /**< the interface index */
158 //  uint16_t srcport;
159 };
160 #else
161 struct coap_packet_t {
162   coap_addr_tuple_t addr_info; /**< local and remote addresses */
163   int ifindex;                /**< the interface index */
164   size_t length;              /**< length of payload */
165   unsigned char payload[COAP_RXBUFFER_SIZE]; /**< payload */
166 };
167 #endif
168 
169 #endif /* COAP_IO_INTERNAL_H_ */
170