Contiki-NG
Loading...
Searching...
No Matches
tcpip.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2004, Swedish Institute of Computer Science.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * This file is part of the Contiki operating system.
30 *
31 * Author: Adam Dunkels <adam@sics.se>
32 *
33 */
34
35/**
36 * \file
37 * Header for the Contiki/uIP interface.
38 * \author Adam Dunkels <adam@sics.se>
39 * \author Mathilde Durvy <mdurvy@cisco.com> (IPv6 related code)
40 * \author Julien Abeille <jabeille@cisco.com> (IPv6 related code)
41 */
42
43/**
44 * \addtogroup uip
45 * @{
46 */
47
48/**
49 * \defgroup tcpip The Contiki/uIP interface
50 * @{
51 *
52 * TCP/IP support in Contiki is implemented using the uIP TCP/IP
53 * stack. For sending and receiving data, Contiki uses the functions
54 * provided by the uIP module, but Contiki adds a set of functions for
55 * connection management. The connection management functions make
56 * sure that the uIP TCP/IP connections are connected to the correct
57 * process.
58 *
59 * Contiki also includes an optional protosocket library that provides
60 * an API similar to the BSD socket API.
61 *
62 * \sa \ref uip "The uIP TCP/IP stack"
63 * \sa \ref psock "Protosockets library"
64 *
65 */
66
67#ifndef TCPIP_H_
68#define TCPIP_H_
69
70#include "contiki.h"
71
72struct uip_conn;
73
74struct tcpip_uipstate {
75 struct process *p;
76 void *state;
77};
78
79#define UIP_APPCALL tcpip_uipcall
80#define UIP_UDP_APPCALL tcpip_uipcall
81#define UIP_ICMP6_APPCALL tcpip_icmp6_call
82
83typedef struct tcpip_uipstate uip_udp_appstate_t;
84typedef struct tcpip_uipstate uip_tcp_appstate_t;
85typedef struct tcpip_uipstate uip_icmp6_appstate_t;
86#include "net/ipv6/uip.h"
87void tcpip_uipcall(void);
88
89/**
90 * \name TCP functions
91 * @{
92 */
93
94/**
95 * Attach a TCP connection to the current process
96 *
97 * This function attaches the current process to a TCP
98 * connection. Each TCP connection must be attached to a process in
99 * order for the process to be able to receive and send
100 * data. Additionally, this function can add a pointer with connection
101 * state to the connection.
102 *
103 * \param conn A pointer to the TCP connection.
104 *
105 * \param appstate An opaque pointer that will be passed to the
106 * process whenever an event occurs on the connection.
107 *
108 */
109void tcp_attach(struct uip_conn *conn, void *appstate);
110#define tcp_markconn(conn, appstate) tcp_attach(conn, appstate)
111
112/**
113 * Open a TCP port.
114 *
115 * This function opens a TCP port for listening. When a TCP connection
116 * request occurs for the port, the process will be sent a tcpip_event
117 * with the new connection request.
118 *
119 * \note Port numbers must always be given in network byte order. The
120 * functions UIP_HTONS() and uip_htons() can be used to convert port numbers
121 * from host byte order to network byte order.
122 *
123 * \param port The port number in network byte order.
124 *
125 */
126void tcp_listen(uint16_t port);
127
128/**
129 * Close a listening TCP port.
130 *
131 * This function closes a listening TCP port.
132 *
133 * \note Port numbers must always be given in network byte order. The
134 * functions UIP_HTONS() and uip_htons() can be used to convert port numbers
135 * from host byte order to network byte order.
136 *
137 * \param port The port number in network byte order.
138 *
139 */
140void tcp_unlisten(uint16_t port);
141
142/**
143 * Open a TCP connection to the specified IP address and port.
144 *
145 * This function opens a TCP connection to the specified port at the
146 * host specified with an IP address. Additionally, an opaque pointer
147 * can be attached to the connection. This pointer will be sent
148 * together with uIP events to the process.
149 *
150 * \note The port number must be provided in network byte order so a
151 * conversion with UIP_HTONS() usually is necessary.
152 *
153 * \note This function will only create the connection. The connection
154 * is not opened directly. uIP will try to open the connection the
155 * next time the uIP stack is scheduled by Contiki.
156 *
157 * \param ripaddr Pointer to the IP address of the remote host.
158 * \param port Port number in network byte order.
159 * \param appstate Pointer to application defined data.
160 *
161 * \return A pointer to the newly created connection, or NULL if
162 * memory could not be allocated for the connection.
163 *
164 */
165struct uip_conn *tcp_connect(const uip_ipaddr_t *ripaddr, uint16_t port,
166 void *appstate);
167
168/**
169 * Cause a specified TCP connection to be polled.
170 *
171 * This function causes uIP to poll the specified TCP connection. The
172 * function is used when the application has data that is to be sent
173 * immediately and do not wish to wait for the periodic uIP polling
174 * mechanism.
175 *
176 * \param conn A pointer to the TCP connection that should be polled.
177 *
178 */
179void tcpip_poll_tcp(struct uip_conn *conn);
180
181/** @} */
182
183/**
184 * \name UDP functions
185 * @{
186 */
187
188struct uip_udp_conn;
189/**
190 * Attach the current process to a UDP connection
191 *
192 * This function attaches the current process to a UDP
193 * connection. Each UDP connection must have a process attached to it
194 * in order for the process to be able to receive and send data over
195 * the connection. Additionally, this function can add a pointer with
196 * connection state to the connection.
197 *
198 * \param conn A pointer to the UDP connection.
199 *
200 * \param appstate An opaque pointer that will be passed to the
201 * process whenever an event occurs on the connection.
202 *
203 */
204void udp_attach(struct uip_udp_conn *conn,
205 void *appstate);
206#define udp_markconn(conn, appstate) udp_attach(conn, appstate)
207
208/**
209 * Create a new UDP connection.
210 *
211 * This function creates a new UDP connection with the specified
212 * remote endpoint.
213 *
214 * \note The port number must be provided in network byte order so a
215 * conversion with UIP_HTONS() usually is necessary.
216 *
217 * \sa udp_bind()
218 *
219 * \param ripaddr Pointer to the IP address of the remote host.
220 * \param port Port number in network byte order.
221 * \param appstate Pointer to application defined data.
222 *
223 * \return A pointer to the newly created connection, or NULL if
224 * memory could not be allocated for the connection.
225 */
226struct uip_udp_conn *udp_new(const uip_ipaddr_t *ripaddr, uint16_t port,
227 void *appstate);
228
229/**
230 * Create a new UDP broadcast connection.
231 *
232 * This function creates a new (link-local) broadcast UDP connection
233 * to a specified port.
234 *
235 * \param port Port number in network byte order.
236 * \param appstate Pointer to application defined data.
237 *
238 * \return A pointer to the newly created connection, or NULL if
239 * memory could not be allocated for the connection.
240 */
241struct uip_udp_conn *udp_broadcast_new(uint16_t port, void *appstate);
242
243/**
244 * Bind a UDP connection to a local port.
245 *
246 * This function binds a UDP connection to a specified local port.
247 *
248 * When a connection is created with udp_new(), it gets a local port
249 * number assigned automatically. If the application needs to bind the
250 * connection to a specified local port, this function should be used.
251 *
252 * \note The port number must be provided in network byte order so a
253 * conversion with UIP_HTONS() usually is necessary.
254 *
255 * \param conn A pointer to the UDP connection that is to be bound.
256 * \param port The port number in network byte order to which to bind
257 * the connection.
258 */
259#define udp_bind(conn, port) uip_udp_bind(conn, port)
260
261/**
262 * Cause a specified UDP connection to be polled.
263 *
264 * This function causes uIP to poll the specified UDP connection. The
265 * function is used when the application has data that is to be sent
266 * immediately and do not wish to wait for the periodic uIP polling
267 * mechanism.
268 *
269 * \param conn A pointer to the UDP connection that should be polled.
270 *
271 */
272void tcpip_poll_udp(struct uip_udp_conn *conn);
273
274/** @} */
275
276/**
277 * \name ICMPv6 functions
278 * @{
279 */
280
281#if UIP_CONF_ICMP6
282
283/**
284 * The ICMP6 event.
285 *
286 * This event is posted to a process whenever a uIP ICMP event has occurred.
287 */
288extern process_event_t tcpip_icmp6_event;
289
290/**
291 * \brief register an ICMPv6 callback
292 * \return 0 if success, 1 if failure (one application already registered)
293 *
294 * This function just registers a process to be polled when
295 * an ICMPv6 message is received.
296 * If no application registers, some ICMPv6 packets will be
297 * processed by the "kernel" as usual (NS, NA, RS, RA, Echo request),
298 * others will be dropped.
299 * If an application registers here, it will be polled with a
300 * process_post_synch every time an ICMPv6 packet is received.
301 */
302uint8_t icmp6_new(void *appstate);
303
304/**
305 * This function is called at reception of an ICMPv6 packet
306 * If an application registered as an ICMPv6 listener (with
307 * icmp6_new), it will be called through a process_post_synch()
308 */
309void tcpip_icmp6_call(uint8_t type);
310#endif /*UIP_CONF_ICMP6*/
311
312/** @} */
313/**
314 * The uIP event.
315 *
316 * This event is posted to a process whenever a uIP event has occurred.
317 */
318extern process_event_t tcpip_event;
319
320
321/**
322 * \name TCP/IP packet processing
323 * @{
324 */
325
326/**
327 * \brief Deliver an incoming packet to the TCP/IP stack
328 *
329 * This function is called by network device drivers to
330 * deliver an incoming packet to the TCP/IP stack. The
331 * incoming packet must be present in the uip_buf buffer,
332 * and the length of the packet must be in the global
333 * uip_len variable.
334 */
335void tcpip_input(void);
336
337/**
338 * \brief Output packet to layer 2
339 * The eventual parameter is the MAC address of the destination.
340 */
341uint8_t tcpip_output(const uip_lladdr_t *);
342
343/**
344 * \brief This function does address resolution and then calls tcpip_output
345 */
346void tcpip_ipv6_output(void);
347
348/**
349 * \brief Is forwarding generally enabled?
350 */
351extern unsigned char tcpip_do_forwarding;
352
353/*
354 * Are we at the moment forwarding the contents of uip_buf[]?
355 */
356extern unsigned char tcpip_is_forwarding;
357
358
359#define tcpip_set_forwarding(forwarding) tcpip_do_forwarding = (forwarding)
360
361/** @} */
362
363PROCESS_NAME(tcpip_process);
364
365#endif /* TCPIP_H_ */
366
367/** @} */
368/** @} */
#define PROCESS_NAME(name)
Declare the name of a process.
Definition process.h:287
void tcp_unlisten(uint16_t port)
Close a listening TCP port.
Definition tcpip.c:214
void udp_attach(struct uip_udp_conn *conn, void *appstate)
Attach the current process to a UDP connection.
Definition tcpip.c:258
process_event_t tcpip_event
The uIP event.
Definition tcpip.c:62
struct uip_conn * tcp_connect(const uip_ipaddr_t *ripaddr, uint16_t port, void *appstate)
Open a TCP connection to the specified IP address and port.
struct uip_udp_conn * udp_new(const uip_ipaddr_t *ripaddr, uint16_t port, void *appstate)
Create a new UDP connection.
Definition tcpip.c:264
void tcp_attach(struct uip_conn *conn, void *appstate)
Attach a TCP connection to the current process.
Definition tcpip.c:250
uint8_t tcpip_output(const uip_lladdr_t *)
Output packet to layer 2 The eventual parameter is the MAC address of the destination.
Definition tcpip.c:108
void tcpip_icmp6_call(uint8_t type)
This function is called at reception of an ICMPv6 packet If an application registered as an ICMPv6 li...
Definition tcpip.c:304
void tcpip_poll_tcp(struct uip_conn *conn)
Cause a specified TCP connection to be polled.
Definition tcpip.c:747
struct uip_udp_conn * udp_broadcast_new(uint16_t port, void *appstate)
Create a new UDP broadcast connection.
Definition tcpip.c:278
unsigned char tcpip_do_forwarding
Is forwarding generally enabled?
uint8_t icmp6_new(void *appstate)
register an ICMPv6 callback
Definition tcpip.c:295
void tcpip_poll_udp(struct uip_udp_conn *conn)
Cause a specified UDP connection to be polled.
Definition tcpip.c:739
void tcpip_ipv6_output(void)
This function does address resolution and then calls tcpip_output.
Definition tcpip.c:619
process_event_t tcpip_icmp6_event
The ICMP6 event.
Definition tcpip.c:64
void tcpip_input(void)
Deliver an incoming packet to the TCP/IP stack.
Definition tcpip.c:433
void tcp_listen(uint16_t port)
Open a TCP port.
Definition tcpip.c:232
Representation of a uIP TCP connection.
Definition uip.h:1258
uip_ipaddr_t ripaddr
The IP address of the remote host.
Definition uip.h:1259
Representation of a uIP UDP connection.
Definition uip.h:1309
uip_ipaddr_t ripaddr
The IP address of the remote peer.
Definition uip.h:1310
uip_udp_appstate_t appstate
The application state.
Definition uip.h:1315
Header file for the uIP TCP/IP stack.