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; }
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) {
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));
}
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");
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;
}
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();
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;
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();
}
}