Skip to content

Commit 11b6f89

Browse files
funmancmassiot
authored andcommitted
udp: use SO_BINDTODEVICE regardless of multicast
It can be useful for bidirectional UDP sockets
1 parent 4d389f9 commit 11b6f89

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

lib/upipe-modules/upipe_udp.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,21 @@ int upipe_udp_open_socket(struct upipe *upipe, const char *_uri, int ttl,
609609
if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void *) &i, sizeof(i)))
610610
upipe_warn(upipe, "fail to increase receive buffer");
611611

612+
#ifdef SO_BINDTODEVICE
613+
if (ifname) {
614+
/* linux specific, needs root or CAP_NET_RAW */
615+
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
616+
ifname, strlen(ifname) + 1) < 0) {
617+
upipe_err_va(upipe, "couldn't bind to device %s (%m)",
618+
ifname);
619+
free(ifname);
620+
close(fd);
621+
return -1;
622+
}
623+
ubase_clean_str(&ifname);
624+
}
625+
#endif
626+
612627
/* Join the multicast group if the socket is a multicast address */
613628
if (bind_addr.ss.ss_family == AF_INET
614629
&& IN_MULTICAST(ntohl(bind_addr.sin.sin_addr.s_addr))) {
@@ -663,20 +678,6 @@ int upipe_udp_open_socket(struct upipe *upipe, const char *_uri, int ttl,
663678
return -1;
664679
}
665680
}
666-
#ifdef SO_BINDTODEVICE
667-
if (ifname) {
668-
/* linux specific, needs root or CAP_NET_RAW */
669-
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
670-
ifname, strlen(ifname) + 1) < 0) {
671-
upipe_err_va(upipe, "couldn't bind to device %s (%m)",
672-
ifname);
673-
free(ifname);
674-
close(fd);
675-
return -1;
676-
}
677-
ubase_clean_str(&ifname);
678-
}
679-
#endif
680681
}
681682
}
682683
}

0 commit comments

Comments
 (0)