diff mbox

[OpenWrt-Devel,libubox,v2] Support broadcast sockets in usock

Message ID 1443794038-2856-1-git-send-email-javierdo1@gmail.com
State Changes Requested
Headers show

Commit Message

Javier Domingo Cansino Oct. 2, 2015, 1:53 p.m. UTC
Signed-off-by: Javier Domingo Cansino <javierdo1@gmail.com>
---
 usock.c | 4 +++-
 usock.h | 1 +
 2 files changed, 4 insertions(+), 1 deletion(-)

Comments

John Crispin Oct. 26, 2015, 2:16 p.m. UTC | #1
Hi,

was about to merge this but then ... see inline

On 02/10/2015 15:53, Javier Domingo Cansino wrote:
> Signed-off-by: Javier Domingo Cansino <javierdo1@gmail.com>
> ---
>  usock.c | 4 +++-
>  usock.h | 1 +
>  2 files changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/usock.c b/usock.c
> index 6748ef3..3dd9957 100644
> --- a/usock.c
> +++ b/usock.c
> @@ -42,6 +42,7 @@ static void usock_set_flags(int sock, unsigned int type)
>  
>  static int usock_connect(int type, struct sockaddr *sa, int sa_len, int family, int socktype, bool server)
>  {
> +	const int one = 1;
>  	int sock;
>  
>  	sock = socket(family, socktype, 0);
> @@ -49,9 +50,10 @@ static int usock_connect(int type, struct sockaddr *sa, int sa_len, int family,
>  		return -1;
>  
>  	usock_set_flags(sock, type);
> +	if (socktype != SOCK_STREAM && type & USOCK_BROADCAST)

should it not be

if (socktype == SOCK_DGRAM .... ?

	John

> +		setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one));
>  
>  	if (server) {
> -		const int one = 1;
>  		setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
>  
>  		if (!bind(sock, sa, sa_len) &&
> diff --git a/usock.h b/usock.h
> index b0b952b..3471f2a 100644
> --- a/usock.h
> +++ b/usock.h
> @@ -26,6 +26,7 @@
>  #define USOCK_NOCLOEXEC		0x0200
>  #define USOCK_NONBLOCK		0x0400
>  #define USOCK_NUMERIC		0x0800
> +#define USOCK_BROADCAST		0x1000
>  #define USOCK_IPV6ONLY		0x2000
>  #define USOCK_IPV4ONLY		0x4000
>  #define USOCK_UNIX		0x8000
>
Javier Domingo Cansino Oct. 26, 2015, 2:30 p.m. UTC | #2
On Mon, Oct 26, 2015 at 2:16 PM John Crispin <blogic@openwrt.org> wrote:

> Hi,
>
> was about to merge this but then ... see inline
>
> On 02/10/2015 15:53, Javier Domingo Cansino wrote:
> > Signed-off-by: Javier Domingo Cansino <javierdo1@gmail.com>
> > ---
> >  usock.c | 4 +++-
> >  usock.h | 1 +
> >  2 files changed, 4 insertions(+), 1 deletion(-)
> >
> > diff --git a/usock.c b/usock.c
> > index 6748ef3..3dd9957 100644
> > --- a/usock.c
> > +++ b/usock.c
> > @@ -42,6 +42,7 @@ static void usock_set_flags(int sock, unsigned int
> type)
> >
> >  static int usock_connect(int type, struct sockaddr *sa, int sa_len, int
> family, int socktype, bool server)
> >  {
> > +     const int one = 1;
> >       int sock;
> >
> >       sock = socket(family, socktype, 0);
> > @@ -49,9 +50,10 @@ static int usock_connect(int type, struct sockaddr
> *sa, int sa_len, int family,
> >               return -1;
> >
> >       usock_set_flags(sock, type);
> > +     if (socktype != SOCK_STREAM && type & USOCK_BROADCAST)
>
> should it not be
>
> if (socktype == SOCK_DGRAM .... ?
>
>         John
>

It is more clear with your expression, they have the same effect anyway.

>
> > +             setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &one,
> sizeof(one));
> >
> >       if (server) {
> > -             const int one = 1;
> >               setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one,
> sizeof(one));
> >
> >               if (!bind(sock, sa, sa_len) &&
> > diff --git a/usock.h b/usock.h
> > index b0b952b..3471f2a 100644
> > --- a/usock.h
> > +++ b/usock.h
> > @@ -26,6 +26,7 @@
> >  #define USOCK_NOCLOEXEC              0x0200
> >  #define USOCK_NONBLOCK               0x0400
> >  #define USOCK_NUMERIC                0x0800
> > +#define USOCK_BROADCAST              0x1000
> >  #define USOCK_IPV6ONLY               0x2000
> >  #define USOCK_IPV4ONLY               0x4000
> >  #define USOCK_UNIX           0x8000
> >
>
diff mbox

Patch

diff --git a/usock.c b/usock.c
index 6748ef3..3dd9957 100644
--- a/usock.c
+++ b/usock.c
@@ -42,6 +42,7 @@  static void usock_set_flags(int sock, unsigned int type)
 
 static int usock_connect(int type, struct sockaddr *sa, int sa_len, int family, int socktype, bool server)
 {
+	const int one = 1;
 	int sock;
 
 	sock = socket(family, socktype, 0);
@@ -49,9 +50,10 @@  static int usock_connect(int type, struct sockaddr *sa, int sa_len, int family,
 		return -1;
 
 	usock_set_flags(sock, type);
+	if (socktype != SOCK_STREAM && type & USOCK_BROADCAST)
+		setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one));
 
 	if (server) {
-		const int one = 1;
 		setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
 
 		if (!bind(sock, sa, sa_len) &&
diff --git a/usock.h b/usock.h
index b0b952b..3471f2a 100644
--- a/usock.h
+++ b/usock.h
@@ -26,6 +26,7 @@ 
 #define USOCK_NOCLOEXEC		0x0200
 #define USOCK_NONBLOCK		0x0400
 #define USOCK_NUMERIC		0x0800
+#define USOCK_BROADCAST		0x1000
 #define USOCK_IPV6ONLY		0x2000
 #define USOCK_IPV4ONLY		0x4000
 #define USOCK_UNIX		0x8000