coap_io.h
Go to the documentation of this file.
/*
* coap_io.h -- Default network I/O functions for libcoap
*
* Copyright (C) 2012-2013 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
#ifndef COAP_IO_H_
#define COAP_IO_H_
#include <sys/types.h>
#include "address.h"
#ifndef COAP_RXBUFFER_SIZE
#define COAP_RXBUFFER_SIZE 1472
#endif /* COAP_RXBUFFER_SIZE */
/*
* It may may make sense to define this larger on busy systems
* (lots of sessions, large number of which are active), by using
* -DCOAP_MAX_EPOLL_EVENTS=nn at compile time.
*/
#ifndef COAP_MAX_EPOLL_EVENTS
#define COAP_MAX_EPOLL_EVENTS 10
#endif /* COAP_MAX_EPOLL_EVENTS */
#ifdef _WIN32
typedef SOCKET coap_fd_t;
#define coap_closesocket closesocket
#define COAP_SOCKET_ERROR SOCKET_ERROR
#define COAP_INVALID_SOCKET INVALID_SOCKET
#else
typedef int coap_fd_t;
#define coap_closesocket close
#define COAP_SOCKET_ERROR (-1)
#define COAP_INVALID_SOCKET (-1)
#endif
struct coap_packet_t;
struct coap_session_t;
struct coap_context_t;
struct coap_pdu_t;
typedef uint16_t coap_socket_flags_t;
typedef struct coap_addr_tuple_t {
coap_address_t remote;
coap_address_t local;
} coap_addr_tuple_t;
typedef struct coap_socket_t {
#if defined(WITH_LWIP)
struct udp_pcb *pcb;
#elif defined(WITH_CONTIKI)
void *conn;
#else
coap_fd_t fd;
#endif /* WITH_LWIP */
coap_socket_flags_t flags;
struct coap_session_t *session; /* Used by the epoll logic for an active session.
Note: It must mot be wrapped with COAP_EPOLL_SUPPORT as
coap_socket_t is seen in applications embedded in
coap_session_t etc. */
struct coap_endpoint_t *endpoint; /* Used by the epoll logic for a listening endpoint.
Note: It must mot be wrapped with COAP_EPOLL_SUPPORT as
coap_socket_t is seen in applications embedded in
coap_session_t etc. */
} coap_socket_t;
#define COAP_SOCKET_EMPTY 0x0000
#define COAP_SOCKET_NOT_EMPTY 0x0001
#define COAP_SOCKET_BOUND 0x0002
#define COAP_SOCKET_CONNECTED 0x0004
#define COAP_SOCKET_WANT_READ 0x0010
#define COAP_SOCKET_WANT_WRITE 0x0020
#define COAP_SOCKET_WANT_ACCEPT 0x0040
#define COAP_SOCKET_WANT_CONNECT 0x0080
#define COAP_SOCKET_CAN_READ 0x0100
#define COAP_SOCKET_CAN_WRITE 0x0200
#define COAP_SOCKET_CAN_ACCEPT 0x0400
#define COAP_SOCKET_CAN_CONNECT 0x0800
#define COAP_SOCKET_MULTICAST 0x1000
struct coap_endpoint_t *coap_malloc_endpoint( void );
void coap_mfree_endpoint( struct coap_endpoint_t *ep );
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);
int
coap_socket_bind_udp(coap_socket_t *sock,
const coap_address_t *listen_addr,
coap_address_t *bound_addr );
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);
int
coap_socket_connect_tcp2(coap_socket_t *sock,
coap_address_t *local_addr,
coap_address_t *remote_addr);
int
coap_socket_bind_tcp(coap_socket_t *sock,
const coap_address_t *listen_addr,
coap_address_t *bound_addr);
int
coap_socket_accept_tcp(coap_socket_t *server,
coap_socket_t *new_client,
coap_address_t *local_addr,
coap_address_t *remote_addr);
void coap_socket_close(coap_socket_t *sock);
ssize_t
coap_socket_send( coap_socket_t *sock, struct coap_session_t *session,
const uint8_t *data, size_t data_len );
ssize_t
coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len);
ssize_t
coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len);
void
coap_epoll_ctl_mod(coap_socket_t *sock, uint32_t events, const char *func);
#ifdef WITH_LWIP
ssize_t
coap_socket_send_pdu( coap_socket_t *sock, struct coap_session_t *session,
struct coap_pdu_t *pdu );
#endif
const char *coap_socket_strerror( void );
ssize_t coap_network_send( coap_socket_t *sock, const struct coap_session_t *session, const uint8_t *data, size_t datalen );
ssize_t coap_network_read( coap_socket_t *sock, struct coap_packet_t *packet );
#ifndef coap_mcast_interface
# define coap_mcast_interface(Local) 0
#endif
void coap_packet_get_memmapped(struct coap_packet_t *packet,
unsigned char **address,
size_t *length);
#ifdef WITH_LWIP
struct pbuf *coap_packet_extract_pbuf(struct coap_packet_t *packet);
#endif
#if defined(WITH_LWIP)
/*
* This is only included in coap_io.h instead of .c in order to be available for
* sizeof in lwippools.h.
* Simple carry-over of the incoming pbuf that is later turned into a node.
*
* Source address data is currently side-banded via ip_current_dest_addr & co
* as the packets have limited lifetime anyway.
*/
struct coap_packet_t {
struct pbuf *pbuf;
const struct coap_endpoint_t *local_interface;
coap_addr_tuple_t addr_info;
int ifindex;
// uint16_t srcport;
};
#else
struct coap_packet_t {
coap_addr_tuple_t addr_info;
int ifindex;
size_t length;
unsigned char payload[COAP_RXBUFFER_SIZE];
};
#endif
typedef struct coap_packet_t coap_packet_t;
typedef enum {
COAP_NACK_TOO_MANY_RETRIES,
COAP_NACK_NOT_DELIVERABLE,
COAP_NACK_RST,
COAP_NACK_TLS_FAILED,
COAP_NACK_ICMP_ISSUE
} coap_nack_reason_t;
#endif /* COAP_IO_H_ */
address.h
Representation of network addresses.
coap_network_send
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_socket_connect_udp
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
coap_socket_bind_tcp
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
coap_socket_flags_t
uint16_t coap_socket_flags_t
Definition: coap_io.h:45
coap_socket_read
ssize_t coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len)
Definition: coap_io.c:735
coap_socket_close
void coap_socket_close(coap_socket_t *sock)
Definition: coap_io.c:631
coap_mfree_endpoint
void coap_mfree_endpoint(struct coap_endpoint_t *ep)
Definition: coap_io.c:180
COAP_RXBUFFER_SIZE
#define COAP_RXBUFFER_SIZE
Definition: coap_io.h:18
coap_socket_write
ssize_t coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len)
Definition: coap_io.c:690
coap_network_read
ssize_t coap_network_read(coap_socket_t *sock, struct coap_packet_t *packet)
Function interface for reading data.
Definition: coap_io.c:1008
coap_malloc_endpoint
struct coap_endpoint_t * coap_malloc_endpoint(void)
Definition: coap_io.c:175
coap_nack_reason_t
coap_nack_reason_t
Definition: coap_io.h:226
COAP_NACK_NOT_DELIVERABLE
@ COAP_NACK_NOT_DELIVERABLE
Definition: coap_io.h:228
COAP_NACK_TOO_MANY_RETRIES
@ COAP_NACK_TOO_MANY_RETRIES
Definition: coap_io.h:227
COAP_NACK_TLS_FAILED
@ COAP_NACK_TLS_FAILED
Definition: coap_io.h:230
COAP_NACK_ICMP_ISSUE
@ COAP_NACK_ICMP_ISSUE
Definition: coap_io.h:231
COAP_NACK_RST
@ COAP_NACK_RST
Definition: coap_io.h:229
coap_socket_connect_tcp1
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
coap_addr_tuple_t
struct coap_addr_tuple_t coap_addr_tuple_t
coap_socket_send
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
coap_socket_bind_udp
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
coap_packet_get_memmapped
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:1002
coap_socket_accept_tcp
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
coap_fd_t
int coap_fd_t
Definition: coap_io.h:36
coap_socket_strerror
const char * coap_socket_strerror(void)
Definition: coap_io.c:1651
coap_epoll_ctl_mod
void coap_epoll_ctl_mod(coap_socket_t *sock, uint32_t events, const char *func)
coap_socket_connect_tcp2
int coap_socket_connect_tcp2(coap_socket_t *sock, coap_address_t *local_addr, coap_address_t *remote_addr)
Definition: coap_io.c:372
coap_socket_t
struct coap_socket_t coap_socket_t
coap_addr_tuple_t
Definition: coap_io.h:49
coap_addr_tuple_t::remote
coap_address_t remote
remote address and port
Definition: coap_io.h:50
coap_addr_tuple_t::local
coap_address_t local
local address and port
Definition: coap_io.h:51
coap_address_t
multi-purpose address abstraction
Definition: address.h:62
coap_context_t
The CoAP stack's global state is stored in a coap_context_t object.
Definition: net.h:147
coap_endpoint_t
Abstraction of virtual endpoint that can be attached to coap_context_t.
Definition: coap_session_internal.h:29
coap_endpoint_t::sock
coap_socket_t sock
socket object for the interface, if any
Definition: coap_session_internal.h:34
coap_packet_t
Definition: coap_io.h:217
coap_packet_t::length
size_t length
length of payload
Definition: coap_io.h:220
coap_packet_t::addr_info
coap_addr_tuple_t addr_info
local and remote addresses
Definition: coap_io.h:218
coap_packet_t::payload
unsigned char payload[COAP_RXBUFFER_SIZE]
payload
Definition: coap_io.h:221
coap_packet_t::ifindex
int ifindex
the interface index
Definition: coap_io.h:219
coap_pdu_t
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
Definition: pdu.h:285
coap_session_t
Definition: coap_session.h:59
coap_socket_t
Definition: coap_io.h:54
coap_socket_t::fd
coap_fd_t fd
Definition: coap_io.h:60
coap_socket_t::endpoint
struct coap_endpoint_t * endpoint
Definition: coap_io.h:67
coap_socket_t::session
struct coap_session_t * session
Definition: coap_io.h:63
coap_socket_t::flags
coap_socket_flags_t flags
Definition: coap_io.h:62
uint32_t
unsigned int uint32_t
Definition: uthash.h:78
uint8_t
unsigned char uint8_t
Definition: uthash.h:79