00001 /** 00002 * \addtogroup net 00003 * @{ 00004 */ 00005 00006 /** 00007 * \defgroup tcpip The Contiki/uIP interface 00008 * @{ 00009 * 00010 * TCP/IP support in Contiki is implemented using the uIP TCP/IP 00011 * stack. For sending and receiving data, Contiki uses the functions 00012 * provided by the uIP module, but Contiki adds a set of functions for 00013 * connection management. The connection management functions make 00014 * sure that the uIP TCP/IP connections are connected to the correct 00015 * process. 00016 * 00017 * Contiki also includes an optional protosocket library that provides 00018 * an API similar to the BSD socket API. 00019 * 00020 * \sa \ref uip "The uIP TCP/IP stack" 00021 * \sa \ref psock "Protosockets library" 00022 * 00023 */ 00024 00025 /** 00026 * \file 00027 * Header for the Contiki/uIP interface. 00028 * \author 00029 * Adam Dunkels <adam@sics.se> 00030 */ 00031 /* 00032 * Copyright (c) 2004, Swedish Institute of Computer Science. 00033 * All rights reserved. 00034 * 00035 * Redistribution and use in source and binary forms, with or without 00036 * modification, are permitted provided that the following conditions 00037 * are met: 00038 * 1. Redistributions of source code must retain the above copyright 00039 * notice, this list of conditions and the following disclaimer. 00040 * 2. Redistributions in binary form must reproduce the above copyright 00041 * notice, this list of conditions and the following disclaimer in the 00042 * documentation and/or other materials provided with the distribution. 00043 * 3. Neither the name of the Institute nor the names of its contributors 00044 * may be used to endorse or promote products derived from this software 00045 * without specific prior written permission. 00046 * 00047 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 00048 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00049 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00050 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 00051 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00052 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00053 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00054 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00055 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00056 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00057 * SUCH DAMAGE. 00058 * 00059 * This file is part of the Contiki operating system. 00060 * 00061 * Author: Adam Dunkels <adam@sics.se> 00062 * 00063 * $Id: tcpip.h,v 1.1 2006/06/17 22:41:19 adamdunkels Exp $ 00064 */ 00065 #ifndef __TCPIP_H__ 00066 #define __TCPIP_H__ 00067 00068 #include "contiki.h" 00069 00070 struct uip_conn; 00071 00072 struct tcpip_uipstate { 00073 struct process *p; 00074 void *state; 00075 }; 00076 00077 #define UIP_APPCALL tcpip_uipcall 00078 #define UIP_UDP_APPCALL tcpip_uipcall 00079 /*#define UIP_APPSTATE_SIZE sizeof(struct tcpip_uipstate)*/ 00080 00081 typedef struct tcpip_uipstate uip_udp_appstate_t; 00082 typedef struct tcpip_uipstate uip_tcp_appstate_t; 00083 00084 #include "net/uip.h" 00085 00086 void tcpip_uipcall(void); 00087 00088 /** 00089 * \name TCP functions 00090 * @{ 00091 */ 00092 00093 /** 00094 * Attach a TCP connection to the current process 00095 * 00096 * This function attaches the current process to a TCP 00097 * connection. Each TCP connection must be attached to a process in 00098 * order for the process to be able to receive and send 00099 * data. Additionally, this function can add a pointer with connection 00100 * state to the connection. 00101 * 00102 * \param conn A pointer to the TCP connection. 00103 * 00104 * \param appstate An opaque pointer that will be passed to the 00105 * process whenever an event occurs on the connection. 00106 * 00107 */ 00108 void tcp_attach(struct uip_conn *conn, 00109 void *appstate); 00110 #define tcp_markconn(conn, appstate) tcp_attach(conn, appstate) 00111 00112 /** 00113 * Open a TCP port. 00114 * 00115 * This function opens a TCP port for listening. When a TCP connection 00116 * request occurs for the port, the process will be sent a tcpip_event 00117 * with the new connection request. 00118 * 00119 * \note Port numbers must always be given in network byte order. The 00120 * functions HTONS() and htons() can be used to convert port numbers 00121 * from host byte order to network byte order. 00122 * 00123 * \param port The port number in network byte order. 00124 * 00125 */ 00126 void tcp_listen(u16_t port); 00127 00128 /** 00129 * Close a listening TCP port. 00130 * 00131 * This function closes a listening TCP port. 00132 * 00133 * \note Port numbers must always be given in network byte order. The 00134 * functions HTONS() and htons() can be used to convert port numbers 00135 * from host byte order to network byte order. 00136 * 00137 * \param port The port number in network byte order. 00138 * 00139 */ 00140 void tcp_unlisten(u16_t port); 00141 00142 /** 00143 * Open a TCP connection to the specified IP address and port. 00144 * 00145 * This function opens a TCP connection to the specified port at the 00146 * host specified with an IP address. Additionally, an opaque pointer 00147 * can be attached to the connection. This pointer will be sent 00148 * together with uIP events to the process. 00149 * 00150 * \note The port number must be provided in network byte order so a 00151 * conversion with HTONS() usually is necessary. 00152 * 00153 * \note This function will only create the connection. The connection 00154 * is not opened directly. uIP will try to open the connection the 00155 * next time the uIP stack is scheduled by Contiki. 00156 * 00157 * \param ripaddr Pointer to the IP address of the remote host. 00158 * \param port Port number in network byte order. 00159 * \param appstate Pointer to application defined data. 00160 * 00161 * \return A pointer to the newly created connection, or NULL if 00162 * memory could not be allocated for the connection. 00163 * 00164 */ 00165 struct uip_conn *tcp_connect(u16_t *ripaddr, u16_t port, 00166 void *appstate); 00167 00168 /** 00169 * Cause a specified TCP connection to be polled. 00170 * 00171 * This function causes uIP to poll the specified TCP connection. The 00172 * function is used when the application has data that is to be sent 00173 * immediately and do not wish to wait for the periodic uIP polling 00174 * mechanism. 00175 * 00176 * \param conn A pointer to the TCP connection that should be polled. 00177 * 00178 */ 00179 void tcpip_poll_tcp(struct uip_conn *conn); 00180 00181 /** @} */ 00182 00183 /** 00184 * \name UDP functions 00185 * @{ 00186 */ 00187 00188 struct uip_udp_conn; 00189 /** 00190 * Attach the current process to a UDP connection 00191 * 00192 * This function attaches the current process to a UDP 00193 * connection. Each UDP connection must have a process attached to it 00194 * in order for the process to be able to receive and send data over 00195 * the connection. Additionally, this function can add a pointer with 00196 * connection state to the connection. 00197 * 00198 * \param conn A pointer to the UDP connection. 00199 * 00200 * \param appstate An opaque pointer that will be passed to the 00201 * process whenever an event occurs on the connection. 00202 * 00203 */ 00204 void udp_attach(struct uip_udp_conn *conn, 00205 void *appstate); 00206 #define udp_markconn(conn, appstate) udp_attach(conn, appstate) 00207 00208 /** 00209 * Create a new UDP connection. 00210 * 00211 * This function creates a new UDP connection with the specified 00212 * remote endpoint. 00213 * 00214 * \note The port number must be provided in network byte order so a 00215 * conversion with HTONS() usually is necessary. 00216 * 00217 * \sa udp_bind() 00218 * 00219 * \param ripaddr Pointer to the IP address of the remote host. 00220 * \param port Port number in network byte order. 00221 * \param appstate Pointer to application defined data. 00222 * 00223 * \return A pointer to the newly created connection, or NULL if 00224 * memory could not be allocated for the connection. 00225 */ 00226 struct uip_udp_conn *udp_new(u16_t *ripaddr, u16_t port, 00227 void *appstate); 00228 00229 /** 00230 * Create a new UDP broadcast connection. 00231 * 00232 * This function creates a new (link-local) broadcast UDP connection 00233 * to a specified port. 00234 * 00235 * \param port Port number in network byte order. 00236 * \param appstate Pointer to application defined data. 00237 * 00238 * \return A pointer to the newly created connection, or NULL if 00239 * memory could not be allocated for the connection. 00240 */ 00241 struct uip_udp_conn *udp_broadcast_new(u16_t port, void *appstate); 00242 00243 /** 00244 * Bind a UDP connection to a local port. 00245 * 00246 * This function binds a UDP conncetion to a specified local port. 00247 * 00248 * When a connction is created with udp_new(), it gets a local port 00249 * number assigned automatically. If the application needs to bind the 00250 * connection to a specified local port, this function should be used. 00251 * 00252 * \note The port number must be provided in network byte order so a 00253 * conversion with HTONS() usually is necessary. 00254 * 00255 * \param conn A pointer to the UDP connection that is to be bound. 00256 * \param port The port number in network byte order to which to bind 00257 * the connection. 00258 */ 00259 #define udp_bind(conn, port) uip_udp_bind(conn, port) 00260 00261 /** 00262 * Cause a specified UDP connection to be polled. 00263 * 00264 * This function causes uIP to poll the specified UDP connection. The 00265 * function is used when the application has data that is to be sent 00266 * immediately and do not wish to wait for the periodic uIP polling 00267 * mechanism. 00268 * 00269 * \param conn A pointer to the UDP connection that should be polled. 00270 * 00271 */ 00272 void tcpip_poll_udp(struct uip_udp_conn *conn); 00273 00274 /** @} */ 00275 00276 /** 00277 * The uIP event. 00278 * 00279 * This event is posted to a process whenever a uIP event has occured. 00280 */ 00281 extern process_event_t tcpip_event; 00282 00283 /** 00284 * \name TCP/IP packet processing 00285 * @{ 00286 */ 00287 00288 /** 00289 * \brief Deliver an incoming packet to the TCP/IP stack 00290 * 00291 * This function is called by network device drivers to 00292 * deliver an incoming packet to the TCP/IP stack. The 00293 * incoming packet must be present in the uip_buf buffer, 00294 * and the length of the packet must be in the global 00295 * uip_len variable. 00296 */ 00297 void tcpip_input(void); 00298 00299 void tcpip_output(void); 00300 void tcpip_set_forwarding(unsigned char f); 00301 00302 /** @} */ 00303 00304 PROCESS_NAME(tcpip_process); 00305 00306 #endif /* __TCPIP_H__ */ 00307 00308 /** @} */ 00309 /** @} */