]> git.meshlink.io Git - catta/blob - avahi-compat-howl/address.c
implement address related functions
[catta] / avahi-compat-howl / address.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
6   avahi is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10  
11   avahi is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14   Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public
17   License along with avahi; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <netdb.h>
28 #include <unistd.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <sys/types.h>
33
34 #include <howl.h>
35
36 #include "warn.h"
37
38 sw_ipv4_address sw_ipv4_address_any(void) {
39     sw_ipv4_address a;
40
41     AVAHI_WARN_LINKAGE;
42
43     a.m_addr = htonl(INADDR_ANY);
44     return a;
45 }
46
47 sw_ipv4_address sw_ipv4_address_loopback(void) {
48     sw_ipv4_address a;
49
50     AVAHI_WARN_LINKAGE;
51
52     a.m_addr = htonl(INADDR_LOOPBACK);
53     return a;
54 }
55
56 sw_result sw_ipv4_address_init(sw_ipv4_address * self) {
57     assert(self);
58
59     AVAHI_WARN_LINKAGE;
60
61     self->m_addr = htonl(INADDR_ANY);
62     return SW_OKAY;
63 }
64
65 sw_result sw_ipv4_address_init_from_saddr(
66     sw_ipv4_address *self,
67     sw_saddr addr) {
68
69     assert(self);
70
71     AVAHI_WARN_LINKAGE;
72
73     self->m_addr = addr;
74     return SW_OKAY;
75 }
76
77 sw_result sw_ipv4_address_init_from_name(
78     sw_ipv4_address *self,
79     sw_const_string name) {
80
81     struct hostent *he;
82
83     assert(self);
84     assert(name);
85
86     AVAHI_WARN_LINKAGE;
87     
88     if (!(he = gethostbyname(name)))
89         return SW_E_UNKNOWN;
90     
91     self->m_addr = *(uint32_t*) he->h_addr;
92     return SW_OKAY;
93 }
94
95 sw_result sw_ipv4_address_init_from_address(
96     sw_ipv4_address *self,
97     sw_ipv4_address addr) {
98
99     assert(self);
100
101     AVAHI_WARN_LINKAGE;
102
103     self->m_addr = addr.m_addr;
104     return SW_OKAY;
105 }
106
107 sw_result sw_ipv4_address_init_from_this_host(sw_ipv4_address *self) {
108     struct sockaddr_in sa;
109     int fd;
110     socklen_t l = sizeof(sa);
111
112     assert(self);
113
114     AVAHI_WARN_LINKAGE;
115     
116     /* This is so fucked up ... */
117
118     memset(&sa, 0, sizeof(sa));
119     sa.sin_family = AF_INET;
120     sa.sin_addr.s_addr = inet_addr("192.168.1.1"); /* Ouch */
121     sa.sin_port = htons(5555);
122
123     if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0 ||
124         connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0 ||
125         getsockname(fd, (struct sockaddr*) &sa, &l) < 0) {
126         if (fd >= 0)
127             close(fd);
128
129         perror("fuck");
130         return SW_E_UNKNOWN;
131     }
132
133     assert(l == sizeof(sa));
134     close(fd);
135
136     self->m_addr = sa.sin_addr.s_addr;
137     
138     return SW_OKAY;
139 }
140
141 sw_result sw_ipv4_address_fina(sw_ipv4_address self) {
142
143     AVAHI_WARN_LINKAGE;
144
145     /* This is ridiculous ... */
146     
147     return SW_OKAY;
148 }
149
150 sw_bool sw_ipv4_address_is_any(sw_ipv4_address self) {
151     AVAHI_WARN_LINKAGE;
152     return self.m_addr == htonl(INADDR_ANY);
153 }
154
155 sw_saddr sw_ipv4_address_saddr(sw_ipv4_address self) {
156     AVAHI_WARN_LINKAGE;
157     return self.m_addr;
158 }
159
160 sw_string sw_ipv4_address_name(
161     sw_ipv4_address self,
162     sw_string name,
163     sw_uint32 len) {
164
165     assert(name);
166     assert(len > 0);
167
168     AVAHI_WARN_LINKAGE;
169
170     if (len < INET_ADDRSTRLEN)
171         return NULL;
172     
173     if (!(inet_ntop(AF_INET, &self.m_addr, name, len)))
174         return NULL;
175             
176     return name;
177 }
178
179 sw_result sw_ipv4_address_decompose(
180     sw_ipv4_address self,
181     sw_uint8 * a1,
182     sw_uint8 * a2,
183     sw_uint8 * a3,
184     sw_uint8 * a4) {
185
186     uint32_t a;
187
188     AVAHI_WARN_LINKAGE;
189     
190     a = ntohl(self.m_addr);
191     
192     assert(a1);
193     assert(a2);
194     assert(a3);
195     assert(a4);
196
197     *a1 = (uint8_t) (a >> 24);
198     *a2 = (uint8_t) (a >> 16);
199     *a3 = (uint8_t) (a >> 8);
200     *a4 = (uint8_t) (a);
201         
202     return SW_OKAY;
203 }
204
205 sw_bool sw_ipv4_address_equals(
206     sw_ipv4_address self,
207     sw_ipv4_address addr) {
208
209     AVAHI_WARN_LINKAGE;
210
211     return self.m_addr == addr.m_addr;
212 }
213