forked from AnttiTakala/SSH2DOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.c
175 lines (152 loc) · 4.82 KB
/
common.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
/* common.c Copyright (c) 2000-2003 Nagy Daniel
*
* $Date: 2006/03/01 20:37:07 $
* $Revision: 1.4 $
*
* Common functions
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "tcp.h"
#include "config.h"
#include "proxy.h"
#if defined (MEMWATCH)
#include "memwatch.h"
#endif
/* external functions, data */
extern Config GlobalConfig; /* configuration variables */
extern unsigned short Configuration; /* configuration variables */
/*
* Fatal error handler
*/
void fatal(const char *fmt, ...)
{
va_list ap;
char buf[1024];
va_start(ap, fmt);
vsprintf(buf, fmt, ap);
va_end(ap);
puts(buf);
printf("%s", sockerr(&GlobalConfig.s));
sock_close(&GlobalConfig.s);
exit(255);
}
/*
* Try to connect, wait for established
*/
static void doconnect(unsigned long ip, unsigned short rp, unsigned short lp)
{
if(!tcp_open(&GlobalConfig.s, lp, ip, rp, NULL))
fatal("Unable to open TCP connection");
/* Negotiate TCP connection */
if(Configuration & VERBOSE_MODE)
puts("Waiting for remote host to connect...");
while(!sock_established(&GlobalConfig.s))
if(!tcp_tick(&GlobalConfig.s))
fatal("Remote host closed connection");
return;
}
/*
* Parse environment
*/
static void envparse(char *str, char **proxyhost, unsigned short *proxyport,
char **proxyuser, char **proxypass)
{
char *p;
short m, n;
if((*proxyhost = (char *)malloc(128)) == NULL)
fatal("Memory allocation error. %s: %d", __FILE__, __LINE__);
if(strchr(str, '@')){ /* we have username and maybe password*/
if((p = *proxyuser = (char *)malloc(32)) == NULL)
fatal("Memory allocation error. %s: %d", __FILE__, __LINE__);
for(n = 0; str[n] != '@' && str[n] != ':'; n++)
p[n] = str[n];
p[n] = 0;
if(str[n++] == ':'){ /* we have password */
if((p = *proxypass = (char *)malloc(32)) == NULL)
fatal("Memory allocation error. %s: %d", __FILE__, __LINE__);
for(m = 0; str[n] != '@'; m++, n++)
p[m] = str[n];
p[m] = 0;
n++;
}
str += n;
}
if((p = strchr(str, ':')) != NULL){ /* we have port specified */
*p++ = 0;
*proxyport = atoi(p);
}
strcpy(*proxyhost, str);
}
/*
* Connect to remote host via TCP
*/
void TCPConnect(char *remotehost, unsigned short remoteport)
{
char *env, method = NOPROXY;
char *proxyuser = NULL, *proxypass = NULL, *proxyhost = NULL;
unsigned short localport, proxyport = 0;
longword remoteip, proxyip;
short n;
/* Allocate local port */
localport = (rand() % 512) + 512;
if(Configuration & NONPRIVILEGED_PORT)
localport = localport + 512;
sock_init(); /* Initialize socket */
if((env = getenv("SOCKS_PROXY")) != NULL){ /* we have socks_proxy env.var */
envparse(env, &proxyhost, &proxyport, &proxyuser, &proxypass);
if(!proxyport)
proxyport = SOCKS_PORT;
method = SOCKS_PROXY;
} else if((env = getenv("HTTP_PROXY")) != NULL){ /* we have http_proxy env.var */
envparse(env, &proxyhost, &proxyport, &proxyuser, &proxypass);
if(!proxyport)
proxyport = HTTP_PORT;
method = HTTP_PROXY;
}
if(proxyhost){ /* Resolve proxy */
if((proxyip = resolve(proxyhost)) == 0)
fatal("Unable to resolve `%s'", proxyhost);
free(proxyhost);
} else { /* Resolve hostname */
if((remoteip = resolve(remotehost)) == 0)
fatal("Unable to resolve `%s'", remotehost);
}
switch(method){
case NOPROXY:
doconnect(remoteip, remoteport, localport);
break;
case SOCKS_PROXY:
doconnect(proxyip, proxyport, localport);
if(socks5_connect(remotehost, remoteport, proxyuser, proxypass))
fatal("Failed to begin relaying via SOCKS");
free(proxyuser);
free(proxypass);
break;
case HTTP_PROXY:
doconnect(proxyip, proxyport, localport);
n = http_connect(remotehost, remoteport, proxyuser, proxypass);
free(proxyuser);
free(proxypass);
if(n)
fatal("failed to begin relaying via HTTP");
break;
}
}