Blob


1 #include <u.h>
2 #define NOPLAN9DEFINES
3 #include <libc.h>
5 #include <sys/types.h>
6 #include <netdb.h>
7 #include <sys/un.h>
8 #include <netinet/in.h>
10 static char *nets[] = { "tcp", "udp", nil };
11 #define CLASS(p) ((*(uchar*)(p))>>6)
13 static struct {
14 char *net;
15 char *service;
16 int port;
17 } porttbl[] = {
18 "tcp", "9fs", 564,
19 "tcp", "whoami", 565,
20 "tcp", "guard", 566,
21 "tcp", "ticket", 567,
22 "tcp", "exportfs", 17007,
23 "tcp", "rexexec", 17009,
24 "tcp", "ncpu", 17010,
25 "tcp", "cpu", 17013,
26 "tcp", "venti", 17034,
27 "tcp", "wiki", 17035,
28 "tcp", "secstore", 5356,
29 };
31 static int
32 parseip(char *host, u32int *pip)
33 {
34 uchar addr[4];
35 int x, i;
36 char *p;
38 p = host;
39 for(i=0; i<4 && *p; i++){
40 x = strtoul(p, &p, 0);
41 if(x < 0 || x >= 256)
42 return -1;
43 if(*p != '.' && *p != 0)
44 return -1;
45 if(*p == '.')
46 p++;
47 addr[i] = x;
48 }
50 switch(CLASS(addr)){
51 case 0:
52 case 1:
53 if(i == 3){
54 addr[3] = addr[2];
55 addr[2] = addr[1];
56 addr[1] = 0;
57 }else if(i == 2){
58 addr[3] = addr[1];
59 addr[2] = 0;
60 addr[1] = 0;
61 }else if(i != 4)
62 return -1;
63 break;
64 case 2:
65 if(i == 3){
66 addr[3] = addr[2];
67 addr[2] = 0;
68 }else if(i != 4)
69 return -1;
70 break;
71 }
72 *pip = *(u32int*)addr;
73 return 0;
74 }
76 int
77 p9dialparse(char *addr, char **pnet, char **punix, u32int *phost, int *pport)
78 {
79 char *net, *host, *port, *e;
80 int i;
81 struct servent *se;
82 struct hostent *he;
83 struct sockaddr_un *sockun;
85 if(strncmp(addr, "/net/", 5) == 0)
86 addr += 5;
88 *punix = nil;
89 net = addr;
90 if((host = strchr(net, '!')) == nil){
91 werrstr("malformed address");
92 return -1;
93 }
94 *host++ = 0;
95 if((port = strchr(host, '!')) == nil){
96 if(strcmp(net, "unix")==0 || strcmp(net, "net")==0){
97 Unix:
98 if(strlen(host)+1 > sizeof sockun->sun_path){
99 werrstr("unix socket name too long");
100 return -1;
102 *punix = host;
103 *pnet = "unix";
104 *phost = 0;
105 *pport = 0;
106 return 0;
108 werrstr("malformed address");
109 return -1;
111 *port++ = 0;
113 if(*host == 0){
114 werrstr("malformed address (empty host)");
115 return -1;
117 if(*port == 0){
118 werrstr("malformed address (empty port)");
119 return -1;
122 if(strcmp(net, "unix") == 0)
123 goto Unix;
125 if(strcmp(net, "tcp")!=0 && strcmp(net, "udp")!=0 && strcmp(net, "net") != 0){
126 werrstr("bad network %s!%s!%s", net, host, port);
127 return -1;
130 /* translate host */
131 if(strcmp(host, "*") == 0)
132 *phost = 0;
133 else if(parseip(host, phost) == 0)
134 {}
135 else if((he = gethostbyname(host)) != nil)
136 *phost = *(u32int*)(he->h_addr);
137 else{
138 werrstr("unknown host %s", host);
139 return -1;
142 /* translate network and port; should return list rather than first */
143 if(strcmp(net, "net") == 0){
144 for(i=0; nets[i]; i++){
145 if((se = getservbyname(port, nets[i])) != nil){
146 *pnet = nets[i];
147 *pport = ntohs(se->s_port);
148 return 0;
153 for(i=0; i<nelem(porttbl); i++){
154 if(strcmp(net, "net") == 0 || strcmp(porttbl[i].net, net) == 0)
155 if(strcmp(porttbl[i].service, port) == 0){
156 *pnet = porttbl[i].net;
157 *pport = porttbl[i].port;
158 return 0;
162 if(strcmp(net, "net") == 0){
163 werrstr("unknown service net!*!%s", port);
164 return -1;
167 if(strcmp(net, "tcp") != 0 && strcmp(net, "udp") != 0){
168 werrstr("unknown network %s", net);
169 return -1;
172 *pnet = net;
173 i = strtol(port, &e, 0);
174 if(*e == 0){
175 *pport = i;
176 return 0;
179 if((se = getservbyname(port, net)) != nil){
180 *pport = ntohs(se->s_port);
181 return 0;
183 werrstr("unknown service %s!*!%s", net, port);
184 return -1;