13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <netinet/tcp.h>
23 p9dial(char *addr, char *local, char *dummy2, int *dummy3)
32 struct sockaddr_in sa, sal;
33 struct sockaddr_un su;
37 werrstr("cannot handle extra arguments in dial");
45 if(p9dialparse(buf, &net, &unix, &host, &port) < 0){
50 if(strcmp(net, "tcp") == 0)
52 else if(strcmp(net, "udp") == 0)
54 else if(strcmp(net, "unix") == 0)
57 werrstr("can only handle tcp, udp, and unix: not %s", net);
63 if((s = socket(AF_INET, proto, 0)) < 0)
72 if(p9dialparse(buf, &net, &unix, &host, &port) < 0){
79 werrstr("bad local address %s for dial %s", local, addr);
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);
87 if(port && getsockopt(s, SOL_SOCKET, SO_TYPE, (void*)&n, &sn) >= 0
90 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&n, sizeof n);
92 if(bind(s, (struct sockaddr*)&sal, sizeof sal) < 0)
98 setsockopt(s, SOL_SOCKET, SO_BROADCAST, &n, sizeof n);
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){
109 if(proto == SOCK_STREAM){
111 setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (char*)&one, sizeof one);
117 werrstr("local address not supported on unix network");
121 memset(&su, 0, sizeof su);
122 su.sun_family = AF_UNIX;
123 if(strlen(unix)+1 > sizeof su.sun_path){
124 werrstr("unix socket name too long");
128 strcpy(su.sun_path, unix);
130 if((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){
131 werrstr("socket: %r");
134 if(connect(s, (struct sockaddr*)&su, sizeof su) < 0){
135 werrstr("connect %s: %r", su.sun_path);