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
20 #define unix xunix
22 int
23 p9dial(char *addr, char *local, char *dummy2, int *dummy3)
24 {
25 char *buf;
26 char *net, *unix;
27 u32int host;
28 int port;
29 int proto;
30 socklen_t sn;
31 int n;
32 struct sockaddr_in sa, sal;
33 struct sockaddr_un su;
34 int s;
36 if(dummy2 || dummy3){
37 werrstr("cannot handle extra arguments in dial");
38 return -1;
39 }
41 buf = strdup(addr);
42 if(buf == nil)
43 return -1;
45 if(p9dialparse(buf, &net, &unix, &host, &port) < 0){
46 free(buf);
47 return -1;
48 }
50 if(strcmp(net, "tcp") == 0)
51 proto = SOCK_STREAM;
52 else if(strcmp(net, "udp") == 0)
53 proto = SOCK_DGRAM;
54 else if(strcmp(net, "unix") == 0)
55 goto Unix;
56 else{
57 werrstr("can only handle tcp, udp, and unix: not %s", net);
58 free(buf);
59 return -1;
60 }
61 free(buf);
63 if((s = socket(AF_INET, proto, 0)) < 0)
64 return -1;
66 if(local){
67 buf = strdup(local);
68 if(buf == nil){
69 close(s);
70 return -1;
71 }
72 if(p9dialparse(buf, &net, &unix, &host, &port) < 0){
73 badlocal:
74 free(buf);
75 close(s);
76 return -1;
77 }
78 if(unix){
79 werrstr("bad local address %s for dial %s", local, addr);
80 goto badlocal;
81 }
82 memset(&sal, 0, sizeof sal);
83 memmove(&sal.sin_addr, &local, 4);
84 sal.sin_family = AF_INET;
85 sal.sin_port = htons(port);
86 sn = sizeof n;
87 if(port && getsockopt(s, SOL_SOCKET, SO_TYPE, (void*)&n, &sn) >= 0
88 && n == SOCK_STREAM){
89 n = 1;
90 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&n, sizeof n);
91 }
92 if(bind(s, (struct sockaddr*)&sal, sizeof sal) < 0)
93 goto badlocal;
94 free(buf);
95 }
97 n = 1;
98 setsockopt(s, SOL_SOCKET, SO_BROADCAST, &n, sizeof n);
99 if(host != 0){
100 memset(&sa, 0, sizeof sa);
101 memmove(&sa.sin_addr, &host, 4);
102 sa.sin_family = AF_INET;
103 sa.sin_port = htons(port);
104 if(connect(s, (struct sockaddr*)&sa, sizeof sa) < 0){
105 close(s);
106 return -1;
109 if(proto == SOCK_STREAM){
110 int one = 1;
111 setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (char*)&one, sizeof one);
113 return s;
115 Unix:
116 if(local){
117 werrstr("local address not supported on unix network");
118 free(buf);
119 return -1;
121 /* Allow regular files in addition to Unix sockets. */
122 if((s = open(unix, ORDWR)) >= 0)
123 return s;
124 memset(&su, 0, sizeof su);
125 su.sun_family = AF_UNIX;
126 if(strlen(unix)+1 > sizeof su.sun_path){
127 werrstr("unix socket name too long");
128 free(buf);
129 return -1;
131 strcpy(su.sun_path, unix);
132 free(buf);
133 if((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){
134 werrstr("socket: %r");
135 return -1;
137 if(connect(s, (struct sockaddr*)&su, sizeof su) < 0){
138 werrstr("connect %s: %r", su.sun_path);
139 close(s);
140 return -1;
142 return s;