aboutsummaryrefslogtreecommitdiff
path: root/src/transport/plugin_transport_unix.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2013-10-09 18:01:16 +0000
committerChristian Grothoff <christian@grothoff.org>2013-10-09 18:01:16 +0000
commit25e37143ca6ddc3ea7d3c2e3fd7c66002a45e060 (patch)
tree8796fd0e148c6914897d6c53d25c6327b5a8263b /src/transport/plugin_transport_unix.c
parent0904af031c4f1411419b3047f893e0672957d146 (diff)
downloadgnunet-25e37143ca6ddc3ea7d3c2e3fd7c66002a45e060.tar.gz
gnunet-25e37143ca6ddc3ea7d3c2e3fd7c66002a45e060.zip
-switching (again) to named sockets, see #2887
Diffstat (limited to 'src/transport/plugin_transport_unix.c')
-rw-r--r--src/transport/plugin_transport_unix.c136
1 files changed, 62 insertions, 74 deletions
diff --git a/src/transport/plugin_transport_unix.c b/src/transport/plugin_transport_unix.c
index e80102a33..e78445a0a 100644
--- a/src/transport/plugin_transport_unix.c
+++ b/src/transport/plugin_transport_unix.c
@@ -348,6 +348,7 @@ reschedule_session_timeout (struct Session *s);
348static void 348static void
349unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); 349unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
350 350
351
351/** 352/**
352 * Function called for a quick conversion of the binary address to 353 * Function called for a quick conversion of the binary address to
353 * a numeric address. Note that the caller must not free the 354 * a numeric address. Note that the caller must not free the
@@ -360,10 +361,47 @@ unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
360 * @return string representing the same address 361 * @return string representing the same address
361 */ 362 */
362static const char * 363static const char *
363unix_address_to_string (void *cls, const void *addr, size_t addrlen); 364unix_address_to_string (void *cls, const void *addr, size_t addrlen)
365{
366 static char rbuf[1024];
367 struct UnixAddress *ua = (struct UnixAddress *) addr;
368 char *addrstr;
369 size_t addr_str_len;
370
371 if ((NULL == addr) || (sizeof (struct UnixAddress) > addrlen))
372 {
373 GNUNET_break(0);
374 return NULL ;
375 }
376 addrstr = (char *) &ua[1];
377 addr_str_len = ntohl (ua->addrlen);
378
379 if (addr_str_len != addrlen - sizeof(struct UnixAddress))
380 {
381 GNUNET_break(0);
382 return NULL ;
383 }
384
385 if ('\0' != addrstr[addr_str_len - 1])
386 {
387 GNUNET_break(0);
388 return NULL ;
389 }
390 if (strlen (addrstr) + 1 != addr_str_len)
391 {
392 GNUNET_break(0);
393 return NULL ;
394 }
395
396 GNUNET_snprintf (rbuf, sizeof(rbuf), "%s.%u.%s", PLUGIN_NAME,
397 ntohl (ua->options), addrstr);
398 return rbuf;
399}
400
364 401
365static struct sockaddr_un * 402static struct sockaddr_un *
366unix_address_to_sockaddr (const char *unixpath , socklen_t *sock_len) 403unix_address_to_sockaddr (const char *unixpath,
404 socklen_t *sock_len)
367{ 405{
368 struct sockaddr_un *un; 406 struct sockaddr_un *un;
369 size_t slen; 407 size_t slen;
@@ -380,14 +418,11 @@ unix_address_to_sockaddr (const char *unixpath , socklen_t *sock_len)
380#if HAVE_SOCKADDR_IN_SIN_LEN 418#if HAVE_SOCKADDR_IN_SIN_LEN
381 un->sun_len = (u_char) slen; 419 un->sun_len = (u_char) slen;
382#endif 420#endif
383#if LINUX
384 un->sun_path[0] = '\0';
385#endif
386
387 (*sock_len) = slen; 421 (*sock_len) = slen;
388 return un; 422 return un;
389} 423}
390 424
425
391/** 426/**
392 * Re-schedule the main 'select' callback (unix_plugin_select) 427 * Re-schedule the main 'select' callback (unix_plugin_select)
393 * for this plugin. 428 * for this plugin.
@@ -426,7 +461,7 @@ reschedule_select (struct Plugin * plugin)
426 461
427 462
428/** 463/**
429 * Closure to 'lookup_session_it'. 464 * Closure to #lookup_session_it.
430 */ 465 */
431struct LookupCtx 466struct LookupCtx
432{ 467{
@@ -440,6 +475,9 @@ struct LookupCtx
440 */ 475 */
441 const struct UnixAddress *ua; 476 const struct UnixAddress *ua;
442 477
478 /**
479 * Number of bytes in @e ua
480 */
443 size_t ua_len; 481 size_t ua_len;
444}; 482};
445 483
@@ -450,7 +488,7 @@ struct LookupCtx
450 * @param cls the 'struct LookupCtx' 488 * @param cls the 'struct LookupCtx'
451 * @param key peer we are looking for (unused) 489 * @param key peer we are looking for (unused)
452 * @param value a session 490 * @param value a session
453 * @return GNUNET_YES if not found (continue looking), GNUNET_NO on success 491 * @return #GNUNET_YES if not found (continue looking), #GNUNET_NO on success
454 */ 492 */
455static int 493static int
456lookup_session_it (void *cls, 494lookup_session_it (void *cls,
@@ -462,7 +500,7 @@ lookup_session_it (void *cls,
462 500
463 if (t->addrlen != lctx->ua_len) 501 if (t->addrlen != lctx->ua_len)
464 { 502 {
465 GNUNET_break (0); 503 GNUNET_break (0);
466 return GNUNET_YES; 504 return GNUNET_YES;
467 } 505 }
468 506
@@ -708,7 +746,7 @@ struct GetSessionIteratorContext
708 * @param cls the 'struct LookupCtx' 746 * @param cls the 'struct LookupCtx'
709 * @param key peer we are looking for (unused) 747 * @param key peer we are looking for (unused)
710 * @param value a session 748 * @param value a session
711 * @return GNUNET_YES if not found (continue looking), GNUNET_NO on success 749 * @return #GNUNET_YES if not found (continue looking), #GNUNET_NO on success
712 */ 750 */
713static int 751static int
714get_session_it (void *cls, 752get_session_it (void *cls,
@@ -755,7 +793,7 @@ session_timeout (void *cls,
755 * 793 *
756 * @param cls closure ('struct Plugin*') 794 * @param cls closure ('struct Plugin*')
757 * @param session the session 795 * @param session the session
758 * @return the network type in HBO or GNUNET_SYSERR 796 * @return the network type in HBO or #GNUNET_SYSERR
759 */ 797 */
760static enum GNUNET_ATS_Network_Type 798static enum GNUNET_ATS_Network_Type
761unix_get_network (void *cls, 799unix_get_network (void *cls,
@@ -952,7 +990,7 @@ unix_plugin_send (void *cls,
952 * @param sender from which peer the message was received 990 * @param sender from which peer the message was received
953 * @param currhdr pointer to the header of the message 991 * @param currhdr pointer to the header of the message
954 * @param ua address to look for 992 * @param ua address to look for
955 * @param ua_len length of the address 993 * @param ua_len length of the address @a ua
956 */ 994 */
957static void 995static void
958unix_demultiplexer (struct Plugin *plugin, struct GNUNET_PeerIdentity *sender, 996unix_demultiplexer (struct Plugin *plugin, struct GNUNET_PeerIdentity *sender,
@@ -1041,19 +1079,18 @@ unix_plugin_select_read (struct Plugin *plugin)
1041 } 1079 }
1042 else 1080 else
1043 { 1081 {
1044#if LINUX 1082 LOG (GNUNET_ERROR_TYPE_DEBUG,
1045 un.sun_path[0] = '/'; 1083 "Read %d bytes from socket %s\n",
1046#endif 1084 (int) ret,
1047 LOG (GNUNET_ERROR_TYPE_DEBUG, "Read %d bytes from socket %s\n", ret, 1085 un.sun_path);
1048 &un.sun_path[0]);
1049 } 1086 }
1050 1087
1051 GNUNET_assert (AF_UNIX == (un.sun_family)); 1088 GNUNET_assert (AF_UNIX == (un.sun_family));
1052 ua_len = sizeof (struct UnixAddress) + strlen (&un.sun_path[0]) +1; 1089 ua_len = sizeof (struct UnixAddress) + strlen (un.sun_path) + 1;
1053 ua = GNUNET_malloc (ua_len); 1090 ua = GNUNET_malloc (ua_len);
1054 ua->addrlen = htonl (strlen (&un.sun_path[0]) +1); 1091 ua->addrlen = htonl (strlen (&un.sun_path[0]) +1);
1055 ua->options = htonl (0); 1092 ua->options = htonl (0);
1056 memcpy (&ua[1], &un.sun_path[0], strlen (&un.sun_path[0]) +1); 1093 memcpy (&ua[1], &un.sun_path[0], strlen (un.sun_path) + 1);
1057 1094
1058 msg = (struct UNIXMessage *) buf; 1095 msg = (struct UNIXMessage *) buf;
1059 csize = ntohs (msg->header.size); 1096 csize = ntohs (msg->header.size);
@@ -1232,7 +1269,7 @@ unix_plugin_select (void *cls,
1232 * Create a slew of UNIX sockets. If possible, use IPv6 and IPv4. 1269 * Create a slew of UNIX sockets. If possible, use IPv6 and IPv4.
1233 * 1270 *
1234 * @param cls closure for server start, should be a struct Plugin * 1271 * @param cls closure for server start, should be a struct Plugin *
1235 * @return number of sockets created or GNUNET_SYSERR on error 1272 * @return number of sockets created or #GNUNET_SYSERR on error
1236 */ 1273 */
1237static int 1274static int
1238unix_transport_server_start (void *cls) 1275unix_transport_server_start (void *cls)
@@ -1250,8 +1287,8 @@ unix_transport_server_start (void *cls)
1250 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket"); 1287 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
1251 return GNUNET_SYSERR; 1288 return GNUNET_SYSERR;
1252 } 1289 }
1253 if (GNUNET_NETWORK_socket_bind (plugin->unix_sock.desc, (const struct sockaddr *) un, un_len, 0) 1290 if (GNUNET_OK !=
1254 != GNUNET_OK) 1291 GNUNET_NETWORK_socket_bind (plugin->unix_sock.desc, (const struct sockaddr *) un, un_len))
1255 { 1292 {
1256 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind"); 1293 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
1257 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc); 1294 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc);
@@ -1274,55 +1311,6 @@ unix_transport_server_start (void *cls)
1274 1311
1275 1312
1276/** 1313/**
1277 * Function called for a quick conversion of the binary address to
1278 * a numeric address. Note that the caller must not free the
1279 * address and that the next call to this function is allowed
1280 * to override the address again.
1281 *
1282 * @param cls closure
1283 * @param addr binary address
1284 * @param addrlen length of the address
1285 * @return string representing the same address
1286 */
1287static const char *
1288unix_address_to_string (void *cls, const void *addr, size_t addrlen)
1289{
1290 static char rbuf[1024];
1291 struct UnixAddress *ua = (struct UnixAddress *) addr;
1292 char *addrstr;
1293 size_t addr_str_len;
1294
1295 if ((NULL == addr) || (sizeof (struct UnixAddress) > addrlen))
1296 {
1297 GNUNET_break(0);
1298 return NULL ;
1299 }
1300 addrstr = (char *) &ua[1];
1301 addr_str_len = ntohl (ua->addrlen);
1302
1303 if (addr_str_len != addrlen - sizeof(struct UnixAddress))
1304 {
1305 GNUNET_break(0);
1306 return NULL ;
1307 }
1308
1309 if ('\0' != addrstr[addr_str_len - 1])
1310 {
1311 GNUNET_break(0);
1312 return NULL ;
1313 }
1314 if (strlen (addrstr) + 1 != addr_str_len)
1315 {
1316 GNUNET_break(0);
1317 return NULL ;
1318 }
1319
1320 GNUNET_snprintf (rbuf, sizeof(rbuf), "%s.%u.%s", PLUGIN_NAME,
1321 ntohl (ua->options), addrstr);
1322 return rbuf;
1323}
1324
1325/**
1326 * Function that will be called to check if a binary address for this 1314 * Function that will be called to check if a binary address for this
1327 * plugin is well-formed and corresponds to an address for THIS peer 1315 * plugin is well-formed and corresponds to an address for THIS peer
1328 * (as per our configuration). Naturally, if absolutely necessary, 1316 * (as per our configuration). Naturally, if absolutely necessary,
@@ -1425,7 +1413,7 @@ static int
1425unix_string_to_address (void *cls, const char *addr, uint16_t addrlen, 1413unix_string_to_address (void *cls, const char *addr, uint16_t addrlen,
1426 void **buf, size_t *added) 1414 void **buf, size_t *added)
1427{ 1415{
1428 struct UnixAddress *ua; 1416 struct UnixAddress *ua;
1429 char *address; 1417 char *address;
1430 char *plugin; 1418 char *plugin;
1431 char *optionstr; 1419 char *optionstr;
@@ -1461,7 +1449,7 @@ unix_string_to_address (void *cls, const char *addr, uint16_t addrlen,
1461 return GNUNET_SYSERR; 1449 return GNUNET_SYSERR;
1462 } 1450 }
1463 optionstr[0] = '\0'; 1451 optionstr[0] = '\0';
1464 optionstr ++; 1452 optionstr++;
1465 options = atol (optionstr); 1453 options = atol (optionstr);
1466 address = strchr (optionstr, '.'); 1454 address = strchr (optionstr, '.');
1467 if (NULL == address) 1455 if (NULL == address)
@@ -1471,7 +1459,7 @@ unix_string_to_address (void *cls, const char *addr, uint16_t addrlen,
1471 return GNUNET_SYSERR; 1459 return GNUNET_SYSERR;
1472 } 1460 }
1473 address[0] = '\0'; 1461 address[0] = '\0';
1474 address ++; 1462 address++;
1475 if (0 != strcmp(plugin, PLUGIN_NAME)) 1463 if (0 != strcmp(plugin, PLUGIN_NAME))
1476 { 1464 {
1477 GNUNET_break (0); 1465 GNUNET_break (0);