libcoap  4.3.0rc1
coap_io.h
Go to the documentation of this file.
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  * This file is part of the CoAP library libcoap. Please see README for terms
7  * of use.
8  */
9 
10 #ifndef COAP_IO_H_
11 #define COAP_IO_H_
12 
13 #include <sys/types.h>
14 
15 #include "coap2/address.h"
16 
17 #ifdef RIOT_VERSION
18 #include "net/gnrc.h"
19 #endif /* RIOT_VERSION */
20 
21 #ifndef COAP_RXBUFFER_SIZE
22 #define COAP_RXBUFFER_SIZE 1472
23 #endif /* COAP_RXBUFFER_SIZE */
24 
25 /*
26  * It may may make sense to define this larger on busy systems
27  * (lots of sessions, large number of which are active), by using
28  * -DCOAP_MAX_EPOLL_EVENTS=nn at compile time.
29  */
30 #ifndef COAP_MAX_EPOLL_EVENTS
31 #define COAP_MAX_EPOLL_EVENTS 10
32 #endif /* COAP_MAX_EPOLL_EVENTS */
33 
34 #ifdef _WIN32
35 typedef SOCKET coap_fd_t;
36 #define coap_closesocket closesocket
37 #define COAP_SOCKET_ERROR SOCKET_ERROR
38 #define COAP_INVALID_SOCKET INVALID_SOCKET
39 #else
40 typedef int coap_fd_t;
41 #define coap_closesocket close
42 #define COAP_SOCKET_ERROR (-1)
43 #define COAP_INVALID_SOCKET (-1)
44 #endif
45 
46 typedef uint16_t coap_socket_flags_t;
47 
52 typedef struct coap_addr_hash_t {
54  uint16_t lport;
56 
57 typedef struct coap_addr_tuple_t {
61 
62 typedef struct coap_socket_t {
63 #if defined(WITH_LWIP)
64  struct udp_pcb *pcb;
65 #elif defined(WITH_CONTIKI)
66  void *conn;
67 #else
69 #endif /* WITH_LWIP */
70 #if defined(RIOT_VERSION)
71  gnrc_pktsnip_t *pkt; /* pointer to received packet for processing */
72 #endif /* RIOT_VERSION */
74  struct coap_session_t *session; /* Used by the epoll logic for an active session.
75  Note: It must mot be wrapped with COAP_EPOLL_SUPPORT as
76  coap_socket_t is seen in applications embedded in
77  coap_session_t etc. */
78  coap_endpoint_t *endpoint; /* Used by the epoll logic for a listening
79  endpoint.
80  Note: It must mot be wrapped with
81  COAP_EPOLL_SUPPORT as coap_socket_t is seen in
82  applications embedded in coap_session_t etc. */
84 
88 #define COAP_SOCKET_EMPTY 0x0000
89 #define COAP_SOCKET_NOT_EMPTY 0x0001
90 #define COAP_SOCKET_BOUND 0x0002
91 #define COAP_SOCKET_CONNECTED 0x0004
92 #define COAP_SOCKET_WANT_READ 0x0010
93 #define COAP_SOCKET_WANT_WRITE 0x0020
94 #define COAP_SOCKET_WANT_ACCEPT 0x0040
95 #define COAP_SOCKET_WANT_CONNECT 0x0080
96 #define COAP_SOCKET_CAN_READ 0x0100
97 #define COAP_SOCKET_CAN_WRITE 0x0200
98 #define COAP_SOCKET_CAN_ACCEPT 0x0400
99 #define COAP_SOCKET_CAN_CONNECT 0x0800
100 #define COAP_SOCKET_MULTICAST 0x1000
104 
105 int
107  const coap_address_t *local_if,
108  const coap_address_t *server,
109  int default_port,
110  coap_address_t *local_addr,
111  coap_address_t *remote_addr);
112 
113 int
115  const coap_address_t *listen_addr,
116  coap_address_t *bound_addr );
117 
119 
120 ssize_t
122  const uint8_t *data, size_t data_len );
123 
124 ssize_t
125 coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len);
126 
127 ssize_t
128 coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len);
129 
130 void
131 coap_epoll_ctl_mod(coap_socket_t *sock, uint32_t events, const char *func);
132 
133 #ifdef WITH_LWIP
134 ssize_t
135 coap_socket_send_pdu( coap_socket_t *sock, struct coap_session_t *session,
136  struct coap_pdu_t *pdu );
137 #endif
138 
139 const char *coap_socket_strerror( void );
140 
141 const char *coap_socket_format_errno(int error);
142 
148 int coap_tcp_is_supported(void);
149 
162 ssize_t coap_network_send( coap_socket_t *sock, const struct coap_session_t *session, const uint8_t *data, size_t datalen );
163 
175 ssize_t coap_network_read( coap_socket_t *sock, struct coap_packet_t *packet );
176 
177 #ifndef coap_mcast_interface
178 # define coap_mcast_interface(Local) 0
179 #endif
180 
186  unsigned char **address,
187  size_t *length);
188 
189 #ifdef WITH_LWIP
194 struct pbuf *coap_packet_extract_pbuf(struct coap_packet_t *packet);
195 #endif
196 
197 #if defined(WITH_LWIP)
198 /*
199  * This is only included in coap_io.h instead of .c in order to be available for
200  * sizeof in lwippools.h.
201  * Simple carry-over of the incoming pbuf that is later turned into a node.
202  *
203  * Source address data is currently side-banded via ip_current_dest_addr & co
204  * as the packets have limited lifetime anyway.
205  */
206 struct coap_packet_t {
207  struct pbuf *pbuf;
208  const coap_endpoint_t *local_interface;
210  int ifindex;
211 // uint16_t srcport;
212 };
213 #else
216  int ifindex;
217  size_t length;
218  unsigned char payload[COAP_RXBUFFER_SIZE];
219 };
220 #endif
221 typedef struct coap_packet_t coap_packet_t;
222 
223 typedef enum {
230 
231 #endif /* COAP_IO_H_ */
Representation of network addresses.
ssize_t coap_network_send(coap_socket_t *sock, const struct coap_session_t *session, const uint8_t *data, size_t datalen)
Function interface for data transmission.
int coap_socket_connect_udp(coap_socket_t *sock, const coap_address_t *local_if, const coap_address_t *server, int default_port, coap_address_t *local_addr, coap_address_t *remote_addr)
Definition: coap_io.c:236
uint16_t coap_socket_flags_t
Definition: coap_io.h:46
ssize_t coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len)
Definition: coap_io.c:472
int coap_tcp_is_supported(void)
Check whether TCP is available.
Definition: coap_tcp.c:31
void coap_socket_close(coap_socket_t *sock)
Definition: coap_io.c:362
struct coap_addr_hash_t coap_addr_hash_t
Only used for servers for hashing incoming packets.
#define COAP_RXBUFFER_SIZE
Definition: coap_io.h:22
ssize_t coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len)
Definition: coap_io.c:421
ssize_t coap_network_read(coap_socket_t *sock, struct coap_packet_t *packet)
Function interface for reading data.
Definition: coap_io.c:746
coap_nack_reason_t
Definition: coap_io.h:223
@ COAP_NACK_NOT_DELIVERABLE
Definition: coap_io.h:225
@ COAP_NACK_TOO_MANY_RETRIES
Definition: coap_io.h:224
@ COAP_NACK_TLS_FAILED
Definition: coap_io.h:227
@ COAP_NACK_ICMP_ISSUE
Definition: coap_io.h:228
@ COAP_NACK_RST
Definition: coap_io.h:226
struct coap_addr_tuple_t coap_addr_tuple_t
ssize_t coap_socket_send(coap_socket_t *sock, struct coap_session_t *session, const uint8_t *data, size_t data_len)
Definition: coap_io.c:1494
int coap_socket_bind_udp(coap_socket_t *sock, const coap_address_t *listen_addr, coap_address_t *bound_addr)
Definition: coap_io.c:152
void coap_packet_get_memmapped(struct coap_packet_t *packet, unsigned char **address, size_t *length)
Given a packet, set msg and msg_len to an address and length of the packet's data in memory.
Definition: coap_io.c:739
int coap_fd_t
Definition: coap_io.h:40
const char * coap_socket_strerror(void)
Definition: coap_io.c:1488
const char * coap_socket_format_errno(int error)
Definition: coap_io.c:1485
void coap_epoll_ctl_mod(coap_socket_t *sock, uint32_t events, const char *func)
void coap_mfree_endpoint(coap_endpoint_t *ep)
Definition: coap_io.c:147
struct coap_socket_t coap_socket_t
coap_endpoint_t * coap_malloc_endpoint(void)
Definition: coap_io.c:142
Only used for servers for hashing incoming packets.
Definition: coap_io.h:52
uint16_t lport
local port
Definition: coap_io.h:54
coap_address_t remote
remote address and port
Definition: coap_io.h:53
coap_address_t remote
remote address and port
Definition: coap_io.h:58
coap_address_t local
local address and port
Definition: coap_io.h:59
multi-purpose address abstraction
Definition: address.h:94
Abstraction of virtual endpoint that can be attached to coap_context_t.
size_t length
length of payload
Definition: coap_io.h:217
coap_addr_tuple_t addr_info
local and remote addresses
Definition: coap_io.h:215
unsigned char payload[COAP_RXBUFFER_SIZE]
payload
Definition: coap_io.h:218
int ifindex
the interface index
Definition: coap_io.h:216
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
Definition: pdu.h:287
coap_socket_t sock
socket object for the session, if any
Definition: coap_session.h:71
coap_endpoint_t * endpoint
Definition: coap_io.h:78
coap_fd_t fd
Definition: coap_io.h:68
struct coap_session_t * session
Definition: coap_io.h:74
coap_socket_flags_t flags
Definition: coap_io.h:73