Link libgnunetblockgroup to libgnunetblock
[oweals/gnunet.git] / src / hello / address.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009 GNUnet e.V.
4
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.
9
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.
14
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file hello/address.c
23  * @brief helper functions for handling addresses
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_hello_lib.h"
28 #include "gnunet_util_lib.h"
29
30
31 /**
32  * Check if an address has a local option set
33  *
34  * @param address the address to check
35  * @param option the respective option to check for
36  * @return #GNUNET_YES or #GNUNET_NO
37  */
38 int
39 GNUNET_HELLO_address_check_option (const struct GNUNET_HELLO_Address * address,
40                                    enum GNUNET_HELLO_AddressInfo option)
41 {
42   if (option == (address->local_info & option))
43     return GNUNET_YES;
44   return GNUNET_NO;
45 }
46
47
48 /**
49  * Get the size of an address struct.
50  *
51  * @param address address
52  * @return the size
53  */
54 size_t
55 GNUNET_HELLO_address_get_size (const struct GNUNET_HELLO_Address * address)
56 {
57   return sizeof (struct GNUNET_HELLO_Address) + address->address_length +
58     strlen (address->transport_name) + 1;
59 }
60
61
62 /**
63  * Allocate an address struct.
64  *
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
71  */
72 struct GNUNET_HELLO_Address *
73 GNUNET_HELLO_address_allocate (const struct GNUNET_PeerIdentity *peer,
74                                const char *transport_name,
75                                const void *address,
76                                size_t address_length,
77                                enum GNUNET_HELLO_AddressInfo local_info)
78 {
79   struct GNUNET_HELLO_Address *addr;
80   size_t slen;
81   char *end;
82
83   slen = strlen (transport_name) + 1;
84   addr = GNUNET_malloc (sizeof (struct GNUNET_HELLO_Address) +
85                         address_length + slen);
86   addr->peer = *peer;
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];
92   GNUNET_memcpy (end,
93           address,
94           address_length);
95   GNUNET_memcpy (&end[address_length],
96           transport_name,
97           slen);
98   return addr;
99 }
100
101
102 /**
103  * Copy an address struct.
104  *
105  * @param address address to copy
106  * @return a copy of the address struct
107  */
108 struct GNUNET_HELLO_Address *
109 GNUNET_HELLO_address_copy (const struct GNUNET_HELLO_Address *address)
110 {
111   if (NULL == address)
112     return NULL;
113   return GNUNET_HELLO_address_allocate (&address->peer,
114                                         address->transport_name,
115                                         address->address,
116                                         address->address_length,
117                                         address->local_info);
118 }
119
120
121 /**
122  * Compare two addresses.  Does NOT compare the peer identity,
123  * that is assumed already to match!
124  *
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.
128  */
129 int
130 GNUNET_HELLO_address_cmp (const struct GNUNET_HELLO_Address *a1,
131                           const struct GNUNET_HELLO_Address *a2)
132 {
133   int ret;
134
135   if ( (NULL == a1) &&
136        (NULL == a2) )
137     return 0;
138   if (NULL == a1)
139     return 1;
140   if (NULL == a2)
141     return -1;
142   ret = strcmp (a1->transport_name, a2->transport_name);
143   if (0 != ret)
144     return ret;
145   if (a1->local_info != a2->local_info)
146     return (((int) a1->local_info) < ((int) a2->local_info)) ? -1 : 1;
147   if (a1->address_length < a2->address_length)
148     return -1;
149   if (a1->address_length > a2->address_length)
150     return 1;
151   return memcmp (a1->address,
152                  a2->address,
153                  a1->address_length);
154 }
155
156
157 /* end of address.c */