- perror("receive");
- break;
- }
- alarm(0);
- if (trace)
- tpacket("received", tp, n);
-#if vax || pdp11
- tp->th_opcode = ntohs(tp->th_opcode);
- tp->th_block = ntohs(tp->th_block);
-#endif
- if (tp->th_opcode == ERROR) {
- printf("Error code %d: %s\n", tp->th_code,
- tp->th_msg);
- break;
- }
- if (tp->th_opcode != DATA || block != tp->th_block)
- goto again;
- size = write(fd, tp->th_data, n - 4);
+ if (n < 0) {
+ perror("tftp: recvfrom");
+ aborted = 1;
+ goto done;
+ }
+ if (to.sin_addr.s_addr != from.sin_addr.s_addr) {
+ tpacket("discarded (wrong host)",
+ &from, rtp, n);
+ goto again;
+ }
+ if (to.sin_port = sin.sin_port)
+ to.sin_port = from.sin_port;
+ if (to.sin_port != from.sin_port) {
+ tpacket("discarded (wrong port)",
+ &from, rtp, n);
+ goto again;
+ }
+ if (trace)
+ tpacket("received", &from, rtp, n);
+ rtp->th_opcode = ntohs(rtp->th_opcode);
+ rtp->th_block = ntohs(rtp->th_block);
+ if (rtp->th_opcode == ERROR) {
+ printf("Error code %d: %s\n", rtp->th_code,
+ rtp->th_msg);
+ aborted = 1;
+ goto done;
+ }
+ } while (rtp->th_opcode != DATA && rtp->th_block != block);
+ size = write(fd, rtp->th_data, n - 4);