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 #ifdef WITH_LWIP
19 # include <lwip/udp.h>
20 #endif
21 
22 /**
23  * Abstract handle that is used to identify a local network interface.
24  */
25 typedef int coap_if_handle_t;
26 
27 /** Invalid interface handle */
28 #define COAP_IF_INVALID -1
29 
30 struct coap_packet_t;
31 typedef struct coap_packet_t coap_packet_t;
32 
33 struct coap_context_t;
34 
35 /**
36  * Abstraction of virtual endpoint that can be attached to coap_context_t. The
37  * tuple (handle, addr) must uniquely identify this endpoint.
38  */
39 typedef struct coap_endpoint_t {
40 #if defined(WITH_POSIX) || defined(WITH_CONTIKI)
41   union {
42     int fd;       /**< on POSIX systems */
43     void *conn;   /**< opaque connection (e.g. uip_conn in Contiki) */
44   } handle;       /**< opaque handle to identify this endpoint */
45 #endif /* WITH_POSIX or WITH_CONTIKI */
46 
47 #ifdef WITH_LWIP
48   struct udp_pcb *pcb;
49  /**< @FIXME --chrysn
50   * this was added in a hurry, not sure it confirms to the overall model */
51   struct coap_context_t *context;
52 #endif /* WITH_LWIP */
53 
54   coap_address_t addr; /**< local interface address */
55   int ifindex;
56   int flags;
57 } coap_endpoint_t;
58 
59 #define COAP_ENDPOINT_NOSEC 0x00
60 #define COAP_ENDPOINT_DTLS  0x01
61 
62 coap_endpoint_t *coap_new_endpoint(const coap_address_t *addr, int flags);
63 
64 void coap_free_endpoint(coap_endpoint_t *ep);
65 
66 /**
67  * Function interface for data transmission. This function returns the number of
68  * bytes that have been transmitted, or a value less than zero on error.
69  *
70  * @param context          The calling CoAP context.
71  * @param local_interface  The local interface to send the data.
72  * @param dst              The address of the receiver.
73  * @param data             The data to send.
74  * @param datalen          The actual length of @p data.
75  *
76  * @return                 The number of bytes written on success, or a value
77  *                         less than zero on error.
78  */
79 ssize_t coap_network_send(struct coap_context_t *context,
80                           const coap_endpoint_t *local_interface,
81                           const coap_address_t *dst,
82                           unsigned char *data, size_t datalen);
83 
84 /**
85  * Function interface for reading data. This function returns the number of
86  * bytes that have been read, or a value less than zero on error. In case of an
87  * error, @p *packet is set to NULL.
88  *
89  * @param ep     The endpoint that is used for reading data from the network.
90  * @param packet A result parameter where a pointer to the received packet
91  *               structure is stored. The caller must call coap_free_packet to
92  *               release the storage used by this packet.
93  *
94  * @return       The number of bytes received on success, or a value less than
95  *               zero on error.
96  */
97 ssize_t coap_network_read(coap_endpoint_t *ep, coap_packet_t **packet);
98 
99 #ifndef coap_mcast_interface
100 # define coap_mcast_interface(Local) 0
101 #endif
102 
103 /**
104  * Releases the storage allocated for @p packet.
105  */
106 void coap_free_packet(coap_packet_t *packet);
107 
108 /**
109  * Populate the coap_endpoint_t *target from the incoming packet's destination
110  * data.
111  *
112  * This is usually used to copy a packet's data into a node's local_if member.
113  */
114 void coap_packet_populate_endpoint(coap_packet_t *packet,
115                                    coap_endpoint_t *target);
116 
117 /**
118  * Given an incoming packet, copy its source address into an address struct.
119  */
120 void coap_packet_copy_source(coap_packet_t *packet, coap_address_t *target);
121 
122 /**
123  * Given a packet, set msg and msg_len to an address and length of the packet's
124  * data in memory.
125  * */
126 void coap_packet_get_memmapped(coap_packet_t *packet,
127                                unsigned char **address,
128                                size_t *length);
129 
130 #ifdef WITH_LWIP
131 /**
132  * Get the pbuf of a packet. The caller takes over responsibility for freeing
133  * the pbuf.
134  */
135 struct pbuf *coap_packet_extract_pbuf(coap_packet_t *packet);
136 #endif
137 
138 #ifdef WITH_CONTIKI
139 /*
140  * This is only included in coap_io.h instead of .c in order to be available for
141  * sizeof in mem.c.
142  */
143 struct coap_packet_t {
144   coap_if_handle_t hnd;         /**< the interface handle */
145   coap_address_t src;           /**< the packet's source address */
146   coap_address_t dst;           /**< the packet's destination address */
147   const coap_endpoint_t *interface;
148   int ifindex;
149   void *session;                /**< opaque session data */
150   size_t length;                /**< length of payload */
151   unsigned char payload[];      /**< payload */
152 };
153 #endif
154 
155 #ifdef WITH_LWIP
156 /*
157  * This is only included in coap_io.h instead of .c in order to be available for
158  * sizeof in lwippools.h.
159  * Simple carry-over of the incoming pbuf that is later turned into a node.
160  *
161  * Source address data is currently side-banded via ip_current_dest_addr & co
162  * as the packets have limited lifetime anyway.
163  */
164 struct coap_packet_t {
165   struct pbuf *pbuf;
166   const coap_endpoint_t *local_interface;
167   uint16_t srcport;
168 };
169 #endif
170 
171 #endif /* _COAP_IO_H_ */
172