9front - general discussion about 9front
 help / color / mirror / Atom feed
* [9front] ape/bsd: Enable setting the local address with bind
@ 2022-01-04 20:00 Benjamin Riefenstahl
  2022-01-21 14:22 ` [9front] " Benjamin Riefenstahl
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Benjamin Riefenstahl @ 2022-01-04 20:00 UTC (permalink / raw)
  To: 9front

[-- Attachment #1: Type: text/plain, Size: 819 bytes --]

The current code ignores the address as passed to the BSD socket
function bind().  It seems that that may not have been possible with
earlier versions of the net API?  I discovered by reading the code and
experimentation that this seems to work fine in the current net API, so
this implements it for BSD sockets.

I removed some previous code that did not make sense to me, so you may
want to check that I did not screw up.  What I mean is "bind *" in
bind() and about "bind 0" in listen().

The handling when ports < 0 are specified is not consistent yet.  Either
we should normalize the ports to be at least 0, or we should return an
error condition for ports < 0.

The documentation in the man page ip(3) for the control commands "bind"
and "announce" does not mention the local address, should that be fixed,
too?


[-- Attachment #2.1: Type: text/plain, Size: 316 bytes --]

from postmaster@4ess:
The following attachment had content that we can't
prove to be harmless.  To avoid possible automatic
execution, we changed the content headers.
The original header was:

	Content-Type: text/x-diff
	Content-Disposition: inline;
 filename=0004-ape-bsd-fix-listening-to-individual-addresses.patch

[-- Attachment #2.2: 0004-ape-bsd-fix-listening-to-individual-addresses.patch.suspect --]
[-- Type: application/octet-stream, Size: 3570 bytes --]

From: Benjamin Riefenstahl <b.riefenstahl@turtle-trading.net>
Date: Sat, 25 Dec 2021 14:03:00 +0000
Subject: [PATCH] ape/bsd: fix listening to individual addresses


The former code does not bind to an address, just to a port.  The net
interface actually supports binding to addresses, so implement that in
the BSD socket code.

Also: Do not issue "bind *", that does not mean what you think it
means.
---
diff f96fae96efebf3eb7917e2c0b55439db2b242f79 4648260a1e4915035b09b502fd81d6716b7f77c0
--- a/sys/src/ape/lib/bsd/_sock_ingetaddr.c	Sat Dec 25 14:59:59 2021
+++ b/sys/src/ape/lib/bsd/_sock_ingetaddr.c	Sat Dec 25 15:03:00 2021
@@ -40,6 +40,29 @@
 }
 
 int
+_sock_inisany(int af, void *addr)
+{
+	int alen;
+	void *any;
+	static struct in_addr inaddr_any;
+
+	switch(af){
+	case AF_INET:
+		alen = sizeof inaddr_any.s_addr;
+		any = &inaddr_any;
+		break;
+	case AF_INET6:
+		alen = sizeof in6addr_any;
+		any = &in6addr_any;
+		break;
+	default:
+		return 0;
+	}
+
+	return 0 == memcmp(addr, any, alen);
+}
+
+int
 _sock_inaddr(int af, char *ip, char *port, void *a, int *alen)
 {
 	int len;
@@ -96,4 +119,24 @@
 		}
 		close(fd);
 	}
+}
+
+char *
+_sock_inaddr2string(Rock *r, char *dest, int dlen)
+{
+	int af = r->domain;
+	void *addr = _sock_inip(&r->addr);
+	int port = _sock_inport(&r->addr);
+	char *d = dest;
+	char *dend = dest+dlen;
+
+	if(!_sock_inisany(af, addr)){
+		inet_ntop(af, addr, d, dlen-1);
+		d = memchr(d, 0, dlen-1);
+		*(d++) = '!';
+	}
+
+	snprintf(d, dend-d, "%d", port);
+
+	return dest;
 }
--- a/sys/src/ape/lib/bsd/bind.c	Sat Dec 25 14:59:59 2021
+++ b/sys/src/ape/lib/bsd/bind.c	Sat Dec 25 15:03:00 2021
@@ -24,7 +24,7 @@
 int
 bind(int fd, void *a, int alen)
 {
-	int n, len, cfd, port;
+	int n, len, cfd;
 	struct sockaddr *sa;
 	Rock *r;
 	char msg[128];
@@ -55,20 +55,23 @@
 		errno = EBADF;
 		return -1;
 	}
-	port = _sock_inport(&r->addr);
-	if(port > 0)
-		snprintf(msg, sizeof msg, "bind %d", port);
-	else
-		strcpy(msg, "bind *");
+
+	strcpy(msg, "bind ");
+	_sock_inaddr2string(r, msg + 5, sizeof msg - 5);
+
 	n = write(cfd, msg, strlen(msg));
 	if(n < 0){
 		errno = EOPNOTSUPP;	/* Improve error reporting!!! */
 		close(cfd);
 		return -1;
 	}
+
 	close(cfd);
-	if(port <= 0)
+
+	if(_sock_inport(&r->addr) <= 0)
 		_sock_ingetaddr(r, &r->addr, 0, "local");
 
 	return 0;
 }
+
+
--- a/sys/src/ape/lib/bsd/listen.c	Sat Dec 25 14:59:59 2021
+++ b/sys/src/ape/lib/bsd/listen.c	Sat Dec 25 15:03:00 2021
@@ -121,7 +121,7 @@
 	int backlog;
 {
 	Rock *r;
-	int n, cfd, port;
+	int n, cfd;
 	char msg[128];
 	struct sockaddr_un *lunix;
 
@@ -139,17 +139,9 @@
 			errno = EBADF;
 			return -1;
 		}
-		port = _sock_inport(&r->addr);
-		if(port >= 0) {
-			if(write(cfd, "bind 0", 6) < 0) {
-				errno = EGREG;
-				close(cfd);
-				return -1;
-			}
-			snprintf(msg, sizeof msg, "announce %d", port);
-		}
-		else
-			strcpy(msg, "announce *");
+		strcpy(msg, "announce ");
+		_sock_inaddr2string(r, msg + 9, sizeof msg - 9);
+
 		n = write(cfd, msg, strlen(msg));
 		if(n < 0){
 			errno = EOPNOTSUPP;	/* Improve error reporting!!! */
--- a/sys/src/ape/lib/bsd/priv.h	Sat Dec 25 14:59:59 2021
+++ b/sys/src/ape/lib/bsd/priv.h	Sat Dec 25 15:03:00 2021
@@ -43,5 +43,7 @@
 extern int	_sock_ipattr(char*);
 extern void*	_sock_inip(struct sockaddr*);
 extern int	_sock_inport(struct sockaddr*);
+extern int	_sock_inisany(int af, void *addr);
 extern int	_sock_inaddr(int, char*, char*, void*, int*);
 extern void	_sock_ingetaddr(Rock*, void*, int*, char*);
+extern char *	_sock_inaddr2string(Rock *r, char *dest, int dlen);

-- 
1.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [9front] Re: ape/bsd: Enable setting the local address with bind
  2022-01-04 20:00 [9front] ape/bsd: Enable setting the local address with bind Benjamin Riefenstahl
@ 2022-01-21 14:22 ` Benjamin Riefenstahl
  2022-01-21 19:22   ` ori
  2022-02-10 17:38 ` Benjamin Riefenstahl
  2022-04-16 17:33 ` [9front] " ori
  2 siblings, 1 reply; 6+ messages in thread
From: Benjamin Riefenstahl @ 2022-01-21 14:22 UTC (permalink / raw)
  To: 9front

Ping?

Nobody interested?


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9front] Re: ape/bsd: Enable setting the local address with bind
  2022-01-21 14:22 ` [9front] " Benjamin Riefenstahl
