forked from telehash/telehash-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
port.c
176 lines (156 loc) · 4.44 KB
/
port.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdarg.h>
#include "switch.h"
#include "util.h"
#include "ext.h"
#include "util_unix.h"
#define SBUF 1024
int getport(char *str)
{
char *bad;
int port;
if(!*str) return -1;
if(str[0] == '-')
{
if(str[1] != 0) return -1;
return 0;
}
port = (int)strtol(str,&bad,10);
if(*bad) return -1;
return port;
}
#define MODE_TOPORT 0
#define MODE_LISTEN 1
#define MODE_STDIN 2
void draino(int fd)
{
if(fd < 0) return;
fcntl(fd,F_SETFL,fcntl(fd,F_GETFL) | O_NONBLOCK);
}
int main(int argc, char *argv[])
{
switch_t s;
chan_t c;
path_t in;
sockc_t sc, sock = NULL;
int udp, listener, port, mode, flag;
char *hn;
struct sockaddr_in sin;
platform_debugging(1);
crypt_init();
s = switch_new(0);
if(util_loadjson(s) != 0 || (udp = util_server(0,100)) <= 0)
{
printf("failed to startup %s or %s\n", strerror(errno), crypt_err());
return -1;
}
if(argc != 3)
{
printf("this hashname is %s\n",s->id->hexname);
printf("test socket tunnel over telehash, source/destination can be hashname, port#, or '-' for stdin/stdout\n");
printf("%s source dest\n",argv[0]);
return 1;
}
hn = argv[1];
if( (util_ishex(hn,64) || hn[0] == '+') && (port = getport(argv[2])) >= 0)
{
// incoming sock channels create new socket to port
mode = MODE_TOPORT;
}else if((port = getport(argv[1])) >= 0 && (hn = util_ishex(argv[2],64))){
// listen on given port, when socket (or if stdin) create sock channel to hashname
if(port == 0)
{
mode = MODE_STDIN;
sock = sockc_connect(s, hn, NULL);
sock->fd = fileno(stdin);
draino(sock->fd);
}else{
mode = MODE_LISTEN;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(port);
listener = socket(AF_INET, SOCK_STREAM, 0);
flag = 1;
setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(int));
if(bind(listener, (struct sockaddr *)&sin, sizeof(struct sockaddr)))
{
perror("bind failed");
return 1;
}
draino(listener);
listen(listener, 1);
}
}else{
perror("invalid args, need a 64-char hex hashname in source or destination and port number or '-' in other");
return 1;
}
DEBUG_PRINTF("loaded hashname %s\n",s->id->hexname);
link_hn(s, bucket_get(s->seeds, 0), NULL);
in = path_new("ipv4");
while(util_readone(s, udp, in) == 0)
{
switch_loop(s);
while((c = switch_pop(s)))
{
if(util_cmp(c->type,"connect") == 0) ext_connect(c);
if(util_cmp(c->type,"sock") == 0 && (sc = ext_sock(c)))
{
// connect new sock to given port
if(sc->state == SOCKC_NEW && mode == MODE_TOPORT && (hn[0] == '+' || util_cmp(hn,c->to->hexname) == 0))
{
sockc_accept(sc);
if(port == 0)
{
sc->fd = fileno(stdout);
}else{
sc->fd = socket(AF_INET, SOCK_STREAM, 0);
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
sin.sin_port = htons(port);
if(connect(sc->fd, (struct sockaddr *)&sin, sizeof(struct sockaddr))) sockc_close(sc);
}
draino(sc->fd);
}
// only track the last active/open sockc in the main loop (we're just a testing utility)
sock = (sc->state == SOCKC_OPEN)?sc:NULL;
}
}
util_sendall(s,udp);
if(mode == MODE_LISTEN && (flag = accept(listener, NULL, NULL)) >= 0)
{
draino(flag);
if(sock) sockc_close(sock);
sock = sockc_connect(s, hn, NULL);
sock->fd = flag;
}
// check any active socket and fd and read/write out any data
if(sock && sock->fd >= 0)
{
while(sock->readable)
{
flag = write(sock->fd,sock->readbuf,sock->readable);
if(flag < 0 && errno == EAGAIN) break;
sockc_zread(sc,flag); // closes if error
}
while(sockc_zwrite(sock,SBUF) && sockc_zwritten(sock,read(sock->fd,sock->zwrite,SBUF)) > 0);
// clean up any closed ones
if(sock->state == SOCKC_CLOSED)
{
if(port && sock->fd) close(sock->fd); // only close sockets
sockc_close(sock);
sock = NULL;
}
}
}
perror("exiting");
return 0;
}