Statistics
| Branch: | Revision:

grapes / som / net_helper-ml.c @ af7522fa

History | View | Annotate | Download (14.1 KB)

1 ffc8bdcf MarcoBiazzini
/*
2
 *  Copyright (c) 2009 Marco Biazzini
3
 *
4 0523cf42 CsabaKiraly
 *  This is free software; see lgpl-2.1.txt
5 ffc8bdcf MarcoBiazzini
 */
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 3f57027f MarcoBiazzini
15 ffc8bdcf MarcoBiazzini
#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 3f57027f MarcoBiazzini
        int refcnt;
33
//        int addrSize;
34
//        int addrStringSize;
35 ffc8bdcf MarcoBiazzini
} 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 af7522fa CsabaKiraly
struct receivedB {
50
        struct nodeID *id;
51
        int len;
52
        uint8_t *data;
53
};
54
static struct receivedB receivedBuffer[NH_BUFFER_SIZE];
55 c2846ae4 MarcoBiazzini
/**/ static int recv_counter =0;
56 ffc8bdcf MarcoBiazzini
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 1509362b AlessandroRusso
        const int size = 1024;
63 af7522fa CsabaKiraly
        if (receivedBuffer[rIdx].data==NULL) {
64
                receivedBuffer[rIdx].data = malloc(size);
65 ffc8bdcf MarcoBiazzini
        }
66
        else {
67
                int count;
68
                for (count=0;count<NH_BUFFER_SIZE;count++) {
69
                        rIdx = (++rIdx)%NH_BUFFER_SIZE;
70 af7522fa CsabaKiraly
                        if (receivedBuffer[rIdx].data==NULL)
71 ffc8bdcf MarcoBiazzini
                                break;
72
                }
73
                if (count==NH_BUFFER_SIZE)
74
                        return -1;
75
                else {
76 af7522fa CsabaKiraly
                        receivedBuffer[rIdx].data = malloc(size);
77 ffc8bdcf MarcoBiazzini
                }
78
        }
79 af7522fa CsabaKiraly
        memset(receivedBuffer[rIdx].data,0,size);
80 ffc8bdcf MarcoBiazzini
        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 1509362b AlessandroRusso
        const int size = 1024;
89 ffc8bdcf MarcoBiazzini
        if (sendingBuffer[sIdx]==NULL) {
90 1509362b AlessandroRusso
                sendingBuffer[sIdx] = malloc(size);
91 ffc8bdcf MarcoBiazzini
        }
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 1509362b AlessandroRusso
                        sendingBuffer[sIdx] = malloc(size);
103 ffc8bdcf MarcoBiazzini
                }
104
        }
105
        return sIdx;
106
}
107
108 97d6cf93 MarcoBiazzini
109 ffc8bdcf MarcoBiazzini
/**
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 3f57027f MarcoBiazzini
        //        me->addrSize = SOCKETID_SIZE;
120
        //        me->addrStringSize = SOCKETID_STRING_SIZE;
121 ffc8bdcf MarcoBiazzini
                me->connID = -1;
122 3f57027f MarcoBiazzini
                me->refcnt = 1;
123
        //        fprintf(stderr,"Net-helper init : received my own socket: %s.\n",node_addr(me));
124 ffc8bdcf MarcoBiazzini
                break;
125
        case -1:
126
                //
127
                fprintf(stderr,"Net-helper init : socket error occurred in ml while creating socket\n");
128 3f57027f MarcoBiazzini
                exit(1);
129 ffc8bdcf MarcoBiazzini
                break;
130
        case 1:
131
                //
132
                fprintf(stderr,"Net-helper init : NAT traversal failed while creating socket\n");
133 3f57027f MarcoBiazzini
                exit(1);
134 ffc8bdcf MarcoBiazzini
                break;
135
        case 2:
136
            fprintf(stderr,"Net-helper init : NAT traversal timeout while creating socket\n");
137 3f57027f MarcoBiazzini
            exit(2);
138 ffc8bdcf MarcoBiazzini
            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 3f57027f MarcoBiazzini
//        fprintf(stderr,"TIMEOUT!!!\n");
156 ffc8bdcf MarcoBiazzini
//        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 3f57027f MarcoBiazzini
//    fprintf(stderr, "Net-helper : remote peer opened the connection %d with arg = %d\n", connectionID,(int)arg);
166 ffc8bdcf MarcoBiazzini
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 368e8b83 TivadarSzemethy
        mlSendData(connectionID,(char *)(sendingBuffer[p->bIdx]),p->mSize,p->msgType,NULL);
180 c2846ae4 MarcoBiazzini
///**/fprintf(stderr,"Sent message of type # %c and size %d\n",
181
//                ((char*)sendingBuffer[p->bIdx])[0]+'0', p->mSize);
182 ffc8bdcf MarcoBiazzini
        free(sendingBuffer[p->bIdx]);
183
        sendingBuffer[p->bIdx] = NULL;
184 3f57027f MarcoBiazzini
//        fprintf(stderr,"Net-helper: Message # %d for connection %d sent!\n ", p->bIdx,connectionID);
185 ffc8bdcf MarcoBiazzini
        //        event_base_loopbreak(base);
186 af439421 CsabaKiraly
        free(p);
187 ffc8bdcf MarcoBiazzini
}
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 1509362b AlessandroRusso
                p->mSize = -1;
