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