4 This file is part of avahi.
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.
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.
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
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <sys/types.h>
38 sw_ipv4_address sw_ipv4_address_any(void) {
43 a.m_addr = htonl(INADDR_ANY);
47 sw_ipv4_address sw_ipv4_address_loopback(void) {
52 a.m_addr = htonl(INADDR_LOOPBACK);
56 sw_result sw_ipv4_address_init(sw_ipv4_address * self) {
61 self->m_addr = htonl(INADDR_ANY);
65 sw_result sw_ipv4_address_init_from_saddr(
66 sw_ipv4_address *self,
77 sw_result sw_ipv4_address_init_from_name(
78 sw_ipv4_address *self,
79 sw_const_string name) {
88 if (!(he = gethostbyname(name)))
91 self->m_addr = *(uint32_t*) he->h_addr;
95 sw_result sw_ipv4_address_init_from_address(
96 sw_ipv4_address *self,
97 sw_ipv4_address addr) {
103 self->m_addr = addr.m_addr;
107 sw_result sw_ipv4_address_init_from_this_host(sw_ipv4_address *self) {
108 struct sockaddr_in sa;
110 socklen_t l = sizeof(sa);
116 /* This is so fucked up ... */
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);
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) {
133 assert(l == sizeof(sa));
136 self->m_addr = sa.sin_addr.s_addr;
141 sw_result sw_ipv4_address_fina(sw_ipv4_address self) {
145 /* This is ridiculous ... */
150 sw_bool sw_ipv4_address_is_any(sw_ipv4_address self) {
152 return self.m_addr == htonl(INADDR_ANY);
155 sw_saddr sw_ipv4_address_saddr(sw_ipv4_address self) {
160 sw_string sw_ipv4_address_name(
161 sw_ipv4_address self,
170 if (len < INET_ADDRSTRLEN)
173 if (!(inet_ntop(AF_INET, &self.m_addr, name, len)))
179 sw_result sw_ipv4_address_decompose(
180 sw_ipv4_address self,
190 a = ntohl(self.m_addr);
197 *a1 = (uint8_t) (a >> 24);
198 *a2 = (uint8_t) (a >> 16);
199 *a3 = (uint8_t) (a >> 8);
205 sw_bool sw_ipv4_address_equals(
206 sw_ipv4_address self,
207 sw_ipv4_address addr) {
211 return self.m_addr == addr.m_addr;