@ 2022-01-21 19:22   ` ori
  0 siblings, 0 replies; 6+ messages in thread
From: ori @ 2022-01-21 19:22 UTC (permalink / raw)
  To: 9front

Quoth Benjamin Riefenstahl <b.riefenstahl@turtle-trading.net>:
> Ping?
> 
> Nobody interested?
> 

Thanks for the ping. I'll look at it
this weekend.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [9front] Re: ape/bsd: Enable setting the local address with bind
  2022-01-04 20:00 [9front] ape/bsd: Enable setting the local address with bind Benjamin Riefenstahl
  2022-01-21 14:22 ` [9front] " Benjamin Riefenstahl
@ 2022-02-10 17:38 ` Benjamin Riefenstahl
  2022-04-16 17:33 ` [9front] " ori
  2 siblings, 0 replies; 6+ messages in thread
From: Benjamin Riefenstahl @ 2022-02-10 17:38 UTC (permalink / raw)
  To: 9front

Hi all,

This is still open.  Ori, will you have time to review it?  I appreciate
that it is probably the most complicated of my patches.

In contrast there is also still open "Re: ape/mkstemp: Set define (was:
Include <bsd.h>" for a more simple change ;-)

Thanks,
benny

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9front] ape/bsd: Enable setting the local address with bind
  2022-01-04 20:00 [9front] ape/bsd: Enable setting the local address with bind Benjamin Riefenstahl
  2022-01-21 14:22 ` [9front] " Benjamin Riefenstahl
  2022-02-10 17:38 ` Benjamin Riefenstahl
