delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2013/03/07/08:48:21

X-Recipient: archive-cygwin AT delorie DOT com
X-SWARE-Spam-Status: No, hits=-4.4 required=5.0 tests=AWL,BAYES_00,DKIM_SIGNED,DKIM_VALID,FREEMAIL_FROM,KHOP_RCVD_TRUST,KHOP_THREADED,RCVD_IN_DNSWL_LOW,RCVD_IN_HOSTKARMA_YE,TW_RV
X-Spam-Check-By: sourceware.org
MIME-Version: 1.0
X-Received: by 10.220.239.71 with SMTP id kv7mr12905473vcb.46.1362664080252; Thu, 07 Mar 2013 05:48:00 -0800 (PST)
In-Reply-To: <20130304093836.GB5468@calimero.vinschen.de>
References: <CANjopZEy2_yzpPpmxFtqBC_jhHNPyh7MHdeCY-QrizRoYM1_7Q AT mail DOT gmail DOT com> <20130304093836 DOT GB5468 AT calimero DOT vinschen DOT de>
Date: Thu, 7 Mar 2013 22:48:00 +0900
Message-ID: <CANjopZHUZ-Azy+n5P1ZkNJuTb3c3zfj93bGz-pHfWra2aXTfRA@mail.gmail.com>
Subject: Re: Unix domain accept() and getperrname() doesn't return the client address.
From: Tanaka Akira <akr AT fsij DOT org>
To: cygwin AT cygwin DOT com
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT cygwin DOT com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

2013/3/4 Corinna Vinschen:

> It's not exactly intentional, but known.  The socket's peername is not
> transmitted during the local socket credential exchange.  So far the
> server assumes an unbound socket on the client side because, well, I
> guess the reason is "nobody asked for it yet".  This could probably
> be implemented with not too much effort, if necessary.

I see.  Thank you.

Is it also known that recvfrom() on Unix domain datagram socket
returns an AF_INET address?

% uname -srvm
CYGWIN_NT-5.1 1.7.18s(0.263/5/3) 20130301 15:56:19 i686
% cat tst-server.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char *argv[])
{
  int s;
  int ret;
  struct sockaddr_un saddr, raddr;
  socklen_t len;
  ssize_t ssize;
  char buf[4096];

  unlink("socket-server");

  memset(&saddr, '\0', sizeof(saddr));
  saddr.sun_family = AF_UNIX;
  strcpy(saddr.sun_path, "socket-server");

  s = socket(AF_UNIX, SOCK_DGRAM, 0);
  if (s == -1) { perror("socket"); exit(EXIT_FAILURE); }

  ret = bind(s, (struct sockaddr *)&saddr, sizeof(saddr));
  if (ret == -1) { perror("bind"); exit(EXIT_FAILURE); }

  len = sizeof(raddr);
  ssize = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *)&raddr, &len);
  if (ssize == -1) { perror("recvfrom"); exit(EXIT_FAILURE); }

  printf("socklen: %d\n", (int)len);
  printf("sun_family: %d\n", (int)raddr.sun_family);

  if (raddr.sun_family == AF_INET) {
    struct sockaddr_in *addr_in = (struct sockaddr_in *)&raddr;
    uint16_t port = ntohs(addr_in->sin_port);
    printf("AF_INET address: %s:%d\n", inet_ntoa(addr_in->sin_addr), port);
  }

  return EXIT_SUCCESS;
}
% cat tst-client.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>

int main(int argc, char *argv[])
{
  int c;
  int ret;
  struct sockaddr_un saddr, caddr;
  ssize_t ssize;

  unlink("socket-client");

  memset(&saddr, '\0', sizeof(saddr));
  saddr.sun_family = AF_UNIX;
  strcpy(saddr.sun_path, "socket-server");

  memset(&caddr, '\0', sizeof(caddr));
  caddr.sun_family = AF_UNIX;
  strcpy(caddr.sun_path, "socket-client");

  c = socket(AF_UNIX, SOCK_DGRAM, 0);
  if (c == -1) { perror("socket"); exit(EXIT_FAILURE); }

  ret = bind(c, (struct sockaddr *)&caddr, sizeof(caddr));
  if (ret == -1) { perror("bind"); exit(EXIT_FAILURE); }

  ssize = sendto(c, "msg", 3, 0, (struct sockaddr *)&saddr, sizeof(saddr));
  if (ssize == -1) { perror("sendto"); exit(EXIT_FAILURE); }

  exit(EXIT_SUCCESS);
}
% gcc -Wall tst-server.c -o tst-server
% gcc -Wall tst-client.c -o tst-client
% ( ./tst-server & sleep 1; ./tst-client; wait )
socklen: 16
sun_family: 2
AF_INET address: 127.0.0.1:1590
-- 
Tanaka Akira

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019