203 ffc8bdcf MarcoBiazzini
        }
204
        //        event_base_loopbreak(base);
205
}
206
207 97d6cf93 MarcoBiazzini
208 ffc8bdcf MarcoBiazzini
/**
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 c2846ae4 MarcoBiazzini
/**/ ++recv_counter;
219 3f57027f MarcoBiazzini
        char str[SOCKETID_STRING_SIZE];
220 ffc8bdcf MarcoBiazzini
        if (arg->remote_socketID != NULL)
221 47ceb0c9 MarcoBiazzini
                mlSocketIDToString(arg->remote_socketID,str,SOCKETID_STRING_SIZE);
222 ffc8bdcf MarcoBiazzini
        else
223
                sprintf(str,"!Unknown!");
224
        if (arg->nrMissingBytes || !arg->firstPacketArrived) {
225
            fprintf(stderr, "Net-helper : corrupted message arrived from %s\n",str);
226 c2846ae4 MarcoBiazzini
/**/    fprintf(stderr, "\tMessage # %d -- Message type: %c -- Missing # %d frags\n",
227
                        recv_counter, buffer[0],arg->nrMissingBytes, arg->firstPacketArrived?" => Missing first!":"");
228 ffc8bdcf MarcoBiazzini
        }
229
        else {
230 3f57027f MarcoBiazzini
        //        fprintf(stderr, "Net-helper : message arrived from %s\n",str);
231 ffc8bdcf MarcoBiazzini
                // 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 af7522fa CsabaKiraly
                        if (receivedBuffer[index].data == NULL) {
236 ffc8bdcf MarcoBiazzini
                                fprintf(stderr, "Net-helper : memory error while creating a new message buffer \n");
237
                                return;
238
                        }
239
                        // creating a new sender nodedID
240 af7522fa CsabaKiraly
                        receivedBuffer[index].id = malloc(sizeof(nodeID));
241
                        if (receivedBuffer[index].id==NULL) {
242
                                free (receivedBuffer[index].data);
243
                                receivedBuffer[index].data = NULL;
244 ffc8bdcf MarcoBiazzini
                                fprintf(stderr, "Net-helper : memory error while creating a new nodeID. Message from %s is lost.\n", str);
245
                                return;
246
                        }
247
                        else {
248 af7522fa CsabaKiraly
                                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 1509362b AlessandroRusso
                                //*(receivedBuffer[index][0]) = buflen;
255 af7522fa CsabaKiraly
                                memcpy(receivedBuffer[index].data,buffer,buflen);
256 ffc8bdcf MarcoBiazzini
                                  // 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 3f57027f MarcoBiazzini
                                        memset(remote->addr, 0, SOCKETID_SIZE);
265 ffc8bdcf MarcoBiazzini
                                        memcpy(remote->addr, arg->remote_socketID ,SOCKETID_SIZE);
266 3f57027f MarcoBiazzini
                                //        remote->addrSize = SOCKETID_SIZE;
267
                                //        remote->addrStringSize = SOCKETID_STRING_SIZE;
268 ffc8bdcf MarcoBiazzini
                                        remote->connID = arg->connectionID;
269 3f57027f MarcoBiazzini
                                        remote->refcnt = 1;
270 ffc8bdcf MarcoBiazzini
                                }
271
                        }
272
                }
273
  }
