udp.h

Go to the documentation of this file.
00001 /*##############################################################################
00002 
00003 nIP - nano IP stack
00004 
00005 File        : udp.h
00006 
00007 Description : User Datagram Protocol
00008 
00009 Copyright notice:
00010 
00011 Copyright (C) 2005 -
00012 Andreas Dittrich, dittrich@informatik.hu-berlin.de
00013 Jon Kowal, kowal@informatik.hu-berlin.de
00014 
00015 This program is free software; you can redistribute it and/or
00016 modify it under the terms of the GNU General Public License
00017 as published by the Free Software Foundation; either version 2
00018 of the License, or (at your option) any later version.
00019 
00020 This program is distributed in the hope that it will be useful,
00021 but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00023 GNU General Public License for more details.
00024 
00025 You should have received a copy of the GNU General Public License
00026 along with this program; if not, write to the Free Software
00027 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00028 
00029 ##############################################################################*/
00030 
00031 #ifndef _NIP_UDP_H
00032  #define _NIP_UDP_H
00033 
00034 #include "nip_init.h"
00035 #include "mem.h"
00036 
00037 #ifndef NIP_UDP_MAX_SOCKETS
00038         /** Define maximum number of UDP sockets for dynamic socket array. */
00039         #define NIP_UDP_MAX_SOCKETS        255
00040 #endif
00041 
00042 #ifndef NIP_UDP_FIXED_SOCKETS
00043         /** Fixed number of UDP sockets to be used for fixed socket configuration */
00044         #define NIP_UDP_FIXED_SOCKETS      4
00045 #endif
00046 
00047 #ifndef NIP_UDP_USE_FIXED_SOCKETS
00048         /** Use fixed socket array (static allocation of memory for fixed numer of
00049          * sockets) or use dynamic array of sockets with at most NIP_UDP_MAX_SOCKETS
00050          * sockets. */
00051         #define NIP_UDP_USE_FIXED_SOCKETS  1
00052 #endif
00053 
00054 #ifndef NIP_UDP_SOCKET_CHECK_INTVL
00055         /** Any socket will not be checked for changes twice within the interval,
00056          * unless its timer has been reset.*/
00057         #define NIP_UDP_SOCKET_CHECK_INTVL 1
00058 #endif
00059 
00060 #ifndef NIP_UDP_PACKET_QUEUE_SIZE
00061         /** Each entry in packet queue has cost of sizeof(struct nip_udp_buffer) */
00062         #define NIP_UDP_PACKET_QUEUE_SIZE  4
00063 #endif
00064 
00065 #if   ( NIP_UDP_MAX_SOCKETS <  0xFF  )
00066         typedef uint8_t  nip_udp_sock_id_t;
00067         typedef uint8_t  nip_udp_sock_cnt_t;
00068         #define NIP_UDP_NO_SOCKET   0xFF
00069 #elif ( NIP_UDP_MAX_SOCKETS < 0xFFFF )
00070         typedef uint16_t nip_udp_sock_id_t;
00071         typedef uint16_t nip_udp_sock_cnt_t;
00072         #define NIP_UDP_NO_SOCKET   0xFFFF
00073 #else
00074         typedef uint32_t nip_udp_sock_id_t;
00075         typedef uint32_t nip_udp_sock_cnt_t;
00076         #define NIP_UDP_NO_SOCKET   0xFFFFFFFF
00077 #endif
00078 
00079 /** End of queue. To be used to invalidate queue pointer */
00080 #define NIP_UDP_EOQ          NIP_UDP_PACKET_QUEUE_SIZE
00081 
00082 #if   ( NIP_UDP_PACKET_QUEUE_SIZE <  255  )
00083         typedef uint8_t  nip_udp_queue_id_t;
00084         typedef uint8_t  nip_udp_queue_cnt_t;
00085 #elif ( NIP_UDP_PACKET_QUEUE_SIZE < 65535 )
00086         typedef uint16_t nip_udp_queue_id_t;
00087         typedef uint16_t nip_udp_queue_cnt_t;
00088 #else
00089         typedef uint32_t nip_udp_queue_id_t;
00090         typedef uint32_t nip_udp_queue_cnt_t;
00091 #endif
00092 
00093 #define NIP_UDP_SOCK_FLG_USED      0x01 /**< socket in use? */
00094 #define NIP_UDP_SOCK_FLG_NON_BLOCK 0x02 /**< non-blocking socket? to be set by user */
00095 #define NIP_UDP_SOCK_FLG_LISTENING 0x04 /**< bind socket to port? to be set by user */
00096 
00097 typedef enum
00098 {
00099         NIP_UDP_RX_BUF,
00100         NIP_UDP_TX_BUF
00101 }
00102 #ifndef DOXYGEN
00103 __attribute__((packed))
00104 #endif
00105 nip_udp_buf_type_t;
00106 
00107 struct nip_udp_sock_addr
00108 {
00109         uint16_t port;
00110         uint8_t  ip[4];
00111 }
00112 #ifndef DOXYGEN
00113 __attribute__((packed))
00114 #endif
00115 ;
00116 
00117 /** Item of UDP Packet Queue. Item is considered unused, when buf equals
00118  * NIP_MEM_NULL*/
00119 struct nip_udp_buffer
00120 {
00121         nip_udp_sock_id_t  sockid;
00122         nip_mem_size_t     size;         /**< current size of packet in buffer */
00123         nip_mem_handle_t   buf;          /**< dynamic buffer handle */
00124         nip_udp_queue_id_t next;         /**< next queue item for this socket */
00125         struct nip_udp_sock_addr addr;  /**< remote address + port        */
00126 }
00127 #ifndef DOXYGEN
00128 __attribute__((packed))
00129 #endif
00130 ;
00131 
00132 struct nip_udp_sock
00133 {
00134         nip_udp_sock_id_t        sockid;    /**< socket id                    */
00135         uint8_t                  flags;     /**< socket configuration         */
00136         struct nip_udp_sock_addr addr;      /**< listening address + port     */
00137         nip_mem_handle_t         tx_packet; /**< handle for transmit buffer   */
00138 
00139         nip_udp_queue_id_t       rx_packet; /**< current item-id in incoming queue*/
00140 
00141 }
00142 #ifndef DOXYGEN
00143 __attribute__((packed))
00144 #endif
00145 ;
00146 
00147 struct nip_udp_header{
00148         uint16_t src_port;
00149         uint16_t dst_port;
00150         uint16_t length;
00151         uint16_t checksum;
00152 }
00153 #ifndef DOXYGEN
00154 __attribute__((packed))
00155 #endif
00156 ;
00157 
00158 nip_udp_sock_id_t nip_udp_socket( struct nip_udp_sock_addr *addr, uint8_t flags );
00159 void nip_udp_close( nip_udp_sock_id_t sockid );
00160 
00161 void nip_udp_disp_send( void );
00162 void nip_udp_disp_receive( void );
00163 
00164 nip_error_t nip_udp_init_send( nip_udp_sock_id_t sockid, nip_mem_size_t size );
00165 nip_error_t nip_udp_write( nip_udp_sock_id_t sockid, uint8_t *buf, nip_mem_size_t size);
00166 nip_error_t nip_udp_transmit( nip_udp_sock_id_t sockid, struct nip_udp_sock_addr *dest_addr, nip_net_if_id_t if_id );
00167 nip_mem_size_t nip_udp_recvfrom( nip_udp_sock_id_t sockid, uint8_t *buf, nip_mem_size_t size, struct nip_udp_sock_addr *from_addr );
00168 
00169 void nip_udp_forward( nip_udp_sock_id_t sockid );
00170 void nip_udp_cancel_send( nip_udp_sock_id_t sockid );
00171 void nip_udp_cancel_recv( nip_udp_sock_id_t sockid );
00172 
00173 nip_mem_handle_t nip_udp_buf_handle( nip_udp_sock_id_t sockid, nip_udp_buf_type_t buftype );
00174 void *nip_udp_data_ptr( nip_udp_sock_id_t sockid, nip_udp_buf_type_t buftype );
00175 void nip_udp_data_mem_ptr( nip_udp_sock_id_t sockid, nip_udp_buf_type_t buftype, nip_mem_ptr_t *res );
00176 void nip_udp_ptr_release( nip_udp_sock_id_t sockid, nip_udp_buf_type_t buftype );
00177 
00178 #endif // _NIP_UDP_H

Generated on Thu Jul 10 01:09:29 2008 for NIP by  doxygen 1.5.5