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 { 50 coap_address_t remote; /**< remote address and port */ 51 coap_address_t local; /**< local address and port */ 52 } 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 60 coap_fd_t fd; 61 #endif /* WITH_LWIP */ 62 coap_socket_flags_t flags; 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. */ 71 } coap_socket_t; 72 73 /** 74 * coap_socket_flags_t values 75 */ 76 #define COAP_SOCKET_EMPTY 0x0000 /**< the socket is not used */ 77 #define COAP_SOCKET_NOT_EMPTY 0x0001 /**< the socket is not empty */ 78 #define COAP_SOCKET_BOUND 0x0002 /**< the socket is bound */ 79 #define COAP_SOCKET_CONNECTED 0x0004 /**< the socket is connected */ 80 #define COAP_SOCKET_WANT_READ 0x0010 /**< non blocking socket is waiting for reading */ 81 #define COAP_SOCKET_WANT_WRITE 0x0020 /**< non blocking socket is waiting for writing */ 82 #define COAP_SOCKET_WANT_ACCEPT 0x0040 /**< non blocking server socket is waiting for accept */ 83 #define COAP_SOCKET_WANT_CONNECT 0x0080 /**< non blocking client socket is waiting for connect */ 84 #define COAP_SOCKET_CAN_READ 0x0100 /**< non blocking socket can now read without blocking */ 85 #define COAP_SOCKET_CAN_WRITE 0x0200 /**< non blocking socket can now write without blocking */ 86 #define COAP_SOCKET_CAN_ACCEPT 0x0400 /**< non blocking server socket can now accept without blocking */ 87 #define COAP_SOCKET_CAN_CONNECT 0x0800 /**< non blocking client socket can now connect without blocking */ 88 #define COAP_SOCKET_MULTICAST 0x1000 /**< socket is used for multicast communication */ 89 90 struct coap_endpoint_t *coap_malloc_endpoint( void ); 91 void coap_mfree_endpoint( struct coap_endpoint_t *ep ); 92 93 int 94 coap_socket_connect_udp(coap_socket_t *sock, 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 102 coap_socket_bind_udp(coap_socket_t *sock, 103 const coap_address_t *listen_addr, 104 coap_address_t *bound_addr ); 105 106 #ifndef COAP_NO_TCP 107 int 108 coap_socket_connect_tcp1(coap_socket_t *sock, 109 const coap_address_t *local_if, 110 const coap_address_t *server, 111 int default_port, 112 coap_address_t *local_addr, 113 coap_address_t *remote_addr); 114 115 int 116 coap_socket_connect_tcp2(coap_socket_t *sock, 117 coap_address_t *local_addr, 118 coap_address_t *remote_addr); 119 120 int 121 coap_socket_bind_tcp(coap_socket_t *sock, 122 const coap_address_t *listen_addr, 123 coap_address_t *bound_addr); 124 125 int 126 coap_socket_accept_tcp(coap_socket_t *server, 127 coap_socket_t *new_client, 128 coap_address_t *local_addr, 129 coap_address_t *remote_addr); 130 #endif 131 132 void coap_socket_close(coap_socket_t *sock); 133 134 ssize_t 135 coap_socket_send( coap_socket_t *sock, struct coap_session_t *session, 136 const uint8_t *data, size_t data_len ); 137 138 ssize_t 139 coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len); 140 141 ssize_t 142 coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len); 143 144 void 145 coap_epoll_ctl_mod(coap_socket_t *sock, uint32_t events, const char *func); 146 147 #ifdef WITH_LWIP 148 ssize_t 149 coap_socket_send_pdu( coap_socket_t *sock, struct coap_session_t *session, 150 struct coap_pdu_t *pdu ); 151 #endif 152 153 const char *coap_socket_strerror( void ); 154 155 /** 156 * Function interface for data transmission. This function returns the number of 157 * bytes that have been transmitted, or a value less than zero on error. 158 * 159 * @param sock Socket to send data with 160 * @param session Addressing information for unconnected sockets, or NULL 161 * @param data The data to send. 162 * @param datalen The actual length of @p data. 163 * 164 * @return The number of bytes written on success, or a value 165 * less than zero on error. 166 */ 167 ssize_t coap_network_send( coap_socket_t *sock, const struct coap_session_t *session, const uint8_t *data, size_t datalen ); 168 169 /** 170 * Function interface for reading data. This function returns the number of 171 * bytes that have been read, or a value less than zero on error. In case of an 172 * error, @p *packet is set to NULL. 173 * 174 * @param sock Socket to read data from 175 * @param packet Received packet metadata and payload. src and dst should be preset. 176 * 177 * @return The number of bytes received on success, or a value less than 178 * zero on error. 179 */ 180 ssize_t coap_network_read( coap_socket_t *sock, struct coap_packet_t *packet ); 181 182 #ifndef coap_mcast_interface 183 # define coap_mcast_interface(Local) 0 184 #endif 185 186 /** 187 * Given a packet, set msg and msg_len to an address and length of the packet's 188 * data in memory. 189 * */ 190 void coap_packet_get_memmapped(struct coap_packet_t *packet, 191 unsigned char **address, 192 size_t *length); 193 194 #ifdef WITH_LWIP 195 /** 196 * Get the pbuf of a packet. The caller takes over responsibility for freeing 197 * the pbuf. 198 */ 199 struct pbuf *coap_packet_extract_pbuf(struct coap_packet_t *packet); 200 #endif 201 202 #if defined(WITH_LWIP) 203 /* 204 * This is only included in coap_io.h instead of .c in order to be available for 205 * sizeof in lwippools.h. 206 * Simple carry-over of the incoming pbuf that is later turned into a node. 207 * 208 * Source address data is currently side-banded via ip_current_dest_addr & co 209 * as the packets have limited lifetime anyway. 210 */ 211 struct coap_packet_t { 212 struct pbuf *pbuf; 213 const struct coap_endpoint_t *local_interface; 214 coap_addr_tuple_t addr_info; /**< local and remote addresses */ 215 int ifindex; /**< the interface index */ 216 // uint16_t srcport; 217 }; 218 #else 219 struct coap_packet_t { 220 coap_addr_tuple_t addr_info; /**< local and remote addresses */ 221 int ifindex; /**< the interface index */ 222 size_t length; /**< length of payload */ 223 unsigned char payload[COAP_RXBUFFER_SIZE]; /**< payload */ 224 }; 225 #endif 226 typedef struct coap_packet_t coap_packet_t; 227 228 typedef enum { 229 COAP_NACK_TOO_MANY_RETRIES, 230 COAP_NACK_NOT_DELIVERABLE, 231 COAP_NACK_RST, 232 COAP_NACK_TLS_FAILED, 233 COAP_NACK_ICMP_ISSUE 234 } coap_nack_reason_t; 235 236 #endif /* COAP_IO_H_ */ 237