Blob


1 #include <u.h>
2 #include <libc.h>
4 #undef accept
5 #undef announce
6 #undef dial
7 #undef setnetmtpt
8 #undef hangup
9 #undef listen
10 #undef netmkaddr
11 #undef reject
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <netinet/tcp.h>
16 #include <sys/un.h>
17 #include <netdb.h>
19 #undef unix
21 int
22 p9dial(char *addr, char *local, char *dummy2, int *dummy3)
23 {
24 char *buf;
25 char *net, *unix;
26 u32int host;
27 int port;
28 int proto;
29 socklen_t sn;
30 int n;
31 struct sockaddr_in sa, sal;
32 struct sockaddr_un su;
33 int s;
35 if(dummy2 || dummy3){
36 werrstr("cannot handle extra arguments in dial");
37 return -1;
38 }
40 buf = strdup(addr);
41 if(buf == nil)
42 return -1;
44 if(p9dialparse(buf, &net, &unix, &host, &port) < 0){
45 free(buf);
46 return -1;
47 }
49 if(strcmp(net, "tcp") == 0)
50 proto = SOCK_STREAM;
51 else if(strcmp(net, "udp") == 0)
52 proto = SOCK_DGRAM;
53 else if(strcmp(net, "unix") == 0)
54 goto Unix;
55 else{
56 werrstr("can only handle tcp, udp, and unix: not %s", net);
57 free(buf);
58 return -1;
59 }
60 free(buf);
62 memset(&sa, 0, sizeof sa);
63 memmove(&sa.sin_addr, &host, 4);
64 sa.sin_family = AF_INET;
65 sa.sin_port = htons(port);
66 if((s = socket(AF_INET, proto, 0)) < 0)
67 return -1;
69 if(local){
70 buf = strdup(local);
71 if(buf == nil){
72 close(s);
73 return -1;
74 }
75 if(p9dialparse(buf, &net, &unix, &host, &port) < 0){
76 badlocal:
77 free(buf);
78 close(s);
79 return -1;
80 }
81 if(unix){
82 werrstr("bad local address %s for dial %s", local, addr);
83 goto badlocal;
84 }
85 memset(&sal, 0, sizeof sal);
86 memmove(&sal.sin_addr, &local, 4);
87 sal.sin_family = AF_INET;
88 sal.sin_port = htons(port);
89 sn = sizeof n;
90 if(port && getsockopt(s, SOL_SOCKET, SO_TYPE, (void*)&n, &sn) >= 0
91 && n == SOCK_STREAM){
92 n = 1;
93 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&n, sizeof n);
94 }
95 if(bind(s, (struct sockaddr*)&sal, sizeof sal) < 0)
96 goto badlocal;
97 free(buf);
98 }
100 if(connect(s, (struct sockaddr*)&sa, sizeof sa) < 0){
101 close(s);
102 return -1;
104 if(proto == SOCK_STREAM){
105 int one = 1;
106 setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (char*)&one, sizeof one);
108 return s;
110 Unix:
111 if(local){
112 werrstr("local address not supported on unix network");
113 free(buf);
114 return -1;
116 memset(&su, 0, sizeof su);
117 su.sun_family = AF_UNIX;
118 if(strlen(unix)+1 > sizeof su.sun_path){
119 werrstr("unix socket name too long");
120 free(buf);
121 return -1;
123 strcpy(su.sun_path, unix);
124 free(buf);
125 if((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
126 return -1;
127 if(connect(s, (struct sockaddr*)&su, sizeof su) < 0){
128 close(s);
129 return -1;
131 return s;