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
|
#include "links.h"
/*
#define LOG_TRANSFER "/tmp/log"
*/
#ifdef LOG_TRANSFER
void log_data(unsigned char *data, int len)
{
int fd;
if ((fd = open(LOG_TRANSFER, O_WRONLY | O_APPEND | O_CREAT, 0622)) != -1) {
write(fd, data, len);
close(fd);
}
}
#else
#define log_data(x, y)
#endif
void exception(struct connection *c)
{
setcstate(c, S_EXCEPT);
retry_connection(c);
}
void close_socket(int *s)
{
if (*s == -1) return;
close(*s);
set_handlers(*s, NULL, NULL, NULL, NULL);
*s = -1;
}
void connected(struct connection *);
struct conn_info {
void (*func)(struct connection *);
struct sockaddr_in sa;
ip addr;
int port;
int *sock;
};
void dns_found(struct connection *, int);
void make_connection(struct connection *c, int port, int *sock, void (*func)(struct connection *))
{
unsigned char *host;
struct conn_info *b;
if (!(host = get_host_name(c->url))) {
setcstate(c, S_INTERNAL);
abort_connection(c);
return;
}
if (!(b = mem_alloc(sizeof(struct conn_info)))) {
mem_free(host);
setcstate(c, S_OUT_OF_MEM);
retry_connection(c);
return;
}
b->func = func;
b->sock = sock;
b->port = port;
c->buffer = b;
setcstate(c, S_DNS);
if (c->no_cache >= NC_RELOAD) find_host_no_cache(host, &b->addr, &c->dnsquery, (void(*)(void *, int))dns_found, c);
else find_host(host, &b->addr, &c->dnsquery, (void(*)(void *, int))dns_found, c);
mem_free(host);
}
int get_pasv_socket(struct connection *c, int cc, int *sock, unsigned char *port)
{
int s;
struct sockaddr_in sa;
struct sockaddr_in sb;
int len = sizeof(sa);
if (getsockname(cc, (struct sockaddr *)&sa, &len)) {
e:
setcstate(c, -errno);
retry_connection(c);
return -2;
}
if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) goto e;
*sock = s;
fcntl(s, F_SETFL, O_NONBLOCK);
again:
memcpy(&sb, &sa, sizeof(struct sockaddr_in));
sb.sin_port = 0;
if (bind(s, (struct sockaddr *)&sb, sizeof sb)) goto e;
len = sizeof(sa);
if (getsockname(s, (struct sockaddr *)&sa, &len)) goto e;
if (listen(s, 1)) goto e;
memcpy(port, &sa.sin_addr.s_addr, 4);
memcpy(port + 4, &sa.sin_port, 2);
#ifdef IP_TOS
{
int on = IPTOS_THROUGHPUT;
setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int));
}
#endif
return 0;
}
void dns_found(struct connection *c, int state)
{
int s;
struct conn_info *b = c->buffer;
if (state) {
setcstate(c, S_NO_DNS);
retry_connection(c);
return;
}
setcstate(c, S_CONN);
if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
setcstate(c, -errno);
retry_connection(c);
return;
}
*b->sock = s;
fcntl(s, F_SETFL, O_NONBLOCK);
memset(&b->sa, 0, sizeof(struct sockaddr_in));
b->sa.sin_family = PF_INET;
b->sa.sin_addr.s_addr = b->addr;
b->sa.sin_port = htons(b->port);
if (connect(s, (struct sockaddr *)&b->sa, sizeof b->sa)) {
if (errno != EALREADY && errno != EINPROGRESS) {
setcstate(c, -errno);
retry_connection(c);
return;
}
set_handlers(s, NULL, (void(*)(void *))connected, (void(*)(void *))exception, c);
} else {
c->buffer = NULL;
b->func(c);
mem_free(b);
}
}
void connected(struct connection *c)
{
struct conn_info *b = c->buffer;
void (*func)(struct connection *) = b->func;
c->buffer = NULL;
if (!connect(*b->sock, (struct sockaddr *)&b->sa, sizeof b->sa) || errno == EISCONN) {
mem_free(b);
func(c);
return;
}
mem_free(b);
setcstate(c, -errno);
retry_connection(c);
}
struct write_buffer {
int sock;
int len;
int pos;
void (*done)(struct connection *);
unsigned char data[1];
};
void write_select(struct connection *c)
{
struct write_buffer *wb;
int wr;
if (!(wb = c->buffer)) {
internal("write socket has no buffer");
setcstate(c, S_INTERNAL);
abort_connection(c);
return;
}
/*printf("ws: %d\n",wb->len-wb->pos);
for (wr = wb->pos; wr < wb->len; wr++) printf("%c", wb->data[wr]);
printf("-\n");*/
if ((wr = write(wb->sock, wb->data + wb->pos, wb->len - wb->pos)) <= 0) {
setcstate(c, wr ? -errno : S_CANT_WRITE);
retry_connection(c);
return;
}
/*printf("wr: %d\n", wr);*/
if ((wb->pos += wr) == wb->len) {
void (*f)(struct connection *) = wb->done;
c->buffer = NULL;
set_handlers(wb->sock, NULL, NULL, NULL, NULL);
mem_free(wb);
f(c);
}
}
void write_to_socket(struct connection *c, int s, unsigned char *data, int len, void (*write_func)(struct connection *))
{
struct write_buffer *wb;
log_data(data, len);
if (!(wb = mem_alloc(sizeof(struct write_buffer) + len))) {
setcstate(c, S_OUT_OF_MEM);
abort_connection(c);
return;
}
wb->sock = s;
wb->len = len;
wb->pos = 0;
wb->done = write_func;
memcpy(wb->data, data, len);
if (c->buffer) mem_free(c->buffer);
c->buffer = wb;
set_handlers(s, NULL, (void (*)())write_select, (void (*)())exception, c);
}
#define READ_SIZE 16384
void read_select(struct connection *c)
{
struct read_buffer *rb;
int rd;
if (!(rb = c->buffer)) {
internal("read socket has no buffer");
setcstate(c, S_INTERNAL);
abort_connection(c);
return;
}
set_handlers(rb->sock, NULL, NULL, NULL, NULL);
if (!(rb = mem_realloc(rb, sizeof(struct read_buffer) + rb->len + READ_SIZE))) {
setcstate(c, S_OUT_OF_MEM);
abort_connection(c);
return;
}
c->buffer = rb;
if ((rd = read(rb->sock, rb->data + rb->len, READ_SIZE)) <= 0) {
if (rb->close && !rd) {
rb->close = 2;
rb->done(c, rb);
return;
}
setcstate(c, rd ? -errno : S_CANT_READ);
/*mem_free(rb);*/
retry_connection(c);
return;
}
log_data(rb->data + rb->len, rd);
rb->len += rd;
rb->done(c, rb);
}
struct read_buffer *alloc_read_buffer(struct connection *c)
{
struct read_buffer *rb;
if (!(rb = mem_alloc(sizeof(struct read_buffer) + READ_SIZE))) {
setcstate(c, S_OUT_OF_MEM);
abort_connection(c);
return NULL;
}
memset(rb, 0, sizeof(struct read_buffer));
return rb;
}
void read_from_socket(struct connection *c, int s, struct read_buffer *buf, void (*read_func)(struct connection *, struct read_buffer *))
{
buf->done = read_func;
buf->sock = s;
if (c->buffer && buf != c->buffer) mem_free(c->buffer);
c->buffer = buf;
set_handlers(s, (void (*)())read_select, NULL, (void (*)())exception, c);
}
void kill_buffer_data(struct read_buffer *rb, int n)
{
if (n > rb->len || n < 0) {
internal("called kill_buffer_data with bad value");
rb->len = 0;
return;
}
memcpy(rb->data, rb->data + n, rb->len - n);
rb->len -= n;
}
|