event_loop_once(ev);
ret = recv(s, pkt, RCVPKTSIZE, MSG_TRUNC);
- if (ret<40) {
+ if (ret < sizeof(*eth)+sizeof(*ip)) {
continue;
}
}
/* IP */
- ip = (struct iphdr *)&pkt[14];
+ ip = (struct iphdr *)(eth+1);
/* We only want IPv4 packets */
if (ip->version != 4) {
continue;
continue;
}
+ /* make sure its not a short packet */
+ if (offsetof(struct tcphdr, ack_seq) + 4 +
+ (ip->ihl*4) + sizeof(*eth) > ret) {
+ continue;
+ }
+
/* TCP */
- tcp = (struct tcphdr *)&pkt[14+ip->ihl*4];
+ tcp = (struct tcphdr *)((ip->ihl*4) + (char *)ip);
+
/* We only want replies from the port we tickled */
if (tcp->source != dst->sin_port) {
continue;