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>
34 #include <avahi-common/gccmacro.h>
39 sw_ipv4_address sw_ipv4_address_any(void) {
44 a.m_addr = htonl(INADDR_ANY);
48 sw_ipv4_address sw_ipv4_address_loopback(void) {
53 a.m_addr = htonl(INADDR_LOOPBACK);
57 sw_result sw_ipv4_address_init(sw_ipv4_address * self) {
62 self->m_addr = htonl(INADDR_ANY);
66 sw_result sw_ipv4_address_init_from_saddr(
67 sw_ipv4_address *self,
78 sw_result sw_ipv4_address_init_from_name(
79 sw_ipv4_address *self,
80 sw_const_string name) {
89 if (!(he = gethostbyname(name)))
92 self->m_addr = *(uint32_t*) he->h_addr;
96 sw_result sw_ipv4_address_init_from_address(
97 sw_ipv4_address *self,
98 sw_ipv4_address addr) {
104 self->m_addr = addr.m_addr;
108 sw_result sw_ipv4_address_init_from_this_host(sw_ipv4_address *self) {
109 struct sockaddr_in sa;
111 socklen_t l = sizeof(sa);
117 /* This is so fucked up ... */
119 memset(&sa, 0, sizeof(sa));
120 sa.sin_family = AF_INET;
121 sa.sin_addr.s_addr = inet_addr("192.168.1.1"); /* Ouch */
122 sa.sin_port = htons(5555);
124 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0 ||
125 connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0 ||
126 getsockname(fd, (struct sockaddr*) &sa, &l) < 0) {
134 assert(l == sizeof(sa));
137 self->m_addr = sa.sin_addr.s_addr;
142 sw_result sw_ipv4_address_fina(AVAHI_GCC_UNUSED sw_ipv4_address self) {
146 /* This is ridiculous ... */
151 sw_bool sw_ipv4_address_is_any(sw_ipv4_address self) {
153 return self.m_addr == htonl(INADDR_ANY);
156 sw_saddr sw_ipv4_address_saddr(sw_ipv4_address self) {
161 sw_string sw_ipv4_address_name(
162 sw_ipv4_address self,
171 if (len < INET_ADDRSTRLEN)
174 if (!(inet_ntop(AF_INET, &self.m_addr, name, len)))
180 sw_result sw_ipv4_address_decompose(
181 sw_ipv4_address self,
191 a = ntohl(self.m_addr);
198 *a1 = (uint8_t) (a >> 24);
199 *a2 = (uint8_t) (a >> 16);
200 *a3 = (uint8_t) (a >> 8);
206 sw_bool sw_ipv4_address_equals(
207 sw_ipv4_address self,
208 sw_ipv4_address addr) {
212 return self.m_addr == addr.m_addr;