Re: [Toybox] [PATCH] netcat: make -l exit after handling a request

2017-07-21 Thread Rob Landley
On 07/19/2017 02:45 PM, Rob Landley wrote:
> On 07/18/2017 01:44 PM, Josh Gao wrote:
>> Sorry for pestering,
> 
> Pester away. (General rule is if I haven't gotten back to you in a week,
> it's my fault and I should be dinged.)

My other general rule is if I don't get to it a couple days _after_ such
poking, I should revert my local changes and apply your patch, then redo
my stuff on top of yours, and worry about any cleanup I want to do after
it's no longer a blocking issue for the submitter.

Which I've now done.

(Sorry, hip deep in $DAYJOB stuff at the moment...)

Rob
___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net


Re: [Toybox] [PATCH] netcat: make -l exit after handling a request

2017-07-19 Thread Rob Landley
On 07/18/2017 01:44 PM, Josh Gao wrote:
> Sorry for pestering,

Pester away. (General rule is if I haven't gotten back to you in a week,
it's my fault and I should be dinged.)

> but have you gotten a chance to take a look at
> these yet? We're starting to grow dependencies on some of these patches
> (tests that want to be checked in Soon are being written against a
> checkout with the patches manually applied)

I'll take a look tonight. Sorry for the delay.

Thanks,

Rob
___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net


Re: [Toybox] [PATCH] netcat: make -l exit after handling a request

2017-07-18 Thread Josh Gao
Sorry for pestering, but have you gotten a chance to take a look at these
yet? We're starting to grow dependencies on some of these patches (tests
that want to be checked in Soon are being written against a checkout with
the patches manually applied)

On Fri, Jun 30, 2017 at 12:17 PM, Josh Gao  wrote:

> Updated the original patch to just jump to cleanup, instead of rearranging
> things.
>
> Also, polished up the proof of concept xgetaddrinfo (this needs a better
> name)
> patch, and used it to implement -4, -6 (plus another patch to do -u).
>
> On Thu, Jun 29, 2017 at 6:59 PM, enh  wrote:
>
>> ping? various networking folks are looking to add tests that use
>> netcat, and i'd rather start them off on toybox netcat rather than BSD
>> and then have to move them across to toybox later. (obviously there's
>> other missing stuff in toybox, but these patches are the only things
>> they actually need right now.)
>>
>> On Wed, Jun 28, 2017 at 3:47 PM, Josh Gao  wrote:
>> > On Sun, Jun 25, 2017 at 12:14 PM, Rob Landley  wrote:
>> >>
>> >> 1) switching it to use xconnect() which it predates, and which is hard
>> >> because the various users in tree all want slighty different things out
>> >> of the getaddrinfo() plumbing and I've made a couple attempts to
>> >> unify/genericize it but keep getting pulled alway by $DAYJOB crisis du
>> >> jour halfway through and forgetting what design problem details I was
>> >> halfway through solving and have to start over again...
>> >
>> >
>> > BTW, I took a quick look at this because we have users that want -4/-6
>> (and
>> > IPv6 support in general). `nc -s` makes it so that you can't use
>> xconnect
>> > because you don't know what to bind to until after you've resolved the
>> > target
>> > address. Something like xbind_and_connect might work, but there's also
>> > things
>> > that we might want to do in between socket and bind (e.g. setting
>> > SO_REUSEADDR).
>> >
>> > The thing that everyone really wants is a way to iterate over
>> getaddrinfo
>> > results;
>> > maybe that's what should be exposed? I have a rough proof of concept
>> patch
>> > attached that implements this and uses it in netcat.
>> >
>> > (There's also another edge case with -s: what happens if the host you
>> pass
>> > in
>> > resolves to multiple addresses? OpenBSD's netcat seems to just bind the
>> > first
>> > compatible address it resolves to, so we can maybe just ignore this.)
>> >
>> > -Josh
>> >
>> > ___
>> > Toybox mailing list
>> > Toybox@lists.landley.net
>> > http://lists.landley.net/listinfo.cgi/toybox-landley.net
>> >
>>
>>
>>
>> --
>> Elliott Hughes - http://who/enh - http://jessies.org/~enh/
>> Android native code/tools questions? Mail me/drop by/add me as a reviewer.
>>
>
>
___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net


