Thursday 29 December 2016

Re: Using the dummy0 interface for a local-only service to be broadcasted by Avahi

On Thu, Dec 29, 2016 at 04:38:05PM -0200, Till Kamppeter wrote:
> On 12/29/2016 04:31 PM, Stéphane Graber wrote:
> > On Thu, Dec 29, 2016 at 04:14:52PM -0200, Till Kamppeter wrote:
> > > On 12/29/2016 02:37 PM, Stéphane Graber wrote:
> > > > > How can I assign a different name to a dummy interface? Can I freely choose
> > > > > a name somehow, for example "ippusbxd"? Or have I to use "dummy1", "dummy2",
> > > > > ... (loading the dummy kernel module with an option to support more than one
> > > > > interface)?
> > > >
> > > > root@castiana:~# ip link add ippusbxd type dummy
> > > > root@castiana:~# ip link set ippusbxd up
> > > > root@castiana:~# ifconfig ippusbxd
> > > > ippusbxd: flags=195<UP,BROADCAST,RUNNING,NOARP> mtu 1500
> > > > inet6 fe80::3004:2dff:feb6:b5c7 prefixlen 64 scopeid 0x20<link>
> > > > ether 32:04:2d:b6:b5:c7 txqueuelen 1000 (Ethernet)
> > > > RX packets 0 bytes 0 (0.0 B)
> > > > RX errors 0 dropped 0 overruns 0 frame 0
> > > > TX packets 2 bytes 140 (140.0 B)
> > > > TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
> > > >
> > > >
> > > > Which gets you your own dummy device with its IPv6 link-local address.
> > >
> > > Thank you very much. I copied and pasted the commands and got an ifconfig
> > > output similar to yours, only with different IP and MAC addresses and
> > > different values in the statistics.
> > >
> > > Then I tried to bind to the IPv6 IP address of this entry, on port 60000 and
> > > this did not work.
> > >
> > > Do I have to create an additional IP address? If yes, how? Do I have to run
> > > additional commands (route?)? Which ones?
> > >
> > > Till
> >
> > Link-local addresses are slightly special in that they are indeed link local.
> >
> > So you can't bind fe80::3004:2dff:feb6:b5c7 as you could in theory have
> > the same address on multiple interfaces. Instead, you need to tell
> > bind() what interface to bind on. This is typically indicated as
> > fe80::3004:2dff:feb6:b5c7%ippusbxd.
> >
> >
> > For example:
> >
> > stgraber@castiana:~$ nc -l fe80::3004:2dff:feb6:b5c7 1234
> > nc: Invalid argument
> >
> > ^ Fails because the kernel doesn't know what interface you want.
> >
> > stgraber@castiana:~$ nc -l fe80::3004:2dff:feb6:b5c7%ippusbxd 1234
> >
> > ^ Works
> >
>
> Thank you. I want to bind with the bind(2) function in C. How do I supply
> the interface here or what function do I need to call instead?
>
> Till

#include <arpa/inet.h>
#include <net/if.h>

int main(int argc, char *argv[])
{
int s;
struct sockaddr_in6 ip6;

// Basic inet6 socket
s = socket(AF_INET6, SOCK_DGRAM, 0);

// Initialize the ip6 struct
ip6.sin6_family=AF_INET6;
ip6.sin6_addr=in6addr_any;
ip6.sin6_port=htons(1234);
ip6.sin6_scope_id=if_nametoindex("ippusbxd");
inet_pton(AF_INET6, "fe80::3004:2dff:feb6:b5c7", (void *)&ip6.sin6_addr.s6_addr);

// Bind
bind(s, (struct sockaddr *)&ip6, sizeof(struct sockaddr_in6));
}


--
Stéphane Graber
Ubuntu developer
http://www.ubuntu.com