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