2 This file is part of GNUnet.
3 Copyright (C) 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file hello/address.c
23 * @brief helper functions for handling addresses
24 * @author Christian Grothoff
27 #include "gnunet_hello_lib.h"
28 #include "gnunet_util_lib.h"
32 * Check if an address has a local option set
34 * @param address the address to check
35 * @param option the respective option to check for
36 * @return #GNUNET_YES or #GNUNET_NO
39 GNUNET_HELLO_address_check_option (const struct GNUNET_HELLO_Address * address,
40 enum GNUNET_HELLO_AddressInfo option)
42 if (option == (address->local_info & option))
49 * Get the size of an address struct.
51 * @param address address
55 GNUNET_HELLO_address_get_size (const struct GNUNET_HELLO_Address * address)
57 return sizeof (struct GNUNET_HELLO_Address) + address->address_length +
58 strlen (address->transport_name) + 1;
63 * Allocate an address struct.
65 * @param peer the peer
66 * @param transport_name plugin name
67 * @param address binary address
68 * @param address_length number of bytes in 'address'
69 * @param local_info additional local information for the address
70 * @return the address struct
72 struct GNUNET_HELLO_Address *
73 GNUNET_HELLO_address_allocate (const struct GNUNET_PeerIdentity *peer,
74 const char *transport_name,
76 size_t address_length,
77 enum GNUNET_HELLO_AddressInfo local_info)
79 struct GNUNET_HELLO_Address *addr;
83 slen = strlen (transport_name) + 1;
84 addr = GNUNET_malloc (sizeof (struct GNUNET_HELLO_Address) +
85 address_length + slen);
87 addr->address = &addr[1];
88 addr->address_length = address_length;
89 addr->local_info = local_info;
90 end = (char *) &addr[1];
91 addr->transport_name = &end[address_length];
95 memcpy (&end[address_length],
103 * Copy an address struct.
105 * @param address address to copy
106 * @return a copy of the address struct
108 struct GNUNET_HELLO_Address *
109 GNUNET_HELLO_address_copy (const struct GNUNET_HELLO_Address *address)
113 return GNUNET_HELLO_address_allocate (&address->peer,
114 address->transport_name,
116 address->address_length,
117 address->local_info);
122 * Compare two addresses. Does NOT compare the peer identity,
123 * that is assumed already to match!
125 * @param a1 first address
126 * @param a2 second address
127 * @return 0 if the addresses are equal, -1 if a1<a2, 1 if a1>a2.
130 GNUNET_HELLO_address_cmp (const struct GNUNET_HELLO_Address *a1,
131 const struct GNUNET_HELLO_Address *a2)
142 ret = strcmp (a1->transport_name, a2->transport_name);
145 if (a1->local_info != a2->local_info)
146 return ((int) a1->local_info) - ((int) a2->local_info);
147 if (a1->address_length < a2->address_length)
149 if (a1->address_length > a2->address_length)
151 return memcmp (a1->address, a2->address, a1->address_length);
155 /* end of address.c */