/[cvs]/eggdrop1.9/testcode/http.c
ViewVC logotype

Contents of /eggdrop1.9/testcode/http.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Thu Apr 25 04:19:54 2002 UTC (17 years, 11 months ago) by stdarg
Branch: MAIN
File MIME type: text/x-chdr
* sclient.c - test program to telnet using a proxy
* http.c/base64.c - http anonymous and authenticated proxies
* socks5.c - socks5 proxy with support for username/password authentication, ipv4, ipv6, and remote hostname resolution
* socks4.c socks4 proxy (no authentication)

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <netinet/in.h>
6 #include "my_socket.h"
7 #include "sockbuf.h"
8
9 typedef struct {
10 char *host;
11 int port;
12 char *username;
13 char *password;
14 int our_idx, their_idx;
15 int sock;
16 int status;
17 } proxy_info_t;
18
19 static int http_on_read(int idx, sockbuf_iobuf_t *data, proxy_info_t *info);
20 static int http_on_eof_and_err(int idx, void *err, proxy_info_t *info);
21 static int http_on_connect(int idx, void *ignore, proxy_info_t *info);
22
23 sockbuf_event_t http_events = {
24 (Function) 5,
25 (Function) "http proxy",
26 http_on_read,
27 NULL,
28 http_on_eof_and_err,
29 http_on_eof_and_err,
30 http_on_connect
31 };
32
33 static void http_free(proxy_info_t *info)
34 {
35 free(info->host);
36 free(info->username);
37 free(info->password);
38 //linemode_off(info->our_idx);
39 sockbuf_delete(info->our_idx);
40 free(info);
41 }
42
43 static void http_err(proxy_info_t *info, int err)
44 {
45 errno = err;
46 sockbuf_filter(info->their_idx, SOCKBUF_ERR, -1, (void *)err);
47 http_free(info);
48 }
49
50 static int http_on_read(int idx, sockbuf_iobuf_t *data, proxy_info_t *info)
51 {
52 if (!strncasecmp("HTTP/1.0 200", data->data, 12)) {
53 /* Yay, we're approved. */
54 info->status = 1;
55 return(0);
56 }
57
58 if (data->len != 0) {
59 /* Just some header that we don't care about. */
60 return(0);
61 }
62
63 /* It's the blank line, so the http response is done. */
64
65 if (info->status != 1) {
66 /* Boo, we weren't approved. */
67 http_err(info, ECONNREFUSED);
68 return(0);
69 }
70
71 /* Successful, switch it over. */
72 sockbuf_set_sock(info->our_idx, -1, 0);
73 sockbuf_set_sock(info->their_idx, info->sock, SOCKBUF_CLIENT);
74 http_free(info);
75
76 return(0);
77 }
78
79 static int http_on_eof_and_err(int idx, void *err, proxy_info_t *info)
80 {
81 if (!err) err = (void *)ECONNREFUSED;
82 http_err(info, (int) err);
83 return(0);
84 }
85
86 static void make_password(char *dest, proxy_info_t *info)
87 {
88 char buf[512];
89 sprintf(buf, "%s %s", info->username, info->password);
90
91 b64enc_buf(buf, strlen(buf), dest);
92 }
93
94 static int http_on_connect(int idx, void *ignore, proxy_info_t *info)
95 {
96 char request[512];
97
98 sprintf(request, "CONNECT %s:%d HTTP/1.0\r\n", info->host, info->port);
99 if (strlen(info->username)) {
100 char buf[512], auth[512];
101
102 make_password(auth, info);
103 sprintf(buf, "Proxy-authenticate: basic %s\r\n", auth);
104 strcat(request, buf);
105 }
106 strcat(request, "\r\n");
107
108 sockbuf_write(idx, request, strlen(request));
109
110 return(0);
111 }
112
113 int http_connect(int idx, char *proxy_host, int proxy_port, char *username, char *password, char *dest_ip, int dest_port)
114 {
115 int sock;
116 proxy_info_t *info;
117
118 sock = socket_create(proxy_host, proxy_port, NULL, 0, SOCKET_CLIENT|SOCKET_TCP|SOCKET_NONBLOCK);
119 if (sock < 0) return(-1);
120
121 info = (proxy_info_t *)malloc(sizeof(*info));
122 info->host = strdup(dest_ip);
123 info->port = dest_port;
124 if (!username) username = "";
125 if (!password) password = "";
126 info->username = strdup(username);
127 info->password = strdup(password);
128
129 info->our_idx = sockbuf_new(sock, SOCKBUF_CLIENT);
130 linemode_on(info->our_idx);
131
132 if (idx >= 0) info->their_idx = idx;
133 else info->their_idx = sockbuf_new(-1, 0);
134
135 info->sock = sock;
136
137 info->status = 0;
138
139 sockbuf_set_handler(info->our_idx, http_events, info);
140
141 return(info->their_idx);
142 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23