unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* datagram source port?
@ 2019-12-19  9:12 Mario Lang
  2019-12-19 10:41 ` Robert Pluim
  0 siblings, 1 reply; 7+ messages in thread
From: Mario Lang @ 2019-12-19  9:12 UTC (permalink / raw)
  To: emacs-devel

Hi.

Is it possible to specify the source port for a datagram process?
I didn't find anything in the elisp Info section about processes.
And specifying :local [127 0 0 1 1234] didn't work either.

Right now, the port is choosen randomly.  That is not ideal for
certain UDP applications, which match on the source port at the
receiving end.

-- 
CYa,
  ⡍⠁⠗⠊⠕



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

* Re: datagram source port?
  2019-12-19  9:12 datagram source port? Mario Lang
@ 2019-12-19 10:41 ` Robert Pluim
  2019-12-19 15:43   ` Eli Zaretskii
  0 siblings, 1 reply; 7+ messages in thread
From: Robert Pluim @ 2019-12-19 10:41 UTC (permalink / raw)
  To: Mario Lang; +Cc: emacs-devel

>>>>> On Thu, 19 Dec 2019 10:12:10 +0100, Mario Lang <mlang@delysid.org> said:

    Mario> Hi.
    Mario> Is it possible to specify the source port for a datagram process?
    Mario> I didn't find anything in the elisp Info section about processes.
    Mario> And specifying :local [127 0 0 1 1234] didn't work either.

The docstring for make-network-process says:

    :local ADDRESS -- ADDRESS is the local address used for the connection.
    This parameter is ignored when opening a client process.

    Mario> Right now, the port is choosen randomly.  That is not ideal for
    Mario> certain UDP applications, which match on the source port at the
    Mario> receiving end.

That seems fragile. Anyway, given that we have
set-process-datagram-address, I donʼt see why we couldn't add
set-process-datagram-source-address as well. Eli, something like this?
Or we could add an optional parameter to set-process-datagram-address
to mean 'set source'.

(I realize weʼre close to cutting the emacs-27 branch, and it needs
documentation etc)

diff --git a/src/process.c b/src/process.c
index 0f82682ae5..60ccff698a 100644
--- a/src/process.c
+++ b/src/process.c
@@ -2756,6 +2756,43 @@ DEFUN ("process-datagram-address", Fprocess_datagram_address, Sprocess_datagram_
 				datagram_address[channel].len);
 }
 
+DEFUN ("set-process-datagram-source-address", Fset_process_datagram_source_address, Sset_process_datagram_source_address,
+       2, 2, 0,
+       doc: /* Set the datagram source address for PROCESS to ADDRESS.
+Return nil upon error setting address, ADDRESS otherwise.
+
+If PROCESS is a non-blocking network process that hasn't been fully
+set up yet, this function will block until socket setup has completed.  */)
+  (Lisp_Object process, Lisp_Object address)
+{
+  int channel;
+  int family;
+  ptrdiff_t len;
+
+  CHECK_PROCESS (process);
+
+  if (NETCONN_P (process))
+    wait_for_socket_fds (process, "set-process-datagram-source-address");
+
+  if (!DATAGRAM_CONN_P (process))
+    return Qnil;
+
+  channel = XPROCESS (process)->outfd;
+
+  len = get_lisp_to_sockaddr_size (address, &family);
+  if (len == 0 || datagram_address[channel].len != len)
+    return Qnil;
+
+  struct sockaddr sa;
+  conv_lisp_to_sockaddr (family, address, &sa, len);
+  if (bind (channel, &sa, len) != 0)
+    return Qnil;
+
+  Lisp_Object contact = XPROCESS (process)->childp;
+  XPROCESS (process)->childp = Fplist_put (contact, QClocal, address);
+  return address;
+}
+
 DEFUN ("set-process-datagram-address", Fset_process_datagram_address, Sset_process_datagram_address,
        2, 2, 0,
        doc: /* Set the datagram address for PROCESS to ADDRESS.
@@ -8487,6 +8524,7 @@ syms_of_process (void)
 #ifdef DATAGRAM_SOCKETS
   defsubr (&Sprocess_datagram_address);
   defsubr (&Sset_process_datagram_address);
+  defsubr (&Sset_process_datagram_source_address);
 #endif
   defsubr (&Saccept_process_output);
   defsubr (&Sprocess_send_region);



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

* Re: datagram source port?
  2019-12-19 10:41 ` Robert Pluim
@ 2019-12-19 15:43   ` Eli Zaretskii
  2019-12-19 17:07     ` Robert Pluim
  0 siblings, 1 reply; 7+ messages in thread
From: Eli Zaretskii @ 2019-12-19 15:43 UTC (permalink / raw)
  To: Robert Pluim; +Cc: mlang, emacs-devel

> From: Robert Pluim <rpluim@gmail.com>
> Date: Thu, 19 Dec 2019 11:41:41 +0100
> Cc: emacs-devel@gnu.org
> 
> That seems fragile. Anyway, given that we have
> set-process-datagram-address, I donʼt see why we couldn't add
> set-process-datagram-source-address as well. Eli, something like this?
> Or we could add an optional parameter to set-process-datagram-address
> to mean 'set source'.

The latter, IMO, because the code is very similar.

> (I realize weʼre close to cutting the emacs-27 branch, and it needs
> documentation etc)

Right.



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