@ 2022-04-16 17:33 ` ori
  2022-04-20 15:35   ` Benjamin Riefenstahl
  2 siblings, 1 reply; 6+ messages in thread
From: ori @ 2022-04-16 17:33 UTC (permalink / raw)
  To: 9front

Apologies for taking so long to respond. Life got busy, and
I forgot.

Quoth Benjamin Riefenstahl <b.riefenstahl@turtle-trading.net>:
> The current code ignores the address as passed to the BSD socket
> function bind().  It seems that that may not have been possible with
> earlier versions of the net API?  I discovered by reading the code and
> experimentation that this seems to work fine in the current net API, so
> this implements it for BSD sockets.
> 
> I removed some previous code that did not make sense to me, so you may
> want to check that I did not screw up.  What I mean is "bind *" in
> bind() and about "bind 0" in listen().
>
> The handling when ports < 0 are specified is not consistent yet.  Either
> we should normalize the ports to be at least 0, or we should return an
> error condition for ports < 0.

This looks like it's attempting (incorrectly?) to implement INADDR_ANY,
binding to any ip address. However, INADDR_ANY is 0:

	/sys/include/ape/netinet/in.h:98:
		#define	INADDR_ANY		(unsigned long)0x00000000

And I see no specification that describes what should be done with
negative ports in the sockets world, so I can only assume that this
is a bug. I don't think 'bind *' should be removed, since it seems
to me that it works as intended for binding to all addresses, but
the ape userspace for it is wrong.
 
> The documentation in the man page ip(3) for the control commands "bind"
> and "announce" does not mention the local address, should that be fixed,
> too?

Yes, that would be wonderful.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9front] ape/bsd: Enable setting the local address with bind
  2022-04-16 17:33 ` [9front] " ori
@ 2022-04-20 15:35   ` Benjamin Riefenstahl
  0 siblings, 0 replies; 6+ messages in thread
From: Benjamin Riefenstahl @ 2022-04-20 15:35 UTC (permalink / raw)
  To: ori; +Cc: 9front

Hi Ori,


Ori writes:
> Apologies for taking so long to respond. Life got busy, and I forgot.

No problem.  I often take my time myself ;-) Thanks for looking at it
now.  Note that this is some time ago for me now, too, so I may remember
wrongly how and why I did things and I will have to redo the tests.

> This looks like it's attempting (incorrectly?) to implement
> INADDR_ANY, binding to any ip address. However, INADDR_ANY is 0:
>
> 	/sys/include/ape/netinet/in.h:98:
> 		#define	INADDR_ANY		(unsigned long)0x00000000

As far as I can tell, using IP address INADDR_ANY and using port 0 to
indicate that the IP stack should choose the actual number both work
with the native control commands, so they do not need much special care
here.

> And I see no specification that describes what should be done with
> negative ports in the sockets world, so I can only assume that this is
> a bug.

I/we should probably just make negative ports an error. 

> I don't think 'bind *' should be removed, since it seems to me that it
> works as intended for binding to all addresses, but the ape userspace
> for it is wrong.

When I tried this a long time ago, when this was called it just returned
errors.  INADDR_ANY just works, I think, so there does not seem to be a
reason for this special handling.

I will have to create some test cases to see what works and what
doesn't.

>> The documentation in the man page ip(3) for the control commands
>> "bind" and "announce" does not mention the local address, should that
>> be fixed, too?

> Yes, that would be wonderful.

Another one for the todo list then  ;-)


Thanks, benny

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-04-20 15:36 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-04 20:00 [9front] ape/bsd: Enable setting the local address with bind Benjamin Riefenstahl
2022-01-21 14:22 ` [9front] " Benjamin Riefenstahl
2022-01-21 19:22   ` ori
2022-02-10 17:38 ` Benjamin Riefenstahl
2022-04-16 17:33 ` [9front] " ori
2022-04-20 15:35   ` Benjamin Riefenstahl

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).