summaryrefslogtreecommitdiff
path: root/src/http_proxy_functions.c
blob: 2c3d680383b68811a0881a1430a30a44661f16e6 (plain)
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
 * active port forwarder - software for secure forwarding
 * Copyright (C) 2003,2004,2005 jeremian <jeremian [at] poczta.fm>
 *
 * 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 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 <config.h>

#include "http_proxy_functions.h"
#include "network.h"
#include "stats.h"
#include "logging.h"

static char isseed;

int
myrand(int down, int up)
{
	struct timeval tv;
	if (!isseed) {
		gettimeofday(&tv, 0);
		srand(tv.tv_sec);
		isseed = 1;
	}
	return ( down + ( rand() % (up - down + 1) ) );
}

void
mysleep(double time)
{
	struct timeval tv;
	tv.tv_sec = (int) time;
	tv.tv_usec = (int)(time * 1000000)%1000000;
	select(0, NULL, NULL, NULL, &tv);
}

void
delete_user(connection* cnts, int i, fd_set* allset)
{
  aflog(LOG_T_MAIN, LOG_I_DEBUG,
      "http proxy: delete_user(%d)", i);
  clear_fd(&(cnts[i].sockfd), allset);
  if (!(cnts[i].state & C_POST_WAIT)) {
    clear_sslFd(cnts[i].postFd, allset);
  }
  if ((cnts[i].type == 0) && (!(cnts[i].state & C_GET_WAIT))) {
    clear_sslFd(cnts[i].getFd, allset);
  }
  cnts[i].state = C_CLOSED;
  cnts[i].sent_ptr = cnts[i].ptr = cnts[i].length = 0;
  cnts[i].type = 0;
}

int
parse_header(SslFd* sf, char* tab, header* hdr, char https)
{
  int n, i, j, state = 0;
  char tmpt[100];
  if (https) {
    n = SSL_read(SslFd_get_ssl(sf), tab, 9000);
  }
  else {
    n = read(SslFd_get_fd(sf), tab, 9000);
  }
  hdr->allreaded = n;
  i = j = 0;
  memset(tmpt, 0, 100);
  hdr->ptr = 0;
  hdr->length = 0;
  while (i < n) {
    if (j == 99)
      return 1;
    switch (state) {
      case 0:
        if ((tab[i] != ' ') && (tab[i] != '\t')) {
          tmpt[j] = tab[i];
          ++j;
        }
        else {
          tmpt[j] = 0;
          if (strcmp(tmpt, "GET") == 0) {
            hdr->type = H_TYPE_GET;
            state = 1;
            break;
          }
          if (strcmp(tmpt, "POST") == 0) {
            hdr->type = H_TYPE_POST;
            state = 1;
            break;
          }
          if ((strcmp(tmpt, "HTTP/1.0") == 0) || (strcmp(tmpt, "HTTP/1.1") == 0)) {
            hdr->type = H_TYPE_OK;
            state = 6;
            break;
          }
          return 1;
        }
        break;
      case 1:
        if ((tab[i] != ' ') && (tab[i] != '\t')) {
          tmpt[0] = tab[i];
          j = 1;
          state = 2;
        }
        break;
      case 2:
        if (tab[i] != '=') {
          tmpt[j] = tab[i];
          ++j;
        }
        else {
          tmpt[j] = 0;
          if (strcmp(tmpt, "/yahpt.html?id")) {
            return 1;
          }
          j = 0;
          state = 3;
        }
        break;
      case 3:
        if ((tab[i] != ' ') && (tab[i] != '\t')) {
          if (j == 9) {
            return 1;
          }
          hdr->id[j] = tab[i];
          ++j;
        }
        else {
          if (j != 9) {
            return 1;
          }
          hdr->id[j] = 0;
          state = 4;
        }
        break;
      case 4:
        if (tab[i] == '\n')
          state = 5;
        break;
      case 5:
        if (tab[i] == '\n') {
          hdr->ptr = i+1;
          hdr->length = n - hdr->ptr;
          return 0;
        }
        if (tab[i] != '\r') {
          state = 4;
        }
        break;
      case 6:
        if ((tab[i] != ' ') && (tab[i] != '\t')) {
          tmpt[0] = tab[i];
          j = 1;
          state = 7;
        }
        break;
      case 7:
        if ((tab[i] == ' ') || (tab[i] == '\t')) {
          tmpt[j] = 0;
          if (strcmp(tmpt, "200")) {
            return 1;
          }
          state = 4;
        }
        else {
          tmpt[j] = tab[i];
          ++j;
        }
        break;
    }
    ++i;
  }
  return 1;
}

