How to set a specified IPv6 address to ff_bind() without error?
markman76 opened this issue · comments
NOH Myungkyu commented
Hello.
I want to assign IPv6 address to ff_bind(), but I see the error as "ff_bind failed, errno:99, Cannot assign requested address".
Please answer to solve this issue.
- Source (UDP Server)
struct sockaddr_in6 local_addr6;
bzero(&local_addr6, sizeof(local_addr6));
local_addr6.sin6_family = AF_INET6;
local_addr6.sin6_port = htons(8888);
int ret = inet_pton(AF_INET6, "fe80::fd98:f6de:1d5f:6c33", (void *)&local_addr6.sin6_addr);
ret = ff_bind(sockfd6, (struct linux_sockaddr *)&local_addr6, sizeof(local_addr6));
if (ret < 0) {
printf("ERROR: sockfd6=%d, ff_bind failed, errno:%d, %s \n", sockfd6, errno, strerror(errno));
exit(1);
}
- Config file (config.ini)
[port0]
addr=172.16.1.1
netmask=255.255.0.0
broadcast=172.16.255.255
gateway=172.16.0.1
addr6=fe80::fd98:f6de:1d5f:6c33
prefix_len=64
gateway6=fe80::78fb:808e:c4d4:f61c
NOH Myungkyu commented
I found out the setting IPv6 address (config.ini and binding to socket) and IPv6 address of ifa_link struct are different.
freebsd/net/if.c
1756 static struct ifaddr *
1757 ifa_ifwithaddr_internal(const struct sockaddr *addr, int getref)
1758 {
1759 struct ifnet *ifp;
1760 struct ifaddr *ifa;
1761
1762 IFNET_RLOCK_NOSLEEP();
1763 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1764 IF_ADDR_RLOCK(ifp);
1765 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1766 if (ifa->ifa_addr->sa_family != addr->sa_family)
1767 continue;
1768 if (sa_equal(addr, ifa->ifa_addr)) { // Two IP address different
1769 if (getref)
1770 ifa_ref(ifa);
1771 IF_ADDR_RUNLOCK(ifp);
1772 goto done;
1773 }
1774 /* IP6 doesn't have broadcast */
1775 if ((ifp->if_flags & IFF_BROADCAST) &&
1776 ifa->ifa_broadaddr &&
1777 ifa->ifa_broadaddr->sa_len != 0 &&
1778 sa_equal(ifa->ifa_broadaddr, addr)) {
1779 if (getref)
1780 ifa_ref(ifa);
1781 IF_ADDR_RUNLOCK(ifp);
1782 goto done;
1783 }
1784 }
1785 IF_ADDR_RUNLOCK(ifp);
1786 }
1787 ifa = NULL;
1788 done:
1789 IFNET_RUNLOCK_NOSLEEP();
1790 return (ifa);
1791 }
- addr is fe80::fd98:f6de:1d5f:6c33
- ifa->ifa_addr is fe80:2::fd98:f6de:1d5f:6c33