]> git.lizzy.rs Git - plan9front.git/blob - sys/src/ape/lib/bsd/getpeername.c
ape: Add mkstemp to /sys/src/ape/lib/ap/gen/mkfile
[plan9front.git] / sys / src / ape / lib / bsd / getpeername.c
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
10 /* bsd extensions */
11 #include <sys/uio.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <sys/un.h>
15
16 #include "priv.h"
17
18 int
19 getpeername(int fd, struct sockaddr *addr, int *alen)
20 {
21         Rock *r;
22         int olen, len;
23         struct sockaddr_un *runix;
24
25         r = _sock_findrock(fd, 0);
26         if(r == 0){
27                 errno = ENOTSOCK;
28                 return -1;
29         }
30
31         switch(r->domain){
32         case PF_INET:
33                 len = sizeof(struct sockaddr_in);
34                 break;
35         case PF_INET6:
36                 len = sizeof(struct sockaddr_in6);
37                 break;
38         case PF_UNIX:
39                 runix = (struct sockaddr_un*)&r->raddr;
40                 len = &runix->sun_path[strlen(runix->sun_path)] - (char*)runix;
41                 break;
42         default:
43                 errno = EAFNOSUPPORT;
44                 return -1;
45         }
46
47         if(alen != 0){
48                 olen = *alen;
49                 *alen = len;
50                 if(olen < len)
51                         len = olen;
52         }
53         if(addr != 0 && len > 0)
54                 memmove(addr, &r->raddr, len);
55
56         return 0;
57 }