libcoap  4.2.1
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 "address.h"
16 
17 #ifndef COAP_RXBUFFER_SIZE
18 #define COAP_RXBUFFER_SIZE 1472
19 #endif /* COAP_RXBUFFER_SIZE */
20 
21 /*
22  * It may may make sense to define this larger on busy systems
23  * (lots of sessions, large number of which are active), by using
24  * -DCOAP_MAX_EPOLL_EVENTS=nn at compile time.
25  */
26 #ifndef COAP_MAX_EPOLL_EVENTS
27 #define COAP_MAX_EPOLL_EVENTS 10
28 #endif /* COAP_MAX_EPOLL_EVENTS */
29 
30 #ifdef _WIN32
31 typedef SOCKET coap_fd_t;
32 #define coap_closesocket closesocket
33 #define COAP_SOCKET_ERROR SOCKET_ERROR
34 #define COAP_INVALID_SOCKET INVALID_SOCKET
35 #else
36 typedef int coap_fd_t;
37 #define coap_closesocket close
38 #define COAP_SOCKET_ERROR (-1)
39 #define COAP_INVALID_SOCKET (-1)
40 #endif
41 
42 struct coap_packet_t;
43 struct coap_session_t;
44 struct coap_context_t;
45 struct coap_pdu_t;
46 
47 typedef uint16_t coap_socket_flags_t;
48 
49 typedef struct coap_addr_tuple_t {
53 
54 typedef struct coap_socket_t {
55 #if defined(WITH_LWIP)
56  struct udp_pcb *pcb;
57 #elif defined(WITH_CONTIKI)
58  void *conn;
59 #else
61 #endif /* WITH_LWIP */
63  struct coap_session_t *session; /* Used by the epoll logic for an active session.
64  Note: It must mot be wrapped with COAP_EPOLL_SUPPORT as
65  coap_socket_t is seen in applications embedded in
66  coap_session_t etc. */
67  struct coap_endpoint_t *endpoint; /* Used by the epoll logic for a listening endpoint.
68  Note: It must mot be wrapped with COAP_EPOLL_SUPPORT as
69  coap_socket_t is seen in applications embedded in
70  coap_session_t etc. */
72 
76 #define COAP_SOCKET_EMPTY 0x0000
77 #define COAP_SOCKET_NOT_EMPTY 0x0001
78 #define COAP_SOCKET_BOUND 0x0002
79 #define COAP_SOCKET_CONNECTED 0x0004
80 #define COAP_SOCKET_WANT_READ 0x0010
81 #define COAP_SOCKET_WANT_WRITE 0x0020
82 #define COAP_SOCKET_WANT_ACCEPT 0x0040
83 #define COAP_SOCKET_WANT_CONNECT 0x0080
84 #define COAP_SOCKET_CAN_READ 0x0100
85 #define COAP_SOCKET_CAN_WRITE 0x0200
86 #define COAP_SOCKET_CAN_ACCEPT 0x0400
87 #define COAP_SOCKET_CAN_CONNECT 0x0800
88 #define COAP_SOCKET_MULTICAST 0x1000
90 struct coap_endpoint_t *coap_malloc_endpoint( void );
91 void coap_mfree_endpoint( struct coap_endpoint_t *ep );
92 
93 int
95  const coap_address_t *local_if,
96  const coap_address_t *server,
97  int default_port,
98  coap_address_t *local_addr,
99  coap_address_t *remote_addr);
100 
101 int
103  const coap_address_t *listen_addr,
104  coap_address_t *bound_addr );
105 
106 int
108  const coap_address_t *local_if,
109  const coap_address_t *server,
110  int default_port,
111  coap_address_t *local_addr,
112  coap_address_t *remote_addr);
113 
114 int
116  coap_address_t *local_addr,
117  coap_address_t *remote_addr);
118 
119 int
121  const coap_address_t *listen_addr,
122  coap_address_t *bound_addr);
123 
124 int
126  coap_socket_t *new_client,
127  coap_address_t *local_addr,
128  coap_address_t *remote_addr);
129 
131 
132 ssize_t
134  const uint8_t *data, size_t data_len );
135 
136 ssize_t
137 coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len);
138 
139 ssize_t
140 coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len);
141 
142 void
143 coap_epoll_ctl_mod(coap_socket_t *sock, uint32_t events, const char *func);
144 
145 #ifdef WITH_LWIP
146 ssize_t
147 coap_socket_send_pdu( coap_socket_t *sock, struct coap_session_t *session,
148  struct coap_pdu_t *pdu );
149 #endif
150 
151 const char *coap_socket_strerror( void );
152 
165 ssize_t coap_network_send( coap_socket_t *sock, const struct coap_session_t *session, const uint8_t *data, size_t datalen );
166 
178 ssize_t coap_network_read( coap_socket_t *sock, struct coap_packet_t *packet );
179 
180 #ifndef coap_mcast_interface
181 # define coap_mcast_interface(Local) 0
182 #endif
183 
188 void coap_packet_get_memmapped(struct coap_packet_t *packet,
189  unsigned char **address,
190  size_t *length);
191 
192 #ifdef WITH_LWIP
193 
197 struct pbuf *coap_packet_extract_pbuf(struct coap_packet_t *packet);
198 #endif
199 
200 #if defined(WITH_LWIP)
201 /*
202  * This is only included in coap_io.h instead of .c in order to be available for
203  * sizeof in lwippools.h.
204  * Simple carry-over of the incoming pbuf that is later turned into a node.
205  *
206  * Source address data is currently side-banded via ip_current_dest_addr & co
207  * as the packets have limited lifetime anyway.
208  */
209 struct coap_packet_t {
210  struct pbuf *pbuf;
211  const struct coap_endpoint_t *local_interface;
212  coap_addr_tuple_t addr_info;
213  int ifindex;
214 // uint16_t srcport;
215 };
216 #else
219  int ifindex;
220  size_t length;
221  unsigned char payload[COAP_RXBUFFER_SIZE];
222 };
223 #endif
225 
226 typedef enum {
233 
234 #endif /* COAP_IO_H_ */
#define COAP_RXBUFFER_SIZE
Definition: coap_io.h:18
struct coap_socket_t coap_socket_t
void coap_socket_close(coap_socket_t *sock)
Definition: coap_io.c:631
coap_address_t remote
remote address and port
Definition: coap_io.h:50
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:529
int coap_socket_bind_udp(coap_socket_t *sock, const coap_address_t *listen_addr, coap_address_t *bound_addr)
Definition: coap_io.c:185
multi-purpose address abstraction
Definition: address.h:62
coap_fd_t fd
Definition: coap_io.h:60
void coap_epoll_ctl_mod(coap_socket_t *sock, uint32_t events, const char *func)
int ifindex
the interface index
Definition: coap_io.h:219
coap_address_t local
local address and port
Definition: coap_io.h:51
coap_nack_reason_t
Definition: coap_io.h:226
Abstraction of virtual endpoint that can be attached to coap_context_t.
struct coap_endpoint_t * endpoint
Definition: coap_io.h:67
int coap_socket_bind_tcp(coap_socket_t *sock, const coap_address_t *listen_addr, coap_address_t *bound_addr)
Definition: coap_io.c:412
int coap_socket_connect_tcp1(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:264
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&#39;s data in memory...
Definition: coap_io.c:1002
const char * coap_socket_strerror(void)
Definition: coap_io.c:1651
int coap_fd_t
Definition: coap_io.h:36
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
Definition: pdu.h:287
int coap_socket_connect_tcp2(coap_socket_t *sock, coap_address_t *local_addr, coap_address_t *remote_addr)
Definition: coap_io.c:372
ssize_t coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len)
Definition: coap_io.c:690
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:1657
Representation of network addresses.
void coap_mfree_endpoint(struct coap_endpoint_t *ep)
Definition: coap_io.c:180
uint16_t coap_socket_flags_t
Definition: coap_io.h:45
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.
unsigned int uint32_t
Definition: uthash.h:78
size_t length
length of payload
Definition: coap_io.h:220
ssize_t coap_network_read(coap_socket_t *sock, struct coap_packet_t *packet)
Function interface for reading data.
Definition: coap_io.c:1008
int coap_socket_accept_tcp(coap_socket_t *server, coap_socket_t *new_client, coap_address_t *local_addr, coap_address_t *remote_addr)
Definition: coap_io.c:492
struct coap_addr_tuple_t coap_addr_tuple_t
coap_socket_flags_t flags
Definition: coap_io.h:62
ssize_t coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len)
Definition: coap_io.c:735
coap_addr_tuple_t addr_info
local and remote addresses
Definition: coap_io.h:218
coap_socket_t sock
socket object for the interface, if any
unsigned char uint8_t
Definition: uthash.h:79
The CoAP stack&#39;s global state is stored in a coap_context_t object.
Definition: net.h:147
struct coap_session_t * session
Definition: coap_io.h:63