1 /* posix */
2 #include <sys/types.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <fcntl.h>
7 #include <string.h>
8 #include <errno.h>
9 #include <sys/stat.h>
10
11 /* bsd extensions */
12 #include <sys/uio.h>
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <sys/un.h>
16
17 #include "priv.h"
18
19 int
getsockname(int fd,void * addr,int * alen)20 getsockname(int fd, void *addr, int *alen)
21 {
22 Rock *r;
23 int i;
24 struct sockaddr_in *lip;
25 struct sockaddr_un *lunix;
26
27 r = _sock_findrock(fd, 0);
28 if(r == 0){
29 errno = ENOTSOCK;
30 return -1;
31 }
32
33 switch(r->domain){
34 case PF_INET:
35 lip = (struct sockaddr_in*)addr;
36 _sock_ingetaddr(r, lip, alen, "local");
37 break;
38 case PF_UNIX:
39 lunix = (struct sockaddr_un*)&r->addr;
40 i = &lunix->sun_path[strlen(lunix->sun_path)] - (char*)lunix;
41 memmove(addr, lunix, i);
42 *alen = i;
43 break;
44 default:
45 errno = EAFNOSUPPORT;
46 return -1;
47 }
48 return 0;
49 }
50