X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/userv-utils/blobdiff_plain/aaa9ab3ad43917293ff70ee9cd3654906118fc08..62ccb81a567f14d36a0c62964ca70c5e035f573e:/ipif/forwarder.c?ds=inline diff --git a/ipif/forwarder.c b/ipif/forwarder.c index 9b7e51e..a9e43e0 100644 --- a/ipif/forwarder.c +++ b/ipif/forwarder.c @@ -107,9 +107,12 @@ static void inbound(void) { int r, i, different, this_saddrlen; const char *emsg; + buf_in.start= buf_in.base+1; + buf_in.size= buffer_size-2; + setnonblock(public_local_fd,1); this_saddrlen= sizeof(this_saddr); - r= recvfrom(public_local_fd, buf_in.base, buffer_size-1, 0, + r= recvfrom(public_local_fd, buf_in.start, buf_in.size, 0, &this_saddr, &this_saddrlen); if (!r) { diag("empty ciphertext"); return; } @@ -118,14 +121,14 @@ static void inbound(void) { return; } if (this_saddr.sin_family != AF_INET) { - fprintf(stderr,"%s: received unknown AF %lu", + fprintf(stderr,"%s: received unknown AF %lu\n", programid, (unsigned long)this_saddr.sin_family); return; } assert(this_saddrlen == sizeof(this_saddr)); - buf_in.size= buffer_size; - buf_in.start= buf_in.base; + assert(r <= buf_in.size); + buf_in.size= r; for (i=n_mechs-1; i>=0; i--) { emsg= mechs[i]->decode(md_in[i],&buf_in); if (emsg) { @@ -136,21 +139,22 @@ static void inbound(void) { alarm(timeout); - different= !public_remote_specd || - memcmp(&this_saddr,&public_remote,sizeof(this_saddr)); + different= (!public_remote_specd || + public_remote.sin_addr.s_addr != this_saddr.sin_addr.s_addr || + public_remote.sin_port != this_saddr.sin_port); if (different) { if (public_remote_specd==2) { - fprintf(stderr, "%s: packet from unexpected sender %s:%lu", + fprintf(stderr, "%s: packet from unexpected sender %s:%lu\n", programid, inet_ntoa(this_saddr.sin_addr), - (unsigned long)this_saddr.sin_port); + (unsigned long)ntohs(this_saddr.sin_port)); return; } - fprintf(stderr, "%s: tunnel open with peer %s:%lu", + fprintf(stderr, "%s: tunnel open with peer %s:%lu\n", programid, inet_ntoa(this_saddr.sin_addr), - (unsigned long)this_saddr.sin_port); + (unsigned long)ntohs(this_saddr.sin_port)); nextsendka= now(); public_remote_specd= 1; memcpy(&public_remote,&this_saddr,sizeof(public_remote)); @@ -162,10 +166,14 @@ static void inbound(void) { } any_recvd= 1; - - buf_in.start[buf_in.size]= 0300; - *--buf_in.start= 0300; - buf_in.size+= 2; + + if (!buf_in.size || *buf_in.start != 0300) { + *--buf_in.start= 0300; + buf_in.size++; + } + if (buf_in.start[buf_in.size-1] != 0300) { + buf_in.start[buf_in.size++]= 0300; + } setnonblock(private_in_fd,0); write_must(private_in_fd, buf_in.start, buf_in.size, "write down"); @@ -213,7 +221,7 @@ static void outbound(void) { after_eaten= accum_buf; while ((delim= memchr(after_eaten, 0300, accum_used))) { this_packet= delim - after_eaten; - sendpacket(after_eaten, this_packet); + if (this_packet) sendpacket(after_eaten, this_packet); accum_used -= this_packet+1; after_eaten = delim+1; } @@ -248,8 +256,9 @@ int main(int argc, const char *const *const argv_in) { arg= getarg_string(); if (*arg) { public_remote_specd= 1; + public_remote.sin_family= AF_INET; arg_assert(inet_aton(arg,&public_remote.sin_addr)); - public_remote.sin_port= getarg_ulong(); + public_remote.sin_port= htons(getarg_ulong()); } encdec_keys_fd= getarg_ulong(); @@ -280,7 +289,7 @@ int main(int argc, const char *const *const argv_in) { if (keepalive) { tnow= now(); - if (tnow >= nextsendka) sendpacket("\300",1); + if (tnow >= nextsendka && public_remote_specd) sendpacket("\300",1); polltimeout= (nextsendka - tnow)*1000; } else { polltimeout= -1; @@ -291,7 +300,7 @@ int main(int argc, const char *const *const argv_in) { if (r==-1 && errno==EINTR) continue; if (r==-1) sysfail("poll"); - if (pollfds[0].revents & POLLIN) inbound(); - if (pollfds[1].revents & POLLOUT) outbound(); + if (pollfds[0].revents & (POLLIN|POLLERR)) inbound(); + if (pollfds[1].revents & (POLLIN|POLLERR)) outbound(); } }