Revision 662a3ab9 src/net_helper-udp.c

View differences:

src/net_helper-udp.c
2 2
 *  Copyright (c) 2010 Luca Abeni
3 3
 *  Copyright (c) 2010 Csaba Kiraly
4 4
 *  Copyright (c) 2010 Alessandro Russo
5
 *  Copyright (c) 2017 Luca Baldesi
5 6
 *
6 7
 *  This is free software; see lgpl-2.1.txt
7 8
 */
......
27 28

  
28 29
#include "net_helper.h"
29 30

  
30
#define MAX_MSG_SIZE 1024 * 60
31
enum L3PROTOCOL {IPv4, IPv6} l3 = IPv4;
32

  
33 31
struct nodeID {
34
  struct sockaddr_storage addr;
35
  int fd;
36
};
37

  
38
#ifdef _WIN32
39
static int inet_aton(const char *cp, struct in_addr *addr)
40
{
41
    if( cp==NULL || addr==NULL )
42
    {
43
        return(0);
44
    }
45

  
46
    addr->s_addr = inet_addr(cp);
47
    return (addr->s_addr == INADDR_NONE) ? 0 : 1;
48
}
49

  
50
struct iovec {                    /* Scatter/gather array items */
51
  void  *iov_base;              /* Starting address */
52
  size_t iov_len;               /* Number of bytes to transfer */
32
	struct sockaddr_storage addr;
33
	uint16_t occurrences;
34
	int fd;
53 35
};
54 36

  
55
struct msghdr {
56
  void         *msg_name;       /* optional address */
57
  socklen_t     msg_namelen;    /* size of address */
58
  struct iovec *msg_iov;        /* scatter/gather array */
59
  size_t        msg_iovlen;     /* # elements in msg_iov */
60
  void         *msg_control;    /* ancillary data, see below */
61
  socklen_t     msg_controllen; /* ancillary data buffer len */
62
  int           msg_flags;      /* flags on received message */
63
};
64

  
65
#define MIN(A,B)    ((A)<(B) ? (A) : (B))
66
ssize_t recvmsg (int sd, struct msghdr *msg, int flags)
67
{
68
  ssize_t bytes_read;
69
  size_t expected_recv_size;
70
  ssize_t left2move;
71
  char *tmp_buf;
72
  char *tmp;
73
  int i;
74

  
75
  assert (msg->msg_iov);
76

  
77
  expected_recv_size = 0;
78
  for (i = 0; i < msg->msg_iovlen; i++)
79
    expected_recv_size += msg->msg_iov[i].iov_len;
80
  tmp_buf = malloc (expected_recv_size);
81
  if (!tmp_buf)
82
    return -1;
83

  
84
  left2move = bytes_read = recvfrom (sd,
85
                                     tmp_buf,
86
                                     expected_recv_size,
87
                                     flags,
88
                                     (struct sockaddr *) msg->msg_name,
89
                                     &msg->msg_namelen);
90

  
91
  for (tmp = tmp_buf, i = 0; i < msg->msg_iovlen; i++)
92
    {
93
      if (left2move <= 0)
94
        break;
95
      assert (msg->msg_iov[i].iov_base);
96
      memcpy (msg->msg_iov[i].iov_base,
97
              tmp, MIN (msg->msg_iov[i].iov_len, left2move));
98
      left2move -= msg->msg_iov[i].iov_len;
99
      tmp += msg->msg_iov[i].iov_len;
100
    }
101

  
102
  free (tmp_buf);
103

  
104
  return bytes_read;
105
}
106

  
107
ssize_t sendmsg (int sd, struct msghdr * msg, int flags)
108
{
109
  ssize_t bytes_send;
110
  size_t expected_send_size;
111
  size_t left2move;
112
  char *tmp_buf;
113
  char *tmp;
114
  int i;
115

  
116
  assert (msg->msg_iov);
117

  
118
  expected_send_size = 0;
119
  for (i = 0; i < msg->msg_iovlen; i++)
120
    expected_send_size += msg->msg_iov[i].iov_len;
121
  tmp_buf = malloc (expected_send_size);
122
  if (!tmp_buf)
123
    return -1;
124

  
125
  for (tmp = tmp_buf, left2move = expected_send_size, i = 0; i <
126
       msg->msg_iovlen; i++)
127
    {
128
      if (left2move <= 0)
129
        break;
130
      assert (msg->msg_iov[i].iov_base);
131
      memcpy (tmp,
132
              msg->msg_iov[i].iov_base,
133
              MIN (msg->msg_iov[i].iov_len, left2move));
134
      left2move -= msg->msg_iov[i].iov_len;
135
      tmp += msg->msg_iov[i].iov_len;
136
    }
137

  
138
  bytes_send = sendto (sd,
139
                       tmp_buf,
140
                       expected_send_size,
141
                       flags,
142
                       (struct sockaddr *) msg->msg_name, msg->msg_namelen);
143

  
144
  free (tmp_buf);
145

  
146
  return bytes_send;
147
}
148

  
149
#endif
150

  
151 37
int wait4data(const struct nodeID *s, struct timeval *tout, int *user_fds)
152 38
/* returns 0 if timeout expires 
153 39
 * returns -1 in case of error of the select function
......
156 42
 * returns 2 if some of the user_fds file descriptors is ready
157 43
 */
