1 | /* Copyright (c) 2001, Stanford University
|
---|
2 | * All rights reserved.
|
---|
3 | *
|
---|
4 | * See the file LICENSE.txt for information on redistributing this software.
|
---|
5 | */
|
---|
6 |
|
---|
7 | #ifndef CR_NET_H
|
---|
8 | #define CR_NET_H
|
---|
9 |
|
---|
10 | #ifdef RT_OS_WINDOWS
|
---|
11 | # include <iprt/win/winsock.h>
|
---|
12 | #endif
|
---|
13 |
|
---|
14 | #include "cr_protocol.h"
|
---|
15 | #include "cr_threads.h"
|
---|
16 |
|
---|
17 | #include <iprt/types.h>
|
---|
18 | #include <iprt/thread.h>
|
---|
19 | #include <iprt/list.h>
|
---|
20 |
|
---|
21 | #ifdef __cplusplus
|
---|
22 | extern "C" {
|
---|
23 | #endif
|
---|
24 |
|
---|
25 | typedef struct CRConnection CRConnection;
|
---|
26 |
|
---|
27 | typedef enum {
|
---|
28 | CR_NO_CONNECTION,
|
---|
29 | CR_VBOXHGCM,
|
---|
30 | CR_DROP_PACKETS
|
---|
31 | } CRConnectionType;
|
---|
32 |
|
---|
33 | typedef void (*CRVoidFunc)( void );
|
---|
34 | typedef int (*CRNetReceiveFunc)( CRConnection *conn, CRMessage *msg, unsigned int len );
|
---|
35 | typedef int (*CRNetConnectFunc)( CRConnection *conn );
|
---|
36 | typedef void (*CRNetCloseFunc)( unsigned int sender_id );
|
---|
37 |
|
---|
38 | typedef struct __recvFuncList {
|
---|
39 | CRNetReceiveFunc recv;
|
---|
40 | struct __recvFuncList *next;
|
---|
41 | } CRNetReceiveFuncList;
|
---|
42 |
|
---|
43 | typedef struct __closeFuncList {
|
---|
44 | CRNetCloseFunc close;
|
---|
45 | struct __closeFuncList *next;
|
---|
46 | } CRNetCloseFuncList;
|
---|
47 |
|
---|
48 | typedef struct __messageListNode {
|
---|
49 | CRMessage *mesg; /* the actual message (header + payload) */
|
---|
50 | unsigned int len; /* length of message (header + payload) */
|
---|
51 | CRConnection *conn; /* some messages are assoc. with specific connections*/
|
---|
52 | struct __messageListNode *next; /* next in list */
|
---|
53 | } CRMessageListNode;
|
---|
54 |
|
---|
55 | typedef struct {
|
---|
56 | CRMessageListNode *head, *tail;
|
---|
57 | int numMessages;
|
---|
58 | CRmutex lock;
|
---|
59 | CRcondition nonEmpty;
|
---|
60 | } CRMessageList;
|
---|
61 |
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Used to accumulate CR_MESSAGE_MULTI_BODY/TAIL chunks into one big buffer.
|
---|
65 | */
|
---|
66 | typedef struct CRMultiBuffer {
|
---|
67 | unsigned int len; /* current length (<= max) (with sizeof_buffer_header) */
|
---|
68 | unsigned int max; /* size in bytes of data buffer */
|
---|
69 | void *buf; /* data buffer */
|
---|
70 | } CRMultiBuffer;
|
---|
71 |
|
---|
72 | #ifdef VBOX_WITH_CRHGSMI
|
---|
73 | # ifdef IN_GUEST
|
---|
74 | typedef struct CRVBOXHGSMI_CLIENT {
|
---|
75 | struct VBOXUHGSMI *pHgsmi;
|
---|
76 | struct VBOXUHGSMI_BUFFER *pCmdBuffer;
|
---|
77 | struct VBOXUHGSMI_BUFFER *pHGBuffer;
|
---|
78 | void *pvHGBuffer;
|
---|
79 | struct CRBufferPool_t *bufpool;
|
---|
80 | } CRVBOXHGSMI_CLIENT, *PCRVBOXHGSMI_CLIENT;
|
---|
81 | #endif /* IN_GUEST */
|
---|
82 | #endif /* #ifdef VBOX_WITH_CRHGSMI */
|
---|
83 | /**
|
---|
84 | * Chromium network connection (bidirectional).
|
---|
85 | */
|
---|
86 | struct CRConnection {
|
---|
87 | int ignore;
|
---|
88 | CRConnectionType type;
|
---|
89 | unsigned int id; /* obtained from the mothership (if brokered) */
|
---|
90 |
|
---|
91 | /* List of messages that we've received on the network connection but
|
---|
92 | * nobody has yet consumed.
|
---|
93 | */
|
---|
94 | CRMessageList messageList;
|
---|
95 |
|
---|
96 | CRMultiBuffer multi;
|
---|
97 |
|
---|
98 | unsigned int mtu; /* max transmission unit size (in bytes) */
|
---|
99 | unsigned int buffer_size;
|
---|
100 | unsigned int krecv_buf_size;
|
---|
101 | int broker; /* is connection brokered through mothership? */
|
---|
102 | int threaded; /* is this a threaded connection? */
|
---|
103 | int swap;
|
---|
104 | int actual_network; /* is this a real network? */
|
---|
105 |
|
---|
106 | unsigned char *userbuf;
|
---|
107 | int userbuf_len;
|
---|
108 |
|
---|
109 | /* To allocate a data buffer of size conn->buffer_size bytes */
|
---|
110 | void *(*Alloc)( CRConnection *conn );
|
---|
111 | /* To indicate the client's done with a data buffer */
|
---|
112 | void (*Free)( CRConnection *conn, void *buf );
|
---|
113 | /* To send a data buffer. If bufp is non-null, it must have been obtained
|
---|
114 | * from Alloc() and it'll be freed when Send() returns.
|
---|
115 | */
|
---|
116 | void (*Send)( CRConnection *conn, void **buf, const void *start, unsigned int len );
|
---|
117 | /* To send a data buffer than can optionally be dropped on the floor */
|
---|
118 | void (*Barf)( CRConnection *conn, void **buf, const void *start, unsigned int len );
|
---|
119 | /* To send 'len' bytes from buffer at 'start', no funny business */
|
---|
120 | void (*SendExact)( CRConnection *conn, const void *start, unsigned int len );
|
---|
121 | /* To receive data. 'len' bytes will be placed into 'buf'. */
|
---|
122 | void (*Recv)( CRConnection *conn, void *buf, unsigned int len );
|
---|
123 | /* To receive one message on the connection */
|
---|
124 | void (*RecvMsg)( CRConnection *conn );
|
---|
125 | /* What's this??? */
|
---|
126 | void (*InstantReclaim)( CRConnection *conn, CRMessage *mess );
|
---|
127 | /* Called when a full CR_MESSAGE_MULTI_HEAD/TAIL message has been received */
|
---|
128 | void (*HandleNewMessage)( CRConnection *conn, CRMessage *mess, unsigned int len );
|
---|
129 | /* To accept a new connection from a client */
|
---|
130 | void (*Accept)( CRConnection *conn);
|
---|
131 | /* To connect to a server (return 0 if error, 1 if success) */
|
---|
132 | int (*Connect)( CRConnection *conn );
|
---|
133 | /* To disconnect from a server */
|
---|
134 | void (*Disconnect)( CRConnection *conn );
|
---|
135 |
|
---|
136 | unsigned int sizeof_buffer_header;
|
---|
137 |
|
---|
138 | /* logging */
|
---|
139 | int total_bytes_sent;
|
---|
140 | int total_bytes_recv;
|
---|
141 | int recv_count;
|
---|
142 | int opcodes_count;
|
---|
143 |
|
---|
144 | /* credits for flow control */
|
---|
145 | int send_credits;
|
---|
146 | int recv_credits;
|
---|
147 |
|
---|
148 | /* VBox HGCM */
|
---|
149 | int index;
|
---|
150 | uint32_t u32ClientID;
|
---|
151 | uint8_t *pBuffer;
|
---|
152 | uint32_t cbBuffer;
|
---|
153 | uint8_t *pHostBuffer;
|
---|
154 | uint32_t cbHostBufferAllocated;
|
---|
155 | uint32_t cbHostBuffer;
|
---|
156 | #ifdef IN_GUEST
|
---|
157 | uint32_t u32InjectClientID;
|
---|
158 | # ifdef VBOX_WITH_CRHGSMI
|
---|
159 | CRVBOXHGSMI_CLIENT HgsmiClient;
|
---|
160 | struct VBOXUHGSMI *pExternalHgsmi;
|
---|
161 | # endif
|
---|
162 | #else
|
---|
163 | # ifdef VBOX_WITH_CRHGSMI
|
---|
164 | struct _crclient *pClient; /* back reference, just for simplicity */
|
---|
165 | CRVBOXHGSMI_CMDDATA CmdData;
|
---|
166 | # endif
|
---|
167 | RTLISTANCHOR PendingMsgList;
|
---|
168 | #endif
|
---|
169 | /* Used on host side to indicate that we are not allowed to store above pointers for later use
|
---|
170 | * in crVBoxHGCMReceiveMessage. As those messages are going to be processed after the corresponding
|
---|
171 | * HGCM call is finished and memory is freed. So we have to store a copy.
|
---|
172 | * This happens when message processing for client associated with this connection
|
---|
173 | * is blocked by another client, which has send us glBegin call and we're waiting to receive glEnd.
|
---|
174 | */
|
---|
175 | uint8_t allow_redir_ptr;
|
---|
176 |
|
---|
177 | uint32_t vMajor, vMinor; /*Protocol version*/
|
---|
178 | };
|
---|
179 |
|
---|
180 |
|
---|
181 | /*
|
---|
182 | * Network functions
|
---|
183 | */
|
---|
184 | extern DECLEXPORT(int) crGetHostname( char *buf, unsigned int len );
|
---|
185 |
|
---|
186 | extern DECLEXPORT(void) crNetInit( CRNetReceiveFunc recvFunc, CRNetCloseFunc closeFunc );
|
---|
187 | extern DECLEXPORT(void) crNetTearDown(void);
|
---|
188 |
|
---|
189 | extern DECLEXPORT(void) *crNetAlloc( CRConnection *conn );
|
---|
190 | extern DECLEXPORT(void) crNetFree( CRConnection *conn, void *buf );
|
---|
191 |
|
---|
192 | extern DECLEXPORT(void) crNetAccept( CRConnection *conn );
|
---|
193 | extern DECLEXPORT(int) crNetConnect( CRConnection *conn );
|
---|
194 | extern DECLEXPORT(void) crNetDisconnect( CRConnection *conn );
|
---|
195 | extern DECLEXPORT(void) crNetFreeConnection( CRConnection *conn );
|
---|
196 |
|
---|
197 | extern DECLEXPORT(void) crNetSend( CRConnection *conn, void **bufp, const void *start, unsigned int len );
|
---|
198 | extern DECLEXPORT(void) crNetBarf( CRConnection *conn, void **bufp, const void *start, unsigned int len );
|
---|
199 | extern DECLEXPORT(void) crNetSendExact( CRConnection *conn, const void *start, unsigned int len );
|
---|
200 | extern DECLEXPORT(void) crNetSingleRecv( CRConnection *conn, void *buf, unsigned int len );
|
---|
201 | extern DECLEXPORT(unsigned int) crNetGetMessage( CRConnection *conn, CRMessage **message );
|
---|
202 | extern DECLEXPORT(unsigned int) crNetPeekMessage( CRConnection *conn, CRMessage **message );
|
---|
203 | extern DECLEXPORT(int) crNetNumMessages(CRConnection *conn);
|
---|
204 | extern DECLEXPORT(void) crNetReadline( CRConnection *conn, void *buf );
|
---|
205 | extern DECLEXPORT(int) crNetRecv(
|
---|
206 | #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
207 | CRConnection *conn
|
---|
208 | #else
|
---|
209 | void
|
---|
210 | #endif
|
---|
211 | );
|
---|
212 | #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
213 | #define CR_WRITEBACK_WAIT(_conn, _writeback) do { \
|
---|
214 | while (_writeback) { \
|
---|
215 | RTThreadYield(); \
|
---|
216 | crNetRecv(_conn); \
|
---|
217 | } \
|
---|
218 | } while (0)
|
---|
219 | #else
|
---|
220 | #define CR_WRITEBACK_WAIT(_conn, _writeback) do { \
|
---|
221 | while (_writeback) { \
|
---|
222 | RTThreadYield(); \
|
---|
223 | crNetRecv(); \
|
---|
224 | } \
|
---|
225 | } while (0)
|
---|
226 |
|
---|
227 | #endif
|
---|
228 | #ifdef IN_GUEST
|
---|
229 | extern DECLEXPORT(uint32_t) crNetHostCapsGet(void);
|
---|
230 | #endif
|
---|
231 | extern DECLEXPORT(void) crNetDefaultRecv( CRConnection *conn, CRMessage *msg, unsigned int len );
|
---|
232 | extern DECLEXPORT(void) crNetDispatchMessage( CRNetReceiveFuncList *rfl, CRConnection *conn, CRMessage *msg, unsigned int len );
|
---|
233 |
|
---|
234 | extern DECLEXPORT(CRConnection *) crNetConnectToServer( const char *server, int mtu, int broker
|
---|
235 | #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
236 | , struct VBOXUHGSMI *pHgsmi
|
---|
237 | #endif
|
---|
238 | );
|
---|
239 | extern DECLEXPORT(CRConnection *) crNetAcceptClient( const char *protocol, const char *hostname, unsigned short port, unsigned int mtu, int broker );
|
---|
240 |
|
---|
241 |
|
---|
242 | extern DECLEXPORT(void) crInitMessageList(CRMessageList *list);
|
---|
243 | extern DECLEXPORT(void) crEnqueueMessage(CRMessageList *list, CRMessage *msg, unsigned int len, CRConnection *conn);
|
---|
244 | extern DECLEXPORT(void) crDequeueMessage(CRMessageList *list, CRMessage **msg, unsigned int *len, CRConnection **conn);
|
---|
245 |
|
---|
246 | extern DECLEXPORT(void) crNetRecvReadPixels( const CRMessageReadPixels *rp, unsigned int len );
|
---|
247 |
|
---|
248 |
|
---|
249 | /*
|
---|
250 | * Socket callback facility
|
---|
251 | */
|
---|
252 | #define CR_SOCKET_CREATE 1
|
---|
253 | #define CR_SOCKET_DESTROY 2
|
---|
254 | typedef void (*CRSocketCallbackProc)(int mode, int socket);
|
---|
255 | extern DECLEXPORT(void) crRegisterSocketCallback(int mode, CRSocketCallbackProc proc);
|
---|
256 |
|
---|
257 |
|
---|
258 | #ifdef __cplusplus
|
---|
259 | }
|
---|
260 | #endif
|
---|
261 |
|
---|
262 | #endif /* CR_NET_H */
|
---|