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 | /**
|
---|
109 | * Chromium network connection (bidirectional).
|
---|
110 | */
|
---|
111 | struct CRConnection {
|
---|
112 | int ignore;
|
---|
113 | CRConnectionType type;
|
---|
114 | unsigned int id; /* obtained from the mothership (if brokered) */
|
---|
115 |
|
---|
116 | /* List of messages that we've received on the network connection but
|
---|
117 | * nobody has yet consumed.
|
---|
118 | */
|
---|
119 | CRMessageList messageList;
|
---|
120 |
|
---|
121 | CRMultiBuffer multi;
|
---|
122 |
|
---|
123 | unsigned int mtu; /* max transmission unit size (in bytes) */
|
---|
124 | unsigned int buffer_size;
|
---|
125 | unsigned int krecv_buf_size;
|
---|
126 | int broker; /* is connection brokered through mothership? */
|
---|
127 | int threaded; /* is this a threaded connection? */
|
---|
128 | int endianness, swap;
|
---|
129 | int actual_network; /* is this a real network? */
|
---|
130 |
|
---|
131 | unsigned char *userbuf;
|
---|
132 | int userbuf_len;
|
---|
133 |
|
---|
134 | char *hostname;
|
---|
135 | int port;
|
---|
136 |
|
---|
137 | /* To allocate a data buffer of size conn->buffer_size bytes */
|
---|
138 | void *(*Alloc)( CRConnection *conn );
|
---|
139 | /* To indicate the client's done with a data buffer */
|
---|
140 | void (*Free)( CRConnection *conn, void *buf );
|
---|
141 | /* To send a data buffer. If bufp is non-null, it must have been obtained
|
---|
142 | * from Alloc() and it'll be freed when Send() returns.
|
---|
143 | */
|
---|
144 | void (*Send)( CRConnection *conn, void **buf, const void *start, unsigned int len );
|
---|
145 | /* To send a data buffer than can optionally be dropped on the floor */
|
---|
146 | void (*Barf)( CRConnection *conn, void **buf, const void *start, unsigned int len );
|
---|
147 | /* To send 'len' bytes from buffer at 'start', no funny business */
|
---|
148 | void (*SendExact)( CRConnection *conn, const void *start, unsigned int len );
|
---|
149 | /* To receive data. 'len' bytes will be placed into 'buf'. */
|
---|
150 | void (*Recv)( CRConnection *conn, void *buf, unsigned int len );
|
---|
151 | /* To receive one message on the connection */
|
---|
152 | void (*RecvMsg)( CRConnection *conn );
|
---|
153 | /* What's this??? */
|
---|
154 | void (*InstantReclaim)( CRConnection *conn, CRMessage *mess );
|
---|
155 | /* Called when a full CR_MESSAGE_MULTI_HEAD/TAIL message has been received */
|
---|
156 | void (*HandleNewMessage)( CRConnection *conn, CRMessage *mess, unsigned int len );
|
---|
157 | /* To accept a new connection from a client */
|
---|
158 | void (*Accept)( CRConnection *conn, const char *hostname, unsigned short port );
|
---|
159 | /* To connect to a server (return 0 if error, 1 if success) */
|
---|
160 | int (*Connect)( CRConnection *conn );
|
---|
161 | /* To disconnect from a server */
|
---|
162 | void (*Disconnect)( CRConnection *conn );
|
---|
163 |
|
---|
164 | unsigned int sizeof_buffer_header;
|
---|
165 |
|
---|
166 | /* logging */
|
---|
167 | int total_bytes_sent;
|
---|
168 | int total_bytes_recv;
|
---|
169 | int recv_count;
|
---|
170 |
|
---|
171 | /* credits for flow control */
|
---|
172 | int send_credits;
|
---|
173 | int recv_credits;
|
---|
174 |
|
---|
175 | /* TCP/IP */
|
---|
176 | CRSocket tcp_socket;
|
---|
177 | int index;
|
---|
178 |
|
---|
179 | CRSocket sdp_socket;
|
---|
180 |
|
---|
181 | /* UDP/IP */
|
---|
182 | CRSocket udp_socket;
|
---|
183 | #ifndef ADDRINFO
|
---|
184 | struct sockaddr_in remoteaddr;
|
---|
185 | #else
|
---|
186 | struct sockaddr_storage remoteaddr;
|
---|
187 | #endif
|
---|
188 |
|
---|
189 | /* UDP/TCP/IP */
|
---|
190 | unsigned int seq;
|
---|
191 | unsigned int ack;
|
---|
192 | void *udp_packet;
|
---|
193 | int udp_packetlen;
|
---|
194 |
|
---|
195 | /* FILE Tracing */
|
---|
196 | enum { CR_FILE_WRITE, CR_FILE_READ } file_direction;
|
---|
197 | char *filename;
|
---|
198 | int fd;
|
---|
199 |
|
---|
200 | /* Myrinet GM */
|
---|
201 | unsigned int gm_node_id;
|
---|
202 | unsigned int gm_port_num;
|
---|
203 |
|
---|
204 | /* Mellanox IB */
|
---|
205 | unsigned int ib_node_id;
|
---|
206 | unsigned int ib_port_num;
|
---|
207 |
|
---|
208 | /* Quadrics Elan3 (teac) */
|
---|
209 | int teac_id;
|
---|
210 | int teac_rank;
|
---|
211 |
|
---|
212 | /* Quadrics Elan3 (tcscomm) */
|
---|
213 | int tcscomm_id;
|
---|
214 | int tcscomm_rank;
|
---|
215 |
|
---|
216 | /* VBox HGCM */
|
---|
217 | uint32_t u32ClientID;
|
---|
218 | uint8_t *pBuffer;
|
---|
219 | uint32_t cbBuffer;
|
---|
220 | uint8_t *pHostBuffer;
|
---|
221 | uint32_t cbHostBufferAllocated;
|
---|
222 | uint32_t cbHostBuffer;
|
---|
223 | #ifdef IN_GUEST
|
---|
224 | uint32_t u32InjectClientID;
|
---|
225 | #endif
|
---|
226 | /* Used on host side to indicate that we are not allowed to store above pointers for later use
|
---|
227 | * in crVBoxHGCMReceiveMessage. As those messages are going to be processed after the correspoding
|
---|
228 | * HGCM call is finished and memory is freed. So we have to store a copy.
|
---|
229 | * This happens when message processing for client associated with this connection
|
---|
230 | * is blocked by another client, which has send us glBegin call and we're waiting to recieve glEnd.
|
---|
231 | */
|
---|
232 | uint8_t allow_redir_ptr;
|
---|
233 |
|
---|
234 | uint32_t vMajor, vMinor; /*Protocol version*/
|
---|
235 | };
|
---|
236 |
|
---|
237 |
|
---|
238 | /*
|
---|
239 | * Network functions
|
---|
240 | */
|
---|
241 | extern DECLEXPORT(int) crGetHostname( char *buf, unsigned int len );
|
---|
242 |
|
---|
243 | extern DECLEXPORT(void) crNetInit( CRNetReceiveFunc recvFunc, CRNetCloseFunc closeFunc );
|
---|
244 | extern DECLEXPORT(void) crNetTearDown();
|
---|
245 |
|
---|
246 | extern DECLEXPORT(void) *crNetAlloc( CRConnection *conn );
|
---|
247 | extern DECLEXPORT(void) crNetFree( CRConnection *conn, void *buf );
|
---|
248 |
|
---|
249 | extern DECLEXPORT(void) crNetAccept( CRConnection *conn, const char *hostname, unsigned short port );
|
---|
250 | extern DECLEXPORT(int) crNetConnect( CRConnection *conn );
|
---|
251 | extern DECLEXPORT(void) crNetDisconnect( CRConnection *conn );
|
---|
252 | extern DECLEXPORT(void) crNetFreeConnection( CRConnection *conn );
|
---|
253 | extern DECLEXPORT(void) crCloseSocket( CRSocket sock );
|
---|
254 |
|
---|
255 | extern DECLEXPORT(void) crNetSend( CRConnection *conn, void **bufp, const void *start, unsigned int len );
|
---|
256 | extern DECLEXPORT(void) crNetBarf( CRConnection *conn, void **bufp, const void *start, unsigned int len );
|
---|
257 | extern DECLEXPORT(void) crNetSendExact( CRConnection *conn, const void *start, unsigned int len );
|
---|
258 | extern DECLEXPORT(void) crNetSingleRecv( CRConnection *conn, void *buf, unsigned int len );
|
---|
259 | extern DECLEXPORT(unsigned int) crNetGetMessage( CRConnection *conn, CRMessage **message );
|
---|
260 | extern DECLEXPORT(unsigned int) crNetPeekMessage( CRConnection *conn, CRMessage **message );
|
---|
261 | extern DECLEXPORT(int) crNetNumMessages(CRConnection *conn);
|
---|
262 | extern DECLEXPORT(void) crNetReadline( CRConnection *conn, void *buf );
|
---|
263 | extern DECLEXPORT(int) crNetRecv( void );
|
---|
264 | extern DECLEXPORT(void) crNetDefaultRecv( CRConnection *conn, CRMessage *msg, unsigned int len );
|
---|
265 | extern DECLEXPORT(void) crNetDispatchMessage( CRNetReceiveFuncList *rfl, CRConnection *conn, CRMessage *msg, unsigned int len );
|
---|
266 |
|
---|
267 | extern DECLEXPORT(CRConnection *) crNetConnectToServer( const char *server, unsigned short default_port, int mtu, int broker );
|
---|
268 | extern DECLEXPORT(CRConnection *) crNetAcceptClient( const char *protocol, const char *hostname, unsigned short port, unsigned int mtu, int broker );
|
---|
269 |
|
---|
270 |
|
---|
271 | extern DECLEXPORT(void) crInitMessageList(CRMessageList *list);
|
---|
272 | extern DECLEXPORT(void) crEnqueueMessage(CRMessageList *list, CRMessage *msg, unsigned int len, CRConnection *conn);
|
---|
273 | extern DECLEXPORT(void) crDequeueMessage(CRMessageList *list, CRMessage **msg, unsigned int *len, CRConnection **conn);
|
---|
274 |
|
---|
275 | extern DECLEXPORT(void) crNetRecvReadPixels( const CRMessageReadPixels *rp, unsigned int len );
|
---|
276 |
|
---|
277 |
|
---|
278 | /*
|
---|
279 | * Quadrics stuff
|
---|
280 | */
|
---|
281 | #define CR_QUADRICS_DEFAULT_LOW_CONTEXT 32
|
---|
282 | #define CR_QUADRICS_DEFAULT_HIGH_CONTEXT 35
|
---|
283 |
|
---|
284 | extern DECLEXPORT(void) crNetSetRank( int my_rank );
|
---|
285 | extern DECLEXPORT(void) crNetSetContextRange( int low_context, int high_context );
|
---|
286 | extern DECLEXPORT(void) crNetSetNodeRange( const char *low_node, const char *high_node );
|
---|
287 | extern DECLEXPORT(void) crNetSetKey( const unsigned char* key, const int keyLength );
|
---|
288 |
|
---|
289 |
|
---|
290 | /*
|
---|
291 | * Socket callback facility
|
---|
292 | */
|
---|
293 | #define CR_SOCKET_CREATE 1
|
---|
294 | #define CR_SOCKET_DESTROY 2
|
---|
295 | typedef void (*CRSocketCallbackProc)(int mode, int socket);
|
---|
296 | extern DECLEXPORT(void) crRegisterSocketCallback(int mode, CRSocketCallbackProc proc);
|
---|
297 |
|
---|
298 |
|
---|
299 | #ifdef __cplusplus
|
---|
300 | }
|
---|
301 | #endif
|
---|
302 |
|
---|
303 | #endif /* CR_NET_H */
|
---|