From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Robert Pluim Newsgroups: gmane.emacs.devel Subject: Re: datagram source port? Date: Thu, 19 Dec 2019 11:41:41 +0100 Message-ID: References: <87mubollw5.fsf@blind.guru> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="146726"; mail-complaints-to="usenet@blaine.gmane.org" Cc: emacs-devel@gnu.org To: Mario Lang Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Dec 19 11:42:17 2019 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1ihtG5-000c4M-Fr for ged-emacs-devel@m.gmane.org; Thu, 19 Dec 2019 11:42:17 +0100 Original-Received: from localhost ([::1]:38484 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihtG4-0004Um-71 for ged-emacs-devel@m.gmane.org; Thu, 19 Dec 2019 05:42:16 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:36625) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihtFb-00045b-5Z for emacs-devel@gnu.org; Thu, 19 Dec 2019 05:41:48 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ihtFZ-0000Aj-OG for emacs-devel@gnu.org; Thu, 19 Dec 2019 05:41:46 -0500 Original-Received: from mail-wr1-x42d.google.com ([2a00:1450:4864:20::42d]:46817) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ihtFZ-0008VK-Gw for emacs-devel@gnu.org; Thu, 19 Dec 2019 05:41:45 -0500 Original-Received: by mail-wr1-x42d.google.com with SMTP id z7so5415148wrl.13 for ; Thu, 19 Dec 2019 02:41:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:references:gmane-reply-to-list:date:in-reply-to :message-id:mime-version:content-transfer-encoding; bh=7DNZVMbWR39tpJxK7anVEqn2Fe3VbcsRUXPR0s24T4I=; b=fU8BB8IsjFE4B6a1n8U+8mE5ASbpsLovhNPBs8HmGT4395IAAX87X44gAmr6sdMjxN 1heST6M8bdp9Z/DcME1cUhsuhn0AoBCz2GRDH1wUl3EfZoRC0GKh8/DXXSHki33PTICI aB1BLuJewuAtOlr55Iob23Yn/0c/sImYffwGiqVqAw5Ci8S5EH8E4hMJv/iindntYaNi 2UbenYQI9O2PKeqvCSFBS9c09GqCozCj6dHWR/+d1Y6Nxo6WUfYfl+ImMPlIsjIDYg7P GkeoI0LPckY5WlhvzdbN3TgqEIDcaOc5WsXK5hupF3dqZgfU2cZ036+ycqril+JtQ4SU 16ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:references :gmane-reply-to-list:date:in-reply-to:message-id:mime-version :content-transfer-encoding; bh=7DNZVMbWR39tpJxK7anVEqn2Fe3VbcsRUXPR0s24T4I=; b=qVJAzJ2dDNZMMnBrDjjYr/dsILY5HMwpPxMlA97WPskOdyEZdQuwXaJoRbhktWUKD1 E7DMAzbzqQXB+kCUXkycU+70iaHmppQcoOh9QK7aw+6E5v99aF+EmM111m2g549OmWwR 8LwBUfgjCgpEr/OWuo7vs22USZQ0vlT7MKzah/NsY4QFVHu7q7QM+j2yoLTRvNEjmpcL x7yFRR6VXzj5r/2dDLp6GKmdLIPzrWvTb3nCdBfrQqQVeFueiUdb1ep3W8wF+YAR9hbd 6sdk00MEdP2owDusvQH0nQfw0pRXLmEBG5pvUORzj8NkC0KM8UrrpYjzdzK+FipI7wI0 WUsQ== X-Gm-Message-State: APjAAAWPNV/cEtmx+ebmEhnLY6KyW58GjyDjyLKBbWOy8paQfhtHkP2I +DmgrhY1zR03rYculeB2kHSlBN8cq4I= X-Google-Smtp-Source: APXvYqxPPSW0fkTKwsWJ3c1F2mOnIQ8HpnprO+mnf8yLTkoN0JjmY2QHzdv46vDQ+inlcKQhgFYZSA== X-Received: by 2002:adf:fd91:: with SMTP id d17mr9083656wrr.340.1576752103517; Thu, 19 Dec 2019 02:41:43 -0800 (PST) Original-Received: from rpluim-mac ([149.5.228.1]) by smtp.gmail.com with ESMTPSA id 25sm5470538wmi.32.2019.12.19.02.41.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Dec 2019 02:41:42 -0800 (PST) Gmane-Reply-To-List: yes In-Reply-To: <87mubollw5.fsf@blind.guru> (Mario Lang's message of "Thu, 19 Dec 2019 10:12:10 +0100") X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::42d X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:243472 Archived-At: >>>>> On Thu, 19 Dec 2019 10:12:10 +0100, Mario Lang sa= id: 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=CA=BCt 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=CA=BCre 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); } =20 +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 =3D XPROCESS (process)->outfd; + + len =3D get_lisp_to_sockaddr_size (address, &family); + if (len =3D=3D 0 || datagram_address[channel].len !=3D len) + return Qnil; + + struct sockaddr sa; + conv_lisp_to_sockaddr (family, address, &sa, len); + if (bind (channel, &sa, len) !=3D 0) + return Qnil; + + Lisp_Object contact =3D XPROCESS (process)->childp; + XPROCESS (process)->childp =3D 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);