]> git.lizzy.rs Git - plan9front.git/blob - sys/src/ape/lib/bsd/getsockname.c
ape: initial IPv6 support, inet_pton()/inet_ntop(), getaddrinfo()/getnameinfo()
[plan9front.git] / sys / src / ape / lib / bsd / getsockname.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 getsockname(int fd, struct sockaddr *addr, int *alen)
20 {
21         Rock *r;
22         int i;
23         struct sockaddr_un *lunix;
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         case PF_INET6:
34                 _sock_ingetaddr(r, addr, alen, "local");
35                 break;
36         case PF_UNIX:
37                 lunix = (struct sockaddr_un*)&r->addr;
38                 i = &lunix->sun_path[strlen(lunix->sun_path)] - (char*)lunix;
39                 memmove(addr, lunix, i);
40                 if(alen != 0)
41                         *alen = i;
42                 break;
43         default:
44                 errno = EAFNOSUPPORT;
45                 return -1;
46         }
47         return 0;
48 }