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 WINDOWS
|
---|
11 | #define WIN32_LEAN_AND_MEAN
|
---|
12 | #pragma warning( push, 3 ) /* shut up about warnings in YOUR OWN HEADER FILES!!! */
|
---|
13 | #include <winsock.h>
|
---|
14 | #endif
|
---|
15 |
|
---|
16 | #include <stdio.h>
|
---|
17 |
|
---|
18 | #ifndef WINDOWS
|
---|
19 | #include <sys/socket.h>
|
---|
20 | #ifndef DARWIN
|
---|
21 | #ifdef AF_INET6
|
---|
22 | /* getaddrinfo & co appeared with ipv6 */
|
---|
23 | #define ADDRINFO
|
---|
24 | #endif
|
---|
25 | #endif
|
---|
26 | #include <netinet/in.h>
|
---|
27 | #endif
|
---|
28 |
|
---|
29 | #ifdef SunOS
|
---|
30 | #include <sys/types.h>
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | #include "cr_protocol.h"
|
---|
34 | #include "cr_threads.h"
|
---|
35 |
|
---|
36 | #include <iprt/types.h>
|
---|
37 |
|
---|
38 | #ifdef __cplusplus
|
---|
39 | extern "C" {
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | #define DEFAULT_SERVER_PORT 7000
|
---|
43 |
|
---|
44 | /* If you change this, update DefaultMothershipPort in mothership.py */
|
---|
45 | #define DEFAULT_MOTHERSHIP_PORT 10000
|
---|
46 |
|
---|
47 | typedef struct CRConnection CRConnection;
|
---|
48 |
|
---|
49 | typedef enum {
|
---|
50 | CR_NO_CONNECTION,
|
---|
51 | CR_SDP,
|
---|
52 | CR_TCPIP,
|
---|
53 | CR_UDPTCPIP,
|
---|
54 | CR_FILE,
|
---|
55 | CR_GM,
|
---|
56 | CR_IB,
|
---|
57 | CR_TEAC,
|
---|
58 | CR_TCSCOMM,
|
---|
59 | CR_VBOXHGCM,
|
---|
60 | CR_DROP_PACKETS
|
---|
61 | } CRConnectionType;
|
---|
62 |
|
---|
63 | #if defined(WINDOWS)
|
---|
64 | typedef SOCKET CRSocket;
|
---|
65 | #else
|
---|
66 | typedef int CRSocket;
|
---|
67 | #endif
|
---|
68 |
|
---|
69 | typedef void (*CRVoidFunc)( void );
|
---|
70 | typedef int (*CRNetReceiveFunc)( CRConnection *conn, CRMessage *msg, unsigned int len );
|
---|
71 | typedef int (*CRNetConnectFunc)( CRConnection *conn );
|
---|
72 | typedef void (*CRNetCloseFunc)( unsigned int sender_id );
|
---|
73 |
|
---|
74 | typedef struct __recvFuncList {
|
---|
75 | CRNetReceiveFunc recv;
|
---|
76 | struct __recvFuncList *next;
|
---|
77 | } CRNetReceiveFuncList;
|
---|
78 |
|
---|
79 | typedef struct __closeFuncList {
|
---|
80 | CRNetCloseFunc close;
|
---|
81 | struct __closeFuncList *next;
|
---|
82 | } CRNetCloseFuncList;
|
---|
83 |
|
---|
84 | typedef struct __messageListNode {
|
---|
85 | CRMessage *mesg; /* the actual message (header + payload) */
|
---|
86 | unsigned int len; /* length of message (header + payload) */
|
---|
87 | CRConnection *conn; /* some messages are assoc. with specific connections*/
|
---|
88 | struct __messageListNode *next; /* next in list */
|
---|
89 | } CRMessageListNode;
|
---|
90 |
|
---|
91 | typedef struct {
|
---|
92 | CRMessageListNode *head, *tail;
|
---|
93 | int numMessages;
|
---|
94 | CRmutex lock;
|
---|
95 | CRcondition nonEmpty;
|
---|
96 | } CRMessageList;
|
---|
97 |
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Used to accumulate CR_MESSAGE_MULTI_BODY/TAIL chunks into one big buffer.
|
---|
101 | */
|
---|
102 | typedef struct CRMultiBuffer {
|
---|
103 | unsigned int len; /* current length (<= max) (with sizeof_buffer_header) */
|
---|
104 | unsigned int max; /* size in bytes of data buffer */
|
---|
105 | void *buf; /* data buffer */
|
---|
106 | } CRMultiBuffer;
|
---|
107 |
|
---|
108 | #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
109 | typedef struct CRVBOXHGSMI_CLIENT {
|
---|
110 | struct VBOXUHGSMI *pHgsmi;
|
---|
111 | struct VBOXUHGSMI_BUFFER *pCmdBuffer;
|
---|
112 | struct VBOXUHGSMI_BUFFER *pHGBuffer;
|
---|
113 | void *pvHGBuffer;
|
---|
114 | struct CRBufferPool_t *bufpool;
|
---|
115 | } CRVBOXHGSMI_CLIENT, *PCRVBOXHGSMI_CLIENT;
|
---|
116 | #endif
|
---|
117 | /**
|
---|
118 | * Chromium network connection (bidirectional).
|
---|
119 | */
|
---|
120 | struct CRConnection {
|
---|
121 | int ignore;
|
---|
122 | CRConnectionType type;
|
---|
123 | unsigned int id; /* obtained from the mothership (if brokered) */
|
---|
124 |
|
---|
125 | /* List of messages that we've received on the network connection but
|
---|
126 | * nobody has yet consumed.
|
---|
127 | */
|
---|
128 | CRMessageList messageList;
|
---|
129 |
|
---|
130 | CRMultiBuffer multi;
|
---|
131 |
|
---|
132 | unsigned int mtu; /* max transmission unit size (in bytes) */
|
---|
133 | unsigned int buffer_size;
|
---|
134 | unsigned int krecv_buf_size;
|
---|
135 | int broker; /* is connection brokered through mothership? */
|
---|
136 | int threaded; /* is this a threaded connection? */
|
---|
137 | int endianness, swap;
|
---|
138 | int actual_network; /* is this a real network? */
|
---|
139 |
|
---|
140 | unsigned char *userbuf;
|
---|
141 | int userbuf_len;
|
---|
142 |
|
---|
143 | char *hostname;
|
---|
144 | int port;
|
---|
145 |
|
---|
146 | /* To allocate a data buffer of size conn->buffer_size bytes */
|
---|
147 | void *(*Alloc)( CRConnection *conn );
|
---|
148 | /* To indicate the client's done with a data buffer */
|
---|
149 | void (*Free)( CRConnection *conn, void *buf );
|
---|
150 | /* To send a data buffer. If bufp is non-null, it must have been obtained
|
---|
151 | * from Alloc() and it'll be freed when Send() returns.
|
---|
152 | */
|
---|
153 | void (*Send)( CRConnection *conn, void **buf, const void *start, unsigned int len );
|
---|
154 | /* To send a data buffer than can optionally be dropped on the floor */
|
---|
155 | void (*Barf)( CRConnection *conn, void **buf, const void *start, unsigned int len );
|
---|
156 | /* To send 'len' bytes from buffer at 'start', no funny business */
|
---|
157 | void (*SendExact)( CRConnection *conn, const void *start, unsigned int len );
|
---|
158 | /* To receive data. 'len' bytes will be placed into 'buf'. */
|
---|
159 | void (*Recv)( CRConnection *conn, void *buf, unsigned int len );
|
---|
160 | /* To receive one message on the connection */
|
---|
161 | void (*RecvMsg)( CRConnection *conn );
|
---|
162 | /* What's this??? */
|
---|
163 | void (*InstantReclaim)( CRConnection *conn, CRMessage *mess );
|
---|
164 | /* Called when a full CR_MESSAGE_MULTI_HEAD/TAIL message has been received */
|
---|
165 | void (*HandleNewMessage)( CRConnection *conn, CRMessage *mess, unsigned int len );
|
---|
166 | /* To accept a new connection from a client */
|
---|
167 | void (*Accept)( CRConnection *conn, const char *hostname, unsigned short port );
|
---|
168 | /* To connect to a server (return 0 if error, 1 if success) */
|
---|
169 | int (*Connect)( CRConnection *conn );
|
---|
170 | /* To disconnect from a server */
|
---|
171 | void (*Disconnect)( CRConnection *conn );
|
---|
172 |
|
---|
173 | unsigned int sizeof_buffer_header;
|
---|
174 |
|
---|
175 | /* logging */
|
---|
176 | int total_bytes_sent;
|
---|
177 | int total_bytes_recv;
|
---|
178 | int recv_count;
|
---|
179 | int opcodes_count;
|
---|
180 |
|
---|
181 | /* credits for flow control */
|
---|
182 | int send_credits;
|
---|
183 | int recv_credits;
|
---|
184 |
|
---|
185 | /* TCP/IP */
|
---|
186 | CRSocket tcp_socket;
|
---|
187 | int index;
|
---|
188 |
|
---|
189 | CRSocket sdp_socket;
|
---|
190 |
|
---|
191 | /* UDP/IP */
|
---|
192 | CRSocket udp_socket;
|
---|
193 | #ifndef ADDRINFO
|
---|
194 | struct sockaddr_in remoteaddr;
|
---|
195 | #else
|
---|
196 | struct sockaddr_storage remoteaddr;
|
---|
197 | #endif
|
---|
198 |
|
---|
199 | /* UDP/TCP/IP */
|
---|
200 | unsigned int seq;
|
---|
201 | unsigned int ack;
|
---|
202 | void *udp_packet;
|
---|
203 | int udp_packetlen;
|
---|
204 |
|
---|
205 | /* FILE Tracing */
|
---|
206 | enum { CR_FILE_WRITE, CR_FILE_READ } file_direction;
|
---|
207 | char *filename;
|
---|
208 | int fd;
|
---|
209 |
|
---|
210 | /* Myrinet GM */
|
---|
211 | unsigned int gm_node_id;
|
---|
212 | unsigned int gm_port_num;
|
---|
213 |
|
---|
214 | /* Mellanox IB */
|
---|
215 | unsigned int ib_node_id;
|
---|
216 | unsigned int ib_port_num;
|
---|
217 |
|
---|
218 | /* Quadrics Elan3 (teac) */
|
---|
219 | int teac_id;
|
---|
220 | int teac_rank;
|
---|
221 |
|
---|
222 | /* Quadrics Elan3 (tcscomm) */
|
---|
223 | int tcscomm_id;
|
---|
224 | int tcscomm_rank;
|
---|
225 |
|
---|
226 | /* VBox HGCM */
|
---|
227 | uint32_t u32ClientID;
|
---|
228 | uint8_t *pBuffer;
|
---|
229 | uint32_t cbBuffer;
|
---|
230 | uint8_t *pHostBuffer;
|
---|
231 | uint32_t cbHostBufferAllocated;
|
---|
232 | uint32_t cbHostBuffer;
|
---|
233 | #ifdef IN_GUEST
|
---|
234 | uint32_t u32InjectClientID;
|
---|
235 | # ifdef VBOX_WITH_CRHGSMI
|
---|
236 | # ifndef VBOX_CRHGSMI_WITH_D3DDEV
|
---|
237 | CRVBOXHGSMI_CLIENT HgsmiClient;
|
---|
238 | # endif
|
---|
239 | # endif
|
---|
240 | #endif
|
---|
241 | /* Used on host side to indicate that we are not allowed to store above pointers for later use
|
---|
242 | * in crVBoxHGCMReceiveMessage. As those messages are going to be processed after the corresponding
|
---|
243 | * HGCM call is finished and memory is freed. So we have to store a copy.
|
---|
244 | * This happens when message processing for client associated with this connection
|
---|
245 | * is blocked by another client, which has send us glBegin call and we're waiting to receive glEnd.
|
---|
246 | */
|
---|
247 | uint8_t allow_redir_ptr;
|
---|
248 |
|
---|
249 | uint32_t vMajor, vMinor; /*Protocol version*/
|
---|
250 | };
|
---|
251 |
|
---|
252 |
|
---|
253 | /*
|
---|
254 | * Network functions
|
---|
255 | */
|
---|
256 | extern DECLEXPORT(int) crGetHostname( char *buf, unsigned int len );
|
---|
257 |
|
---|
258 | extern DECLEXPORT(void) crNetInit( CRNetReceiveFunc recvFunc, CRNetCloseFunc closeFunc );
|
---|
259 | extern DECLEXPORT(void) crNetTearDown();
|
---|
260 |
|
---|
261 | extern DECLEXPORT(void) *crNetAlloc( CRConnection *conn );
|
---|
262 | extern DECLEXPORT(void) crNetFree( CRConnection *conn, void *buf );
|
---|
263 |
|
---|
264 | extern DECLEXPORT(void) crNetAccept( CRConnection *conn, const char *hostname, unsigned short port );
|
---|
265 | extern DECLEXPORT(int) crNetConnect( CRConnection *conn );
|
---|
266 | extern DECLEXPORT(void) crNetDisconnect( CRConnection *conn );
|
---|
267 | extern DECLEXPORT(void) crNetFreeConnection( CRConnection *conn );
|
---|
268 | extern DECLEXPORT(void) crCloseSocket( CRSocket sock );
|
---|
269 |
|
---|
270 | extern DECLEXPORT(void) crNetSend( CRConnection *conn, void **bufp, const void *start, unsigned int len );
|
---|
271 | extern DECLEXPORT(void) crNetBarf( CRConnection *conn, void **bufp, const void *start, unsigned int len );
|
---|
272 | extern DECLEXPORT(void) crNetSendExact( CRConnection *conn, const void *start, unsigned int len );
|
---|
273 | extern DECLEXPORT(void) crNetSingleRecv( CRConnection *conn, void *buf, unsigned int len );
|
---|
274 | extern DECLEXPORT(unsigned int) crNetGetMessage( CRConnection *conn, CRMessage **message );
|
---|
275 | extern DECLEXPORT(unsigned int) crNetPeekMessage( CRConnection *conn, CRMessage **message );
|
---|
276 | extern DECLEXPORT(int) crNetNumMessages(CRConnection *conn);
|
---|
277 | extern DECLEXPORT(void) crNetReadline( CRConnection *conn, void *buf );
|
---|
278 | extern DECLEXPORT(int) crNetRecv( void );
|
---|
279 | extern DECLEXPORT(void) crNetDefaultRecv( CRConnection *conn, CRMessage *msg, unsigned int len );
|
---|
280 | extern DECLEXPORT(void) crNetDispatchMessage( CRNetReceiveFuncList *rfl, CRConnection *conn, CRMessage *msg, unsigned int len );
|
---|
281 |
|
---|
282 | extern DECLEXPORT(CRConnection *) crNetConnectToServer( const char *server, unsigned short default_port, int mtu, int broker );
|
---|
283 | extern DECLEXPORT(CRConnection *) crNetAcceptClient( const char *protocol, const char *hostname, unsigned short port, unsigned int mtu, int broker );
|
---|
284 |
|
---|
285 |
|
---|
286 | extern DECLEXPORT(void) crInitMessageList(CRMessageList *list);
|
---|
287 | extern DECLEXPORT(void) crEnqueueMessage(CRMessageList *list, CRMessage *msg, unsigned int len, CRConnection *conn);
|
---|
288 | extern DECLEXPORT(void) crDequeueMessage(CRMessageList *list, CRMessage **msg, unsigned int *len, CRConnection **conn);
|
---|
289 |
|
---|
290 | extern DECLEXPORT(void) crNetRecvReadPixels( const CRMessageReadPixels *rp, unsigned int len );
|
---|
291 |
|
---|
292 |
|
---|
293 | /*
|
---|
294 | * Quadrics stuff
|
---|
295 | */
|
---|
296 | #define CR_QUADRICS_DEFAULT_LOW_CONTEXT 32
|
---|
297 | #define CR_QUADRICS_DEFAULT_HIGH_CONTEXT 35
|
---|
298 |
|
---|
299 | extern DECLEXPORT(void) crNetSetRank( int my_rank );
|
---|
300 | extern DECLEXPORT(void) crNetSetContextRange( int low_context, int high_context );
|
---|
301 | extern DECLEXPORT(void) crNetSetNodeRange( const char *low_node, const char *high_node );
|
---|
302 | extern DECLEXPORT(void) crNetSetKey( const unsigned char* key, const int keyLength );
|
---|
303 |
|
---|
304 |
|
---|
305 | /*
|
---|
306 | * Socket callback facility
|
---|
307 | */
|
---|
308 | #define CR_SOCKET_CREATE 1
|
---|
309 | #define CR_SOCKET_DESTROY 2
|
---|
310 | typedef void (*CRSocketCallbackProc)(int mode, int socket);
|
---|
311 | extern DECLEXPORT(void) crRegisterSocketCallback(int mode, CRSocketCallbackProc proc);
|
---|
312 |
|
---|
313 |
|
---|
314 | #ifdef __cplusplus
|
---|
315 | }
|
---|
316 | #endif
|
---|
317 |
|
---|
318 | #endif /* CR_NET_H */
|
---|