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