Line
Link Here
|
0 |
-- ez-ipupdate.c |
0 |
++ ez-ipupdate.c |
Lines 175-180
Link Here
|
175 |
# ifdef HAVE_SYS_SOCKIO_H |
175 |
# ifdef HAVE_SYS_SOCKIO_H |
176 |
# include <sys/sockio.h> |
176 |
# include <sys/sockio.h> |
177 |
# endif |
177 |
# endif |
|
|
178 |
# ifdef __linux__ |
179 |
# include <linux/if.h> |
180 |
# include <linux/netlink.h> |
181 |
# include <linux/rtnetlink.h> |
182 |
# endif |
178 |
#endif |
183 |
#endif |
179 |
|
184 |
|
180 |
#include <dprintf.h> |
185 |
#include <dprintf.h> |
Lines 1382-1387
Link Here
|
1382 |
} |
1387 |
} |
1383 |
|
1388 |
|
1384 |
/* |
1389 |
/* |
|
|
1390 |
* socketbind |
1391 |
* |
1392 |
* open a socket and bind() it |
1393 |
*/ |
1394 |
void socketbind(int *sock) |
1395 |
{ |
1396 |
#ifdef __linux__ |
1397 |
static struct sockaddr_nl local; |
1398 |
*sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); |
1399 |
if(*sock < 0) { |
1400 |
perror("socket"); |
1401 |
exit(1); |
1402 |
} |
1403 |
local.nl_family = AF_NETLINK; |
1404 |
local.nl_pad = 0; |
1405 |
local.nl_pid = getpid(); |
1406 |
local.nl_groups = 0; |
1407 |
if(bind(*sock, (struct sockaddr*) &local, sizeof(local)) < 0) { |
1408 |
perror("bind"); |
1409 |
exit(1); |
1410 |
} |
1411 |
#else |
1412 |
*sock = socket(AF_INET, SOCK_STREAM, 0); |
1413 |
#endif |
1414 |
} |
1415 |
|
1416 |
/* |
1385 |
* do_connect |
1417 |
* do_connect |
1386 |
* |
1418 |
* |
1387 |
* connect a socket and return the file descriptor |
1419 |
* connect a socket and return the file descriptor |
Lines 1612-1617
Link Here
|
1612 |
int get_if_addr(int sock, char *name, struct sockaddr_in *sin) |
1644 |
int get_if_addr(int sock, char *name, struct sockaddr_in *sin) |
1613 |
{ |
1645 |
{ |
1614 |
#ifdef IF_LOOKUP |
1646 |
#ifdef IF_LOOKUP |
|
|
1647 |
#ifdef __linux__ |
1648 |
struct { |
1649 |
struct nlmsghdr nlmsg_info; |
1650 |
struct ifaddrmsg ifaddrmsg_info; |
1651 |
char buffer[2048]; |
1652 |
} req; |
1653 |
struct nlmsghdr *curr; |
1654 |
int len; |
1655 |
char buf[8192]; |
1656 |
|
1657 |
memset(&req, 0, sizeof(req)); |
1658 |
req.nlmsg_info.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); |
1659 |
req.nlmsg_info.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP; |
1660 |
req.nlmsg_info.nlmsg_type = RTM_GETADDR; |
1661 |
req.nlmsg_info.nlmsg_pid = getpid(); |
1662 |
req.ifaddrmsg_info.ifa_family = AF_INET; |
1663 |
if(send(sock, &req, req.nlmsg_info.nlmsg_len, 0) < 0) { |
1664 |
perror("sendmsg(sock)"); |
1665 |
return -1; |
1666 |
} |
1667 |
|
1668 |
len = recv(sock, buf, sizeof(buf), 0); |
1669 |
if(len < 0) { |
1670 |
perror("recv"); |
1671 |
return -1; |
1672 |
} else if(len == 0) { |
1673 |
dprintf((stderr, "No interfaces found")); |
1674 |
return -1; |
1675 |
} |
1676 |
|
1677 |
/* Initialize sin except for address */ |
1678 |
bzero(sin, sizeof(struct sockaddr_in)); |
1679 |
sin->sin_family = AF_INET; |
1680 |
|
1681 |
/* We take the last non-private IP with matching name */ |
1682 |
int found = 0; |
1683 |
curr = (struct nlmsghdr *) buf; |
1684 |
for(; NLMSG_OK(curr, len); curr = NLMSG_NEXT(curr, len)) { |
1685 |
struct ifaddrmsg *curraddr = (struct ifaddrmsg *) NLMSG_DATA(curr); |
1686 |
struct rtattr *datalist = (struct rtattr *) IFA_RTA(curraddr); |
1687 |
int datalen = IFA_PAYLOAD(curr); |
1688 |
int mystat = 0; |
1689 |
struct in_addr sin_addr; |
1690 |
in_addr_t addr; |
1691 |
for(; RTA_OK(datalist, datalen); datalist = RTA_NEXT(datalist, datalen)) { |
1692 |
switch(datalist->rta_type) { |
1693 |
case IFA_LABEL: |
1694 |
if(strcmp((char *)RTA_DATA(datalist), name) != 0) |
1695 |
mystat = -1; |
1696 |
break; |
1697 |
case IFA_ADDRESS: |
1698 |
addr = ((struct in_addr *)RTA_DATA(datalist))->s_addr; |
1699 |
/* addr: 192.168.0.0/16 || 172.16.0.0/12 || 10.0.0.0/8 */ |
1700 |
if(((addr & 0xFFFF) == 0xA8C0) |
1701 |
|| ((addr & 0xF0FF) == 0x10AC) |
1702 |
|| ((addr & 0xFF) == 0x0A)) { |
1703 |
mystat = -1; |
1704 |
} |
1705 |
else { |
1706 |
/* We must not store yet sin->sin_addr, since name might not match */ |
1707 |
sin_addr = *((struct in_addr *)RTA_DATA(datalist)); |
1708 |
mystat = 1; |
1709 |
} |
1710 |
break; |
1711 |
default: |
1712 |
break; |
1713 |
} |
1714 |
if(mystat < 0) |
1715 |
break; |
1716 |
} |
1717 |
if(mystat > 0) { |
1718 |
sin->sin_addr = sin_addr; |
1719 |
found = 1; |
1720 |
/* If you want to take the first non-private IP with matching name |
1721 |
uncomment the next break command: |
1722 |
break; */ |
1723 |
} |
1724 |
} |
1725 |
if(found) { |
1726 |
dprintf((stderr, "%s: %s\n", name, inet_ntoa(sin->sin_addr))); |
1727 |
return 0; |
1728 |
} |
1729 |
dprintf((stderr, "%s: %s\n", name, "has no non-private address")); |
1730 |
return -1; |
1731 |
#else |
1732 |
/* ifndef __linux__ */ |
1615 |
struct ifreq ifr; |
1733 |
struct ifreq ifr; |
1616 |
|
1734 |
|
1617 |
memset(&ifr, 0, sizeof(ifr)); |
1735 |
memset(&ifr, 0, sizeof(ifr)); |
Lines 1645-1651
Link Here
|
1645 |
return -1; |
1763 |
return -1; |
1646 |
} |
1764 |
} |
1647 |
return -1; |
1765 |
return -1; |
|
|
1766 |
#endif |
1767 |
/* endif __linux__ */ |
1648 |
#else |
1768 |
#else |
|
|
1769 |
/* ifndef IF_LOOKUP */ |
1649 |
return -1; |
1770 |
return -1; |
1650 |
#endif |
1771 |
#endif |
1651 |
} |
1772 |
} |
Lines 4497-4503
Link Here
|
4497 |
#ifdef IF_LOOKUP |
4618 |
#ifdef IF_LOOKUP |
4498 |
if(options & OPT_DAEMON) |
4619 |
if(options & OPT_DAEMON) |
4499 |
{ |
4620 |
{ |
4500 |
sock = socket(AF_INET, SOCK_STREAM, 0); |
4621 |
socketbind(&sock); |
4501 |
} |
4622 |
} |
4502 |
#endif |
4623 |
#endif |
4503 |
|
4624 |
|
Lines 4754-4760
Link Here
|
4754 |
struct sockaddr_in sin; |
4875 |
struct sockaddr_in sin; |
4755 |
int sock; |
4876 |
int sock; |
4756 |
|
4877 |
|
4757 |
sock = socket(AF_INET, SOCK_STREAM, 0); |
4878 |
socketbind(&sock); |
4758 |
if(get_if_addr(sock, interface, &sin) != 0) |
4879 |
if(get_if_addr(sock, interface, &sin) != 0) |
4759 |
{ |
4880 |
{ |
4760 |
exit(1); |
4881 |
exit(1); |
Lines 4800-4806
Link Here
|
4800 |
struct sockaddr_in sin; |
4921 |
struct sockaddr_in sin; |
4801 |
int sock; |
4922 |
int sock; |
4802 |
|
4923 |
|
4803 |
sock = socket(AF_INET, SOCK_STREAM, 0); |
4924 |
socketbind(&sock); |
4804 |
if(get_if_addr(sock, interface, &sin) == 0) |
4925 |
if(get_if_addr(sock, interface, &sin) == 0) |
4805 |
{ |
4926 |
{ |
4806 |
if(address) { free(address); } |
4927 |
if(address) { free(address); } |
Lines 4855-4861
Link Here
|
4855 |
struct sockaddr_in sin; |
4976 |
struct sockaddr_in sin; |
4856 |
int sock; |
4977 |
int sock; |
4857 |
|
4978 |
|
4858 |
sock = socket(AF_INET, SOCK_STREAM, 0); |
4979 |
socketbind(&sock); |
4859 |
if(get_if_addr(sock, interface, &sin) != 0) |
4980 |
if(get_if_addr(sock, interface, &sin) != 0) |
4860 |
{ |
4981 |
{ |
4861 |
exit(1); |
4982 |
exit(1); |