This repository was archived by the owner on Dec 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.c
More file actions
199 lines (176 loc) · 5.14 KB
/
common.c
File metadata and controls
199 lines (176 loc) · 5.14 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* LMiniVPN - Common Part
* Author: Zhouyu Lin
*
*/
#include "common.h"
/* SIGCHLD handler. */
void sigchld_hdl (int sig)
{
/* Wait for all dead processes.
* We use a non-blocking call to be sure this signal handler will not
* block if a child was cleaned up in another part of the program. */
while (waitpid(-1, NULL, WNOHANG) > 0)
{
}
}
/* Generate random numbers. */
int randgen(uint8_t *data, int length)
{
uint32_t seed;
FILE* urandom = fopen("/dev/urandom", "r");
fread(&seed, sizeof(uint32_t), 1, urandom);
fclose(urandom);
srand(seed);
while(length > 0)
{
length--;
*(data+length) = rand()%256;
}
return 0;
}
/**************************************************************************
* tun_alloc: allocates or reconnects to a tun/tap device. The caller *
* needs to reserve enough space in *dev. *
**************************************************************************/
int tun_alloc(char *dev, int flags)
{
struct ifreq ifr;
int fd, err;
if( (fd = open("/dev/net/tun", O_RDWR)) < 0 )
{
perror("tun_alloc(): Opening /dev/net/tun");
return fd;
}
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = flags;
if (*dev)
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
if( (err = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0 )
{
perror("tun_alloc(): ioctl(TUNSETIFF)");
close(fd);
return err;
}
strcpy(dev, ifr.ifr_name);
return fd;
}
/**************************************************************************
* cread: read routine that checks for errors and exits if an error is *
* returned. *
**************************************************************************/
int cread(SSL* fd, char *buf, int n)
{
int nread;
if((nread=SSL_read(fd, buf, n))<0)
{
perror("cread() -> read()");
exit(1);
}
return nread;
}
/**************************************************************************
* cwrite: write routine that checks for errors and exits if an error is *
* returned. *
**************************************************************************/
int cwrite(SSL* fd, char *buf, int n)
{
int nwrite;
if((nwrite=SSL_write(fd, buf, n))<0)
{
perror("cwite() -> write()");
exit(1);
}
return nwrite;
}
/**************************************************************************
* read_n: ensures we read exactly n bytes, and puts those into "buf". *
* (unless EOF, of course) *
**************************************************************************/
int read_n(SSL* fd, char *buf, int n)
{
int nread, left = n;
while(left > 0)
{
if ((nread = cread(fd, buf, left))==0)
{
return 0 ;
}
else
{
left -= nread;
buf += nread;
}
}
return n;
}
/**************************************************************************
* do_debug: prints debugging stuff (doh!) *
**************************************************************************/
void do_debug(char *msg, ...)
{
va_list argp;
if(debug)
{
va_start(argp, msg);
vfprintf(stderr, msg, argp);
va_end(argp);
}
}
/**************************************************************************
* my_err: prints custom error messages on stderr. *
**************************************************************************/
void my_err(char *msg, ...)
{
va_list argp;
va_start(argp, msg);
vfprintf(stderr, msg, argp);
va_end(argp);
}
/* wait to read from tcp socket. */
int wait_readtcp(SSL* fd,message *result)
{
int nread, plength;
nread = read_n(fd, (char *)&plength, sizeof(plength));
if(nread == 0)
{
do_debug("TCP: Client disconnected. Bye.\n");
SSL_free (fd);
exit(1);
}
/* read packet */
nread = read_n(fd, (char *)result, ntohs(plength));
return nread;
}
/* send tcp message */
int tcpsend(SSL* fd, message *send, int length)
{
int plength,nwrite;
plength = htons(length);
nwrite = cwrite(fd, (char *)&plength, sizeof(plength));
nwrite = cwrite(fd, (char *)send, length);
return 0;
}
/**************************************************************************
* usage: prints usage and exits. *
**************************************************************************/
void usage(int role)
{
fprintf(stderr, "Usage:\n");
if (role==1)
fprintf(stderr, "%s [-i <ifacename>] [-c <hostname>] [-p <port>] [-u|-a] [-d]\n", progname);
else
fprintf(stderr, "%s [-p <port>] [-u|-a] [-d]\n", progname);
fprintf(stderr, "%s -h\n", progname);
fprintf(stderr, "\n");
if (role==1)
{
fprintf(stderr, "-i <ifacename>: Name of interface to use\n");
fprintf(stderr, "-c <hostname>: specify server hostnmae (-c <hostname>) (mandatory)\n");
}
fprintf(stderr, "-p <port>: port to use, default 55555\n");
fprintf(stderr, "-u|-a: use TUN (-u, default) or TAP (-a)\n");
fprintf(stderr, "-d: outputs debug information while running\n");
fprintf(stderr, "-h: prints this help text\n");
exit(1);
}