]> git.lizzy.rs Git - plan9front.git/blob - sys/src/ape/lib/bsd/getsockname.c
Import sources from 2011-03-30 iso image - lib
[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_in *lip;
24         struct sockaddr_un *lunix;
25
26         r = _sock_findrock(fd, 0);
27         if(r == 0){
28                 errno = ENOTSOCK;
29                 return -1;
30         }
31
32         switch(r->domain){
33         case PF_INET:
34                 lip = (struct sockaddr_in*)addr;
35                 _sock_ingetaddr(r, lip, alen, "local");
36                 break;
37         case PF_UNIX:
38                 lunix = (struct sockaddr_un*)&r->addr;
39                 i = &lunix->sun_path[strlen(lunix->sun_path)] - (char*)lunix;
40                 memmove(addr, lunix, i);
41                 *alen = i;
42                 break;
43         default:
44                 errno = EAFNOSUPPORT;
45                 return -1;
46         }
47         return 0;
48 }