Webserver+3d print
Embed:
(wiki syntax)
Show/hide line numbers
socket.h
Go to the documentation of this file.
00001 /** 00002 * @file socket.h 00003 * @brief Socket API 00004 * 00005 * @section License 00006 * 00007 * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved. 00008 * 00009 * This file is part of CycloneTCP Open. 00010 * 00011 * This program is free software; you can redistribute it and/or 00012 * modify it under the terms of the GNU General Public License 00013 * as published by the Free Software Foundation; either version 2 00014 * of the License, or (at your option) any later version. 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU General Public License for more details. 00020 * 00021 * You should have received a copy of the GNU General Public License 00022 * along with this program; if not, write to the Free Software Foundation, 00023 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00024 * 00025 * @author Oryx Embedded SARL (www.oryx-embedded.com) 00026 * @version 1.7.6 00027 **/ 00028 00029 #ifndef _SOCKET_H 00030 #define _SOCKET_H 00031 00032 //Forward declaration of Socket structure 00033 struct _Socket; 00034 #define Socket struct _Socket 00035 00036 //Dependencies 00037 #include "core/net.h" 00038 #include "core/ip.h" 00039 #include "core/tcp.h" 00040 00041 //Number of sockets that can be opened simultaneously 00042 #ifndef SOCKET_MAX_COUNT 00043 #define SOCKET_MAX_COUNT 16 00044 #elif (SOCKET_MAX_COUNT < 1) 00045 #error SOCKET_MAX_COUNT parameter is not valid 00046 #endif 00047 00048 //Dynamic port range (lower limit) 00049 #ifndef SOCKET_EPHEMERAL_PORT_MIN 00050 #define SOCKET_EPHEMERAL_PORT_MIN 49152 00051 #elif (SOCKET_EPHEMERAL_PORT_MIN < 1024) 00052 #error SOCKET_EPHEMERAL_PORT_MIN parameter is not valid 00053 #endif 00054 00055 //Dynamic port range (upper limit) 00056 #ifndef SOCKET_EPHEMERAL_PORT_MAX 00057 #define SOCKET_EPHEMERAL_PORT_MAX 65535 00058 #elif (SOCKET_EPHEMERAL_PORT_MAX <= SOCKET_EPHEMERAL_PORT_MIN || SOCKET_EPHEMERAL_PORT_MAX > 65535) 00059 #error SOCKET_EPHEMERAL_PORT_MAX parameter is not valid 00060 #endif 00061 00062 00063 /** 00064 * @brief Socket types 00065 **/ 00066 00067 typedef enum 00068 { 00069 SOCKET_TYPE_UNUSED = 0, 00070 SOCKET_TYPE_STREAM = 1, 00071 SOCKET_TYPE_DGRAM = 2, 00072 SOCKET_TYPE_RAW_IP = 3, 00073 SOCKET_TYPE_RAW_ETH = 4 00074 } SocketType; 00075 00076 00077 /** 00078 * @brief IP protocols 00079 **/ 00080 00081 typedef enum 00082 { 00083 SOCKET_IP_PROTO_ICMP = 1, 00084 SOCKET_IP_PROTO_IGMP = 2, 00085 SOCKET_IP_PROTO_TCP = 6, 00086 SOCKET_IP_PROTO_UDP = 17, 00087 SOCKET_IP_PROTO_ICMPV6 = 58 00088 } SocketIpProtocol; 00089 00090 00091 /** 00092 * @brief Ethernet protocols 00093 **/ 00094 00095 typedef enum 00096 { 00097 SOCKET_ETH_PROTO_ALL = 0x0000, 00098 SOCKET_ETH_PROTO_IPV4 = 0x0800, 00099 SOCKET_ETH_PROTO_ARP = 0x0806, 00100 SOCKET_ETH_PROTO_IPV6 = 0x86DD 00101 } SocketEthProtocol; 00102 00103 00104 /** 00105 * @brief Flags used by I/O functions 00106 **/ 00107 00108 typedef enum 00109 { 00110 SOCKET_FLAG_PEEK = 0x0200, 00111 SOCKET_FLAG_DONT_ROUTE = 0x0400, 00112 SOCKET_FLAG_WAIT_ALL = 0x0800, 00113 SOCKET_FLAG_DONT_WAIT = 0x0100, 00114 SOCKET_FLAG_BREAK_CHAR = 0x1000, 00115 SOCKET_FLAG_BREAK_CRLF = 0x100A, 00116 SOCKET_FLAG_WAIT_ACK = 0x2000, 00117 SOCKET_FLAG_NO_DELAY = 0x4000, 00118 SOCKET_FLAG_DELAY = 0x8000 00119 } SocketFlags; 00120 00121 00122 //The SOCKET_FLAG_BREAK macro causes the I/O functions to stop reading 00123 //data whenever the specified break character is encountered 00124 #define SOCKET_FLAG_BREAK(c) (SOCKET_FLAG_BREAK_CHAR | LSB(c)) 00125 00126 00127 /** 00128 * @brief Flags used by shutdown function 00129 **/ 00130 00131 typedef enum 00132 { 00133 SOCKET_SD_RECEIVE = 0, 00134 SOCKET_SD_SEND = 1, 00135 SOCKET_SD_BOTH = 2 00136 } SocketShutdownFlags; 00137 00138 00139 /** 00140 * @brief Socket events 00141 **/ 00142 00143 typedef enum 00144 { 00145 SOCKET_EVENT_TIMEOUT = 0x0000, 00146 SOCKET_EVENT_CONNECTED = 0x0001, 00147 SOCKET_EVENT_CLOSED = 0x0002, 00148 SOCKET_EVENT_TX_READY = 0x0004, 00149 SOCKET_EVENT_TX_DONE = 0x0008, 00150 SOCKET_EVENT_TX_ACKED = 0x0010, 00151 SOCKET_EVENT_TX_SHUTDOWN = 0x0020, 00152 SOCKET_EVENT_RX_READY = 0x0040, 00153 SOCKET_EVENT_RX_SHUTDOWN = 0x0080, 00154 SOCKET_EVENT_LINK_UP = 0x0100, 00155 SOCKET_EVENT_LINK_DOWN = 0x0200 00156 } SocketEvent; 00157 00158 00159 /** 00160 * @brief Host types 00161 **/ 00162 00163 typedef enum 00164 { 00165 HOST_TYPE_ANY = 0, 00166 HOST_TYPE_IPV4 = 16, 00167 HOST_TYPE_IPV6 = 32 00168 } HostType; 00169 00170 00171 /** 00172 * @brief Name resolution protocols 00173 **/ 00174 00175 typedef enum 00176 { 00177 HOST_NAME_RESOLVER_ANY = 0, 00178 HOST_NAME_RESOLVER_DNS = 1, 00179 HOST_NAME_RESOLVER_MDNS = 2, 00180 HOST_NAME_RESOLVER_NBNS = 4, 00181 HOST_NAME_RESOLVER_LLMNR = 8 00182 } HostnameResolver; 00183 00184 00185 /** 00186 * @brief Receive queue item 00187 **/ 00188 00189 typedef struct _SocketQueueItem 00190 { 00191 struct _SocketQueueItem *next; 00192 IpAddr srcIpAddr; 00193 uint16_t srcPort; 00194 IpAddr destIpAddr; 00195 NetBuffer *buffer; 00196 size_t offset; 00197 } SocketQueueItem; 00198 00199 00200 /** 00201 * @brief Structure describing a socket 00202 **/ 00203 00204 struct _Socket 00205 { 00206 uint_t descriptor; 00207 uint_t type; 00208 uint_t protocol; 00209 NetInterface *interface; 00210 IpAddr localIpAddr; 00211 uint16_t localPort; 00212 IpAddr remoteIpAddr; 00213 uint16_t remotePort; 00214 systime_t timeout; 00215 uint8_t ttl; 00216 int_t errnoCode; 00217 OsEvent event; 00218 uint_t eventMask; 00219 uint_t eventFlags; 00220 OsEvent *userEvent; 00221 00222 //TCP specific variables 00223 #if (TCP_SUPPORT == ENABLED) 00224 TcpState state; ///<Current state of the TCP finite state machine 00225 bool_t ownedFlag; ///<The user is the owner of the TCP socket 00226 bool_t closedFlag; ///<The connection has been closed properly 00227 bool_t resetFlag; ///<The connection has been reset 00228 00229 uint16_t smss; ///<Sender maximum segment size 00230 uint16_t rmss; ///<Receiver maximum segment size 00231 uint32_t iss; ///<Initial send sequence number 00232 uint32_t irs; ///<Initial receive sequence number 00233 00234 uint32_t sndUna; ///<Data that have been sent but not yet acknowledged 00235 uint32_t sndNxt; ///<Sequence number of the next byte to be sent 00236 uint16_t sndUser; ///<Amount of data buffered but not yet sent 00237 uint16_t sndWnd; ///<Size of the send window 00238 uint16_t maxSndWnd; ///<Maximum send window it has seen so far on the connection 00239 uint32_t sndWl1; ///<Segment sequence number used for last window update 00240 uint32_t sndWl2; ///<Segment acknowledgment number used for last window update 00241 00242 uint32_t rcvNxt; ///<Receive next sequence number 00243 uint16_t rcvUser; ///<Number of data received but not yet consumed 00244 uint16_t rcvWnd; ///<Receive window 00245 00246 bool_t rttBusy; ///<RTT measurement is being performed 00247 uint32_t rttSeqNum; ///<Sequence number identifying a TCP segment 00248 systime_t rttStartTime; ///<Round-trip start time 00249 systime_t srtt; ///<Smoothed round-trip time 00250 systime_t rttvar; ///<Round-trip time variation 00251 systime_t rto; ///<Retransmission timeout 00252 00253 #if (TCP_CONGEST_CONTROL_SUPPORT == ENABLED) 00254 TcpCongestState congestState; ///<Congestion state 00255 uint16_t cwnd; ///<Congestion window 00256 uint16_t ssthresh; ///<Slow start threshold 00257 uint_t dupAckCount; ///<Number of consecutive duplicate ACKs 00258 uint_t n; ///<Number of bytes acknowledged during the whole round-trip 00259 uint32_t recover; ///<NewReno modification to TCP's fast recovery algorithm 00260 #endif 00261 00262 TcpTxBuffer txBuffer; ///<Send buffer 00263 size_t txBufferSize; ///<Size of the send buffer 00264 TcpRxBuffer rxBuffer; ///<Receive buffer 00265 size_t rxBufferSize; ///<Size of the receive buffer 00266 00267 TcpQueueItem *retransmitQueue; ///<Retransmission queue 00268 TcpTimer retransmitTimer; ///<Retransmission timer 00269 uint_t retransmitCount; ///<Number of retransmissions 00270 00271 TcpSynQueueItem *synQueue; ///<SYN queue for listening sockets 00272 uint_t synQueueSize; ///<Maximum number of pending connections for listening sockets 00273 00274 uint_t wndProbeCount; ///<Zero window probe counter 00275 systime_t wndProbeInterval; ///<Interval between successive probes 00276 00277 TcpTimer persistTimer; ///<Persist timer 00278 TcpTimer overrideTimer; ///<Override timer 00279 TcpTimer finWait2Timer; ///<FIN-WAIT-2 timer 00280 TcpTimer timeWaitTimer; ///<2MSL timer 00281 00282 bool_t sackPermitted; ///<SACK Permitted option received 00283 TcpSackBlock sackBlock[TCP_MAX_SACK_BLOCKS]; ///<List of non-contiguous blocks that have been received 00284 uint_t sackBlockCount; ///<Number of non-contiguous blocks that have been received 00285 #endif 00286 00287 //UDP specific variables 00288 #if (UDP_SUPPORT == ENABLED || RAW_SOCKET_SUPPORT == ENABLED) 00289 SocketQueueItem *receiveQueue; 00290 #endif 00291 }; 00292 00293 00294 /** 00295 * @brief Structure describing socket events 00296 **/ 00297 00298 typedef struct 00299 { 00300 Socket *socket; ///<Handle to a socket to monitor 00301 uint_t eventMask; ///<Requested events 00302 uint_t eventFlags; ///<Returned events 00303 } SocketEventDesc; 00304 00305 00306 //Global variables 00307 extern Socket socketTable[SOCKET_MAX_COUNT]; 00308 00309 //Socket related functions 00310 error_t socketInit(void); 00311 00312 Socket *socketOpen(uint_t type, uint_t protocol); 00313 00314 error_t socketSetTimeout(Socket *socket, systime_t timeout); 00315 error_t socketSetTxBufferSize(Socket *socket, size_t size); 00316 error_t socketSetRxBufferSize(Socket *socket, size_t size); 00317 00318 error_t socketBindToInterface(Socket *socket, NetInterface *interface); 00319 error_t socketBind(Socket *socket, const IpAddr *localIpAddr, uint16_t localPort); 00320 error_t socketConnect(Socket *socket, const IpAddr *remoteIpAddr, uint16_t remotePort); 00321 error_t socketListen(Socket *socket, uint_t backlog); 00322 Socket *socketAccept(Socket *socket, IpAddr *clientIpAddr, uint16_t *clientPort); 00323 00324 error_t socketSend(Socket *socket, const void *data, 00325 size_t length, size_t *written, uint_t flags); 00326 00327 error_t socketSendTo(Socket *socket, const IpAddr *destIpAddr, uint16_t destPort, 00328 const void *data, size_t length, size_t *written, uint_t flags); 00329 00330 error_t socketReceive(Socket *socket, void *data, 00331 size_t size, size_t *received, uint_t flags); 00332 00333 error_t socketReceiveFrom(Socket *socket, IpAddr *srcIpAddr, uint16_t *srcPort, 00334 void *data, size_t size, size_t *received, uint_t flags); 00335 00336 error_t socketReceiveEx(Socket *socket, IpAddr *srcIpAddr, uint16_t *srcPort, 00337 IpAddr *destIpAddr, void *data, size_t size, size_t *received, uint_t flags); 00338 00339 error_t socketGetLocalAddr(Socket *socket, IpAddr *localIpAddr, uint16_t *localPort); 00340 error_t socketGetRemoteAddr(Socket *socket, IpAddr *remoteIpAddr, uint16_t *remotePort); 00341 00342 error_t socketShutdown(Socket *socket, uint_t how); 00343 void socketClose(Socket *socket); 00344 00345 error_t socketPoll(SocketEventDesc *eventDesc, uint_t size, OsEvent *extEvent, systime_t timeout); 00346 error_t socketRegisterEvents(Socket *socket, OsEvent *event, uint_t eventMask); 00347 error_t socketUnregisterEvents(Socket *socket); 00348 error_t socketGetEvents(Socket *socket, uint_t *eventFlags); 00349 00350 error_t getHostByName(NetInterface *interface, 00351 const char_t *name, IpAddr *ipAddr, uint_t flags); 00352 00353 #endif 00354
Generated on Tue Jul 12 2022 17:10:16 by
