Hello, I have created raw socket as below
rawSockfd = socket(AF_INET,SOCK_RAW,IPPROTO_IP)
Added flag 5 sec SO_RCVTIMEO, IP_HDRINCL to 1 via setsockopt.
Sending IP Packet as below:
struct sockaddr_in connection = getSockAddr(dstIPAddress);
long bytes = sendto(rawSockfd, (uint8_t *)packet, size, 0, (struct sockaddr *)&connection, sizeof(struct sockaddr));
I am trying to receive as below:
long rsize = recvfrom(rawSock, buffer, size, 0, (struct sockaddr *)&connection, (socklen_t *)&addrlen);
This works fine for ICMP, UDP. recvfrom able to read packet back.
We are facing issue during TCP. recvfrom returns error: Resource temporarily unavailable after 5 sec timeout. If we remove timeout flag SO_RCVTIMEO then it gets stuck forever.
TCPdump shows following logs on destination. Instead of SYN ACK it's getting Reset:
09:21:03.972632 IP 10.215.179.1.54745 > 10.207.134.154.8181: Flags [SEW], seq 358899317, win 65535, options [mss 1380,nop,wscale 6,nop,nop,TS val 426499980 ecr 0,sackOK,eol], length 0
09:21:03.972755 IP 10.207.134.154.8181 > 10.215.179.1.54745: Flags [R.], seq 0, ack 358899318, win 0, length 0
is this something macOS not sending TCP response back to rawsocket or something is wrong in my code?
Raw Socket recvfrom not working for TCP
This is not going to work on any BSD Sockets implementation. You can’t use raw sockets to read TCP or UDP. To quote my trusty (and dusty, literally!) copy of Stevens [1]:
The following rules apply;
- Received UDP packets and received TCP packets are never [their emphasis] passed to a raw socket.
This is UNIX Network Programming, Volume 1, Second Edition, section 25.4 Raw Socket Input, page 659.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
[1] http://www.unpbook.com