* Re: datagram source port?
  2019-12-19 15:43   ` Eli Zaretskii
@ 2019-12-19 17:07     ` Robert Pluim
  2020-01-15  9:49       ` Robert Pluim
  0 siblings, 1 reply; 7+ messages in thread
From: Robert Pluim @ 2019-12-19 17:07 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: mlang, emacs-devel

>>>>> On Thu, 19 Dec 2019 17:43:46 +0200, Eli Zaretskii <eliz@gnu.org> said:

    >> From: Robert Pluim <rpluim@gmail.com>
    >> Date: Thu, 19 Dec 2019 11:41:41 +0100
    >> Cc: emacs-devel@gnu.org
    >> 
    >> That seems fragile. Anyway, given that we have
    >> set-process-datagram-address, I donʼt see why we couldn't add
    >> set-process-datagram-source-address as well. Eli, something like this?
    >> Or we could add an optional parameter to set-process-datagram-address
    >> to mean 'set source'.

    Eli> The latter, IMO, because the code is very similar.

OK. Iʼve fixed a very embarassing bug with IPv6, and need to test it
with a datagram process in server mode. Iʼve added it to my long list
for this weekend (but itʼs vacation time, which helps :-)

Robert



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

* Re: datagram source port?
  2019-12-19 17:07     ` Robert Pluim
@ 2020-01-15  9:49       ` Robert Pluim
  2020-01-15 16:27         ` Eli Zaretskii
  0 siblings, 1 reply; 7+ messages in thread
From: Robert Pluim @ 2020-01-15  9:49 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: mlang, emacs-devel

>>>>> On Thu, 19 Dec 2019 18:07:17 +0100, Robert Pluim <rpluim@gmail.com> said:

>>>>> On Thu, 19 Dec 2019 17:43:46 +0200, Eli Zaretskii <eliz@gnu.org> said:
    >>> From: Robert Pluim <rpluim@gmail.com>
    >>> Date: Thu, 19 Dec 2019 11:41:41 +0100
    >>> Cc: emacs-devel@gnu.org
    >>> 
    >>> That seems fragile. Anyway, given that we have
    >>> set-process-datagram-address, I donʼt see why we couldn't add
    >>> set-process-datagram-source-address as well. Eli, something like this?
    >>> Or we could add an optional parameter to set-process-datagram-address
    >>> to mean 'set source'.

    Eli> The latter, IMO, because the code is very similar.

    Robert> OK. Iʼve fixed a very embarassing bug with IPv6, and need to test it
    Robert> with a datagram process in server mode. Iʼve added it to my long list
    Robert> for this weekend (but itʼs vacation time, which helps :-)

I took another quick look at this, and noticed that the windows port
doesn't support datagram sockets at all, although the underlying
Winsock does. Is there a reason for that?

Robert



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

* Re: datagram source port?
  2020-01-15  9:49       ` Robert Pluim
@ 2020-01-15 16:27         ` Eli Zaretskii
  2020-01-16  8:04           ` Robert Pluim
  0 siblings, 1 reply; 7+ messages in thread
From: Eli Zaretskii @ 2020-01-15 16:27 UTC (permalink / raw)
  To: Robert Pluim; +Cc: mlang, emacs-devel

> From: Robert Pluim <rpluim@gmail.com>
> Cc: mlang@delysid.org,  emacs-devel@gnu.org
> Date: Wed, 15 Jan 2020 10:49:33 +0100
> 
> I took another quick look at this, and noticed that the windows port
> doesn't support datagram sockets at all, although the underlying
> Winsock does. Is there a reason for that?

This comment from nt/inc/ms-w32.h explains that:

  /* And the select implementation does 1-byte read-ahead waiting
     for received packets, so datagrams are broken too.  */
  #define BROKEN_DATAGRAM_SOCKETS 1

(Let me know if you want me to explain what it means by "1-byte
read-ahead".  Or just read the large commentary that starts around
line 870 of w32proc.c.)

I don't know enough about datagram sockets, but I hope you do, in
which case the above comment ought to be the answer.

Thanks.



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

* Re: datagram source port?
  2020-01-15 16:27         ` Eli Zaretskii
@ 2020-01-16  8:04           ` Robert Pluim
  0 siblings, 0 replies; 7+ messages in thread
From: Robert Pluim @ 2020-01-16  8:04 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: mlang, emacs-devel

>>>>> On Wed, 15 Jan 2020 18:27:38 +0200, Eli Zaretskii <eliz@gnu.org> said:

    >> From: Robert Pluim <rpluim@gmail.com>
    >> Cc: mlang@delysid.org,  emacs-devel@gnu.org
    >> Date: Wed, 15 Jan 2020 10:49:33 +0100
    >> 
    >> I took another quick look at this, and noticed that the windows port
    >> doesn't support datagram sockets at all, although the underlying
    >> Winsock does. Is there a reason for that?

    Eli> This comment from nt/inc/ms-w32.h explains that:

    Eli>   /* And the select implementation does 1-byte read-ahead waiting
    Eli>      for received packets, so datagrams are broken too.  */
    Eli>   #define BROKEN_DATAGRAM_SOCKETS 1

Eek.

    Eli> (Let me know if you want me to explain what it means by "1-byte
    Eli> read-ahead".  Or just read the large commentary that starts around
    Eli> line 870 of w32proc.c.)

    Eli> I don't know enough about datagram sockets, but I hope you do, in
    Eli> which case the above comment ought to be the answer.

Yes, itʼs the answer. Some brave soul could no doubt rewrite the w32
select implementation to fix this, but thatʼs not me.

Robert



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

end of thread, other threads:[~2020-01-16  8:04 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-19  9:12 datagram source port? Mario Lang
2019-12-19 10:41 ` Robert Pluim
2019-12-19 15:43   ` Eli Zaretskii
2019-12-19 17:07     ` Robert Pluim
2020-01-15  9:49       ` Robert Pluim
2020-01-15 16:27         ` Eli Zaretskii
2020-01-16  8:04           ` Robert Pluim

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).