/*
net.c -- most of the network code
- Copyright (C) 1998,99 Ivo Timmermans <zarq@iname.com>
+ Copyright (C) 1998,1999,2000 Ivo Timmermans <zarq@iname.com>
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
real_packet_t rp;
cp
do_encrypt((vpn_packet_t*)packet, &rp, cl->key);
- rp.from = myself->vpn_ip;
+ rp.from = htonl(myself->vpn_ip);
+ rp.data.len = htons(rp.data.len);
+ rp.len = htons(rp.len);
if(debug_lvl > 3)
- syslog(LOG_ERR, "Sent %d bytes to %lx", rp.len, cl->vpn_ip);
+ syslog(LOG_ERR, "Sent %d bytes to %lx", ntohs(rp.len), cl->vpn_ip);
- if((r = send(cl->socket, (char*)&rp, rp.len, 0)) < 0)
+ if((r = send(cl->socket, (char*)&rp, ntohs(rp.len), 0)) < 0)
{
syslog(LOG_ERR, "Error sending data: %m");
return -1;
return xsend(cl, packet);
}
-int send_broadcast(conn_list_t *cl, vpn_packet_t *packet)
-{
- conn_list_t *p;
-cp
- for(p = cl; p != NULL; p = p->next)
- if(send_packet(p->real_ip, packet) < 0)
- {
- syslog(LOG_ERR, "Could not send a broadcast packet to %08lx (%08lx): %m",
- p->vpn_ip, p->real_ip);
- break; /* FIXME: should retry later, and send a ping over the metaconnection. */
- }
-cp
- return 0;
-}
-
/*
open the local ethertap device
*/
p->real_ip = ntohl(ci.sin_addr.s_addr);
p->meta_socket = sfd;
p->status.meta = 1;
-
+ p->buflen = 0;
+
syslog(LOG_NOTICE, "Connection from %s:%d", p->hostname, htons(ci.sin_port));
if(send_basic_info(p) < 0)
return -1;
}
total_socket_in += lenin;
+
+ rp.data.len = ntohs(rp.data.len);
+ rp.len = ntohs(rp.len);
+ rp.from = ntohl(rp.from);
+
if(rp.len >= 0)
{
f = lookup_conn(rp.from);
int handle_incoming_meta_data(conn_list_t *cl)
{
int x, l = sizeof(x);
- unsigned char tmp[1600];
- int request;
+ int request, oldlen, i;
int lenin = 0;
cp
if(getsockopt(cl->meta_socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
return -1;
}
- if(read(cl->meta_socket, &tmp, 1) <= 0)
+ if(cl->buflen >= MAXBUFSIZE)
{
- syslog(LOG_ERR, "Receive failed: %m");
+ syslog(LOG_ERR, "Metadata read buffer full! Discarding contents.");
+ cl->buflen = 0;
+ }
+
+ lenin = read(cl->meta_socket, cl->buffer, MAXBUFSIZE-cl->buflen);
+
+ if(lenin<=0)
+ {
+ syslog(LOG_ERR, "Metadata socket read error: %m");
return -1;
}
- request = (int)(tmp[0]);
+ oldlen = cl->buflen;
+ cl->buflen += lenin;
- if(debug_lvl > 3)
- syslog(LOG_DEBUG, "got request %d", request);
-
- /* This is a hack. After an ACK request, multiple ADD_HOSTs can
- follow. So if the request is one of these, only read as much
- bytes as necessary. (Luckily the ADD_HOST request is of fixed
- length) :P -- ivo */
-
- if(request != ACK)
+ for(;;)
{
- if(request == ADD_HOST)
- {
- if((lenin = read(cl->meta_socket, &tmp[1], sizeof(add_host_t) - 1)) <= 0)
- {
- syslog(LOG_ERR, "Receive failed for ADD_HOST: %m");
- return -1;
- }
- }
+ cl->reqlen = 0;
+
+ for(i = oldlen; i < cl->buflen; i++)
+ {
+ if(cl->buffer[i] == '\n')
+ {
+ cl->buffer[i] = 0; /* replace end-of-line by end-of-string so we can use sscanf */
+ cl->reqlen = i + 1;
+ break;
+ }
+ }
+
+ if(cl->reqlen)
+ {
+ if(sscanf(cl->buffer, "%d", &request) == 1)
+ {
+ if(request_handlers[request] == NULL)
+ {
+ syslog(LOG_ERR, "Unknown request: %s", cl->buffer);
+ return 0;
+ }
+
+ if(debug_lvl > 3)
+ syslog(LOG_DEBUG, "Got request: %s", cl->buffer);
+
+ request_handlers[request](cl);
+ }
+ else
+ {
+ syslog(LOG_ERR, "Bogus data received: %s", cl->buffer);
+ }
+
+ cl->buflen -= cl->reqlen;
+ memmove(cl->buffer, cl->buffer + cl->reqlen, cl->buflen);
+ oldlen = 0;
+ }
else
- {
- if((lenin = read(cl->meta_socket, &tmp[1], sizeof(tmp) - 1)) <= 0)
- {
- if(errno != EAGAIN) /* talk about hacks... */
- {
- syslog(LOG_ERR, "Receive failed: %m");
- return -1;
- }
- }
- }
+ {
+ break;
+ }
}
-
- lenin++;
- if(request_handlers[request] == NULL)
- syslog(LOG_ERR, "Unknown request %d.", request);
- else
- if(request_handlers[request](cl, tmp, lenin) < 0)
- return -1;
cp
return 0;
}