158 44
{
159
  fd_set fds;
160
  int i, res, max_fd;
161

  
162
  FD_ZERO(&fds);
163
  if (s) {
164
    max_fd = s->fd;
165
    FD_SET(s->fd, &fds);
166
  } else {
167
    max_fd = -1;
168
  }
169
  if (user_fds) {
170
    for (i = 0; user_fds[i] != -1; i++) {
171
      FD_SET(user_fds[i], &fds);
172
      if (user_fds[i] > max_fd) {
173
        max_fd = user_fds[i];
174
      }
175
    }
176
  }
177
  res = select(max_fd + 1, &fds, NULL, NULL, tout);
178
  if (res <= 0) {
179
    return res;
180
  }
181
  if (s && FD_ISSET(s->fd, &fds)) {
182
    return 1;
183
  }
184

  
185
  /* If execution arrives here, user_fds cannot be 0
186
     (an FD is ready, and it's not s->fd) */
187
  for (i = 0; user_fds[i] != -1; i++) {
188
    if (!FD_ISSET(user_fds[i], &fds)) {
189
      user_fds[i] = -2;
190
    }
191
  }
192

  
193
  return 2;
45
	fd_set fds;
46
	int i, res, max_fd;
47

  
48
	FD_ZERO(&fds);
49
	if (s && s->fd >= 0) {
50
		max_fd = s->fd;
51
		FD_SET(s->fd, &fds);
52
	} else {
53
		max_fd = -1;
54
	}
55
	if (user_fds) {
56
		for (i = 0; user_fds[i] != -1; i++) {
57
			FD_SET(user_fds[i], &fds);
58
			if (user_fds[i] > max_fd) {
59
				max_fd = user_fds[i];
60
			}
61
		}
62
	}
63
	res = select(max_fd + 1, &fds, NULL, NULL, tout);
64
	if (res <= 0) {
65
		return res;
66
	}
67
	if (s && FD_ISSET(s->fd, &fds)) {
68
		return 1;
69
	}
70

  
71
	/* If execution arrives here, user_fds cannot be 0
72
	(an FD is ready, and it's not s->fd) */
73
	for (i = 0; user_fds[i] != -1; i++) {
74
		if (!FD_ISSET(user_fds[i], &fds)) {
75
			user_fds[i] = -2;
76
		}
77
	}
78

  
79
	return 2;
194 80
}
195 81

  
196 82
int register_network_fds(const struct nodeID *s, fd_register_f func, void *handler)
......
202 88

  
203 89
struct nodeID *create_node(const char *IPaddr, int port)
204 90
{
205
  struct nodeID *s;
206
  int res;
207
  struct addrinfo hints, *result;
208

  
209
  memset(&hints, 0, sizeof(hints));
210
  hints.ai_family = AF_UNSPEC;
211
  hints.ai_flags = AI_NUMERICHOST;
212

  
213
  s = malloc(sizeof(struct nodeID));
214
  memset(s, 0, sizeof(struct nodeID));
215

  
216
  if ((res = getaddrinfo(IPaddr, NULL, &hints, &result)))
217
  {
218
    fprintf(stderr, "Cannot resolve hostname '%s'\n", IPaddr);
219
    free(s);
220
    return NULL;
221
  }
222
  s->addr.ss_family = result->ai_family;
223
  switch (result->ai_family)
224
  {
225
    case (AF_INET):
226
      ((struct sockaddr_in *)&s->addr)->sin_port = htons(port);
227
      res = inet_pton (result->ai_family, IPaddr, &((struct sockaddr_in *)&s->addr)->sin_addr);
228
    break;
229
    case (AF_INET6):
230
      ((struct sockaddr_in6 *)&s->addr)->sin6_port = htons(port);
231
      res = inet_pton (result->ai_family, IPaddr, &(((struct sockaddr_in6 *) &s->addr)->sin6_addr));
232
    break;
233
    default:
234
      fprintf(stderr, "Cannot resolve address family %d for '%s'\n", result->ai_family, IPaddr);
235
      res = 0;
236
      break;
237
  }
238
  freeaddrinfo(result);
239
  if (res != 1)
240
  {
241
    fprintf(stderr, "Could not convert address '%s'\n", IPaddr);
242
    free(s);
243

  
244
    return NULL;
245
  }
246

  
247
  s->fd = -1;
248

  
249
  return s;
91
	struct nodeID *s = NULL;
92
	int error = 0;
93
	struct addrinfo hints, *result = NULL;
94

  
95
	if (IPaddr && port > 0)
96
	{
97
		memset(&hints, 0, sizeof(hints));
98
		hints.ai_family = AF_UNSPEC;
99
		hints.ai_flags = AI_NUMERICHOST;
100

  
101
		s = malloc(sizeof(struct nodeID));
102
		memset(s, 0, sizeof(struct nodeID));
103
		s->occurrences = 1;
104
		s->fd = -1;
105

  
106
		if ((error = getaddrinfo(IPaddr, NULL, &hints, &result)) == 0)
107
		{
108
			s->addr.ss_family = result->ai_family;
109
			switch (result->ai_family) {
110
				case (AF_INET):
111
					((struct sockaddr_in *)&s->addr)->sin_port = htons(port);
112
					error = inet_pton (result->ai_family, IPaddr, &((struct sockaddr_in *)&s->addr)->sin_addr);
113
					if (error > 0)
114
						error = 0;
115
					break;
116
				case (AF_INET6):
117
					((struct sockaddr_in6 *)&s->addr)->sin6_port = htons(port);
118
					error = inet_pton (result->ai_family, IPaddr, &(((struct sockaddr_in6 *) &s->addr)->sin6_addr));
119
					if (error > 0)
120
						error = 0;
121
					break;
122
				default:
123
					fprintf(stderr, "Cannot resolve address family %d for '%s'\n", result->ai_family, IPaddr);
124
					error = -1;
125
			} 
126
		}
127
	}
128
	if (error)
129
	{
130
		fprintf(stderr, "Cannot resolve hostname '%s'\n", IPaddr);
131
		nodeid_free(s);
132
		s = NULL;
133
	}
134
	if (result)
135
		freeaddrinfo(result);
136

  
137
	return s;
250 138
}
251 139

  
252 140
struct nodeID *net_helper_init(const char *my_addr, int port, const char *config)
253 141
{
254
  int res;
255
  struct nodeID *myself;
256

  
257
  myself = create_node(my_addr, port);
258
  if (myself == NULL) {
259
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
260

  
261
    return NULL;
262
  }
263
  myself->fd =  socket(myself->addr.ss_family, SOCK_DGRAM, 0);
264
  if (myself->fd < 0) {
265
    free(myself);
266

  
267
    return NULL;
268
  }
269
//  TODO:
270
//  if (addr->sa_family == AF_INET6) {
271
//      r = setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on));
272
//  }
273

  
274
  fprintf(stderr, "My sock: %d\n", myself->fd);
275

  
276
  switch (myself->addr.ss_family)
277
  {
278
    case (AF_INET):
279
        res = bind(myself->fd, (struct sockaddr *)&myself->addr, sizeof(struct sockaddr_in));
280
    break;
281
    case (AF_INET6):
282
        res = bind(myself->fd, (struct sockaddr *)&myself->addr, sizeof(struct sockaddr_in6));
283
    break;
284
    default:
285
      fprintf(stderr, "Cannot resolve address family %d in bind\n", myself->addr.ss_family);
286
      res = 0;
287
    break;
288
  }
289

  
290
  if (res < 0) {
291
    /* bind failed: not a local address... Just close the socket! */
292
    close(myself->fd);
293
    free(myself);
294

  
295
    return NULL;
296
  }
297

  
298
  return myself;
142
	int res;
143
	struct nodeID *myself = NULL;;
144

  
145
	if (my_addr && port > 0)
146
	{
147
		myself = create_node(my_addr, port);
148
		if (myself)
149
			myself->fd =  socket(myself->addr.ss_family, SOCK_DGRAM, 0);
150
		if (myself && myself->fd >= 0)
151
			switch (myself->addr.ss_family)
152
			{
153
				case (AF_INET):
154
					res = bind(myself->fd, (struct sockaddr *)&myself->addr, sizeof(struct sockaddr_in));
155
					break;
156
				case (AF_INET6):
157
					res = bind(myself->fd, (struct sockaddr *)&myself->addr, sizeof(struct sockaddr_in6));
158
					break;
159
				default:
160
					fprintf(stderr, "Cannot resolve address family %d in bind\n", myself->addr.ss_family);
161
					res = -1;
162
					break;
163
			}
164
		if (myself && (myself->fd < 0 || res < 0))
165
		{
166
			nodeid_free(myself);
167
			myself = NULL;
168
		}
169

  
170
	}
171
	return myself;
299 172
}
300 173

  
301 174
void bind_msg_type (uint8_t msgtype)
302 175
{
303 176
}
304 177

  
305
struct my_hdr_t {
306
  uint8_t m_seq;
307
  uint8_t frag_seq;
308
  uint8_t frags;
309
} __attribute__((packed));
310

  
311 178
int send_to_peer(const struct nodeID *from, const struct nodeID *to, const uint8_t *buffer_ptr, int buffer_size)
312 179
{
313
  struct msghdr msg = {0};
314
  static struct my_hdr_t my_hdr;
315
  struct iovec iov[2];
316
  int res;
317

  
318
  if (buffer_size <= 0) return -1;
319

  
320
  iov[0].iov_base = &my_hdr;
321
  iov[0].iov_len = sizeof(struct my_hdr_t);
322
  msg.msg_name = (void *)&to->addr;
323
  msg.msg_namelen = sizeof(struct sockaddr_storage);
324
  msg.msg_iovlen = 2;
325
  msg.msg_iov = iov;
326

  
327
  my_hdr.m_seq++;
328
  my_hdr.frags = (buffer_size / (MAX_MSG_SIZE)) + 1;
329
  my_hdr.frag_seq = 0;
330

  
331
  do {
332
    iov[1].iov_base = (void *)buffer_ptr;
333
    if (buffer_size > MAX_MSG_SIZE) {
334
      iov[1].iov_len = MAX_MSG_SIZE;
335
    } else {
336
      iov[1].iov_len = buffer_size;
337
    }
338
    my_hdr.frag_seq++;
339

  
340
    buffer_size -= iov[1].iov_len;
341
    buffer_ptr += iov[1].iov_len;
342
    res = sendmsg(from->fd, &msg, 0);
343

  
344
    if (res  < 0){
345
      int error = errno;
346
      fprintf(stderr,"net-helper: sendmsg failed errno %d: %s\n", error, strerror(error));
347
    }
348
  } while (buffer_size > 0);
349

  
350
  return res;
180
	return sendto(from->fd, buffer_ptr, buffer_size, MSG_CONFIRM, (const struct sockaddr *)&(to->addr), sizeof(struct sockaddr_storage));
351 181
}
352 182

  
353 183
int recv_from_peer(const struct nodeID *local, struct nodeID **remote, uint8_t *buffer_ptr, int buffer_size)
354 184
{
355
  int res, recv, m_seq, frag_seq;
356
  struct sockaddr_storage raddr;
357
  struct msghdr msg = {0};
358
  static struct my_hdr_t my_hdr;
359
  struct iovec iov[2];
360

  
361
  iov[0].iov_base = &my_hdr;
362
  iov[0].iov_len = sizeof(struct my_hdr_t);
363
  msg.msg_name = &raddr;
364
  msg.msg_namelen = sizeof(struct sockaddr_storage);
365
  msg.msg_iovlen = 2;
366
  msg.msg_iov = iov;
367

  
368
  *remote = malloc(sizeof(struct nodeID));
369
  if (*remote == NULL) {
370
    return -1;
371
  }
372
  memset(*remote, 0, sizeof(struct nodeID));
373

  
374
  recv = 0;
375
  m_seq = -1;
376
  frag_seq = 0;
377
  do {
378
    iov[1].iov_base = buffer_ptr;
379
    if (buffer_size > MAX_MSG_SIZE) {
380
      iov[1].iov_len = MAX_MSG_SIZE;
381
    } else {
382
      iov[1].iov_len = buffer_size;
383
    }
384
    buffer_size -= iov[1].iov_len;
385
    buffer_ptr += iov[1].iov_len;
386
    res = recvmsg(local->fd, &msg, 0);
387
    recv += (res - sizeof(struct my_hdr_t));
388
    if (m_seq != -1 && my_hdr.m_seq != m_seq) {
389
      return -1;
390
    } else {
391
      m_seq = my_hdr.m_seq;
392
    }
393
    if (my_hdr.frag_seq != frag_seq + 1) {
394
      return -1;
395
    } else {
396
     frag_seq++;
397
    }
398
  } while ((my_hdr.frag_seq < my_hdr.frags) && (buffer_size > 0));
399
  memcpy(&(*remote)->addr, &raddr, msg.msg_namelen);
400
  (*remote)->fd = -1;
401

  
402
  return recv;
185
	struct nodeID * node;
186
	int res;
187
	socklen_t len;
188

  
189
	node = malloc(sizeof(struct nodeID));
190
	memset(node, 0, sizeof(struct nodeID));
191
	node->occurrences = 1;
192
	node->fd = -1;
193
	len = sizeof(struct nodeID);
194

  
195
	res = recvfrom(local->fd, buffer_ptr, buffer_size, 0, (struct sockaddr *)&(node->addr), &len);
196

  
197
	if (res <=0 )
198
	{
199
		nodeid_free(node);
200
		node = NULL;
201
	}
202
	*remote = node;
203

  
204
	return res;
403 205
}
404 206

  
405 207
int node_addr(const struct nodeID *s, char *addr, int len)
406 208
{
407
  int n;
209
	int n = -1;
408 210

  
409
	if (s && node_ip(s, addr, len)>=0)
211
	if (addr && len > 0)
410 212
	{
411
		n = snprintf(addr + strlen(addr), len - strlen(addr) - 1, ":%d", node_port(s));
412
	} else
413
		n = snprintf(addr, len , "None");
414

  
415
  return n;
213
		if (s)
214
		{
215
			n = nodeid_dump((uint8_t *) addr, s, len);
216
			if (n>0)
217
				addr[n-1] = '\0';
218
		} else
219
			n = snprintf(addr, len , "None");
220
	}