Re: [Toybox] [PATCH] netcat: make -l exit after handling a request

2017-06-30 Thread Josh Gao
Updated the original patch to just jump to cleanup, instead of rearranging
things.

Also, polished up the proof of concept xgetaddrinfo (this needs a better
name)
patch, and used it to implement -4, -6 (plus another patch to do -u).

On Thu, Jun 29, 2017 at 6:59 PM, enh  wrote:

> ping? various networking folks are looking to add tests that use
> netcat, and i'd rather start them off on toybox netcat rather than BSD
> and then have to move them across to toybox later. (obviously there's
> other missing stuff in toybox, but these patches are the only things
> they actually need right now.)
>
> On Wed, Jun 28, 2017 at 3:47 PM, Josh Gao  wrote:
> > On Sun, Jun 25, 2017 at 12:14 PM, Rob Landley  wrote:
> >>
> >> 1) switching it to use xconnect() which it predates, and which is hard
> >> because the various users in tree all want slighty different things out
> >> of the getaddrinfo() plumbing and I've made a couple attempts to
> >> unify/genericize it but keep getting pulled alway by $DAYJOB crisis du
> >> jour halfway through and forgetting what design problem details I was
> >> halfway through solving and have to start over again...
> >
> >
> > BTW, I took a quick look at this because we have users that want -4/-6
> (and
> > IPv6 support in general). `nc -s` makes it so that you can't use xconnect
> > because you don't know what to bind to until after you've resolved the
> > target
> > address. Something like xbind_and_connect might work, but there's also
> > things
> > that we might want to do in between socket and bind (e.g. setting
> > SO_REUSEADDR).
> >
> > The thing that everyone really wants is a way to iterate over getaddrinfo
> > results;
> > maybe that's what should be exposed? I have a rough proof of concept
> patch
> > attached that implements this and uses it in netcat.
> >
> > (There's also another edge case with -s: what happens if the host you
> pass
> > in
> > resolves to multiple addresses? OpenBSD's netcat seems to just bind the
> > first
> > compatible address it resolves to, so we can maybe just ignore this.)
> >
> > -Josh
> >
> > ___
> > Toybox mailing list
> > Toybox@lists.landley.net
> > http://lists.landley.net/listinfo.cgi/toybox-landley.net
> >
>
>
>
> --
> Elliott Hughes - http://who/enh - http://jessies.org/~enh/
> Android native code/tools questions? Mail me/drop by/add me as a reviewer.
>
From bebee0943874f77bb4a7ff7126ba13738e50a3e3 Mon Sep 17 00:00:00 2001
From: Josh Gao 
Date: Wed, 28 Jun 2017 15:41:56 -0700
Subject: [PATCH 1/4] Add xgetaddrinfo, use it in netcat.

Add a helper function that iterates over getaddrinfo results with a
callback, and switch netcat over to it to add support for IPv6.
---
 lib/lib.h |   2 +
 lib/net.c |  53 ++-
 toys/net/netcat.c | 191 ++
 3 files changed, 144 insertions(+), 102 deletions(-)

diff --git a/lib/lib.h b/lib/lib.h
index 0b93bde..7d12233 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -284,6 +284,8 @@ void tty_sigreset(int i);
 // net.c
 int xsocket(int domain, int type, int protocol);
 void xsetsockopt(int fd, int level, int opt, void *val, socklen_t len);
+int xgetaddrinfo(char *host, char *port, int family, int socktype, int protocol,
+ int flags, int (*cb)(struct addrinfo*, void*), void *cookie);
 int xconnect(char *host, char *port, int family, int socktype, int protocol,
   int flags);
 int xpoll(struct pollfd *fds, int nfds, int timeout);
diff --git a/lib/net.c b/lib/net.c
index df2551f..b4f5f65 100644
--- a/lib/net.c
+++ b/lib/net.c
@@ -15,36 +15,55 @@ void xsetsockopt(int fd, int level, int opt, void *val, socklen_t len)
   if (-1 == setsockopt(fd, level, opt, val, len)) perror_exit("setsockopt");
 }
 
