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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file hello/address.c
21 * @brief helper functions for handling addresses
22 * @author Christian Grothoff
25 #include "gnunet_hello_lib.h"
26 #include "gnunet_util_lib.h"
30 * Check if an address has a local option set
32 * @param address the address to check
33 * @param option the respective option to check for
34 * @return #GNUNET_YES or #GNUNET_NO
37 GNUNET_HELLO_address_check_option (const struct GNUNET_HELLO_Address * address,
38 enum GNUNET_HELLO_AddressInfo option)
40 if (option == (address->local_info & option))
47 * Get the size of an address struct.
49 * @param address address
53 GNUNET_HELLO_address_get_size (const struct GNUNET_HELLO_Address * address)
55 return sizeof (struct GNUNET_HELLO_Address) + address->address_length +
56 strlen (address->transport_name) + 1;
61 * Allocate an address struct.
63 * @param peer the peer
64 * @param transport_name plugin name
65 * @param address binary address
66 * @param address_length number of bytes in 'address'
67 * @param local_info additional local information for the address
68 * @return the address struct
70 struct GNUNET_HELLO_Address *
71 GNUNET_HELLO_address_allocate (const struct GNUNET_PeerIdentity *peer,
72 const char *transport_name,
74 size_t address_length,
75 enum GNUNET_HELLO_AddressInfo local_info)
77 struct GNUNET_HELLO_Address *addr;
81 slen = strlen (transport_name) + 1;
82 addr = GNUNET_malloc (sizeof (struct GNUNET_HELLO_Address) +
83 address_length + slen);
85 addr->address = &addr[1];
86 addr->address_length = address_length;
87 addr->local_info = local_info;
88 end = (char *) &addr[1];
89 addr->transport_name = &end[address_length];
93 GNUNET_memcpy (&end[address_length],
101 * Copy an address struct.
103 * @param address address to copy
104 * @return a copy of the address struct
106 struct GNUNET_HELLO_Address *
107 GNUNET_HELLO_address_copy (const struct GNUNET_HELLO_Address *address)
111 return GNUNET_HELLO_address_allocate (&address->peer,
112 address->transport_name,
114 address->address_length,
115 address->local_info);
120 * Compare two addresses. Does NOT compare the peer identity,
121 * that is assumed already to match!
123 * @param a1 first address
124 * @param a2 second address
125 * @return 0 if the addresses are equal, -1 if a1<a2, 1 if a1>a2.
128 GNUNET_HELLO_address_cmp (const struct GNUNET_HELLO_Address *a1,
129 const struct GNUNET_HELLO_Address *a2)
140 ret = strcmp (a1->transport_name, a2->transport_name);
143 if (a1->local_info != a2->local_info)
144 return (((int) a1->local_info) < ((int) a2->local_info)) ? -1 : 1;
145 if (a1->address_length < a2->address_length)
147 if (a1->address_length > a2->address_length)
149 return memcmp (a1->address,
155 /* end of address.c */