all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Robert Pluim <rpluim@gmail.com>
To: Mario Lang <mlang@delysid.org>
Cc: emacs-devel@gnu.org
Subject: Re: datagram source port?
Date: Thu, 19 Dec 2019 11:41:41 +0100	[thread overview]
Message-ID: <m24kxwbnru.fsf@gmail.com> (raw)
In-Reply-To: <87mubollw5.fsf@blind.guru> (Mario Lang's message of "Thu, 19 Dec 2019 10:12:10 +0100")

>>>>> 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);



  reply	other threads:[~2019-12-19 10:41 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-19  9:12 datagram source port? Mario Lang
2019-12-19 10:41 ` Robert Pluim [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m24kxwbnru.fsf@gmail.com \
    --to=rpluim@gmail.com \
    --cc=emacs-devel@gnu.org \
    --cc=mlang@delysid.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.