datagram source port?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

datagram source port?

Mario Lang
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,
  ⡍⠁⠗⠊⠕

Reply | Threaded
Open this post in threaded view
|

Re: datagram source port?

Robert Pluim
>>>>> On Thu, 19 Dec 2019 10:12:10 +0100, Mario Lang <[hidden email]> 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);

Reply | Threaded
Open this post in threaded view
|

Re: datagram source port?

Eli Zaretskii
> From: Robert Pluim <[hidden email]>
> Date: Thu, 19 Dec 2019 11:41:41 +0100
> Cc: [hidden email]
>
> 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.

Reply | Threaded
Open this post in threaded view
|

Re: datagram source port?

Robert Pluim
>>>>> On Thu, 19 Dec 2019 17:43:46 +0200, Eli Zaretskii <[hidden email]> said:

    >> From: Robert Pluim <[hidden email]>
    >> Date: Thu, 19 Dec 2019 11:41:41 +0100
    >> Cc: [hidden email]
    >>
    >> 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