libcoap  4.3.0beta
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 #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 
48 typedef struct coap_addr_tuple_t {
52 
53 typedef struct coap_socket_t {
54 #if defined(WITH_LWIP)
55  struct udp_pcb *pcb;
56 #elif defined(WITH_CONTIKI)
57  void *conn;
58 #else
60 #endif /* WITH_LWIP */
61 #if defined(RIOT_VERSION)
62  gnrc_pktsnip_t *pkt; /* pointer to received packet for processing */
63 #endif /* RIOT_VERSION */
65  struct coap_session_t *session; /* Used by the epoll logic for an active session.
66  Note: It must mot be wrapped with COAP_EPOLL_SUPPORT as
67  coap_socket_t is seen in applications embedded in
68  coap_session_t etc. */
69  coap_endpoint_t *endpoint; /* Used by the epoll logic for a listening
70  endpoint.
71  Note: It must mot be wrapped with
72  COAP_EPOLL_SUPPORT as coap_socket_t is seen in
73  applications embedded in coap_session_t etc. */
75 
79 #define COAP_SOCKET_EMPTY 0x0000
80 #define COAP_SOCKET_NOT_EMPTY 0x0001
81 #define COAP_SOCKET_BOUND 0x0002
82 #define COAP_SOCKET_CONNECTED 0x0004
83 #define COAP_SOCKET_WANT_READ 0x0010
84 #define COAP_SOCKET_WANT_WRITE 0x0020
85 #define COAP_SOCKET_WANT_ACCEPT 0x0040
86 #define COAP_SOCKET_WANT_CONNECT 0x0080
87 #define COAP_SOCKET_CAN_READ 0x0100
88 #define COAP_SOCKET_CAN_WRITE 0x0200
89 #define COAP_SOCKET_CAN_ACCEPT 0x0400
90 #define COAP_SOCKET_CAN_CONNECT 0x0800
91 #define COAP_SOCKET_MULTICAST 0x1000
95 
96 int
98  const coap_address_t *local_if,
99  const coap_address_t *server,
100  int default_port,
101  coap_address_t *local_addr,
102  coap_address_t *remote_addr);
103 
104 int
106  const coap_address_t *listen_addr,
107  coap_address_t *bound_addr );
108 
110 
111 ssize_t
113  const uint8_t *data, size_t data_len );
114 
115 ssize_t
116 coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len);
117 
118 ssize_t
119 coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len);
120 
121 void
122 coap_epoll_ctl_mod(coap_socket_t *sock, uint32_t events, const char *func);
123 
124 #ifdef WITH_LWIP
125 ssize_t
126 coap_socket_send_pdu( coap_socket_t *sock, struct coap_session_t *session,
127  struct coap_pdu_t *pdu );
128 #endif
129 
130 const char *coap_socket_strerror( void );
131 
132 const char *coap_socket_format_errno(int error);
133 
139 int coap_tcp_is_supported(void);
140 
153 ssize_t coap_network_send( coap_socket_t *sock, const struct coap_session_t *session, const uint8_t *data, size_t datalen );
154 
166 ssize_t coap_network_read( coap_socket_t *sock, struct coap_packet_t *packet );
167 
168 #ifndef coap_mcast_interface
169 # define coap_mcast_interface(Local) 0
170 #endif
171 
177  unsigned char **address,
178  size_t *length);
179 
180 #ifdef WITH_LWIP
185 struct pbuf *coap_packet_extract_pbuf(struct coap_packet_t *packet);
186 #endif
187 
188 #if defined(WITH_LWIP)
189 /*
190  * This is only included in coap_io.h instead of .c in order to be available for
191  * sizeof in lwippools.h.
192  * Simple carry-over of the incoming pbuf that is later turned into a node.
193  *
194  * Source address data is currently side-banded via ip_current_dest_addr & co
195  * as the packets have limited lifetime anyway.
196  */
197 struct coap_packet_t {
198  struct pbuf *pbuf;
199  const coap_endpoint_t *local_interface;
201  int ifindex;
202 // uint16_t srcport;
203 };
204 #else
207  int ifindex;
208  size_t length;
209  unsigned char payload[COAP_RXBUFFER_SIZE];
210 };
211 #endif
212 typedef struct coap_packet_t coap_packet_t;
213 
214 typedef enum {
221 
222 #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:457
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:347
#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:406
ssize_t coap_network_read(coap_socket_t *sock, struct coap_packet_t *packet)
Function interface for reading data.
Definition: coap_io.c:736
coap_nack_reason_t
Definition: coap_io.h:214
@ COAP_NACK_NOT_DELIVERABLE
Definition: coap_io.h:216
@ COAP_NACK_TOO_MANY_RETRIES
Definition: coap_io.h:215
@ COAP_NACK_TLS_FAILED
Definition: coap_io.h:218
@ COAP_NACK_ICMP_ISSUE
Definition: coap_io.h:219
@ COAP_NACK_RST
Definition: coap_io.h:217
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:1465
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:729
int coap_fd_t
Definition: coap_io.h:40
const char * coap_socket_strerror(void)
Definition: coap_io.c:1459
const char * coap_socket_format_errno(int error)
Definition: coap_io.c:1456
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
coap_address_t remote
remote address and port
Definition: coap_io.h:49
coap_address_t local
local address and port
Definition: coap_io.h:50
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:208
coap_addr_tuple_t addr_info
local and remote addresses
Definition: coap_io.h:206
unsigned char payload[COAP_RXBUFFER_SIZE]
payload
Definition: coap_io.h:209
int ifindex
the interface index
Definition: coap_io.h:207
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_address_t local_if
optional local interface address
Definition: coap_session.h:67
coap_endpoint_t * endpoint
Definition: coap_io.h:69
coap_fd_t fd
Definition: coap_io.h:59
struct coap_session_t * session
Definition: coap_io.h:65
coap_socket_flags_t flags
Definition: coap_io.h:64
unsigned int uint32_t
Definition: uthash.h:78
unsigned char uint8_t
Definition: uthash.h:79