mikroSDK Reference Manual
socket.h
Go to the documentation of this file.
1
31#ifndef _SOCKET_H
32#define _SOCKET_H
33
34//Forward declaration of Socket structure
35struct _Socket;
36#define Socket struct _Socket
37
38//Dependencies
39#include "core/net.h"
40#include "core/ethernet.h"
41#include "core/ip.h"
42#include "core/tcp.h"
43
44//Number of sockets that can be opened simultaneously
45#ifndef SOCKET_MAX_COUNT
46 #define SOCKET_MAX_COUNT 16
47#elif (SOCKET_MAX_COUNT < 1)
48 #error SOCKET_MAX_COUNT parameter is not valid
49#endif
50
51//Maximum number of multicast groups joined
52#ifndef SOCKET_MAX_MULTICAST_GROUPS
53 #define SOCKET_MAX_MULTICAST_GROUPS 1
54#elif (SOCKET_MAX_MULTICAST_GROUPS < 1)
55 #error SOCKET_MAX_MULTICAST_GROUPS parameter is not valid
56#endif
57
58//Dynamic port range (lower limit)
59#ifndef SOCKET_EPHEMERAL_PORT_MIN
60 #define SOCKET_EPHEMERAL_PORT_MIN 49152
61#elif (SOCKET_EPHEMERAL_PORT_MIN < 1024)
62 #error SOCKET_EPHEMERAL_PORT_MIN parameter is not valid
63#endif
64
65//Dynamic port range (upper limit)
66#ifndef SOCKET_EPHEMERAL_PORT_MAX
67 #define SOCKET_EPHEMERAL_PORT_MAX 65535
68#elif (SOCKET_EPHEMERAL_PORT_MAX <= SOCKET_EPHEMERAL_PORT_MIN || SOCKET_EPHEMERAL_PORT_MAX > 65535)
69 #error SOCKET_EPHEMERAL_PORT_MAX parameter is not valid
70#endif
71
72//C++ guard
73#ifdef __cplusplus
74extern "C" {
75#endif
76
77
82typedef enum
83{
84 SOCKET_TYPE_UNUSED = 0,
85 SOCKET_TYPE_STREAM = 1,
86 SOCKET_TYPE_DGRAM = 2,
87 SOCKET_TYPE_RAW_IP = 3,
88 SOCKET_TYPE_RAW_ETH = 4
90
91
96typedef enum
97{
98 SOCKET_IP_PROTO_ICMP = 1,
99 SOCKET_IP_PROTO_IGMP = 2,
100 SOCKET_IP_PROTO_TCP = 6,
101 SOCKET_IP_PROTO_UDP = 17,
102 SOCKET_IP_PROTO_ICMPV6 = 58
104
105
110typedef enum
111{
112 SOCKET_ETH_PROTO_ALL = 0x0000,
113 SOCKET_ETH_PROTO_LLC = 0x05DC,
114 SOCKET_ETH_PROTO_IPV4 = 0x0800,
115 SOCKET_ETH_PROTO_ARP = 0x0806,
116 SOCKET_ETH_PROTO_IPV6 = 0x86DD
118
119
124typedef enum
125{
126 SOCKET_FLAG_PEEK = 0x0200,
127 SOCKET_FLAG_DONT_ROUTE = 0x0400,
128 SOCKET_FLAG_WAIT_ALL = 0x0800,
129 SOCKET_FLAG_DONT_WAIT = 0x0100,
130 SOCKET_FLAG_BREAK_CHAR = 0x1000,
131 SOCKET_FLAG_BREAK_CRLF = 0x100A,
132 SOCKET_FLAG_WAIT_ACK = 0x2000,
133 SOCKET_FLAG_NO_DELAY = 0x4000,
134 SOCKET_FLAG_DELAY = 0x8000
136
137
138//The SOCKET_FLAG_BREAK macro causes the I/O functions to stop reading
139//data whenever the specified break character is encountered
140#define SOCKET_FLAG_BREAK(c) (SOCKET_FLAG_BREAK_CHAR | LSB(c))
141
142
147typedef enum
148{
149 SOCKET_SD_RECEIVE = 0,
150 SOCKET_SD_SEND = 1,
151 SOCKET_SD_BOTH = 2
153
154
159typedef enum
160{
161 SOCKET_EVENT_NONE = 0x0000,
162 SOCKET_EVENT_ACCEPT = 0x0001,
163 SOCKET_EVENT_CONNECTED = 0x0002,
164 SOCKET_EVENT_CLOSED = 0x0004,
165 SOCKET_EVENT_TX_READY = 0x0008,
166 SOCKET_EVENT_TX_DONE = 0x0010,
167 SOCKET_EVENT_TX_ACKED = 0x0020,
168 SOCKET_EVENT_TX_SHUTDOWN = 0x0040,
169 SOCKET_EVENT_RX_READY = 0x0080,
170 SOCKET_EVENT_RX_SHUTDOWN = 0x0100,
171 SOCKET_EVENT_LINK_UP = 0x0200,
172 SOCKET_EVENT_LINK_DOWN = 0x0400
174
175
180typedef enum
181{
182 SOCKET_OPTION_REUSE_ADDR = 0x0001,
183 SOCKET_OPTION_BROADCAST = 0x0002,
184 SOCKET_OPTION_IPV4_MULTICAST_LOOP = 0x0004,
185 SOCKET_OPTION_IPV4_PKT_INFO = 0x0008,
186 SOCKET_OPTION_IPV4_RECV_TOS = 0x0010,
187 SOCKET_OPTION_IPV4_RECV_TTL = 0x0020,
188 SOCKET_OPTION_IPV6_MULTICAST_LOOP = 0x0040,
189 SOCKET_OPTION_IPV6_ONLY = 0x0080,
190 SOCKET_OPTION_IPV6_PKT_INFO = 0x0100,
191 SOCKET_OPTION_IPV6_RECV_TRAFFIC_CLASS = 0x0200,
192 SOCKET_OPTION_IPV6_RECV_HOP_LIMIT = 0x0400,
193 SOCKET_OPTION_TCP_NO_DELAY = 0x0800
195
196
201typedef enum
202{
203 HOST_TYPE_ANY = 0,
204 HOST_TYPE_IPV4 = 16,
205 HOST_TYPE_IPV6 = 32
206} HostType;
207
208
213typedef enum
214{
215 HOST_NAME_RESOLVER_ANY = 0,
216 HOST_NAME_RESOLVER_DNS = 1,
217 HOST_NAME_RESOLVER_MDNS = 2,
218 HOST_NAME_RESOLVER_NBNS = 4,
219 HOST_NAME_RESOLVER_LLMNR = 8
221
222
227typedef struct
228{
229 void *data;
230 size_t size;
231 size_t length;
232 uint8_t ttl;
233 uint8_t tos;
234 NetInterface *interface;
236 uint16_t srcPort;
238 uint16_t destPort;
239#if (ETH_SUPPORT == ENABLED)
240 MacAddr srcMacAddr;
241 MacAddr destMacAddr;
242 uint16_t ethType;
243#endif
244#if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
245 uint8_t switchPort;
246#endif
247#if (ETH_TIMESTAMP_SUPPORT == ENABLED)
248 int32_t timestampId;
250#endif
251} SocketMsg;
252
253
258typedef struct _SocketQueueItem
259{
260 struct _SocketQueueItem *next;
261 NetInterface *interface;
262 IpAddr srcIpAddr;
263 uint16_t srcPort;
264 IpAddr destIpAddr;
265 NetBuffer *buffer;
266 size_t offset;
267 NetRxAncillary ancillary;
269
270
276{
277 uint_t descriptor;
278 uint_t type;
279 uint_t protocol;
280 NetInterface *interface;
281 IpAddr localIpAddr;
282 uint16_t localPort;
283 IpAddr remoteIpAddr;
284 uint16_t remotePort;
285 uint32_t options;
286 systime_t timeout;
287 uint8_t tos;
288 uint8_t ttl;
289 uint8_t multicastTtl;
290 IpAddr multicastGroups[SOCKET_MAX_MULTICAST_GROUPS];
291#if (ETH_VLAN_SUPPORT == ENABLED)
292 int8_t vlanPcp;
293 int8_t vlanDei;
294#endif
295#if (ETH_VMAN_SUPPORT == ENABLED)
296 int8_t vmanPcp;
297 int8_t vmanDei;
298#endif
299 int_t errnoCode;
300 OsEvent event;
301 uint_t eventMask;
302 uint_t eventFlags;
303 OsEvent *userEvent;
304
305//TCP specific variables
306#if (TCP_SUPPORT == ENABLED)
308 bool_t ownedFlag;
309 bool_t closedFlag;
310 bool_t resetFlag;
311
312 uint16_t mss;
313 uint16_t smss;
314 uint16_t rmss;
315 uint32_t iss;
316 uint32_t irs;
317
318 uint32_t sndUna;
319 uint32_t sndNxt;
320 uint16_t sndUser;
321 uint16_t sndWnd;
322 uint16_t maxSndWnd;
323 uint32_t sndWl1;
324 uint32_t sndWl2;
325
326 uint32_t rcvNxt;
327 uint16_t rcvUser;
328 uint16_t rcvWnd;
329
330 bool_t rttBusy;
331 uint32_t rttSeqNum;
336
337#if (TCP_CONGEST_CONTROL_SUPPORT == ENABLED)
339 uint16_t cwnd;
340 uint16_t ssthresh;
341 uint_t dupAckCount;
342 uint_t n;
343 uint32_t recover;
344#endif
345
346#if (TCP_KEEP_ALIVE_SUPPORT == ENABLED)
353#endif
354
355#if (TCP_SACK_SUPPORT == ENABLED)
357#endif
358
359 TcpSackBlock sackBlock[TCP_MAX_SACK_BLOCKS];
361
366
370
373
376
381#endif
382
383//UDP specific variables
384#if (UDP_SUPPORT == ENABLED || RAW_SOCKET_SUPPORT == ENABLED)
385 SocketQueueItem *receiveQueue;
386#endif
387};
388
389
394typedef struct
395{
396 Socket *socket;
397 uint_t eventMask;
398 uint_t eventFlags;
400
401
402//Global constants
403extern const SocketMsg SOCKET_DEFAULT_MSG;
404
405//Global variables
406extern Socket socketTable[SOCKET_MAX_COUNT];
407
408//Socket related functions
409error_t socketInit(void);
410
411Socket *socketOpen(uint_t type, uint_t protocol);
412
413error_t socketSetTimeout(Socket *socket, systime_t timeout);
414
415error_t socketSetTtl(Socket *socket, uint8_t ttl);
416error_t socketSetMulticastTtl(Socket *socket, uint8_t ttl);
417
418error_t socketSetDscp(Socket *socket, uint8_t dscp);
419
420error_t socketSetVlanPcp(Socket *socket, uint8_t pcp);
421error_t socketSetVlanDei(Socket *socket, bool_t dei);
422error_t socketSetVmanPcp(Socket *socket, uint8_t pcp);
423error_t socketSetVmanDei(Socket *socket, bool_t dei);
424
425error_t socketEnableBroadcast(Socket *socket, bool_t enabled);
426error_t socketJoinMulticastGroup(Socket *socket, const IpAddr *groupAddr);
427error_t socketLeaveMulticastGroup(Socket *socket, const IpAddr *groupAddr);
428
429error_t socketEnableKeepAlive(Socket *socket, bool_t enabled);
430
431error_t socketSetKeepAliveParams(Socket *socket, systime_t idle,
432 systime_t interval, uint_t maxProbes);
433
434error_t socketSetMaxSegmentSize(Socket *socket, size_t mss);
435
436error_t socketSetTxBufferSize(Socket *socket, size_t size);
437error_t socketSetRxBufferSize(Socket *socket, size_t size);
438
439error_t socketSetInterface(Socket *socket, NetInterface *interface);
440NetInterface *socketGetInterface(Socket *socket);
441
442error_t socketBind(Socket *socket, const IpAddr *localIpAddr,
443 uint16_t localPort);
444
445error_t socketConnect(Socket *socket, const IpAddr *remoteIpAddr,
446 uint16_t remotePort);
447
448error_t socketListen(Socket *socket, uint_t backlog);
449
450Socket *socketAccept(Socket *socket, IpAddr *clientIpAddr,
451 uint16_t *clientPort);
452
453error_t socketSend(Socket *socket, const void *data, size_t length,
454 size_t *written, uint_t flags);
455
456error_t socketSendTo(Socket *socket, const IpAddr *destIpAddr, uint16_t destPort,
457 const void *data, size_t length, size_t *written, uint_t flags);
458
459error_t socketSendMsg(Socket *socket, const SocketMsg *message, uint_t flags);
460
461error_t socketReceive(Socket *socket, void *data,
462 size_t size, size_t *received, uint_t flags);
463
464error_t socketReceiveFrom(Socket *socket, IpAddr *srcIpAddr, uint16_t *srcPort,
465 void *data, size_t size, size_t *received, uint_t flags);
466
467error_t socketReceiveEx(Socket *socket, IpAddr *srcIpAddr, uint16_t *srcPort,
468 IpAddr *destIpAddr, void *data, size_t size, size_t *received, uint_t flags);
469
470error_t socketReceiveMsg(Socket *socket, SocketMsg *message, uint_t flags);
471
472error_t socketGetLocalAddr(Socket *socket, IpAddr *localIpAddr,
473 uint16_t *localPort);
474
475error_t socketGetRemoteAddr(Socket *socket, IpAddr *remoteIpAddr,
476 uint16_t *remotePort);
477
478error_t socketShutdown(Socket *socket, uint_t how);
479void socketClose(Socket *socket);
480
481error_t socketPoll(SocketEventDesc *eventDesc, uint_t size, OsEvent *extEvent,
482 systime_t timeout);
483
484error_t getHostByName(NetInterface *interface, const char_t *name,
485 IpAddr *ipAddr, uint_t flags);
486
487//C++ guard
488#ifdef __cplusplus
489}
490#endif
491
492#endif
error_t
Error codes.
Definition error.h:43
Ethernet.
IPv4 and IPv6 common routines.
TCP/IP stack core.
uint_t OsEvent
Event object.
Definition os_port_none.h:104
uint32_t systime_t
System time.
Definition os_port_none.h:90
SocketEthProtocol
Ethernet protocols.
Definition socket.h:111
struct _SocketQueueItem SocketQueueItem
Receive queue item.
SocketFlags
Flags used by I/O functions.
Definition socket.h:125
SocketShutdownFlags
Flags used by shutdown function.
Definition socket.h:148
SocketIpProtocol
IP protocols.
Definition socket.h:97
SocketOptions
Socket options.
Definition socket.h:181
SocketType
Socket types.
Definition socket.h:83
HostType
Host types.
Definition socket.h:202
HostnameResolver
Name resolution protocols.
Definition socket.h:214
SocketEvent
Socket events.
Definition socket.h:160
IP network address.
Definition ip.h:72
Structure describing a buffer that spans multiple chunks.
Definition net_mem.h:89
Timer.
Definition net_misc.h:171
Timestamp.
Definition net_misc.h:105
Structure describing socket events.
Definition socket.h:395
Socket * socket
Handle to a socket to monitor.
Definition socket.h:396
uint_t eventMask
Requested events.
Definition socket.h:397
uint_t eventFlags
Returned events.
Definition socket.h:398
Message and ancillary data.
Definition socket.h:228
size_t size
Size of the payload, in bytes.
Definition socket.h:230
uint16_t srcPort
Source port.
Definition socket.h:236
uint16_t destPort
Destination port.
Definition socket.h:238
uint8_t ttl
Time-to-live value.
Definition socket.h:232
int32_t timestampId
Unique identifier for hardware time stamping.
Definition socket.h:248
uint8_t tos
Type-of-service value.
Definition socket.h:233
IpAddr srcIpAddr
Source IP address.
Definition socket.h:235
NetInterface * interface
Underlying network interface.
Definition socket.h:234
uint8_t switchPort
Switch port identifier.
Definition socket.h:245
uint16_t ethType
Ethernet type field.
Definition socket.h:242
MacAddr destMacAddr
Destination MAC address.
Definition socket.h:241
IpAddr destIpAddr
Destination IP address.
Definition socket.h:237
NetTimestamp timestamp
Captured time stamp.
Definition socket.h:249
MacAddr srcMacAddr
Source MAC address.
Definition socket.h:240
size_t length
Actual length of the payload, in bytes.
Definition socket.h:231
void * data
Pointer to the payload.
Definition socket.h:229
Receive buffer.
Definition tcp.h:438
SACK block.
Definition tcp.h:415
Transmit buffer.
Definition tcp.h:426
Receive queue item.
Definition socket.h:259
Structure describing a socket.
Definition socket.h:276
bool_t closedFlag
The connection has been closed properly.
Definition socket.h:309
uint16_t maxSndWnd
Maximum send window it has seen so far on the connection.
Definition socket.h:322
systime_t rttStartTime
Round-trip start time.
Definition socket.h:332
uint16_t sndUser
Amount of data buffered but not yet sent.
Definition socket.h:320
uint_t retransmitCount
Number of retransmissions.
Definition socket.h:369
uint16_t sndWnd
Size of the send window.
Definition socket.h:321
uint16_t rcvWnd
Receive window.
Definition socket.h:328
systime_t keepAliveTimestamp
Keep-alive timestamp.
Definition socket.h:352
uint_t n
Number of bytes acknowledged during the whole round-trip.
Definition socket.h:342
uint32_t sndNxt
Sequence number of the next byte to be sent.
Definition socket.h:319
uint_t synQueueSize
Maximum number of pending connections for listening sockets.
Definition socket.h:372
NetTimer overrideTimer
Override timer.
Definition socket.h:378
uint8_t ttl
Time-to-live value for unicast datagrams.
Definition socket.h:288
bool_t rttBusy
RTT measurement is being performed.
Definition socket.h:330
uint32_t sndWl2
Segment acknowledgment number used for last window update.
Definition socket.h:324
uint32_t rcvNxt
Receive next sequence number.
Definition socket.h:326
int8_t vmanPcp
VMAN priority (802.1ad)
Definition socket.h:296
NetTimer persistTimer
Persist timer.
Definition socket.h:377
size_t txBufferSize
Size of the send buffer.
Definition socket.h:363
uint16_t cwnd
Congestion window.
Definition socket.h:339
int8_t vlanPcp
VLAN priority (802.1Q)
Definition socket.h:292
uint32_t sndWl1
Segment sequence number used for last window update.
Definition socket.h:323
uint32_t iss
Initial send sequence number.
Definition socket.h:315
systime_t keepAliveIdle
Keep-alive idle time.
Definition socket.h:348
TcpCongestState congestState
Congestion state.
Definition socket.h:338
int8_t vmanDei
Drop eligible indicator.
Definition socket.h:297
systime_t rttvar
Round-trip time variation.
Definition socket.h:334
uint_t wndProbeCount
Zero window probe counter.
Definition socket.h:374
uint8_t tos
Type-of-service value.
Definition socket.h:287
uint8_t multicastTtl
Time-to-live value for multicast datagrams.
Definition socket.h:289
uint16_t rcvUser
Number of data received but not yet consumed.
Definition socket.h:327
bool_t ownedFlag
The user is the owner of the TCP socket.
Definition socket.h:308
systime_t keepAliveInterval
Time interval between subsequent keep-alive probes.
Definition socket.h:349
uint_t sackBlockCount
Number of non-contiguous blocks that have been received.
Definition socket.h:360
uint_t dupAckCount
Number of consecutive duplicate ACKs.
Definition socket.h:341
TcpState state
Current state of the TCP finite state machine.
Definition socket.h:307
NetTimer finWait2Timer
FIN-WAIT-2 timer.
Definition socket.h:379
TcpSynQueueItem * synQueue
SYN queue for listening sockets.
Definition socket.h:371
bool_t sackPermitted
SACK Permitted option received.
Definition socket.h:356
bool_t keepAliveEnabled
Specifies whether TCP keep-alive mechanism is enabled.
Definition socket.h:347
uint32_t sndUna
Data that have been sent but not yet acknowledged.
Definition socket.h:318
bool_t resetFlag
The connection has been reset.
Definition socket.h:310
uint16_t mss
Maximum segment size.
Definition socket.h:312
int8_t vlanDei
Drop eligible indicator.
Definition socket.h:293
uint32_t recover
NewReno modification to TCP's fast recovery algorithm.
Definition socket.h:343
TcpQueueItem * retransmitQueue
Retransmission queue.
Definition socket.h:367
uint_t keepAliveMaxProbes
Number of keep-alive probes.
Definition socket.h:350
size_t rxBufferSize
Size of the receive buffer.
Definition socket.h:365
uint32_t options
Socket options.
Definition socket.h:285
uint32_t irs
Initial receive sequence number.
Definition socket.h:316
uint16_t rmss
Receiver maximum segment size.
Definition socket.h:314
uint_t keepAliveProbeCount
Keep-alive probe counter.
Definition socket.h:351
uint32_t rttSeqNum
Sequence number identifying a TCP segment.
Definition socket.h:331
systime_t rto
Retransmission timeout.
Definition socket.h:335
systime_t srtt
Smoothed round-trip time.
Definition socket.h:333
uint16_t ssthresh
Slow start threshold.
Definition socket.h:340
NetTimer retransmitTimer
Retransmission timer.
Definition socket.h:368
uint16_t smss
Sender maximum segment size.
Definition socket.h:313
IpAddr multicastGroups[SOCKET_MAX_MULTICAST_GROUPS]
Multicast groups.
Definition socket.h:290
TcpSackBlock sackBlock[TCP_MAX_SACK_BLOCKS]
List of non-contiguous blocks that have been received.
Definition socket.h:359
TcpTxBuffer txBuffer
Send buffer.
Definition socket.h:362
NetTimer timeWaitTimer
2MSL timer
Definition socket.h:380
TcpRxBuffer rxBuffer
Receive buffer.
Definition socket.h:364
systime_t wndProbeInterval
Interval between successive probes.
Definition socket.h:375
Retransmission queue item.
Definition tcp.h:382
SYN queue item.
Definition tcp.h:396
TCP (Transmission Control Protocol)
TcpState
TCP FSM states.
Definition tcp.h:267
TcpCongestState
TCP congestion states.
Definition tcp.h:287