summaryrefslogtreecommitdiffstats
path: root/avahi-common/address.c
blob: b982a2cb7ca46f3b0257d6eb3ecc728c0456211b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* $Id$ */

/***
  This file is part of avahi.
 
  avahi is free software; you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License as
  published by the Free Software Foundation; either version 2.1 of the
  License, or (at your option) any later version.
 
  avahi is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
  Public License for more details.
 
  You should have received a copy of the GNU Lesser General Public
  License along with avahi; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>

#include "address.h"

guint avahi_address_get_size(const AvahiAddress *a) {
    g_assert(a);

    if (a->family == AVAHI_PROTO_INET)
        return 4;
    else if (a->family == AVAHI_PROTO_INET6)
        return 16;

    return 0;
}

gint avahi_address_cmp(const AvahiAddress *a, const AvahiAddress *b) {
    g_assert(a);
    g_assert(b);
    
    if (a->family != b->family)
        return -1;

    return memcmp(a->data.data, b->data.data, avahi_address_get_size(a));
}

gchar *avahi_address_snprint(char *s, guint length, const AvahiAddress *a) {
    g_assert(s);
    g_assert(length);
    g_assert(a);
    return (gchar*) inet_ntop(a->family, a->data.data, s, length);
}

gchar* avahi_reverse_lookup_name_ipv4(const AvahiIPv4Address *a) {
    guint32 n = ntohl(a->address);
    g_assert(a);

    return g_strdup_printf("%u.%u.%u.%u.in-addr.arpa", n & 0xFF, (n >> 8) & 0xFF, (n >> 16) & 0xFF, n >> 24);
}

static gchar *reverse_lookup_name_ipv6(const AvahiIPv6Address *a, const gchar *suffix) {
    
    return g_strdup_printf("%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%s",
                           a->address[15] & 0xF,
                           a->address[15] >> 4,
                           a->address[14] & 0xF,
                           a->address[14] >> 4,
                           a->address[13] & 0xF,
                           a->address[13] >> 4,
                           a->address[12] & 0xF,
                           a->address[12] >> 4,
                           a->address[11] & 0xF,
                           a->address[11] >> 4,
                           a->address[10] & 0xF,
                           a->address[10] >> 4,
                           a->address[9] & 0xF,
                           a->address[9] >> 4,
                           a->address[8] & 0xF,
                           a->address[8] >> 4,
                           a->address[7] & 0xF,
                           a->address[7] >> 4,
                           a->address[6] & 0xF,
                           a->address[6] >> 4,
                           a->address[5] & 0xF,
                           a->address[5] >> 4,
                           a->address[4] & 0xF,
                           a->address[4] >> 4,
                           a->address[3] & 0xF,
                           a->address[3] >> 4,
                           a->address[2] & 0xF,
                           a->address[2] >> 4,
                           a->address[1] & 0xF,
                           a->address[1] >> 4,
                           a->address[0] & 0xF,
                           a->address[0] >> 4,
                           suffix);
}

gchar *avahi_reverse_lookup_name_ipv6_arpa(const AvahiIPv6Address *a) {
    return reverse_lookup_name_ipv6(a, "ip6.arpa");
}

gchar *avahi_reverse_lookup_name_ipv6_int(const AvahiIPv6Address *a) {
    return reverse_lookup_name_ipv6(a, "ip6.int");
}

AvahiAddress *avahi_address_parse(const gchar *s, AvahiProtocol family, AvahiAddress *ret_addr) {
    g_assert(ret_addr);
    g_assert(s);

    if (family == AVAHI_PROTO_UNSPEC) {
        if (inet_pton(AF_INET, s, ret_addr->data.data) <= 0) {
            if (inet_pton(AF_INET6, s, ret_addr->data.data) <= 0)
                return NULL;
            else
                ret_addr->family = AVAHI_PROTO_INET6;
        } else
            ret_addr->family = AVAHI_PROTO_INET;
    } else {
        if (inet_pton(family, s, ret_addr->data.data) <= 0)
            return NULL;
        
        ret_addr->family = family;
    }
    
    return ret_addr;
}

AvahiAddress *avahi_address_from_sockaddr(const struct sockaddr* sa, AvahiAddress *ret_addr) {
    g_assert(sa);
    g_assert(ret_addr);

    g_assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);

    ret_addr->family = sa->sa_family;

    if (sa->sa_family == AF_INET)
        memcpy(&ret_addr->data.ipv4, &((struct sockaddr_in*) sa)->sin_addr, sizeof(ret_addr->data.ipv4));
    else
        memcpy(&ret_addr->data.ipv6, &((struct sockaddr_in6*) sa)->sin6_addr, sizeof(ret_addr->data.ipv6));

    return ret_addr;
}

guint16 avahi_port_from_sockaddr(const struct sockaddr* sa) {
    g_assert(sa);

    g_assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);

    if (sa->sa_family == AF_INET)
        return ntohs(((struct sockaddr_in*) sa)->sin_port);
    else
        return ntohs(((struct sockaddr_in6*) sa)->sin6_port);
}

gboolean avahi_address_is_ipv4_in_ipv6(const AvahiAddress *a) {
    static const guint8 ipv4_in_ipv6[] = {
        0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,
        0xFF, 0xFF, 0xFF, 0xFF };
    
    g_assert(a);

    if (a->family != AVAHI_PROTO_INET6)
        return FALSE;

    return memcmp(a->data.ipv6.address, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0;
}