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