1 /* Copyright (C) 2014, Felix Morgner <felix.morgner@gmail.com>
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
9 - Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer.
11 - Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
14 - Neither the name of the Developers nor the names of its contributors may
15 be used to endorse or promote products derived from this software without
16 specific prior written permission.
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
22 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 char* Util_addressToString(struct sockaddr_storage *address)
34 char* addressString = NULL;
36 if (address->ss_family == AF_INET) {
37 addressString = malloc(INET_ADDRSTRLEN);
38 inet_ntop(AF_INET, &((struct sockaddr_in *)address)->sin_addr, addressString, INET_ADDRSTRLEN);
39 } else if(address->ss_family == AF_INET6) {
40 addressString = malloc(INET6_ADDRSTRLEN);
41 inet_ntop(AF_INET6, &((struct sockaddr_in6 *)address)->sin6_addr, addressString, INET6_ADDRSTRLEN);
47 int Util_addressToPort(struct sockaddr_storage *address)
51 if (address->ss_family == AF_INET) {
52 port = ntohs(((struct sockaddr_in *)address)->sin_port);
53 } else if(address->ss_family == AF_INET6) {
54 port = ntohs(((struct sockaddr_in6 *)address)->sin6_port);
60 char* Util_clientAddressToString(client_t *client)
62 return Util_addressToString(&client->remote_tcp);
65 int Util_clientAddressToPortTCP(client_t *client)
67 return Util_addressToPort(&client->remote_tcp);
70 int Util_clientAddressToPortUDP(client_t *client)
72 return Util_addressToPort(&client->remote_udp);