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>
37 sw_ipv4_address sw_ipv4_address_any(void) {
42 a.m_addr = htonl(INADDR_ANY);
46 sw_ipv4_address sw_ipv4_address_loopback(void) {
51 a.m_addr = htonl(INADDR_LOOPBACK);
55 sw_result sw_ipv4_address_init(sw_ipv4_address * self) {
60 self->m_addr = htonl(INADDR_ANY);
64 sw_result sw_ipv4_address_init_from_saddr(
65 sw_ipv4_address *self,
76 sw_result sw_ipv4_address_init_from_name(
77 sw_ipv4_address *self,
78 sw_const_string name) {
87 if (!(he = gethostbyname(name)))
90 self->m_addr = *(uint32_t*) he->h_addr;
94 sw_result sw_ipv4_address_init_from_address(
95 sw_ipv4_address *self,
96 sw_ipv4_address addr) {
102 self->m_addr = addr.m_addr;
106 sw_result sw_ipv4_address_init_from_this_host(sw_ipv4_address *self) {
107 struct sockaddr_in sa;
109 socklen_t l = sizeof(sa);
115 /* This is so fucked up ... */
117 memset(&sa, 0, sizeof(sa));
118 sa.sin_family = AF_INET;
119 sa.sin_addr.s_addr = inet_addr("192.168.1.1"); /* Ouch */
120 sa.sin_port = htons(5555);
122 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0 ||
123 connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0 ||
124 getsockname(fd, (struct sockaddr*) &sa, &l) < 0) {
132 assert(l == sizeof(sa));
135 self->m_addr = sa.sin_addr.s_addr;
140 sw_result sw_ipv4_address_fina(sw_ipv4_address self) {
144 /* This is ridiculous ... */
149 sw_bool sw_ipv4_address_is_any(sw_ipv4_address self) {
151 return self.m_addr == htonl(INADDR_ANY);
154 sw_saddr sw_ipv4_address_saddr(sw_ipv4_address self) {
159 sw_string sw_ipv4_address_name(
160 sw_ipv4_address self,
169 if (len < INET_ADDRSTRLEN)
172 if (!(inet_ntop(AF_INET, &self.m_addr, name, len)))
178 sw_result sw_ipv4_address_decompose(
179 sw_ipv4_address self,
189 a = ntohl(self.m_addr);
196 *a1 = (uint8_t) (a >> 24);
197 *a2 = (uint8_t) (a >> 16);
198 *a3 = (uint8_t) (a >> 8);
204 sw_bool sw_ipv4_address_equals(
205 sw_ipv4_address self,
206 sw_ipv4_address addr) {
210 return self.m_addr == addr.m_addr;