274
//        event_base_loopbreak(base);
275
}
276
277 97d6cf93 MarcoBiazzini
278 61a07891 AlessandroRusso
struct nodeID *net_helper_init(const char *IPaddr, int port) {
279 ffc8bdcf MarcoBiazzini
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 3f57027f MarcoBiazzini
        memset(me,0,sizeof(nodeID));
288 ffc8bdcf MarcoBiazzini
        me->addr = malloc(SOCKETID_SIZE);
289
        if (me->addr == NULL) {
290
                free(me);
291
                return NULL;
292
        }
293 3f57027f MarcoBiazzini
        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 ffc8bdcf MarcoBiazzini
297
        int i;
298
        for (i=0;i<NH_BUFFER_SIZE;i++) {
299
                sendingBuffer[i] = NULL;
300 af7522fa CsabaKiraly
                receivedBuffer[i].data = NULL;
301 ffc8bdcf MarcoBiazzini
        }
302
303 47ceb0c9 MarcoBiazzini
        mlRegisterErrorConnectionCb(&connError_cb);
304
        mlRegisterRecvConnectionCb(&receive_conn_cb);
305
        mlInit(1,tout,port,IPaddr,0,NULL,&init_myNodeID_cb,base);
306 3f57027f MarcoBiazzini
        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 ffc8bdcf MarcoBiazzini
//        fprintf(stderr,"Net-helper init : back from init!\n");
312 3f57027f MarcoBiazzini
313 ffc8bdcf MarcoBiazzini
        return me;
314
}
315
316
317 0d334358 MarcoBiazzini
void bind_msg_type (unsigned char msgtype) {
318
319 47ceb0c9 MarcoBiazzini
                        mlRegisterRecvDataCb(&recv_data_cb,msgtype);
320 61a07891 AlessandroRusso
}
321
322
323 ffc8bdcf MarcoBiazzini
/**
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 3f57027f MarcoBiazzini
 * @return The dimension of the buffer or -1 if a connection error occurred.
330 ffc8bdcf MarcoBiazzini
 */
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 1509362b AlessandroRusso
        sendingBuffer[index] = realloc(sendingBuffer[index],buffer_size);
340
        memset(sendingBuffer[index],0,buffer_size);
341 ffc8bdcf MarcoBiazzini
        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 368e8b83 TivadarSzemethy
        send_params params = {0,0,0,0};
347
        to->connID = mlOpenConnection(to->addr,&connReady_cb,p, params);
348 ffc8bdcf MarcoBiazzini
        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 3f57027f MarcoBiazzini
                free(p);
353
                return -1;
354 ffc8bdcf MarcoBiazzini
        }
355
        else {
356 7d5a02b7 MarcoBiazzini
                return buffer_size; //p->mSize;
357 ffc8bdcf MarcoBiazzini
        }
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 a2503c6a CsabaKiraly
        int size;
373 ffc8bdcf MarcoBiazzini
        // this should never happen... if it does, index handling is faulty...
374 af7522fa CsabaKiraly
        if (receivedBuffer[rIdx].id==NULL) {
375 ffc8bdcf MarcoBiazzini
                fprintf(stderr, "Net-helper : memory error while creating a new nodeID \n");
376
                return -1;
377
        }
378
379 af7522fa CsabaKiraly
        (*remote) = receivedBuffer[rIdx].id;
380 ffc8bdcf MarcoBiazzini
        // retrieve a msg from the buffer
381 af7522fa CsabaKiraly
        size = receivedBuffer[rIdx].len;
382 a2503c6a CsabaKiraly
        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 af7522fa CsabaKiraly
        memcpy(buffer_ptr, receivedBuffer[rIdx].data, size);
387
        free(receivedBuffer[rIdx].data);
388
        receivedBuffer[rIdx].data = NULL;
389
        receivedBuffer[rIdx].id = NULL;
390 ffc8bdcf MarcoBiazzini
391
//        fprintf(stderr, "Net-helper : I've got mail!!!\n");
392
393 3f57027f MarcoBiazzini
        return size;
