• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * coap_io.h -- Default network I/O functions for libcoap
3  *
4  * Copyright (C) 2012-2023 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 /**
13  * @file coap_io_internal.h
14  * @brief Internal network I/O functions
15  */
16 
17 #ifndef COAP_IO_INTERNAL_H_
18 #define COAP_IO_INTERNAL_H_
19 
20 #include "coap_internal.h"
21 #include "coap_layers_internal.h"
22 #include <sys/types.h>
23 
24 #include "coap_address.h"
25 
26 #ifdef WITH_CONTIKI
27 struct uip_udp_conn;
28 #endif /* WITH_CONTIKI */
29 
30 #ifdef RIOT_VERSION
31 #include "net/gnrc.h"
32 #endif /* RIOT_VERSION */
33 
34 struct coap_socket_t {
35 #if defined(WITH_LWIP)
36   struct udp_pcb *pcb;
37 #elif defined(WITH_CONTIKI)
38   struct uip_udp_conn *udp_conn;
39   coap_context_t *context;
40 #else
41   coap_fd_t fd;
42 #endif /* WITH_LWIP */
43 #if defined(RIOT_VERSION)
44   gnrc_pktsnip_t *pkt; /**< pointer to received packet for processing */
45 #endif /* RIOT_VERSION */
46   coap_socket_flags_t flags; /**< 1 or more of COAP_SOCKET* flag values */
47   coap_session_t *session; /**< Used to determine session owner. */
48 #if COAP_SERVER_SUPPORT
49   coap_endpoint_t *endpoint; /**< Used by the epoll logic for a listening
50                                   endpoint. */
51 #endif /* COAP_SERVER_SUPPORT */
52 #if COAP_CLIENT_SUPPORT
53   coap_address_t mcast_addr; /**< remote address and port (multicast track) */
54 #endif /* COAP_CLIENT_SUPPORT */
55   coap_layer_func_t lfunc[COAP_LAYER_LAST]; /**< Layer functions to use */
56 };
57 
58 /**
59  * coap_socket_flags_t values
60  */
61 #define COAP_SOCKET_EMPTY        0x0000  /**< the socket is not used */
62 #define COAP_SOCKET_NOT_EMPTY    0x0001  /**< the socket is not empty */
63 #define COAP_SOCKET_BOUND        0x0002  /**< the socket is bound */
64 #define COAP_SOCKET_CONNECTED    0x0004  /**< the socket is connected */
65 #define COAP_SOCKET_WANT_READ    0x0010  /**< non blocking socket is waiting for reading */
66 #define COAP_SOCKET_WANT_WRITE   0x0020  /**< non blocking socket is waiting for writing */
67 #define COAP_SOCKET_WANT_ACCEPT  0x0040  /**< non blocking server socket is waiting for accept */
68 #define COAP_SOCKET_WANT_CONNECT 0x0080  /**< non blocking client socket is waiting for connect */
69 #define COAP_SOCKET_CAN_READ     0x0100  /**< non blocking socket can now read without blocking */
70 #define COAP_SOCKET_CAN_WRITE    0x0200  /**< non blocking socket can now write without blocking */
71 #define COAP_SOCKET_CAN_ACCEPT   0x0400  /**< non blocking server socket can now accept without blocking */
72 #define COAP_SOCKET_CAN_CONNECT  0x0800  /**< non blocking client socket can now connect without blocking */
73 #define COAP_SOCKET_MULTICAST    0x1000  /**< socket is used for multicast communication */
74 
75 #ifdef COAP_SUPPORT_SOCKET_BROADCAST
76 #define COAP_SOCKET_BROADCAST    0x2000  /**< socket is used for broadcast communication */
77 #endif
78 
79 #if COAP_SERVER_SUPPORT
80 coap_endpoint_t *coap_malloc_endpoint(void);
81 void coap_mfree_endpoint(coap_endpoint_t *ep);
82 #endif /* COAP_SERVER_SUPPORT */
83 
84 const char *coap_socket_format_errno(int error);
85 
86 #if COAP_CLIENT_SUPPORT
87 int coap_socket_connect_udp(coap_socket_t *sock,
88                             const coap_address_t *local_if,
89                             const coap_address_t *server,
90                             int default_port,
91                             coap_address_t *local_addr,
92                             coap_address_t *remote_addr);
93 #endif /* COAP_CLIENT_SUPPORT */
94 
95 int coap_socket_bind_udp(coap_socket_t *sock,
96                          const coap_address_t *listen_addr,
97                          coap_address_t *bound_addr);
98 
99 /**
100  * Function interface to close off a socket.
101  *
102  * @param sock             Socket to close.
103  *
104  */
105 void coap_socket_close(coap_socket_t *sock);
106 
107 /**
108  * Function interface for data stream sending off a socket.
109  *
110  * @param sock             Socket to send data over.
111  * @param data             The data to send.
112  * @param data_len         The length of @p data.
113  *
114  * @return                 >=0 Number of bytes sent.
115  *                         -1 Error error in errno).
116  */
117 ssize_t coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len);
118 
119 /**
120  * Function interface for data stream receiving off a socket.
121  *
122  * @param sock             Socket to receive data on.
123  * @param data             The data to receive.
124  * @param data_len         The maximum length of @p data.
125  *
126  * @return                 >=0 Number of bytes read.
127  *                         -1 Error error in errno).
128  */
129 ssize_t coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len);
130 
131 /**
132  * Epoll specific function to add the state of events that epoll is to track
133  * for the appropriate file descriptor.
134  *
135  * @param sock    Socket information.
136  * @param events  The Epoll events to update
137  * @param func    Function that this function was called from.
138  *
139  */
140 void coap_epoll_ctl_add(coap_socket_t *sock, uint32_t events, const char *func);
141 
142 /**
143  * Epoll specific function to modify the state of events that epoll is tracking
144  * on the appropriate file descriptor.
145  *
146  * @param sock    Socket information.
147  * @param events  The Epoll events to update
148  * @param func    Function that this function was called from.
149  *
150  */
151 void coap_epoll_ctl_mod(coap_socket_t *sock, uint32_t events, const char *func);
152 
153 /**
154  * Update the epoll timer fd as to when it is to trigger.
155  *
156  * @param context The context to update the epoll timer on.
157  * @param delay The time to delay before the epoll timer fires.
158  */
159 void coap_update_epoll_timer(coap_context_t *context, coap_tick_t delay);
160 
161 #ifdef WITH_LWIP
162 ssize_t coap_socket_send_pdu(coap_socket_t *sock, coap_session_t *session,
163                              coap_pdu_t *pdu);
164 #endif
165 
166 /**
167  * Function interface for data transmission. This function returns the number of
168  * bytes that have been transmitted, or a value less than zero on error.
169  *
170  * @param sock          Socket to send data over.
171  * @param session       Addressing information for unconnected sockets, or NULL
172  * @param data          The data to send.
173  * @param datalen       The actual length of @p data.
174  *
175  * @return              The number of bytes written on success, or a value
176  *                      less than zero on error.
177  */
178 ssize_t coap_socket_send(coap_socket_t *sock, const coap_session_t *session,
179                          const uint8_t *data, size_t datalen);
180 
181 /**
182  * Function interface for reading data. This function returns the number of
183  * bytes that have been read, or a value less than zero on error. In case of an
184  * error, @p *packet is set to NULL.
185  *
186  * @param sock   Socket to read data from.
187  * @param packet Received packet metadata and payload. src and dst
188  *               should be preset.
189  *
190  * @return       The number of bytes received on success, or a value less than
191  *               zero on error.
192  */
193 ssize_t coap_socket_recv(coap_socket_t *sock, coap_packet_t *packet);
194 
195 #ifndef coap_mcast_interface
196 # define coap_mcast_interface(Local) 0
197 #endif
198 
199 /**
200  * Given a packet, set msg and msg_len to an address and length of the packet's
201  * data in memory.
202  * */
203 void coap_packet_get_memmapped(coap_packet_t *packet,
204                                unsigned char **address,
205                                size_t *length);
206 
207 #ifdef WITH_LWIP
208 void coap_io_process_timeout(void *arg);
209 #endif
210 
211 struct coap_packet_t {
212   coap_addr_tuple_t addr_info; /**< local and remote addresses */
213   int ifindex;                /**< the interface index */
214   size_t length;             /**< length of payload */
215   unsigned char *payload;    /**< payload */
216 };
217 
218 #ifdef WITH_CONTIKI
219 void coap_start_io_process(void);
220 void coap_stop_io_process(void);
221 #endif /* WITH_CONTIKI */
222 
223 #endif /* COAP_IO_INTERNAL_H_ */
224