libcoap  4.2.0
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 <assert.h>
14 #include <sys/types.h>
15 
16 #include "address.h"
17 
18 #ifndef COAP_RXBUFFER_SIZE
19 #define COAP_RXBUFFER_SIZE 1472
20 #endif /* COAP_RXBUFFER_SIZE */
21 
22 #ifdef _WIN32
23 typedef SOCKET coap_fd_t;
24 #define coap_closesocket closesocket
25 #define COAP_SOCKET_ERROR SOCKET_ERROR
26 #define COAP_INVALID_SOCKET INVALID_SOCKET
27 #else
28 typedef int coap_fd_t;
29 #define coap_closesocket close
30 #define COAP_SOCKET_ERROR (-1)
31 #define COAP_INVALID_SOCKET (-1)
32 #endif
33 
34 struct coap_packet_t;
35 struct coap_session_t;
36 struct coap_pdu_t;
37 
38 typedef uint16_t coap_socket_flags_t;
39 
40 typedef struct coap_socket_t {
41 #if defined(WITH_LWIP)
42  struct udp_pcb *pcb;
43 #elif defined(WITH_CONTIKI)
44  void *conn;
45 #else
47 #endif /* WITH_LWIP */
50 
54 #define COAP_SOCKET_EMPTY 0x0000
55 #define COAP_SOCKET_NOT_EMPTY 0x0001
56 #define COAP_SOCKET_BOUND 0x0002
57 #define COAP_SOCKET_CONNECTED 0x0004
58 #define COAP_SOCKET_WANT_READ 0x0010
59 #define COAP_SOCKET_WANT_WRITE 0x0020
60 #define COAP_SOCKET_WANT_ACCEPT 0x0040
61 #define COAP_SOCKET_WANT_CONNECT 0x0080
62 #define COAP_SOCKET_CAN_READ 0x0100
63 #define COAP_SOCKET_CAN_WRITE 0x0200
64 #define COAP_SOCKET_CAN_ACCEPT 0x0400
65 #define COAP_SOCKET_CAN_CONNECT 0x0800
66 #define COAP_SOCKET_MULTICAST 0x1000
68 struct coap_endpoint_t *coap_malloc_endpoint( void );
69 void coap_mfree_endpoint( struct coap_endpoint_t *ep );
70 
71 int
73  const coap_address_t *local_if,
74  const coap_address_t *server,
75  int default_port,
76  coap_address_t *local_addr,
77  coap_address_t *remote_addr);
78 
79 int
81  const coap_address_t *listen_addr,
82  coap_address_t *bound_addr );
83 
84 int
86  const coap_address_t *local_if,
87  const coap_address_t *server,
88  int default_port,
89  coap_address_t *local_addr,
90  coap_address_t *remote_addr);
91 
92 int
94  coap_address_t *local_addr,
95  coap_address_t *remote_addr);
96 
97 int
99  const coap_address_t *listen_addr,
100  coap_address_t *bound_addr);
101 
102 int
104  coap_socket_t *new_client,
105  coap_address_t *local_addr,
106  coap_address_t *remote_addr);
107 
108 void coap_socket_close(coap_socket_t *sock);
109 
110 ssize_t
111 coap_socket_send( coap_socket_t *sock, struct coap_session_t *session,
112  const uint8_t *data, size_t data_len );
113 
114 ssize_t
115 coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len);
116 
117 ssize_t
118 coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len);
119 
120 #ifdef WITH_LWIP
121 ssize_t
122 coap_socket_send_pdu( coap_socket_t *sock, struct coap_session_t *session,
123  struct coap_pdu_t *pdu );
124 #endif
125 
126 const char *coap_socket_strerror( void );
127 
140 ssize_t coap_network_send( coap_socket_t *sock, const struct coap_session_t *session, const uint8_t *data, size_t datalen );
141 
153 ssize_t coap_network_read( coap_socket_t *sock, struct coap_packet_t *packet );
154 
155 #ifndef coap_mcast_interface
156 # define coap_mcast_interface(Local) 0
157 #endif
158 
163 void coap_packet_get_memmapped(struct coap_packet_t *packet,
164  unsigned char **address,
165  size_t *length);
166 
167 void coap_packet_set_addr( struct coap_packet_t *packet, const coap_address_t *src,
168  const coap_address_t *dst );
169 
170 #ifdef WITH_LWIP
171 
175 struct pbuf *coap_packet_extract_pbuf(struct coap_packet_t *packet);
176 #endif
177 
178 #if defined(WITH_LWIP)
179 /*
180  * This is only included in coap_io.h instead of .c in order to be available for
181  * sizeof in lwippools.h.
182  * Simple carry-over of the incoming pbuf that is later turned into a node.
183  *
184  * Source address data is currently side-banded via ip_current_dest_addr & co
185  * as the packets have limited lifetime anyway.
186  */
187 struct coap_packet_t {
188  struct pbuf *pbuf;
189  const struct coap_endpoint_t *local_interface;
190  coap_address_t src;
191  coap_address_t dst;
192  int ifindex;
193 // uint16_t srcport;
194 };
195 #else
199  int ifindex;
200  size_t length;
201  unsigned char payload[COAP_RXBUFFER_SIZE];
202 };
203 #endif
205 
206 typedef enum {
212 
213 #endif /* COAP_IO_H_ */
#define COAP_RXBUFFER_SIZE
Definition: coap_io.h:19
struct coap_socket_t coap_socket_t
void coap_socket_close(coap_socket_t *sock)
Definition: coap_io.c:626
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:527
int coap_socket_bind_udp(coap_socket_t *sock, const coap_address_t *listen_addr, coap_address_t *bound_addr)
Definition: coap_io.c:192
multi-purpose address abstraction
Definition: address.h:62
coap_fd_t fd
Definition: coap_io.h:46
int ifindex
the interface index
Definition: coap_io.h:199
coap_nack_reason_t
Definition: coap_io.h:206
Abstraction of virtual endpoint that can be attached to coap_context_t.
Definition: coap_session.h:301
int coap_socket_bind_tcp(coap_socket_t *sock, const coap_address_t *listen_addr, coap_address_t *bound_addr)
Definition: coap_io.c:413
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:268
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:912
const char * coap_socket_strerror(void)
Definition: coap_io.c:1390
int coap_fd_t
Definition: coap_io.h:28
coap_address_t src
the packet&#39;s source address
Definition: coap_io.h:197
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:373
ssize_t coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len)
Definition: coap_io.c:635
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:1396
Representation of network addresses.
void coap_mfree_endpoint(struct coap_endpoint_t *ep)
Definition: coap_io.c:186
void coap_packet_set_addr(struct coap_packet_t *packet, const coap_address_t *src, const coap_address_t *dst)
Definition: coap_io.c:917
uint16_t coap_socket_flags_t
Definition: coap_io.h:36
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.
coap_address_t dst
the packet&#39;s destination address
Definition: coap_io.h:198
size_t length
length of payload
Definition: coap_io.h:200
ssize_t coap_network_read(coap_socket_t *sock, struct coap_packet_t *packet)
Function interface for reading data.
Definition: coap_io.c:923
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:490
coap_socket_flags_t flags
Definition: coap_io.h:48
ssize_t coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len)
Definition: coap_io.c:665
unsigned char uint8_t
Definition: uthash.h:79