394 ffc8bdcf MarcoBiazzini
}
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 af7522fa CsabaKiraly
        while(receivedBuffer[rIdx].data==NULL && timeoutFired==0) {
402 ffc8bdcf MarcoBiazzini
        //        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 af7522fa CsabaKiraly
        if (receivedBuffer[rIdx].data!=NULL)
409 ffc8bdcf MarcoBiazzini
                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 3f57027f MarcoBiazzini
//        remote->addrSize = SOCKETID_SIZE;
428
//        remote->addrStringSize = SOCKETID_STRING_SIZE;
429 ffc8bdcf MarcoBiazzini
        remote->addr = getRemoteSocketID(rem_IP, rem_port);
430 368e8b83 TivadarSzemethy
        send_params params = {0,0,0,0};
431
        remote->connID = mlOpenConnection(remote->addr,&connReady_cb,NULL, params);
432 3f57027f MarcoBiazzini
        remote->refcnt = 1;
433 ffc8bdcf MarcoBiazzini
        return remote;
434
}
435
436
// TODO: check why closing the connection is annoying for the ML
437 c38b2ca7 MarcoBiazzini
void nodeid_free(struct nodeID *n) {
438 3f57027f MarcoBiazzini
439 47ceb0c9 MarcoBiazzini
//        mlCloseConnection(n->connID);
440
//        mlCloseSocket(n->addr);
441 3f57027f MarcoBiazzini
//        free(n);
442
        if (n && (--(n->refcnt) == 0)) {
443 47ceb0c9 MarcoBiazzini
        //        mlCloseConnection(n->connID);
444
                mlCloseSocket(n->addr);
445 3f57027f MarcoBiazzini
                free(n);
446
        }
447 ffc8bdcf MarcoBiazzini
}
448
449
450
const char *node_addr(const struct nodeID *s)
451
{
452
  static char addr[256];
453 47ceb0c9 MarcoBiazzini
  // TODO: mlSocketIDToString always return 0 !!!
454
  int r = mlSocketIDToString(s->addr,addr,256);
455 ffc8bdcf MarcoBiazzini
  if (!r)
456
          return addr;
457
  else
458
          return "";
459
}
460
461 3f57027f MarcoBiazzini
struct nodeID *nodeid_dup(struct nodeID *s)
462 ffc8bdcf MarcoBiazzini
{
463 3f57027f MarcoBiazzini
//  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 ffc8bdcf MarcoBiazzini
}
484
485
int nodeid_equal(const struct nodeID *s1, const struct nodeID *s2)
486
{
487 47ceb0c9 MarcoBiazzini
        return (mlCompareSocketIDs(s1->addr,s2->addr) == 0);
488 ffc8bdcf MarcoBiazzini
}
489
490
int nodeid_dump(uint8_t *b, const struct nodeID *s)
491
{
492 47ceb0c9 MarcoBiazzini
  mlSocketIDToString(s->addr,(char *)b,SOCKETID_STRING_SIZE);
493 3f57027f MarcoBiazzini
  //fprintf(stderr,"Dumping nodeID : ho scritto %s (%d bytes)\n",b, strlen((char *)b));
494 ffc8bdcf MarcoBiazzini
495 3f57027f MarcoBiazzini
//        memcpy(b, s->addr,SOCKETID_SIZE);//sizeof(struct sockaddr_in6)*2
496
//        return SOCKETID_SIZE;//sizeof(struct sockaddr_in6)*2;
497 ffc8bdcf MarcoBiazzini
498 f6ee5da1 CsabaKiraly
  return 1 + strlen((char *)b);        //terminating \0 IS included in the size
499 ffc8bdcf MarcoBiazzini
}
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 3f57027f MarcoBiazzini
          memset(res,0,sizeof(struct nodeID));
507 ffc8bdcf MarcoBiazzini
          res->addr = malloc(SOCKETID_SIZE);
508
          if (res->addr != NULL) {
509 3f57027f MarcoBiazzini
                  memset(res->addr,0,SOCKETID_SIZE);
510
                  //memcpy(res->addr, b, SOCKETID_SIZE);
511
                  //*len = SOCKETID_SIZE;
512 f6ee5da1 CsabaKiraly
                  *len = strlen((char*)b) + 1;
513 47ceb0c9 MarcoBiazzini
                  mlStringToSocketID((char *)b,res->addr);
514 3f57027f MarcoBiazzini
        //          fprintf(stderr,"Node undumped : %s\n",node_addr(res));
515
        //          res->addrSize = SOCKETID_SIZE;
516
        //          res->addrStringSize = SOCKETID_STRING_SIZE;
517 ffc8bdcf MarcoBiazzini
                  res->connID = -1;
518 3f57027f MarcoBiazzini
                  res->refcnt = 1;
519 ffc8bdcf MarcoBiazzini
          }
520
          else {
521
                  free(res);
522
                  res = NULL;
523 3f57027f MarcoBiazzini
                  // TODO: what about *len in this case???
524 ffc8bdcf MarcoBiazzini
                  fprintf(stderr,"Net-helper : Error while 'undumping' nodeID...\n");
525
          }
526
  }
527 3f57027f MarcoBiazzini
528 ffc8bdcf MarcoBiazzini
529
  return res;
530
}