-int xconnect(char *host, char *port, int family, int socktype, int protocol,
- int flags)
+int xgetaddrinfo(char *host, char *port, int family, int socktype, int protocol,
+ int flags, int (*cb)(struct addrinfo*, void*), void *cookie)
 {
-  struct addrinfo info, *ai, *ai2;
-  int fd;
+  int rc;
+  struct addrinfo info, *ai, *ai_head;
 
-  memset(&info, 0, sizeof(struct addrinfo));
+  memset(&info, 0, sizeof(info));
   info.ai_family = family;
   info.ai_socktype = socktype;
   info.ai_protocol = protocol;
   info.ai_flags = flags;
 
-  fd = getaddrinfo(host, port, &info, &ai);
-  if (fd || !ai)
-error_exit("Connect '%s%s%s': %s", host, port ? ":" : "", port ? port : "",
-  fd ? gai_strerror(fd) : "not found");
-
-  // Try all the returned addresses. Report errors if last entry can't connect.
-  for (ai2 = ai; ai; ai = ai->ai_next) {
-fd = (ai->ai_next ? socket : xsocket)(ai->ai_family, ai->ai_socktype,
-  ai->ai_protocol);
-if (!connect(fd, ai->ai_addr, ai->ai_addrlen)) break;
-else if (!ai->ai_next) perror_exit("connect");
+  rc = getaddrinfo(host, port, &info, &ai);
+  if (rc || !ai) {
+error_exit("getaddrinfo '%s%s%s': %s", host, port ? ":" 

Re: [Toybox] [PATCH] netcat: make -l exit after handling a request

2017-06-29 Thread enh
ping? various networking folks are looking to add tests that use
netcat, and i'd rather start them off on toybox netcat rather than BSD
and then have to move them across to toybox later. (obviously there's
other missing stuff in toybox, but these patches are the only things
they actually need right now.)

On Wed, Jun 28, 2017 at 3:47 PM, Josh Gao  wrote:
> On Sun, Jun 25, 2017 at 12:14 PM, Rob Landley  wrote:
>>
>> 1) switching it to use xconnect() which it predates, and which is hard
>> because the various users in tree all want slighty different things out
>> of the getaddrinfo() plumbing and I've made a couple attempts to
>> unify/genericize it but keep getting pulled alway by $DAYJOB crisis du
>> jour halfway through and forgetting what design problem details I was
>> halfway through solving and have to start over again...
>
>
> BTW, I took a quick look at this because we have users that want -4/-6 (and
> IPv6 support in general). `nc -s` makes it so that you can't use xconnect
> because you don't know what to bind to until after you've resolved the
> target
> address. Something like xbind_and_connect might work, but there's also
> things
> that we might want to do in between socket and bind (e.g. setting
> SO_REUSEADDR).
>
> The thing that everyone really wants is a way to iterate over getaddrinfo
> results;
> maybe that's what should be exposed? I have a rough proof of concept patch
> attached that implements this and uses it in netcat.
>
> (There's also another edge case with -s: what happens if the host you pass
> in
> resolves to multiple addresses? OpenBSD's netcat seems to just bind the
> first
> compatible address it resolves to, so we can maybe just ignore this.)
>
> -Josh
>
> ___
> Toybox mailing list
> Toybox@lists.landley.net
> http://lists.landley.net/listinfo.cgi/toybox-landley.net
>



-- 
Elliott Hughes - http://who/enh - http://jessies.org/~enh/
Android native code/tools questions? Mail me/drop by/add me as a reviewer.
___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net


Re: [Toybox] [PATCH] netcat: make -l exit after handling a request

2017-06-28 Thread Josh Gao
On Sun, Jun 25, 2017 at 12:14 PM, Rob Landley  wrote:
>
> 1) switching it to use xconnect() which it predates, and which is hard
> because the various users in tree all want slighty different things out
> of the getaddrinfo() plumbing and I've made a couple attempts to
> unify/genericize it but keep getting pulled alway by $DAYJOB crisis du
> jour halfway through and forgetting what design problem details I was
> halfway through solving and have to start over again...
>

BTW, I took a quick look at this because we have users that want -4/-6 (and
IPv6 support in general). `nc -s` makes it so that you can't use xconnect
because you don't know what to bind to until after you've resolved the
target
address. Something like xbind_and_connect might work, but there's also
things
that we might want to do in between socket and bind (e.g. setting
SO_REUSEADDR).

The thing that everyone really wants is a way to iterate over getaddrinfo
results;
maybe that's what should be exposed? I have a rough proof of concept patch
attached that implements this and uses it in netcat.

(There's also another edge case with -s: what happens if the host you pass
in
resolves to multiple addresses? OpenBSD's netcat seems to just bind the
first
compatible address it resolves to, so we can maybe just ignore this.)

-Josh
From cd3a377366ed25dcdb821559b39b8319b65eabf0 Mon Sep 17 00:00:00 2001
From: Josh Gao 
Date: Wed, 28 Jun 2017 15:41:56 -0700
Subject: [PATCH] Add xgetaddrinfo, use it in netcat.

Add a helper function that iterates over getaddrinfo results with a
callback, and switch netcat over to it to add support for IPv6.
---
 lib/lib.h |   2 +
 lib/net.c |  53 +-
 toys/net/netcat.c | 204 +-
 3 files changed, 148 insertions(+), 111 deletions(-)

diff --git a/lib/lib.h b/lib/lib.h
index 0b93bde..7d12233 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -284,6 +284,8 @@ void tty_sigreset(int i);
 // net.c
 int xsocket(int domain, int type, int protocol);
 void xsetsockopt(int fd, int level, int opt, void *val, socklen_t len);
+int xgetaddrinfo(char *host, char *port, int family, int socktype, int protocol,
+ int flags, int (*cb)(struct addrinfo*, void*), void *cookie);
 int xconnect(char *host, char *port, int family, int socktype, int protocol,
   int flags);
 int xpoll(struct pollfd *fds, int nfds, int timeout);
diff --git a/lib/net.c b/lib/net.c
index df2551f..b4f5f65 100644
--- a/lib/net.c
+++ b/lib/net.c
@@ -15,36 +15,55 @@ void xsetsockopt(int fd, int level, int opt, void *val, socklen_t len)
   if (-1 == setsockopt(fd, level, opt, val, len)) perror_exit("setsockopt");
 }
 
-int xconnect(char *host, char *port, int family, int socktype, int protocol,
- int flags)
+int xgetaddrinfo(char *host, char *port, int family, int socktype, int protocol,
+ int flags, int (*cb)(struct addrinfo*, void*), void *cookie)
 {
-  struct addrinfo info, *ai, *ai2;
-  int fd;
+  int rc;
+  struct addrinfo info, *ai, *ai_head;
 
-  memset(&info, 0, sizeof(struct addrinfo));
+  memset(&info, 0, sizeof(info));
   info.ai_family = family;
   info.ai_socktype = socktype;
   info.ai_protocol = protocol;
   info.ai_flags = flags;
 
-  fd = getaddrinfo(host, port, &info, &ai);
-  if (fd || !ai)
-error_exit("Connect '%s%s%s': %s", host, port ? ":" : "", port ? port : "",
-  fd ? gai_strerror(fd) : "not found");
-
-  // Try all the returned addresses. Report errors if last entry can't connect.
-  for (ai2 = ai; ai; ai = ai->ai_next) {
-fd = (ai->ai_next ? socket : xsocket)(ai->ai_family, ai->ai_socktype,
-  ai->ai_protocol);
-if (!connect(fd, ai->ai_addr, ai->ai_addrlen)) break;
-else if (!ai->ai_next) perror_exit("connect");
+  rc = getaddrinfo(host, port, &info, &ai);
+  if (rc || !ai) {
+error_exit("getaddrinfo '%s%s%s': %s", host, port ? ":" : "", port ? port : "",
+  rc ? gai_strerror(rc) : "not found");
+  }
+
+  for (ai_head = ai; ai; ai = ai->ai_next) {
+rc = cb(ai, cookie);
+if (rc != -1) break;
+  }
+  freeaddrinfo(ai_head);
+
+  return rc;
+}
+
+static int _xconnect(struct addrinfo* ai, void *unused) {
+  int fd;
+
+  fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
+  if (fd == -1) {
+return -1;
+  } else if (!connect(fd, ai->ai_addr, ai->ai_addrlen)) {
 close(fd);
+return -1;
   }
-  freeaddrinfo(ai2);
 
   return fd;
 }
 
+int xconnect(char *host, char *port, int family, int socktype, int protocol,
+ int flags)
+{
+  int rc = xgetaddrinfo(host, port, family, socktype, protocol, flags, _xconnect, 0);
+  if (rc == -1) perror_exit("connect");
+  return rc;
+}
+
 int xpoll(struct pollfd *fds, int nfds, int timeout)
 {
   int i;
diff --git a/toys/net/netcat.c b/toys/net/netcat.c
index 13ba311..285979f 100644
--- a/toys/net/netcat.c
+++ b/toys/net/netcat.c
@@ -5,7 +5,7 @@
  * TODO: udp, ipv6, genericize for telnet/microcom/tail-f
 
 USE_NETCAT(OLDTOY(nc, netcat, TOY

Re: [Toybox] [PATCH] netcat: make -l exit after handling a request

2017-06-26 Thread Josh Gao
On Sun, Jun 25, 2017 at 12:14 PM, Rob Landley  wrote:
>
> > terminal1$ echo foo | toybox nc -l -p 1234 > bar
> > terminal1$ cat bar
> > bar
>
> Your call there wouldn't work with the "stop after -l" version either,
> the -p would have to come before the -l. :P


Seems to work for me?


> > terminal2$ echo bar | toybox nc localhost 1234 > foo
> > terminal2$ cat foo
> > foo
>
> Um... yes? That's what it's supposed to do, and what it's doing without
> this patch?
>
> Your first hunk told it to run an instance of netcat in chat mode (no
> command to run, so instead stdin and stdout of netcat get forwarded to
> the connection). This will block until the connection is made, although
> if you're redirecting the input you can background it if you want to.
>
> The second hunk then made a connection. The "bar" you wrote to the
> second instance got written to the first instance's output, and the
> "foo" you wrote to the first instance got forwarded to the second
> instance's output. When each instance gets EOF from stdin it calls
> shutdown(fd) to let the other instance know, so both instances exit.
>
> I just built netcat from clean toybox, and it did that. I don't see the
> problem?
>

That's not what I'm seeing. With a freshly compiled tip of tree master
(279eb227), the listener doesn't ever exit:

# Terminal 1
jmgao@cyclops2:/android/upstream/toybox$ echo foo | ./toybox nc localhost
1234
foo
jmgao@cyclops2:/android/upstream/toybox$ echo bar | ./toybox nc localhost
1234
jmgao@cyclops2:/android/upstream/toybox$ echo baz | ./toybox nc localhost
1234
jmgao@cyclops2:/android/upstream/toybox$

# Terminal 2
jmgao@cyclops2:/android/upstream/toybox$ echo foo | ./toybox nc -l -p 1234
foo
bar
baz
^C
jmgao@cyclops2:/android/upstream/toybox$

`toybox nc -l -p 1234 echo foo` works fine because the process execs
without forking.
Using -l without a command will fall through to pollinate, finish, and then
try to listen again.
___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net


Re: [Toybox] [PATCH] netcat: make -l exit after handling a request

2017-06-25 Thread Rob Landley
On 06/09/2017 04:22 PM, Josh Gao wrote:
> On Fri, Jun 9, 2017 at 1:31 PM, Rob Landley  > wrote:
> 
> On 06/09/2017 01:05 PM, Josh Gao wrote:
> > Patch attached.
> 
> What specifically did you fix? (I.E. is this the same "netcat -L
> zombies" issue I have on my todo list?)
> 
> 
> This makes `nc -l` do what it says it does, and listen for exactly one
> connection before exiting, instead of handling connections until a ^C.

Ok, getting around to looking at this one (sorry for the delay).

I have a longish todo list for netcat, the oldest entry in which is that
commit f492fccc9ceb was sort of awkward, and I'm pretty sure I POSTED
about why it was awkward but the second half of
http://lists.landley.net/pipermail/toybox-landley.net/2015-January/date.html
is of course missing from the archive because Dreamhost is incompetent.
(I think there are now four holes in the archive because of them? I'd
have to check my notes. http://landley.net/dreamhost.txt and
http://landley.net/dreamhost2.txt have plenty more I haven't bothered to
put online because what's the point?)

Anyway, the problem I have with that commit is now this:

  netcat 127.0.0.1 80 -t

doesn't work. It treats -t as a third non-option argument because it hit
a non-option argument first and stopped parsing. But it ONLY does that
if you enable server mode! If you build without server mode, it works fine.)

Originally I had it so that hitting -l or -L would stop the option
parsing (there's an optstr "l^" syntax), but then somebody did (and I
quote from their email):

  netcat -l ls -p 1234

And they were surprised it didn't work. (Sigh. It still doesn't, but now
it doesn't work in the xargs way, which I assume more people understand?
Except xargs doesn't parse any non-option arguments _other_ than the
command line to run, and netcat more or less requires them in dialout
mode...)

Other netcat todo items include:

1) switching it to use xconnect() which it predates, and which is hard
because the various users in tree all want slighty different things out
of the getaddrinfo() plumbing and I've made a couple attempts to
unify/genericize it but keep getting pulled alway by $DAYJOB crisis du
jour halfway through and forgetting what design problem details I was
halfway through solving and have to start over again...

2) adding UDP mode so it can handle netconsole, and then deleting
tcpsvd.c out of pending (which was a third party submission for
functionality I'd always planned to have netcat implement).

3) converting netcat to use xvfork() so the fiddly sequencing in commit
4e867b8a3527 isn't necessary anymore. (See the comment "gcc's insane
optimizer can overwrite this", and the comment about liveness analysis
below that. I posted about this to the list back in October, and also
blogged about it but I fell behind editing my blog and restarted in
january so that entry's not actually uploaded; that gap isn't
dreamhost's fault it's me not having enough hours in the day since my
startup ran out of money last summer and those of us left took on extra
responsibilities to cover the lost headcount. It's on the todo list...)

The vfork() issue may actually be a musl-libc bug because built against
musl vfork() hasn't got attribute returns_twice and thus gcc is
"optimizing" stack usage next time it makes a function call, not
protecting local variable values it thinks it won't be reused, and thus
when the parent resumes it has garbled local variables unless you hit it
with a rock like that commit did. But XVFORK() uses a wrapper function
that _is_ annotated returns_twice and thus it should work reliably
whatever musl's headers do?

(Ok, it doesn't really, the vfork() call is an argument to the
returns_twice function, so vfork is called and the result is passed into
that function call, twice. But that should be close enough? Ok, I can
imagine a pathological "optimizer" moving the vfork() call up to the
start of the outer function, calling it once, and cacheing its value. I
wouldn't put that kind of lunacy past modern gcc. But given I can't
#undef a function prototype to redo it, and can't return from a function
after vfork, this is the closest I can come up with to make it work?
This is half the reason the linux kernel craps memory barriers all over
the place, to block gcc's optimizer from doing stupid reordering of
straightforward C...)

> terminal1$ echo foo | toybox nc -l -p 1234 > bar
> terminal1$ cat bar
> bar

Your call there wouldn't work with the "stop after -l" version either,
the -p would have to come before the -l. :P

> terminal2$ echo bar | toybox nc localhost 1234 > foo
> terminal2$ cat foo
> foo

Um... yes? That's what it's supposed to do, and what it's doing without
this patch?

Your first hunk told it to run an instance of netcat in chat mode (no
command to run, so instead stdin and stdout of netcat get forwarded to
the connection). This will block until the connection is made, although
if y

Re: [Toybox] [PATCH] netcat: make -l exit after handling a request

2017-06-09 Thread Josh Gao
On Fri, Jun 9, 2017 at 1:31 PM, Rob Landley  wrote:

> On 06/09/2017 01:05 PM, Josh Gao wrote:
> > Patch attached.
>
> What specifically did you fix? (I.E. is this the same "netcat -L
> zombies" issue I have on my todo list?)
>

This makes `nc -l` do what it says it does, and listen for exactly one
connection before exiting, instead of handling connections until a ^C.

terminal1$ echo foo | toybox nc -l -p 1234 > bar
terminal1$ cat bar
bar

terminal2$ echo bar | toybox nc localhost 1234 > foo
terminal2$ cat foo
foo

(Unrelated: is matching the BSD netcat's command line interface a goal? I
keep
accidentally doing `toybox nc -l 1234` and getting bitten by it.)

I'm -> <- this close to getting a release out, the only issue left after
> dmesg is fixing the help text generator, lemme do that, cut a release,
> and _then_ dive into this. :)
>

Sure, thanks :-)

-Josh
___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net


Re: [Toybox] [PATCH] netcat: make -l exit after handling a request

2017-06-09 Thread Rob Landley
On 06/09/2017 01:05 PM, Josh Gao wrote:
> Patch attached.

What specifically did you fix? (I.E. is this the same "netcat -L
zombies" issue I have on my todo list?)

Sigh, I have local netcat changes I need to merge this with, although
the most intrusive part of that is cleaning it up to use XVFORK() so it
doesn't have that weird liveness analysis workaround ala
http://lists.landley.net/pipermail/toybox-landley.net/2016-December/008796.html
which _might_ still be a musl bug with vfork not annotated
attribute(returns_twice)...

I'm -> <- this close to getting a release out, the only issue left after
dmesg is fixing the help text generator, lemme do that, cut a release,
and _then_ dive into this. :)

Thanks,

Rob
___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net


[Toybox] [PATCH] netcat: make -l exit after handling a request

2017-06-09 Thread Josh Gao
Patch attached.
From eceb0b80dcce8a0d20f05b4a47b2e1a69be9b9c9 Mon Sep 17 00:00:00 2001
From: Josh Gao 
Date: Fri, 9 Jun 2017 10:51:18 -0700
Subject: [PATCH] netcat: make -l exit after handling a request.

Bug: http://b/62305466
---
 toys/net/netcat.c | 17 -
 1 file changed, 8 insertions(+), 9 deletions(-)

diff --git a/toys/net/netcat.c b/toys/net/netcat.c
index 13ba311..795f7b3 100644
--- a/toys/net/netcat.c
+++ b/toys/net/netcat.c
@@ -126,11 +126,15 @@ void netcat_main(void)
 // TODO xconnect
   if (connect(sockfd, (struct sockaddr *)address, sizeof(*address))<0)
 perror_exit("connect");
+
+  // We have a connection. Disarm timeout.
+  set_alarm(0);
+
   in1 = out2 = sockfd;
 
-// Listen for incoming connections
-
+  pollinate(in1, in2, out1, out2, TT.idle, TT.quit_delay);
 } else {
+  // Listen for incoming connections
   socklen_t len = sizeof(*address);
 
   if (listen(sockfd, 5)) error_exit("listen");
@@ -143,7 +147,7 @@ void netcat_main(void)
 if (CFG_TOYBOX_FORK && toys.optc && xfork()) goto cleanup;
   }
 
-  for (;;) {
+  do {
 child = 0;
 len = sizeof(*address); // gcc's insane optimizer can overwrite this
 in1 = out2 = accept(sockfd, (struct sockaddr *)address, &len);
@@ -189,15 +193,10 @@ void netcat_main(void)
 
 pollinate(in1, in2, out1, out2, TT.idle, TT.quit_delay);
 close(in1);
-  }
+  } while (!(toys.optflags&FLAG_l));
 }
   }
 
-  // We have a connection. Disarm timeout.
-  set_alarm(0);
-
-  pollinate(in1, in2, out1, out2, TT.idle, TT.quit_delay);
-
 cleanup:
   if (CFG_TOYBOX_FREE) {
 close(in1);
-- 
2.13.0.506.g27d5fe0cd-goog

___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net