1 /* 2 * coap_io.h -- Default network I/O functions for libcoap 3 * 4 * Copyright (C) 2012-2013 Olaf Bergmann <bergmann@tzi.org> 5 * Copyright (c) 2021 Huawei Device Co., Ltd. All rights reserved. 6 * 7 * This file is part of the CoAP library libcoap. Please see README for terms 8 * of use. 9 */ 10 11 #ifndef COAP_IO_H_ 12 #define COAP_IO_H_ 13 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 /* 23 * It may may make sense to define this larger on busy systems 24 * (lots of sessions, large number of which are active), by using 25 * -DCOAP_MAX_EPOLL_EVENTS=nn at compile time. 26 */ 27 #ifndef COAP_MAX_EPOLL_EVENTS 28 #define COAP_MAX_EPOLL_EVENTS 10 29 #endif /* COAP_MAX_EPOLL_EVENTS */ 30 31 #ifdef _WIN32 32 typedef SOCKET coap_fd_t; 33 #define coap_closesocket closesocket 34 #define COAP_SOCKET_ERROR SOCKET_ERROR 35 #define COAP_INVALID_SOCKET INVALID_SOCKET 36 #else 37 typedef int coap_fd_t; 38 #define coap_closesocket close 39 #define COAP_SOCKET_ERROR (-1) 40 #define COAP_INVALID_SOCKET (-1) 41 #endif 42 43 struct coap_packet_t; 44 struct coap_session_t; 45 struct coap_context_t; 46 struct coap_pdu_t; 47 48 typedef uint16_t coap_socket_flags_t; 49 50 typedef struct coap_addr_tuple_t { 51 coap_address_t remote; /**< remote address and port */ 52 coap_address_t local; /**< local address and port */ 53 } coap_addr_tuple_t; 54 55 typedef struct coap_socket_t { 56 #if defined(WITH_LWIP) 57 struct udp_pcb *pcb; 58 #elif defined(WITH_CONTIKI) 59 void *conn; 60 #else 61 coap_fd_t fd; 62 #endif /* WITH_LWIP */ 63 coap_socket_flags_t flags; 64 struct coap_session_t *session; /* Used by the epoll logic for an active session. 65 Note: It must mot be wrapped with COAP_EPOLL_SUPPORT as 66 coap_socket_t is seen in applications embedded in 67 coap_session_t etc. */ 68 struct coap_endpoint_t *endpoint; /* Used by the epoll logic for a listening endpoint. 69 Note: It must mot be wrapped with COAP_EPOLL_SUPPORT as 70 coap_socket_t is seen in applications embedded in 71 coap_session_t etc. */ 72 } coap_socket_t; 73 74 /** 75 * coap_socket_flags_t values 76 */ 77 #define COAP_SOCKET_EMPTY 0x0000 /**< the socket is not used */ 78 #define COAP_SOCKET_NOT_EMPTY 0x0001 /**< the socket is not empty */ 79 #define COAP_SOCKET_BOUND 0x0002 /**< the socket is bound */ 80 #define COAP_SOCKET_CONNECTED 0x0004 /**< the socket is connected */ 81 #define COAP_SOCKET_WANT_READ 0x0010 /**< non blocking socket is waiting for reading */ 82 #define COAP_SOCKET_WANT_WRITE 0x0020 /**< non blocking socket is waiting for writing */ 83 #define COAP_SOCKET_WANT_ACCEPT 0x0040 /**< non blocking server socket is waiting for accept */ 84 #define COAP_SOCKET_WANT_CONNECT 0x0080 /**< non blocking client socket is waiting for connect */ 85 #define COAP_SOCKET_CAN_READ 0x0100 /**< non blocking socket can now read without blocking */ 86 #define COAP_SOCKET_CAN_WRITE 0x0200 /**< non blocking socket can now write without blocking */ 87 #define COAP_SOCKET_CAN_ACCEPT 0x0400 /**< non blocking server socket can now accept without blocking */ 88 #define COAP_SOCKET_CAN_CONNECT 0x0800 /**< non blocking client socket can now connect without blocking */ 89 #define COAP_SOCKET_MULTICAST 0x1000 /**< socket is used for multicast communication */ 90 #define COAP_SOCKET_BROADCAST 0x2000 /**< socket is used for broadcast communication */ 91 92 struct coap_endpoint_t *coap_malloc_endpoint( void ); 93 void coap_mfree_endpoint( struct coap_endpoint_t *ep ); 94 95 int 96 coap_socket_connect_udp(coap_socket_t *sock, 97 const coap_address_t *local_if, 98 const coap_address_t *server, 99 int default_port, 100 coap_address_t *local_addr, 101 coap_address_t *remote_addr); 102 103 int 104 coap_socket_bind_udp(coap_socket_t *sock, 105 const coap_address_t *listen_addr, 106 coap_address_t *bound_addr ); 107 108 int 109 coap_socket_connect_tcp1(coap_socket_t *sock, 110 const coap_address_t *local_if, 111 const coap_address_t *server, 112 int default_port, 113 coap_address_t *local_addr, 114 coap_address_t *remote_addr); 115 116 int 117 coap_socket_connect_tcp2(coap_socket_t *sock, 118 coap_address_t *local_addr, 119 coap_address_t *remote_addr); 120 121 int 122 coap_socket_bind_tcp(coap_socket_t *sock, 123 const coap_address_t *listen_addr, 124 coap_address_t *bound_addr); 125 126 int 127 coap_socket_accept_tcp(coap_socket_t *server, 128 coap_socket_t *new_client, 129 coap_address_t *local_addr, 130 coap_address_t *remote_addr); 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