Blame


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