K2.6 patches and update.
[tomato.git] / release / src / router / minidlna / getifaddr.c
blob55443d5a88b0e0767d3f993bc5a19336885c99b7
1 /* MiniUPnP project
2 * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
4 * Copyright (c) 2006, Thomas Bernard
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <sys/ioctl.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <net/if.h>
37 #include <arpa/inet.h>
38 #include <netinet/in.h>
39 #include <netdb.h>
40 #include <errno.h>
41 #if defined(sun)
42 #include <sys/sockio.h>
43 #endif
45 #include "getifaddr.h"
46 #include "log.h"
48 int
49 getifaddr(const char * ifname, char * buf, int len)
51 /* SIOCGIFADDR struct ifreq * */
52 int s;
53 struct ifreq ifr;
54 int ifrlen;
55 struct sockaddr_in * addr;
56 uint32_t mask;
57 int i;
59 ifrlen = sizeof(ifr);
60 s = socket(PF_INET, SOCK_DGRAM, 0);
61 if(s < 0)
63 DPRINTF(E_ERROR, L_GENERAL, "socket(PF_INET, SOCK_DGRAM): %s\n", strerror(errno));
64 return -1;
66 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
67 if(ioctl(s, SIOCGIFADDR, &ifr, &ifrlen) < 0)
69 DPRINTF(E_ERROR, L_GENERAL, "ioctl(s, SIOCGIFADDR, ...): %s\n", strerror(errno));
70 close(s);
71 return -1;
73 addr = (struct sockaddr_in *)&ifr.ifr_addr;
74 if(!inet_ntop(AF_INET, &addr->sin_addr, buf, len))
76 DPRINTF(E_ERROR, L_GENERAL, "inet_ntop(): %s\n", strerror(errno));
77 close(s);
78 return -1;
80 if(ioctl(s, SIOCGIFNETMASK, &ifr, &ifrlen) == 0)
82 addr = (struct sockaddr_in *)&ifr.ifr_netmask;
83 mask = ntohl(addr->sin_addr.s_addr);
84 for (i = 0; i < 32; i++)
86 if ((mask >> i) & 1)
87 break;
89 mask = 32 - i;
90 if (mask)
92 i = strlen(buf);
93 snprintf(buf+i, len-i, "/%u", mask);
96 else
97 DPRINTF(E_ERROR, L_GENERAL, "ioctl(s, SIOCGIFNETMASK, ...): %s\n", strerror(errno));
98 close(s);
99 return 0;
103 getsysaddr(char * buf, int len)
105 int i;
106 int s = socket(PF_INET, SOCK_STREAM, 0);
107 struct sockaddr_in addr;
108 struct ifreq ifr;
109 uint32_t mask;
110 int ret = -1;
112 for (i=1; i > 0; i++)
114 ifr.ifr_ifindex = i;
115 if( ioctl(s, SIOCGIFNAME, &ifr) < 0 )
116 break;
117 if(ioctl(s, SIOCGIFADDR, &ifr, sizeof(struct ifreq)) < 0)
118 continue;
119 memcpy(&addr, &ifr.ifr_addr, sizeof(addr));
120 if(strncmp(inet_ntoa(addr.sin_addr), "127.", 4) == 0)
121 continue;
122 if(ioctl(s, SIOCGIFNETMASK, &ifr, sizeof(struct ifreq)) < 0)
123 continue;
124 if(!inet_ntop(AF_INET, &addr.sin_addr, buf, len))
126 DPRINTF(E_ERROR, L_GENERAL, "inet_ntop(): %s\n", strerror(errno));
127 close(s);
128 break;
130 ret = 0;
132 memcpy(&addr, &ifr.ifr_netmask, sizeof(addr));
133 mask = ntohl(addr.sin_addr.s_addr);
134 for (i = 0; i < 32; i++)
136 if ((mask >> i) & 1)
137 break;
139 mask = 32 - i;
140 if (mask)
142 i = strlen(buf);
143 snprintf(buf+i, len-i, "/%u", mask);
145 break;
147 close(s);
149 return(ret);
153 getsyshwaddr(char * buf, int len)
155 struct if_nameindex *ifaces, *if_idx;
156 unsigned char mac[6];
157 struct ifreq ifr;
158 int fd;
159 int ret = -1;
161 memset(&mac, '\0', sizeof(mac));
162 /* Get the spatially unique node identifier */
163 fd = socket(AF_INET, SOCK_DGRAM, 0);
164 if( fd < 0 )
165 return(ret);
167 ifaces = if_nameindex();
168 if(!ifaces)
169 return(ret);
171 for(if_idx = ifaces; if_idx->if_index; if_idx++)
173 strncpy(ifr.ifr_name, if_idx->if_name, IFNAMSIZ);
174 if(ioctl(fd, SIOCGIFFLAGS, &ifr) < 0)
175 continue;
176 if(ifr.ifr_ifru.ifru_flags & IFF_LOOPBACK)
177 continue;
178 if( ioctl(fd, SIOCGIFHWADDR, &ifr) < 0 )
179 continue;
180 if( MACADDR_IS_ZERO(ifr.ifr_hwaddr.sa_data) )
181 continue;
182 ret = 0;
183 break;
185 if_freenameindex(ifaces);
186 close(fd);
188 if(ret == 0)
190 if(len > 12)
192 memmove(mac, ifr.ifr_hwaddr.sa_data, 6);
193 sprintf(buf, "%02x%02x%02x%02x%02x%02x",
194 mac[0]&0xFF, mac[1]&0xFF, mac[2]&0xFF,
195 mac[3]&0xFF, mac[4]&0xFF, mac[5]&0xFF);
197 else if(len == 6)
199 memmove(buf, ifr.ifr_hwaddr.sa_data, 6);
202 return ret;
206 get_remote_mac(struct in_addr ip_addr, unsigned char * mac)
208 struct in_addr arp_ent;
209 FILE * arp;
210 char remote_ip[16];
211 int matches, hwtype, flags;
212 memset(mac, 0xFF, 6);
214 arp = fopen("/proc/net/arp", "r");
215 if( !arp )
216 return 1;
217 while( !feof(arp) )
219 matches = fscanf(arp, "%15s 0x%8X 0x%8X %2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx",
220 remote_ip, &hwtype, &flags,
221 &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]);
222 if( matches != 9 )
223 continue;
224 inet_pton(AF_INET, remote_ip, &arp_ent);
225 if( ip_addr.s_addr == arp_ent.s_addr )
226 break;
227 mac[0] = 0xFF;
229 fclose(arp);
231 if( mac[0] == 0xFF )
233 memset(mac, 0xFF, 6);
234 return 1;
237 return 0;