2 This file is part of GNUnet.
3 Copyright (C) 2009 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
17 * @file hello/address.c
18 * @brief helper functions for handling addresses
19 * @author Christian Grothoff
22 #include "gnunet_hello_lib.h"
23 #include "gnunet_util_lib.h"
27 * Check if an address has a local option set
29 * @param address the address to check
30 * @param option the respective option to check for
31 * @return #GNUNET_YES or #GNUNET_NO
34 GNUNET_HELLO_address_check_option (const struct GNUNET_HELLO_Address * address,
35 enum GNUNET_HELLO_AddressInfo option)
37 if (option == (address->local_info & option))
44 * Get the size of an address struct.
46 * @param address address
50 GNUNET_HELLO_address_get_size (const struct GNUNET_HELLO_Address * address)
52 return sizeof (struct GNUNET_HELLO_Address) + address->address_length +
53 strlen (address->transport_name) + 1;
58 * Allocate an address struct.
60 * @param peer the peer
61 * @param transport_name plugin name
62 * @param address binary address
63 * @param address_length number of bytes in 'address'
64 * @param local_info additional local information for the address
65 * @return the address struct
67 struct GNUNET_HELLO_Address *
68 GNUNET_HELLO_address_allocate (const struct GNUNET_PeerIdentity *peer,
69 const char *transport_name,
71 size_t address_length,
72 enum GNUNET_HELLO_AddressInfo local_info)
74 struct GNUNET_HELLO_Address *addr;
78 slen = strlen (transport_name) + 1;
79 addr = GNUNET_malloc (sizeof (struct GNUNET_HELLO_Address) +
80 address_length + slen);
82 addr->address = &addr[1];
83 addr->address_length = address_length;
84 addr->local_info = local_info;
85 end = (char *) &addr[1];
86 addr->transport_name = &end[address_length];
90 GNUNET_memcpy (&end[address_length],
98 * Copy an address struct.
100 * @param address address to copy
101 * @return a copy of the address struct
103 struct GNUNET_HELLO_Address *
104 GNUNET_HELLO_address_copy (const struct GNUNET_HELLO_Address *address)
108 return GNUNET_HELLO_address_allocate (&address->peer,
109 address->transport_name,
111 address->address_length,
112 address->local_info);
117 * Compare two addresses. Does NOT compare the peer identity,
118 * that is assumed already to match!
120 * @param a1 first address
121 * @param a2 second address
122 * @return 0 if the addresses are equal, -1 if a1<a2, 1 if a1>a2.
125 GNUNET_HELLO_address_cmp (const struct GNUNET_HELLO_Address *a1,
126 const struct GNUNET_HELLO_Address *a2)
137 ret = strcmp (a1->transport_name, a2->transport_name);
140 if (a1->local_info != a2->local_info)
141 return (((int) a1->local_info) < ((int) a2->local_info)) ? -1 : 1;
142 if (a1->address_length < a2->address_length)
144 if (a1->address_length > a2->address_length)
146 return memcmp (a1->address,
152 /* end of address.c */