]> git.lizzy.rs Git - plan9front.git/blob - sys/src/ape/lib/bsd/_sock_ingetaddr.c
audiohda: fix syntax error
[plan9front.git] / sys / src / ape / lib / bsd / _sock_ingetaddr.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 <errno.h>
8 #include <string.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 void*
19 _sock_inip(struct sockaddr *a)
20 {
21         switch(a->sa_family){
22         case AF_INET:
23                 return &((struct sockaddr_in*)a)->sin_addr;
24         case AF_INET6:
25                 return &((struct sockaddr_in6*)a)->sin6_addr;
26         }
27         return 0;
28 }
29
30 int
31 _sock_inport(struct sockaddr *a)
32 {
33         switch(a->sa_family){
34         case AF_INET:
35                 return ntohs(((struct sockaddr_in*)a)->sin_port);
36         case AF_INET6:
37                 return ntohs(((struct sockaddr_in6*)a)->sin6_port);
38         }
39         return 0;
40 }
41
42 int
43 _sock_inaddr(int af, char *ip, char *port, void *a, int *alen)
44 {
45         int len;
46
47         len = 0;
48         if(af == AF_INET){
49                 struct sockaddr_in *in = a;
50
51                 len = sizeof(*in);
52                 memset(in, 0, len);
53                 in->sin_family = af;
54                 if(port != 0 && *port != 0)
55                         in->sin_port = htons(atoi(port));
56                 if(ip != 0 && *ip != 0)
57                         inet_pton(af, ip, &in->sin_addr);
58         } else if(af == AF_INET6){
59                 struct sockaddr_in6 *in = a;
60
61                 len = sizeof(*in);
62                 memset(in, 0, len);
63                 in->sin6_family = af;
64                 if(port != 0 && *port != 0)
65                         in->sin6_port = htons(atoi(port));
66                 if(ip != 0 && *ip != 0)
67                         inet_pton(af, ip, &in->sin6_addr);
68         }
69         if(alen != 0)
70                 *alen = len;
71         return len;
72 }
73
74 void
75 _sock_ingetaddr(Rock *r, void *a, int *alen, char *file)
76 {
77         char name[Ctlsize], *p;
78         int n, fd;
79
80         if(r->domain != PF_INET && r->domain != PF_INET6)
81                 return;
82         /* get remote address */
83         strcpy(name, r->ctl);
84         p = strrchr(name, '/');
85         strcpy(p+1, file);
86         fd = open(name, O_RDONLY);
87         if(fd >= 0){
88                 n = read(fd, name, sizeof(name)-1);
89                 if(n > 0){
90                         name[n] = 0;
91                         p = strchr(name, '!');
92                         if(p){
93                                 *p++ = 0;
94                                 _sock_inaddr(r->domain, name, p, a, alen);
95                         }
96                 }
97                 close(fd);
98         }
99 }