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