[PATCH 2/2] Adds some resolv functionality (XCC)
[akaros.git] / user / bsd / _sock_ingetaddr.c
1 /* 
2  * This file is part of the UCB release of Plan 9. It is subject to the license
3  * terms in the LICENSE file found in the top-level directory of this
4  * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
5  * part of the UCB release of Plan 9, including this file, may be copied,
6  * modified, propagated, or distributed except according to the terms contained
7  * in the LICENSE file.
8  */
9 /* posix */
10 #include <sys/types.h>
11 #include <unistd.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <fcntl.h>
15 #include <errno.h>
16 #include <string.h>
17
18 /* bsd extensions */
19 #include <sys/uio.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <sys/un.h>
23 #include <arpa/inet.h>
24
25 #include "priv.h"
26
27 void
28 _sock_ingetaddr(Rock *r, struct sockaddr_in *ip, int *alen, char *a)
29 {
30         int n, fd;
31         char *p;
32         char name[Ctlsize];
33
34         /* get remote address */
35         strcpy(name, r->ctl);
36         p = strrchr(name, '/');
37         strcpy(p+1, a);
38         fd = open(name, O_RDONLY);
39         if(fd >= 0){
40                 n = read(fd, name, sizeof(name)-1);
41                 if(n > 0){
42                         name[n] = 0;
43                         p = strchr(name, '!');
44                         if(p){
45                                 *p++ = 0;
46                                 ip->sin_family = AF_INET;
47                                 ip->sin_port = htons(atoi(p));
48                                 ip->sin_addr.s_addr = inet_addr(name);
49                                 if(alen)
50                                         *alen = sizeof(struct sockaddr_in);
51                         }
52                 }
53                 close(fd);
54         }
55
56 }