[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: datagram source port?

From: Robert Pluim
Subject: Re: datagram source port?
Date: Thu, 19 Dec 2019 11:41:41 +0100

>>>>> On Thu, 19 Dec 2019 10:12:10 +0100, Mario Lang <address@hidden> 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_
+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, 
        2, 2, 0,
        doc: /* Set the datagram address for PROCESS to ADDRESS.
@@ -8487,6 +8524,7 @@ syms_of_process (void)
   defsubr (&Sprocess_datagram_address);
   defsubr (&Sset_process_datagram_address);
+  defsubr (&Sset_process_datagram_source_address);
   defsubr (&Saccept_process_output);
   defsubr (&Sprocess_send_region);

reply via email to

[Prev in Thread] Current Thread [Next in Thread]