29792e44c27d72180c737ea076679c36c53a03c0
[umurmur.git] / src / util.c
1 /* Copyright (C) 2014, Felix Morgner <felix.morgner@gmail.com>
2
3    All rights reserved.
4
5    Redistribution and use in source and binary forms, with or without
6    modification, are permitted provided that the following conditions
7    are met:
8
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.
17
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.
29 */
30 #include "util.h"
31
32 char* Util_addressToString(struct sockaddr_storage *address)
33 {
34         char* addressString = NULL;
35
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);
42         }
43
44         return addressString;
45 }
46
47 int Util_addressToPort(struct sockaddr_storage *address)
48 {
49         int port = 0;
50
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);
55         }
56
57         return port;
58 }
59
60 char* Util_clientAddressToString(client_t *client)
61 {
62         return Util_addressToString(&client->remote_tcp);
63 }
64
65 int Util_clientAddressToPortTCP(client_t *client)
66 {
67         return Util_addressToPort(&client->remote_tcp);
68 }
69
70 int Util_clientAddressToPortUDP(client_t *client)
71 {
72         return Util_addressToPort(&client->remote_udp);
73 }
74