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 <avahi-common/gccmacro.h> 35 36#include "howl.h" 37#include "warn.h" 38 39sw_ipv4_address sw_ipv4_address_any(void) { 40 sw_ipv4_address a; 41 42 AVAHI_WARN_LINKAGE; 43 44 a.m_addr = htonl(INADDR_ANY); 45 return a; 46} 47 48sw_ipv4_address sw_ipv4_address_loopback(void) { 49 sw_ipv4_address a; 50 51 AVAHI_WARN_LINKAGE; 52 53 a.m_addr = htonl(INADDR_LOOPBACK); 54 return a; 55} 56 57sw_result sw_ipv4_address_init(sw_ipv4_address * self) { 58 assert(self); 59 60 AVAHI_WARN_LINKAGE; 61 62 self->m_addr = htonl(INADDR_ANY); 63 return SW_OKAY; 64} 65 66sw_result sw_ipv4_address_init_from_saddr( 67 sw_ipv4_address *self, 68 sw_saddr addr) { 69 70 assert(self); 71 72 AVAHI_WARN_LINKAGE; 73 74 self->m_addr = addr; 75 return SW_OKAY; 76} 77 78sw_result sw_ipv4_address_init_from_name( 79 sw_ipv4_address *self, 80 sw_const_string name) { 81 82 struct hostent *he; 83 84 assert(self); 85 assert(name); 86 87 AVAHI_WARN_LINKAGE; 88 89 if (!(he = gethostbyname(name))) 90 return SW_E_UNKNOWN; 91 92 self->m_addr = *(uint32_t*) he->h_addr; 93 return SW_OKAY; 94} 95 96sw_result sw_ipv4_address_init_from_address( 97 sw_ipv4_address *self, 98 sw_ipv4_address addr) { 99 100 assert(self); 101 102 AVAHI_WARN_LINKAGE; 103 104 self->m_addr = addr.m_addr; 105 return SW_OKAY; 106} 107 108sw_result sw_ipv4_address_init_from_this_host(sw_ipv4_address *self) { 109 struct sockaddr_in sa; 110 int fd; 111 socklen_t l = sizeof(sa); 112 113 assert(self); 114 115 AVAHI_WARN_LINKAGE; 116 117 /* This is so fucked up ... */ 118 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); 123 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) { 127 if (fd >= 0) 128 close(fd); 129 130 perror("fuck"); 131 return SW_E_UNKNOWN; 132 } 133 134 assert(l == sizeof(sa)); 135 close(fd); 136 137 self->m_addr = sa.sin_addr.s_addr; 138 139 return SW_OKAY; 140} 141 142sw_result sw_ipv4_address_fina(AVAHI_GCC_UNUSED sw_ipv4_address self) { 143 144 AVAHI_WARN_LINKAGE; 145 146 /* This is ridiculous ... */ 147 148 return SW_OKAY; 149} 150 151sw_bool sw_ipv4_address_is_any(sw_ipv4_address self) { 152 AVAHI_WARN_LINKAGE; 153 return self.m_addr == htonl(INADDR_ANY); 154} 155 156sw_saddr sw_ipv4_address_saddr(sw_ipv4_address self) { 157 AVAHI_WARN_LINKAGE; 158 return self.m_addr; 159} 160 161sw_string sw_ipv4_address_name( 162 sw_ipv4_address self, 163 sw_string name, 164 sw_uint32 len) { 165 166 assert(name); 167 assert(len > 0); 168 169 AVAHI_WARN_LINKAGE; 170 171 if (len < INET_ADDRSTRLEN) 172 return NULL; 173 174 if (!(inet_ntop(AF_INET, &self.m_addr, name, len))) 175 return NULL; 176 177 return name; 178} 179 180sw_result sw_ipv4_address_decompose( 181 sw_ipv4_address self, 182 sw_uint8 * a1, 183 sw_uint8 * a2, 184 sw_uint8 * a3, 185 sw_uint8 * a4) { 186 187 uint32_t a; 188 189 AVAHI_WARN_LINKAGE; 190 191 a = ntohl(self.m_addr); 192 193 assert(a1); 194 assert(a2); 195 assert(a3); 196 assert(a4); 197 198 *a1 = (uint8_t) (a >> 24); 199 *a2 = (uint8_t) (a >> 16); 200 *a3 = (uint8_t) (a >> 8); 201 *a4 = (uint8_t) (a); 202 203 return SW_OKAY; 204} 205 206sw_bool sw_ipv4_address_equals( 207 sw_ipv4_address self, 208 sw_ipv4_address addr) { 209 210 AVAHI_WARN_LINKAGE; 211 212 return self.m_addr == addr.m_addr; 213} 214 215