grapes / som / net_helper-ml.c @ ab61e3b6
History | View | Annotate | Download (14.1 KB)
1 |
/*
|
---|---|
2 |
* Copyright (c) 2009 Marco Biazzini
|
3 |
*
|
4 |
* This is free software; see lgpl-2.1.txt
|
5 |
*/
|
6 |
|
7 |
#include <event2/event.h> |
8 |
#include <arpa/inet.h> |
9 |
#include <unistd.h> |
10 |
#include <stdlib.h> |
11 |
#include <stdio.h> |
12 |
#include <string.h> |
13 |
|
14 |
|
15 |
#include "net_helper.h" |
16 |
#include "ml.h" |
17 |
#include "ml_helpers.h" |
18 |
|
19 |
/**
|
20 |
* libevent pointer
|
21 |
*/
|
22 |
struct event_base *base;
|
23 |
|
24 |
#define NH_BUFFER_SIZE 100 |
25 |
|
26 |
static int sIdx = 0; |
27 |
static int rIdx = 0; |
28 |
|
29 |
typedef struct nodeID { |
30 |
socketID_handle addr; |
31 |
int connID; // connection associated to this node, -1 if myself |
32 |
int refcnt;
|
33 |
// int addrSize;
|
34 |
// int addrStringSize;
|
35 |
} nodeID; |
36 |
|
37 |
typedef struct msgData_cb { |
38 |
int bIdx; // index of the message in the proper buffer |
39 |
unsigned char msgType; // message type |
40 |
int mSize; // message size |
41 |
} msgData_cb; |
42 |
|
43 |
static nodeID *me; //TODO: is it possible to get rid of this (notwithstanding ml callback)?? |
44 |
static int timeoutFired = 0; |
45 |
|
46 |
// pointers to the msgs to be send
|
47 |
static uint8_t *sendingBuffer[NH_BUFFER_SIZE];
|
48 |
// pointers to the received msgs + sender nodeID
|
49 |
struct receivedB {
|
50 |
struct nodeID *id;
|
51 |
int len;
|
52 |
uint8_t *data; |
53 |
}; |
54 |
static struct receivedB receivedBuffer[NH_BUFFER_SIZE]; |
55 |
/**/ static int recv_counter =0; |
56 |
|
57 |
/**
|
58 |
* Look for a free slot in the received buffer and allocates it for immediate use
|
59 |
* @return the index of a free slot in the received msgs buffer, -1 if no free slot available.
|
60 |
*/
|
61 |
static int next_R() { |
62 |
const int size = 1024; |
63 |
if (receivedBuffer[rIdx].data==NULL) { |
64 |
receivedBuffer[rIdx].data = malloc(size); |
65 |
} |
66 |
else {
|
67 |
int count;
|
68 |
for (count=0;count<NH_BUFFER_SIZE;count++) { |
69 |
rIdx = (++rIdx)%NH_BUFFER_SIZE; |
70 |
if (receivedBuffer[rIdx].data==NULL) |
71 |
break;
|
72 |
} |
73 |
if (count==NH_BUFFER_SIZE)
|
74 |
return -1; |
75 |
else {
|
76 |
receivedBuffer[rIdx].data = malloc(size); |
77 |
} |
78 |
} |
79 |
memset(receivedBuffer[rIdx].data,0,size);
|
80 |
return rIdx;
|
81 |
} |
82 |
|
83 |
/**
|
84 |
* Look for a free slot in the sending buffer and allocates it for immediate use
|
85 |
* @return the index of a free slot in the sending msgs buffer, -1 if no free slot available.
|
86 |
*/
|
87 |
static int next_S() { |
88 |
const int size = 1024; |
89 |
if (sendingBuffer[sIdx]==NULL) { |
90 |
sendingBuffer[sIdx] = malloc(size); |
91 |
} |
92 |
else {
|
93 |
int count;
|
94 |
for (count=0;count<NH_BUFFER_SIZE;count++) { |
95 |
sIdx = (++sIdx)%NH_BUFFER_SIZE; |
96 |
if (sendingBuffer[sIdx]==NULL) |
97 |
break;
|
98 |
} |
99 |
if (count==NH_BUFFER_SIZE)
|
100 |
return -1; |
101 |
else {
|
102 |
sendingBuffer[sIdx] = malloc(size); |
103 |
} |
104 |
} |
105 |
return sIdx;
|
106 |
} |
107 |
|
108 |
|
109 |
/**
|
110 |
* Callback used by ml to confirm its initialization. Create a valid self nodeID and register to receive data from remote peers.
|
111 |
* @param local_socketID
|
112 |
* @param errorstatus
|
113 |
*/
|
114 |
static void init_myNodeID_cb (socketID_handle local_socketID,int errorstatus) { |
115 |
switch (errorstatus) {
|
116 |
case 0: |
117 |
//
|
118 |
memcpy(me->addr,local_socketID,SOCKETID_SIZE); |
119 |
// me->addrSize = SOCKETID_SIZE;
|
120 |
// me->addrStringSize = SOCKETID_STRING_SIZE;
|
121 |
me->connID = -1;
|
122 |
me->refcnt = 1;
|
123 |
// fprintf(stderr,"Net-helper init : received my own socket: %s.\n",node_addr(me));
|
124 |
break;
|
125 |
case -1: |
126 |
//
|
127 |
fprintf(stderr,"Net-helper init : socket error occurred in ml while creating socket\n");
|
128 |
exit(1);
|
129 |
break;
|
130 |
case 1: |
131 |
//
|
132 |
fprintf(stderr,"Net-helper init : NAT traversal failed while creating socket\n");
|
133 |
exit(1);
|
134 |
break;
|
135 |
case 2: |
136 |
fprintf(stderr,"Net-helper init : NAT traversal timeout while creating socket\n");
|
137 |
exit(2);
|
138 |
break;
|
139 |
default : // should never happen |
140 |
//
|
141 |
fprintf(stderr,"Net-helper init : Unknown error in ml while creating socket\n");
|
142 |
} |
143 |
|
144 |
} |
145 |
|
146 |
/**
|
147 |
* Timeout callback to be set in the eventlib loop as needed
|
148 |
* @param socket
|
149 |
* @param flag
|
150 |
* @param arg
|
151 |
*/
|
152 |
static void t_out_cb (int socket, short flag, void* arg) { |
153 |
|
154 |
timeoutFired = 1;
|
155 |
// fprintf(stderr,"TIMEOUT!!!\n");
|
156 |
// event_base_loopbreak(base);
|
157 |
} |
158 |
|
159 |
/**
|
160 |
* Callback called by ml when a remote node ask for a connection
|
161 |
* @param connectionID
|
162 |
* @param arg
|
163 |
*/
|
164 |
static void receive_conn_cb(int connectionID, void *arg) { |
165 |
// fprintf(stderr, "Net-helper : remote peer opened the connection %d with arg = %d\n", connectionID,(int)arg);
|
166 |
|
167 |
} |
168 |
|
169 |
/**
|
170 |
* Callback called by the ml when a connection is ready to be used to send data to a remote peer
|
171 |
* @param connectionID
|
172 |
* @param arg
|
173 |
*/
|
174 |
static void connReady_cb (int connectionID, void *arg) { |
175 |
|
176 |
msgData_cb *p; |
177 |
p = (msgData_cb *)arg; |
178 |
if (p == NULL) return; |
179 |
mlSendData(connectionID,(char *)(sendingBuffer[p->bIdx]),p->mSize,p->msgType,NULL); |
180 |
///**/fprintf(stderr,"Sent message of type # %c and size %d\n",
|
181 |
// ((char*)sendingBuffer[p->bIdx])[0]+'0', p->mSize);
|
182 |
free(sendingBuffer[p->bIdx]); |
183 |
sendingBuffer[p->bIdx] = NULL;
|
184 |
// fprintf(stderr,"Net-helper: Message # %d for connection %d sent!\n ", p->bIdx,connectionID);
|
185 |
// event_base_loopbreak(base);
|
186 |
free(p); |
187 |
} |
188 |
|
189 |
/**
|
190 |
* Callback called by ml when a connection error occurs
|
191 |
* @param connectionID
|
192 |
* @param arg
|
193 |
*/
|
194 |
static void connError_cb (int connectionID, void *arg) { |
195 |
// simply get rid of the msg in the buffer....
|
196 |
msgData_cb *p; |
197 |
p = (msgData_cb *)arg; |
198 |
if (p != NULL) { |
199 |
fprintf(stderr,"Net-helper: Connection %d could not be established to send msg %d.\n ", connectionID,p->bIdx);
|
200 |
free(sendingBuffer[p->bIdx]); |
201 |
sendingBuffer[p->bIdx] = NULL;
|
202 |
p->mSize = -1;
|
203 |
} |
204 |
// event_base_loopbreak(base);
|
205 |
} |
206 |
|
207 |
|
208 |
/**
|
209 |
* Callback to receive data from ml
|
210 |
* @param buffer
|
211 |
* @param buflen
|
212 |
* @param msgtype
|
213 |
* @param arg
|
214 |
*/
|
215 |
static void recv_data_cb(char *buffer, int buflen, unsigned char msgtype, recv_params *arg) { |
216 |
// TODO: lacks a void* arg... moreover: recv_params has a msgtype, but there is also a msgtype explicit argument...
|
217 |
// fprintf(stderr, "Net-helper : called back with some news...\n");
|
218 |
/**/ ++recv_counter;
|
219 |
char str[SOCKETID_STRING_SIZE];
|
220 |
if (arg->remote_socketID != NULL) |
221 |
mlSocketIDToString(arg->remote_socketID,str,SOCKETID_STRING_SIZE); |
222 |
else
|
223 |
sprintf(str,"!Unknown!");
|
224 |
if (arg->nrMissingBytes || !arg->firstPacketArrived) {
|
225 |
fprintf(stderr, "Net-helper : corrupted message arrived from %s\n",str);
|
226 |
/**/ fprintf(stderr, "\tMessage # %d -- Message type: %c -- Missing # %d frags\n", |
227 |
recv_counter, buffer[0],arg->nrMissingBytes, arg->firstPacketArrived?" => Missing first!":""); |
228 |
} |
229 |
else {
|
230 |
// fprintf(stderr, "Net-helper : message arrived from %s\n",str);
|
231 |
// buffering the received message only if possible, otherwise ignore it...
|
232 |
int index = next_R();
|
233 |
if (index >=0) { |
234 |
// receivedBuffer[index][0] = malloc(buflen);
|
235 |
if (receivedBuffer[index].data == NULL) { |
236 |
fprintf(stderr, "Net-helper : memory error while creating a new message buffer \n");
|
237 |
return;
|
238 |
} |
239 |
// creating a new sender nodedID
|
240 |
receivedBuffer[index].id = malloc(sizeof(nodeID));
|
241 |
if (receivedBuffer[index].id==NULL) { |
242 |
free (receivedBuffer[index].data); |
243 |
receivedBuffer[index].data = NULL;
|
244 |
fprintf(stderr, "Net-helper : memory error while creating a new nodeID. Message from %s is lost.\n", str);
|
245 |
return;
|
246 |
} |
247 |
else {
|
248 |
size_t lenlen = sizeof(int); |
249 |
memset(receivedBuffer[index].id, 0, sizeof(struct nodeID)); |
250 |
nodeID *remote; remote = receivedBuffer[index].id; |
251 |
receivedBuffer[index].data = realloc(receivedBuffer[index].data,buflen); |
252 |
memset(receivedBuffer[index].data,0,buflen);
|
253 |
receivedBuffer[index].len = buflen; |
254 |
//*(receivedBuffer[index][0]) = buflen;
|
255 |
memcpy(receivedBuffer[index].data,buffer,buflen); |
256 |
// get the socketID of the sender
|
257 |
remote->addr = malloc(SOCKETID_SIZE); |
258 |
if (remote->addr == NULL) { |
259 |
free (remote); |
260 |
fprintf(stderr, "Net-helper : memory error while creating a new nodeID \n");
|
261 |
return;
|
262 |
} |
263 |
else {
|
264 |
memset(remote->addr, 0, SOCKETID_SIZE);
|
265 |
memcpy(remote->addr, arg->remote_socketID ,SOCKETID_SIZE); |
266 |
// remote->addrSize = SOCKETID_SIZE;
|
267 |
// remote->addrStringSize = SOCKETID_STRING_SIZE;
|
268 |
remote->connID = arg->connectionID; |
269 |
remote->refcnt = 1;
|
270 |
} |
271 |
} |
272 |
} |
273 |
} |
274 |
// event_base_loopbreak(base);
|
275 |
} |
276 |
|
277 |
|
278 |
struct nodeID *net_helper_init(const char *IPaddr, int port) { |
279 |
|
280 |
struct timeval tout = {1, 0}; |
281 |
base = event_base_new(); |
282 |
|
283 |
me = malloc(sizeof(nodeID));
|
284 |
if (me == NULL) { |
285 |
return NULL; |
286 |
} |
287 |
memset(me,0,sizeof(nodeID)); |
288 |
me->addr = malloc(SOCKETID_SIZE); |
289 |
if (me->addr == NULL) { |
290 |
free(me); |
291 |
return NULL; |
292 |
} |
293 |
memset(me->addr,0,SOCKETID_SIZE);
|
294 |
me->connID = -10; // dirty trick to spot later if the ml has called back ... |
295 |
me->refcnt = 1;
|
296 |
|
297 |
int i;
|
298 |
for (i=0;i<NH_BUFFER_SIZE;i++) { |
299 |
sendingBuffer[i] = NULL;
|
300 |
receivedBuffer[i].data = NULL;
|
301 |
} |
302 |
|
303 |
mlRegisterErrorConnectionCb(&connError_cb); |
304 |
mlRegisterRecvConnectionCb(&receive_conn_cb); |
305 |
mlInit(1,tout,port,IPaddr,0,NULL,&init_myNodeID_cb,base); |
306 |
while (me->connID<-1) { |
307 |
// event_base_once(base,-1, EV_TIMEOUT, &t_out_cb, NULL, &tout);
|
308 |
event_base_loop(base,EVLOOP_ONCE); |
309 |
} |
310 |
timeoutFired = 0;
|
311 |
// fprintf(stderr,"Net-helper init : back from init!\n");
|
312 |
|
313 |
return me;
|
314 |
} |
315 |
|
316 |
|
317 |
void bind_msg_type (unsigned char msgtype) { |
318 |
|
319 |
mlRegisterRecvDataCb(&recv_data_cb,msgtype); |
320 |
} |
321 |
|
322 |
|
323 |
/**
|
324 |
* Called by the application to send data to a remote peer
|
325 |
* @param from
|
326 |
* @param to
|
327 |
* @param buffer_ptr
|
328 |
* @param buffer_size
|
329 |
* @return The dimension of the buffer or -1 if a connection error occurred.
|
330 |
*/
|
331 |
int send_to_peer(const struct nodeID *from, struct nodeID *to, const uint8_t *buffer_ptr, int buffer_size) |
332 |
{ |
333 |
// if buffer is full, discard the message and return an error flag
|
334 |
int index = next_S();
|
335 |
if (index<0) { |
336 |
// free(buffer_ptr);
|
337 |
return -1; |
338 |
} |
339 |
sendingBuffer[index] = realloc(sendingBuffer[index],buffer_size); |
340 |
memset(sendingBuffer[index],0,buffer_size);
|
341 |
memcpy(sendingBuffer[index],buffer_ptr,buffer_size); |
342 |
// free(buffer_ptr);
|
343 |
msgData_cb *p = malloc(sizeof(msgData_cb));
|
344 |
p->bIdx = index; p->mSize = buffer_size; p->msgType = (unsigned char)buffer_ptr[0]; |
345 |
int current = p->bIdx;
|
346 |
send_params params = {0,0,0,0}; |
347 |
to->connID = mlOpenConnection(to->addr,&connReady_cb,p, params); |
348 |
if (to->connID<0) { |
349 |
free(sendingBuffer[current]); |
350 |
sendingBuffer[current] = NULL;
|
351 |
fprintf(stderr,"Net-helper: Couldn't get a connection ID to send msg %d.\n ", p->bIdx);
|
352 |
free(p); |
353 |
return -1; |
354 |
} |
355 |
else {
|
356 |
return buffer_size; //p->mSize; |
357 |
} |
358 |
|
359 |
} |
360 |
|
361 |
|
362 |
/**
|
363 |
* Called by an application to receive data from remote peers
|
364 |
* @param local
|
365 |
* @param remote
|
366 |
* @param buffer_ptr
|
367 |
* @param buffer_size
|
368 |
* @return The number of received bytes or -1 if some error occurred.
|
369 |
*/
|
370 |
int recv_from_peer(const struct nodeID *local, struct nodeID **remote, uint8_t *buffer_ptr, int buffer_size) |
371 |
{ |
372 |
int size;
|
373 |
// this should never happen... if it does, index handling is faulty...
|
374 |
if (receivedBuffer[rIdx].id==NULL) { |
375 |
fprintf(stderr, "Net-helper : memory error while creating a new nodeID \n");
|
376 |
return -1; |
377 |
} |
378 |
|
379 |
(*remote) = receivedBuffer[rIdx].id; |
380 |
// retrieve a msg from the buffer
|
381 |
size = receivedBuffer[rIdx].len; |
382 |
if (size>buffer_size) {
|
383 |
fprintf(stderr, "Net-helper : recv_from_peer: buffer too small (size:%d > buffer_size: %d)!\n",size,buffer_size);
|
384 |
return -1; |
385 |
} |
386 |
memcpy(buffer_ptr, receivedBuffer[rIdx].data, size); |
387 |
free(receivedBuffer[rIdx].data); |
388 |
receivedBuffer[rIdx].data = NULL;
|
389 |
receivedBuffer[rIdx].id = NULL;
|
390 |
|
391 |
// fprintf(stderr, "Net-helper : I've got mail!!!\n");
|
392 |
|
393 |
return size;
|
394 |
} |
395 |
|
396 |
|
397 |
int wait4data(const struct nodeID *n, struct timeval *tout) { |
398 |
|
399 |
// fprintf(stderr,"Net-helper : Waiting for data to come...\n");
|
400 |
event_base_once(base,-1, EV_TIMEOUT, &t_out_cb, NULL, tout); |
401 |
while(receivedBuffer[rIdx].data==NULL && timeoutFired==0) { |
402 |
// event_base_dispatch(base);
|
403 |
event_base_loop(base,EVLOOP_ONCE); |
404 |
} |
405 |
timeoutFired = 0;
|
406 |
// fprintf(stderr,"Back from eventlib loop.\n");
|
407 |
|
408 |
if (receivedBuffer[rIdx].data!=NULL) |
409 |
return 1; |
410 |
else
|
411 |
return 0; |
412 |
} |
413 |
|
414 |
|
415 |
|
416 |
|
417 |
struct nodeID *create_node(const char *rem_IP, int rem_port) { |
418 |
struct nodeID *remote = malloc(sizeof(nodeID)); |
419 |
if (remote == NULL) { |
420 |
return NULL; |
421 |
} |
422 |
// remote->addr = malloc(sizeof(SOCKETID_SIZE));
|
423 |
// if (remote->addr == NULL) {
|
424 |
// free(remote);
|
425 |
// return NULL;
|
426 |
// }
|
427 |
// remote->addrSize = SOCKETID_SIZE;
|
428 |
// remote->addrStringSize = SOCKETID_STRING_SIZE;
|
429 |
remote->addr = getRemoteSocketID(rem_IP, rem_port); |
430 |
send_params params = {0,0,0,0}; |
431 |
remote->connID = mlOpenConnection(remote->addr,&connReady_cb,NULL, params);
|
432 |
remote->refcnt = 1;
|
433 |
return remote;
|
434 |
} |
435 |
|
436 |
// TODO: check why closing the connection is annoying for the ML
|
437 |
void nodeid_free(struct nodeID *n) { |
438 |
|
439 |
// mlCloseConnection(n->connID);
|
440 |
// mlCloseSocket(n->addr);
|
441 |
// free(n);
|
442 |
if (n && (--(n->refcnt) == 0)) { |
443 |
// mlCloseConnection(n->connID);
|
444 |
mlCloseSocket(n->addr); |
445 |
free(n); |
446 |
} |
447 |
} |
448 |
|
449 |
|
450 |
const char *node_addr(const struct nodeID *s) |
451 |
{ |
452 |
static char addr[256]; |
453 |
// TODO: mlSocketIDToString always return 0 !!!
|
454 |
int r = mlSocketIDToString(s->addr,addr,256); |
455 |
if (!r)
|
456 |
return addr;
|
457 |
else
|
458 |
return ""; |
459 |
} |
460 |
|
461 |
struct nodeID *nodeid_dup(struct nodeID *s) |
462 |
{ |
463 |
// struct nodeID *res;
|
464 |
//
|
465 |
// res = malloc(sizeof(struct nodeID));
|
466 |
// if (res != NULL) {
|
467 |
// res->addr = malloc(SOCKETID_SIZE);
|
468 |
// if (res->addr != NULL) {
|
469 |
// memcpy(res->addr, s->addr, SOCKETID_SIZE);
|
470 |
// // res->addrSize = SOCKETID_SIZE;
|
471 |
// // res->addrStringSize = SOCKETID_STRING_SIZE;
|
472 |
// res->connID = s->connID;
|
473 |
// }
|
474 |
// else {
|
475 |
// free(res);
|
476 |
// res = NULL;
|
477 |
// fprintf(stderr,"Net-helper : Error while duplicating nodeID...\n");
|
478 |
// }
|
479 |
// }
|
480 |
// return res;
|
481 |
s->refcnt++; |
482 |
return s;
|
483 |
} |
484 |
|
485 |
int nodeid_equal(const struct nodeID *s1, const struct nodeID *s2) |
486 |
{ |
487 |
return (mlCompareSocketIDs(s1->addr,s2->addr) == 0); |
488 |
} |
489 |
|
490 |
int nodeid_dump(uint8_t *b, const struct nodeID *s) |
491 |
{ |
492 |
mlSocketIDToString(s->addr,(char *)b,SOCKETID_STRING_SIZE);
|
493 |
//fprintf(stderr,"Dumping nodeID : ho scritto %s (%d bytes)\n",b, strlen((char *)b));
|
494 |
|
495 |
// memcpy(b, s->addr,SOCKETID_SIZE);//sizeof(struct sockaddr_in6)*2
|
496 |
// return SOCKETID_SIZE;//sizeof(struct sockaddr_in6)*2;
|
497 |
|
498 |
return 1 + strlen((char *)b); //terminating \0 IS included in the size |
499 |
} |
500 |
|
501 |
struct nodeID *nodeid_undump(const uint8_t *b, int *len) |
502 |
{ |
503 |
struct nodeID *res;
|
504 |
res = malloc(sizeof(struct nodeID)); |
505 |
if (res != NULL) { |
506 |
memset(res,0,sizeof(struct nodeID)); |
507 |
res->addr = malloc(SOCKETID_SIZE); |
508 |
if (res->addr != NULL) { |
509 |
memset(res->addr,0,SOCKETID_SIZE);
|
510 |
//memcpy(res->addr, b, SOCKETID_SIZE);
|
511 |
//*len = SOCKETID_SIZE;
|
512 |
*len = strlen((char*)b) + 1; |
513 |
mlStringToSocketID((char *)b,res->addr);
|
514 |
// fprintf(stderr,"Node undumped : %s\n",node_addr(res));
|
515 |
// res->addrSize = SOCKETID_SIZE;
|
516 |
// res->addrStringSize = SOCKETID_STRING_SIZE;
|
517 |
res->connID = -1;
|
518 |
res->refcnt = 1;
|
519 |
} |
520 |
else {
|
521 |
free(res); |
522 |
res = NULL;
|
523 |
// TODO: what about *len in this case???
|
524 |
fprintf(stderr,"Net-helper : Error while 'undumping' nodeID...\n");
|
525 |
} |
526 |
} |
527 |
|
528 |
|
529 |
return res;
|
530 |
} |