2 Plan9 URI address handling.
4 Author(s): Enrico Weigelt, metux IT service <weigelt@metux.de>
11 #include <9p-mixp/srv_addr.h>
12 #include <9p-mixp/err.h>
13 #include "mixp_local.h"
17 ninep://localhost:9999/
21 static MIXP_SERVER_ADDRESS
* __parse_uri(const char* addr
, const char* fulladdr
, MIXP_PROTO_ID proto
)
23 MIXP_SERVER_ADDRESS
* as
= calloc(1,sizeof(MIXP_SERVER_ADDRESS
));
24 as
->addrstr
= strdup(fulladdr
);
29 char buffer
[4096]; // FIXME !
31 if ((tmp
=strchr(addr
,'/'))!=NULL
)
33 as
->path
= strdup(tmp
);
34 memset(&buffer
, 0, sizeof(buffer
));
35 strncpy(buffer
,addr
,(tmp
-addr
));
43 if ((tmp
=strchr(buffer
,':'))!=NULL
)
47 as
->hostname
= strdup(buffer
);
52 as
->hostname
= strdup(buffer
);
58 static MIXP_SERVER_ADDRESS
* __parse_uri_unix(const char* addr
, const char* fulladdr
, MIXP_PROTO_ID proto
)
60 MIXP_SERVER_ADDRESS
* as
= calloc(1,sizeof(MIXP_SERVER_ADDRESS
));
61 as
->addrstr
= strdup(fulladdr
);
69 as
->path
= strdup(addr
);
70 as
->hostname
= strdup("");
75 static MIXP_SERVER_ADDRESS
* __parse_old_tcp(const char* addr
)
77 MIXP_SERVER_ADDRESS
* a
= calloc(1,sizeof(MIXP_SERVER_ADDRESS
));
81 char* tmp
= strchr(buffer
,'!');
93 a
->addrstr
= strdup(addr
);
94 a
->proto
= P9_PROTO_TCP
;
96 a
->path
= strdup("/");
101 static inline const char* cmpprefix(const char* str
, const char* prefix
)
103 int sz
= strlen(prefix
);
104 if (strncmp(str
,prefix
,sz
)==0)
110 MIXP_SERVER_ADDRESS
* mixp_srv_addr_parse(const char* addr
)
114 mixp_werrstr("null address given");
118 if (strlen(addr
)>4096)
120 mixp_werrstr("address too long");
126 // --- try different URI schemes ---
128 // simple 9p:// scheme for implicit tcp
129 if ((c
=cmpprefix(addr
,"9p://")))
130 return __parse_uri(c
, addr
, P9_PROTO_TCP
);
133 if ((c
=cmpprefix(addr
,"9p:tcp://")))
134 return __parse_uri(c
, addr
, P9_PROTO_TCP
);
135 if ((c
=cmpprefix(addr
,"9p:tcp:/")))
136 return __parse_uri(c
, addr
, P9_PROTO_TCP
);
139 if (strncmp(addr
,"9p:unix:/",9)==0)
140 return __parse_uri_unix(addr
+8, addr
, P9_PROTO_UNIX
);
142 // shortcut 9p:/ scheme - implicit unix
143 if ((c
=cmpprefix(addr
,"9p:/")))
144 return __parse_uri(c
, addr
, P9_PROTO_TCP
);
146 if ((c
=cmpprefix(addr
,"ninep://")))
147 return __parse_uri(c
, addr
, P9_PROTO_TCP
);
149 if (strncmp(addr
,"tcp!",4)==0)
150 return __parse_old_tcp(addr
+4);
152 fprintf(stderr
,"could not parse address \"%s\"\n", addr
);
156 int mixp_srv_addr_free(MIXP_SERVER_ADDRESS
* addr
)
164 free(addr
->hostname
);
170 free(addr
->username
);