221
	return n;
416 222
}
417 223

  
418 224
struct nodeID *nodeid_dup(const struct nodeID *s)
419 225
{
420
  struct nodeID *res;
421

  
422
  res = malloc(sizeof(struct nodeID));
423
  if (res != NULL) {
424
    memcpy(res, s, sizeof(struct nodeID));
425
  }
226
	struct nodeID * n;
426 227

  
427
  return res;
228
	n = (struct nodeID *) s;
229
	if (n)
230
		n->occurrences++;
231
  return n;
428 232
}
429 233

  
430 234
int nodeid_equal(const struct nodeID *s1, const struct nodeID *s2)
431 235
{
432
	return (nodeid_cmp(s1,s2) == 0);
433
//  return (memcmp(&s1->addr, &s2->addr, sizeof(struct sockaddr_storage)) == 0);
236
	if (s1 && s2)
237
		return (nodeid_cmp(s1, s2) == 0) ? 1 : 0;
238
	return 0;
434 239
}
435 240

  
436 241
int nodeid_cmp(const struct nodeID *s1, const struct nodeID *s2)
437 242
{
438
	char ip1[80], ip2[80];
439
	int port1,port2,res;
440

  
441
  if(s1 && s2)
442
  {
443
    port1=node_port(s1);
444
    port2=node_port(s2);
445
    node_ip(s1,ip1,80);
446
    node_ip(s2,ip2,80);
447

  
448
  //	int res = (port1^port2)|strcmp(ip1,ip2);
449
  //	fprintf(stderr,"Comparing %s:%d and %s:%d\n",ip1,port1,ip2,port2);
450
  //	fprintf(stderr,"Result: %d\n",res);
451
    res  = strcmp(ip1,ip2);
452
    if (res!=0)
453
      return res;
454
    else
455
      return port1-port2;
456
  //		return port1 == port2 ? 0 : 1;
457

  
458
  //  return memcmp(&s1->addr, &s2->addr, sizeof(struct sockaddr_storage));
459
  }
460
  else
461
    return 0;
243
	char ip1[INET6_ADDRSTRLEN], ip2[INET6_ADDRSTRLEN];
244
	int res = 0;
245

  
246
	if (s1 && s2 && (s1 != s2))
247
	{
248
		node_ip(s1, ip1, INET6_ADDRSTRLEN);
249
		node_ip(s2, ip2, INET6_ADDRSTRLEN);
250
		res = strcmp(ip1, ip2);
251
		if (res == 0)
252
			res = node_port(s1) - node_port(s2);
253
	} else {
254
		if (s1 && !s2)
255
			res = 1;
256
		if (s2 && !s1)
257
			res = -1;
258
	}
259
	return res;
462 260
}
463 261

  
464 262
int nodeid_dump(uint8_t *b, const struct nodeID *s, size_t max_write_size)
465 263
{
466
  if (max_write_size < sizeof(struct sockaddr_storage)) return -1;
264
	char ip[INET6_ADDRSTRLEN];
265
	int port;
266
	int res = -1;
467 267

  
468
  memcpy(b, &s->addr, sizeof(struct sockaddr_storage));
469

  
470
  return sizeof(struct sockaddr_storage);
268
	if (s && b)
269
	{
270
		node_ip(s, ip, INET6_ADDRSTRLEN);
271
		port = node_port(s);
272
		if (max_write_size >= strlen(ip) + 1 + 5)
273
			res = sprintf((char *)b, "%s:%d-", ip, port);
274
	}
275
	return res;
471 276
}
472 277

  
473 278
struct nodeID *nodeid_undump(const uint8_t *b, int *len)
474 279
{
475
  struct nodeID *res;
476
  res = malloc(sizeof(struct nodeID));
477
  if (res != NULL) {
478
    memcpy(&res->addr, b, sizeof(struct sockaddr_storage));
479
    res->fd = -1;
480
  }
481
  *len = sizeof(struct sockaddr_storage);
482

  
483
  return res;
280
	char * ptr;
281
	char * socket;
282
	int port;
283
	struct nodeID *res = NULL;
284

  
285
	if (b && len)
286
	{
287
		ptr = strchr((const char *) b, '-');
288
		*len = ptr-(char *)b + 1;
289
		socket = malloc(sizeof(char) * (*len));
290
		memmove(socket, b, sizeof(char) * (*len));
291
		socket[(*len)-1] = '\0';
292

  
293
		ptr = strrchr(socket, ':');
294
		port = atoi(ptr+1);
295

  
296
		*ptr = '\0';
297

  
298
		res = create_node(socket, port);
299
		free(socket);
300
	}
301
	return res;
484 302
}
485 303

  
486 304
void nodeid_free(struct nodeID *s)
487 305
{
488
  if (s)
489
  {
490
    if (s->fd >= 0)
491
      close(s->fd);
492
    free(s);
493
  }
306
	if (s)
307
	{
308
		s->occurrences--;
309
		if (s->occurrences == 0)
310
		{
311
			if (s->fd >= 0)
312
				close(s->fd);
313
			free(s);
314
		}
315
	}
494 316
}
495 317

  
496 318
int node_ip(const struct nodeID *s, char *ip, int len)
497 319
{
498
	int res = 0;
499
  switch (s->addr.ss_family)
500
  {
501
    case AF_INET:
502
      inet_ntop(s->addr.ss_family, &((const struct sockaddr_in *)&s->addr)->sin_addr, ip, len);
503
      break;
504
    case AF_INET6:
505
      inet_ntop(s->addr.ss_family, &((const struct sockaddr_in6 *)&s->addr)->sin6_addr, ip, len);
506
      break;
507
    default:
508
			res = -1;
509
      break;
510
  }
511
  if (!ip) {
512
	  perror("inet_ntop");
513
		res = -1;
514
  }
515
  if (ip && res <0 && len)
516
    ip[0] = '\0';
517
  return res;
320
	const char *res = NULL;
321

  
322
	switch (s->addr.ss_family)
323
	{
324
		case AF_INET:
325
			res = inet_ntop(s->addr.ss_family, &((const struct sockaddr_in *)&s->addr)->sin_addr, ip, len);
326
			break;
327
		case AF_INET6:
328
			res = inet_ntop(s->addr.ss_family, &((const struct sockaddr_in6 *)&s->addr)->sin6_addr, ip, len);
329
			break;
330
	}
331
	if (!res && len)
332
		ip[0] = '\0';
333

  
334
	return res ? 0 : -1;
518 335
}
519 336

  
520 337
int node_port(const struct nodeID *s)
521 338
{
522
  int res;
523
  switch (s->addr.ss_family)
524
  {
525
    case AF_INET:
526
      res = ntohs(((const struct sockaddr_in *) &s->addr)->sin_port);
527
      break;
528
    case AF_INET6:
529
      res = ntohs(((const struct sockaddr_in6 *)&s->addr)->sin6_port);
530
      break;
531
    default:
532
      res = -1;
533
      break;
534
  }
535
  return res;
339
	int res = -1;
340

  
341
	switch (s->addr.ss_family) {
342
		case AF_INET:
343
			res = ntohs(((const struct sockaddr_in *) &s->addr)->sin_port);
344
			break;
345
		case AF_INET6:
346
			res = ntohs(((const struct sockaddr_in6 *)&s->addr)->sin6_port);
347
			break;
348
	}
349
	return res;
536 350
}

Also available in: Unified diff