void
set_fd(int fd, int* maxfdp1, fd_set* allset)
{
  FD_SET(fd, allset);
  (*maxfdp1) = ((*maxfdp1) > fd) ? (*maxfdp1) : (fd + 1);
}

void
close_fd(int* fd)
{
  close(*fd);
}

void
clear_fd(int* fd, fd_set* set)
{
  FD_CLR(*fd, set);
  close_fd(fd);
}

int
read_message(int fd, int length, connection* client, char* tab, int ptr)
{
  int j = 0;
  int tmp = 0;
  while (j < length) {
    if (client->curreceived + length-j > client->toreceive) {
      writen(fd, (unsigned char*) (tab+ptr+j), client->toreceive - client->curreceived);
      j += client->toreceive - client->curreceived;
      client->curreceived += client->toreceive - client->curreceived;
      if (client->read_state == 0) {
        switch (tab[ptr + j]) {
          case 'M': {
                      if (j + 5 <= length) {
                        memcpy(&tmp, &tab[ptr + j + 1], 4);
                        client->toreceive = ntohl(tmp);
                        client->curreceived = 0;
                        j += 5;
                      }
                      else if (j + 1 < length) {
                        memcpy(client->readed_length, &tab[ptr + j + 1], length - j - 1);
                        client->read_state = length - j;
                        j += length - j;
                      }
                      else {
                        ++j;
                        client->read_state = 1;
                      }
                      break;
                    }
          case 'T': {
                      ++j;
                      break;
                    }
          default: {
                     return 1;
                   }
        }
      }
      else {
        if (j + 5 - client->read_state <= length) {
          memcpy(&client->readed_length[client->read_state-1], &tab[ptr + j], 5 - client->read_state);
          memcpy(&tmp, client->readed_length, 4);
          client->toreceive = ntohl(tmp);
          client->curreceived = 0;
          j += 5 - client->read_state;
          client->read_state = 0;
        }
        else {
          memcpy(&client->readed_length[client->read_state-1], &tab[ptr + j], length - j);
          client->read_state += length - j;
          j += length -j;
        }
      }
    }
    else {
      client->curreceived += length-j;
      writen(fd, (unsigned char*) (tab+ptr+j), length-j);
      j += length-j;
    }
  }
  return 0;
}

/*
 * Function name: clear sslFd
 * Description: Close the socket encapsulated in SslFd structure, remove this file descriptor
 *              from fd_set and clear ssl structure.
 * Arguments: sf - pointer to SslFd structure
 *            set - pointer to fd_set structure
 */

void
clear_sslFd(SslFd* sf, fd_set* set)
{
  clear_fd((&(sf->fd)), set);
  if (SslFd_get_ssl(sf)) {
    SSL_clear(SslFd_get_ssl(sf));
  }
}

/*
 * Function name: http_write
 * Description: Write the message via http/https proxy.
 * Arguments: https - if the https proxy will be used instead of http proxy
 *            sf - pointer to SslFd structure
 *            buf - buffer containing the data to send
 *            amount - how much butes will be send
 * Returns: The result of writen or SSL_writen function, depending on 'https' value.
 */

int
http_write(char https, SslFd* sf, unsigned char* buf, int amount)
{
  if (https) {
    return SSL_writen(SslFd_get_ssl(sf), buf, amount);
  }
  else {
    return writen(SslFd_get_fd(sf), buf, amount);
  }
}

/*
 * Function name: http_read
 * Description: Read the message via http/https proxy.
 * Arguments: https - if the https proxy will be used instead of http proxy
 *            sf - pointer to SslFd structure
 *            buf - buffer for the received data
 *            amount - how much bytes will be received
 * Returns: The result of read or SSL_read function, depending on 'https' value.
 */

int
http_read(char https, SslFd* sf, unsigned char* buf, int amount)
{
  if (https) {
    return SSL_read(SslFd_get_ssl(sf), buf, amount);
  }
  else {
    return read(SslFd_get_fd(sf), buf, amount);
  }
}