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){
49 if(strcmp(net, "unix") != 0 && host == 0){
50 werrstr("invalid dial address 0.0.0.0 (aka *)");
55 if(strcmp(net, "tcp") == 0)
57 else if(strcmp(net, "udp") == 0)
59 else if(strcmp(net, "unix") == 0)
62 werrstr("can only handle tcp, udp, and unix: not %s", net);
68 if((s = socket(AF_INET, proto, 0)) < 0)
77 if(p9dialparse(buf, &net, &unix, &host, &port) < 0){
84 werrstr("bad local address %s for dial %s", local, addr);
87 memset(&sal, 0, sizeof sal);
88 memmove(&sal.sin_addr, &local, 4);
89 sal.sin_family = AF_INET;
90 sal.sin_port = htons(port);
92 if(port && getsockopt(s, SOL_SOCKET, SO_TYPE, (void*)&n, &sn) >= 0
95 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&n, sizeof n);
97 if(bind(s, (struct sockaddr*)&sal, sizeof sal) < 0)
103 setsockopt(s, SOL_SOCKET, SO_BROADCAST, &n, sizeof n);
105 memset(&sa, 0, sizeof sa);
106 memmove(&sa.sin_addr, &host, 4);
107 sa.sin_family = AF_INET;
108 sa.sin_port = htons(port);
109 if(connect(s, (struct sockaddr*)&sa, sizeof sa) < 0){
114 if(proto == SOCK_STREAM){
116 setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (char*)&one, sizeof one);
122 werrstr("local address not supported on unix network");
126 /* Allow regular files in addition to Unix sockets. */
127 if((s = open(unix, ORDWR)) >= 0)
129 memset(&su, 0, sizeof su);
130 su.sun_family = AF_UNIX;
131 if(strlen(unix)+1 > sizeof su.sun_path){
132 werrstr("unix socket name too long");
136 strcpy(su.sun_path, unix);
138 if((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){
139 werrstr("socket: %r");
142 if(connect(s, (struct sockaddr*)&su, sizeof su) < 0){
143 werrstr("connect %s: %r", su.sun_path);