This is the mail archive of the
cygwin
mailing list for the Cygwin project.
Re: Unix domain accept() and getperrname() doesn't return the client address.
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