linux raw socket programming question

Posted by user194420 on Stack Overflow See other posts from Stack Overflow or by user194420
Published on 2009-10-22T09:18:48Z Indexed on 2010/04/30 6:07 UTC
Read the original article Hit count: 280

Filed under:
|
|

Hi all,

I am trying to create a raw socket which send and receive message with ip/tcp header under linux. I can successfully binds to a port and receive tcp message(ie:syn) However, the message seems to be handled by the os, but not mine. I am just a reader of it(like wireshark). My raw socket binds to port 8888, and then i try to telnet to that port . In wireshark, it shows that the port 8888 reply a "rst ack" when it receive the "syn" request. In my program, it shows that it receive a new message and it doesnot reply with any message.

Any way to actually binds to that port?(prevent os handle it)

Here is part of my code, i try to cut those error checking for easy reading

sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);

int tmp = 1;
const int *val = &tmp;
setsockopt (sockfd, IPPROTO_IP, IP_HDRINCL, val, sizeof (tmp));

servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(8888);
bind(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr));

//call recv in loop

© Stack Overflow or respective owner

Related posts about raw

Related posts about socket