1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
|
/** @file
Copyright (c) 2005 - 2006, Intel Corporation
All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
Module Name:
Socket.h
Abstract:
**/
#ifndef _SOCKET_H_
#define _SOCKET_H_
#include <PiDxe.h>
#include <Protocol/IP4.h>
#include <Protocol/Tcp4.h>
#include <Protocol/Udp4.h>
#include <Library/NetLib.h>
#include <Library/DebugLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>
#include <Library/UefiDriverEntryPoint.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/BaseLib.h>
#include <Library/UefiLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/BaseMemoryLib.h>
#define SOCK_SND_BUF 0
#define SOCK_RCV_BUF 1
#define SOCK_BUFF_LOW_WATER 2 * 1024
#define SOCK_RCV_BUFF_SIZE 8 * 1024
#define SOCK_SND_BUFF_SIZE 8 * 1024
#define SOCK_BACKLOG 5
#define PROTO_RESERVED_LEN 20
#define SO_NO_MORE_DATA 0x0001
//
//
//
// When a socket is created it enters into SO_UNCONFIGURED,
// no actions can be taken on this socket, only after calling
// SockConfigure. The state transition diagram of socket is
// as following:
//
// SO_UNCONFIGURED --- SO_CONFIGURED --- SO_CONNECTING
// ^ | |
// | ---> SO_LISTENING |
// | |
// |------------------SO_DISCONNECTING<-- SO_CONNECTED
//
// A passive socket can only go into SO_LISTENING and
// SO_UNCONFIGURED state. SO_XXXING state is a middle state
// when a socket is undergoing a protocol procedure such
// as requesting a TCP connection.
//
//
//
typedef enum {
SO_CLOSED = 0,
SO_LISTENING,
SO_CONNECTING,
SO_CONNECTED,
SO_DISCONNECTING
} SOCK_STATE;
typedef enum {
SO_UNCONFIGURED = 0,
SO_CONFIGURED_ACTIVE,
SO_CONFIGURED_PASSIVE,
SO_NO_MAPPING
} SOCK_CONFIGURE_STATE;
#define SOCK_NO_MORE_DATA(Sock) ((Sock)->Flag |= SO_NO_MORE_DATA)
#define SOCK_IS_UNCONFIGURED(Sock) ((Sock)->ConfigureState == SO_UNCONFIGURED)
#define SOCK_IS_CONFIGURED(Sock) \
(((Sock)->ConfigureState == SO_CONFIGURED_ACTIVE) || \
((Sock)->ConfigureState == SO_CONFIGURED_PASSIVE))
#define SOCK_IS_CONFIGURED_ACTIVE(Sock) \
((Sock)->ConfigureState == SO_CONFIGURED_ACTIVE)
#define SOCK_IS_CONNECTED_PASSIVE(Sock) \
((Sock)->ConfigureState == SO_CONFIGURED_PASSIVE)
#define SOCK_IS_NO_MAPPING(Sock) \
((Sock)->ConfigureState == SO_NO_MAPPING)
#define SOCK_IS_CLOSED(Sock) ((Sock)->State == SO_CLOSED)
#define SOCK_IS_LISTENING(Sock) ((Sock)->State == SO_LISTENING)
#define SOCK_IS_CONNECTING(Sock) ((Sock)->State == SO_CONNECTING)
#define SOCK_IS_CONNECTED(Sock) ((Sock)->State == SO_CONNECTED)
#define SOCK_IS_DISCONNECTING(Sock) ((Sock)->State == SO_DISCONNECTING)
#define SOCK_IS_NO_MORE_DATA(Sock) (0 != ((Sock)->Flag & SO_NO_MORE_DATA))
#define SOCK_SIGNATURE EFI_SIGNATURE_32 ('S', 'O', 'C', 'K')
#define SOCK_FROM_THIS(a) CR ((a), SOCKET, NetProtocol, SOCK_SIGNATURE)
#define SET_RCV_BUFFSIZE(Sock, Size) ((Sock)->RcvBuffer.HighWater = (Size))
#define GET_RCV_BUFFSIZE(Sock) ((Sock)->RcvBuffer.HighWater)
#define GET_RCV_DATASIZE(Sock) (((Sock)->RcvBuffer.DataQueue)->BufSize)
#define SET_SND_BUFFSIZE(Sock, Size) ((Sock)->SndBuffer.HighWater = (Size))
#define GET_SND_BUFFSIZE(Sock) ((Sock)->SndBuffer.HighWater)
#define GET_SND_DATASIZE(Sock) (((Sock)->SndBuffer.DataQueue)->BufSize)
#define SET_BACKLOG(Sock, Value) ((Sock)->BackLog = (Value))
#define GET_BACKLOG(Sock) ((Sock)->BackLog)
#define SOCK_ERROR(Sock, Error) ((Sock)->SockError = (Error))
#define SND_BUF_HDR_LEN(Sock) \
((SockBufFirst (&((Sock)->SndBuffer)))->TotalSize)
#define RCV_BUF_HDR_LEN(Sock) \
((SockBufFirst (&((Sock)->RcvBuffer)))->TotalSize)
#define SOCK_FROM_TOKEN(Token) (((SOCK_TOKEN *) (Token))->Sock)
#define PROTO_TOKEN_FORM_SOCK(SockToken, Type) \
((Type *) (((SOCK_TOKEN *) (SockToken))->Token))
typedef struct _SOCKET SOCKET;
typedef struct _SOCK_COMPLETION_TOKEN {
EFI_EVENT Event;
EFI_STATUS Status;
} SOCK_COMPLETION_TOKEN;
typedef struct _SOCK_IO_TOKEN {
SOCK_COMPLETION_TOKEN Token;
union {
VOID *RxData;
VOID *TxData;
} Packet;
} SOCK_IO_TOKEN;
//
// the request issued from socket layer to protocol layer
//
typedef enum {
SOCK_ATTACH, // attach current socket to a new PCB
SOCK_DETACH, // detach current socket from the PCB
SOCK_CONFIGURE, // configure attached PCB
SOCK_FLUSH, // flush attached PCB
SOCK_SND, // need protocol to send something
SOCK_SNDPUSH, // need protocol to send pushed data
SOCK_SNDURG, // need protocol to send urgent data
SOCK_CONSUMED, // application has retrieved data from socket
SOCK_CONNECT, // need to connect to a peer
SOCK_CLOSE, // need to close the protocol process
SOCK_ABORT, // need to reset the protocol process
SOCK_POLL, // need to poll to the protocol layer
SOCK_ROUTE, // need to add a route information
SOCK_MODE, // need to get the mode data of the protocol
SOCK_GROUP // need to join a mcast group
} SOCK_REQUEST;
//
// the socket type
//
typedef enum {
SOCK_DGRAM, // this socket providing datagram service
SOCK_STREAM // this socket providing stream service
} SOCK_TYPE;
//
// the handler of protocol for request from socket
//
typedef
EFI_STATUS
(*SOCK_PROTO_HANDLER) (
IN SOCKET * Socket, // the socket issuing the request to protocol
IN SOCK_REQUEST Request, // the request issued by socket
IN VOID *RequestData // the request related data
);
//
// the buffer structure of rcvd data and send data used by socket
//
typedef struct _SOCK_BUFFER {
UINT32 HighWater; // the buffersize upper limit of sock_buffer
UINT32 LowWater; // the low warter mark of sock_buffer
NET_BUF_QUEUE *DataQueue; // the queue to buffer data
} SOCK_BUFFER;
//
// the initialize data for create a new socket
//
typedef struct _SOCK_INIT_DATA {
SOCK_TYPE Type;
SOCK_STATE State;
SOCKET *Parent; // the parent of this socket
UINT32 BackLog; // the connection limit for listening socket
UINT32 SndBufferSize; // the high warter mark of send buffer
UINT32 RcvBufferSize; // the high warter mark of receive buffer
VOID *Protocol; // the pointer to protocol function template
// wanted to install on socket
SOCK_PROTO_HANDLER ProtoHandler;
EFI_HANDLE DriverBinding; // the driver binding handle
} SOCK_INIT_DATA;
//
// socket provided oprerations for low layer protocol
//
//
// socket provided operations for user interface
//
VOID
SockSetState (
IN SOCKET *Sock,
IN SOCK_STATE State
);
//
// when the connection establishment process for a Sock
// is finished low layer protocol calling this function
// to notify socket layer
//
VOID
SockConnEstablished (
IN SOCKET *Sock
);
VOID
SockConnClosed (
IN SOCKET *Sock
);
//
// called by low layer protocol to trim send buffer of
// Sock, when Count data is sent out completely
//
VOID
SockDataSent (
IN SOCKET *Sock,
IN UINT32 Count
);
//
// called by low layer protocol to get Len of data from
// socket to send and copy it in Dest
//
UINT32
SockGetDataToSend (
IN SOCKET *Sock,
IN UINT32 Offset,
IN UINT32 Len,
IN UINT8 *Dest
);
//
// called by low layer protocol to notify socket no more data can be
// received
//
VOID
SockNoMoreData (
IN SOCKET *Sock
);
//
// called by low layer protocol to append a NetBuffer
// to rcv buffer of sock
//
VOID
SockDataRcvd (
IN SOCKET *Sock,
IN NET_BUF *NetBuffer,
IN UINT32 UrgLen
);
UINT32
SockGetFreeSpace (
IN SOCKET *Sock,
IN UINT32 Which
);
SOCKET *
SockClone (
IN SOCKET *Sock
);
VOID
SockRcvdErr (
IN SOCKET *Sock,
IN EFI_STATUS Error
);
//
// the socket structure representing a network service access point
//
typedef struct _SOCKET {
//
// socket description information
//
UINT32 Signature;
EFI_HANDLE SockHandle; // the virtual handle of the socket
EFI_HANDLE DriverBinding; // socket't driver binding protocol
SOCK_CONFIGURE_STATE ConfigureState;
SOCK_TYPE Type;
SOCK_STATE State;
UINT16 Flag;
NET_LOCK Lock; // the lock of socket
SOCK_BUFFER SndBuffer; // send buffer of application's data
SOCK_BUFFER RcvBuffer; // receive buffer of received data
EFI_STATUS SockError; // the error returned by low layer protocol
BOOLEAN IsDestroyed;
//
// fields used to manage the connection request
//
UINT32 BackLog; // the limit of connection to this socket
UINT32 ConnCnt; // the current count of connections to it
SOCKET *Parent; // listening parent that accept the connection
NET_LIST_ENTRY ConnectionList; // the connections maintained by this socket
//
// the queue to buffer application's asynchronous token
//
NET_LIST_ENTRY ListenTokenList;
NET_LIST_ENTRY RcvTokenList;
NET_LIST_ENTRY SndTokenList;
NET_LIST_ENTRY ProcessingSndTokenList;
SOCK_COMPLETION_TOKEN *ConnectionToken; // app's token to signal if connected
SOCK_COMPLETION_TOKEN *CloseToken; // app's token to signal if closed
//
// interface for low level protocol
//
SOCK_PROTO_HANDLER ProtoHandler; // the request handler of protocol
UINT8 ProtoReserved[PROTO_RESERVED_LEN]; // Data fields reserved for protocol
union {
EFI_TCP4_PROTOCOL TcpProtocol;
EFI_UDP4_PROTOCOL UdpProtocol;
} NetProtocol;
} SOCKET;
//
// the token structure buffered in socket layer
//
typedef struct _SOCK_TOKEN {
NET_LIST_ENTRY TokenList; // the entry to add in the token list
SOCK_COMPLETION_TOKEN *Token; // The application's token
UINT32 RemainDataLen; // unprocessed data length
SOCKET *Sock; // the poninter to the socket this token
// belongs to
} SOCK_TOKEN;
//
// reserved data to access the NET_BUF delivered by UDP driver
//
typedef struct _UDP_RSV_DATA {
EFI_TIME TimeStamp;
EFI_UDP4_SESSION_DATA Session;
} UDP_RSV_DATA;
//
// reserved data to access the NET_BUF delivered by TCP driver
//
typedef struct _TCP_RSV_DATA {
UINT32 UrgLen;
} TCP_RSV_DATA;
//
// call it to creat a socket and attach it to a PCB
//
SOCKET *
SockCreateChild (
IN SOCK_INIT_DATA *SockInitData,
IN VOID *ProtoData,
IN UINT32 Len
);
//
// call it to destroy a socket and its related PCB
//
EFI_STATUS
SockDestroyChild (
IN SOCKET *Sock
);
//
// call it to configure a socket and its related PCB
//
EFI_STATUS
SockConfigure (
IN SOCKET *Sock,
IN VOID *ConfigData
);
//
// call it to connect a socket to the peer
//
EFI_STATUS
SockConnect (
IN SOCKET *Sock,
IN VOID *Token
);
//
// call it to issue an asynchronous listen token to the socket
//
EFI_STATUS
SockAccept (
IN SOCKET *Sock,
IN VOID *Token
);
//
// Call it to send data using this socket
//
EFI_STATUS
SockSend (
IN SOCKET *Sock,
IN VOID *Token
);
//
// Call it to receive data from this socket
//
EFI_STATUS
SockRcv (
IN SOCKET *Sock,
IN VOID *Token
);
//
// Call it to flush a socket
//
EFI_STATUS
SockFlush (
IN SOCKET *Sock
);
//
// Call it to close a socket in the light of policy in Token
//
EFI_STATUS
SockClose (
IN SOCKET *Sock,
IN VOID *Token,
IN BOOLEAN OnAbort
);
//
// Call it to get the mode data of low layer protocol
//
EFI_STATUS
SockGetMode (
IN SOCKET *Sock,
IN VOID *Mode
);
//
// call it to add this socket instance into a group
//
EFI_STATUS
SockGroup (
IN SOCKET *Sock,
IN VOID *GroupInfo
);
//
// call it to add a route entry for this socket instance
//
EFI_STATUS
SockRoute (
IN SOCKET *Sock,
IN VOID *RouteInfo
);
//
// Supporting function to operate on socket buffer
//
NET_BUF *
SockBufFirst (
IN SOCK_BUFFER *Sockbuf
);
NET_BUF *
SockBufNext (
IN SOCK_BUFFER *Sockbuf,
IN NET_BUF *SockEntry
);
#endif
|