I want my application to create TCP SYN packet and send on network. I did not set IP_HDRINCL
using setsockopt
because I want the kernel to fill the IP Header.
I tried
- using a byte buffer to include TCP Header + Payload
- using a byte buffer to include IP Header + TCP Header + Payload.
Both above methods returned sendto
error -1 and errno is set to 88
Following is the code I used
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <errno.h>
int main()
{
int sockfd;
if(sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP) < 0)
{
printf("sokcet functin failed : \n");
exit (-1);
}
char packet[512];
struct sockaddr_in remote; // remote address
struct iphdr *ip = (struct iphdr *) packet;
struct tcphdr *tcp = (struct tcphdr *) packet + sizeof(struct iphdr);
/*
struct tcphdr *tcp = (struct tcphdr *) packet; // tcp header
*/
remote.sin_family = AF_INET; // family
remote.sin_addr.s_addr = inet_addr("192.168.0.57"); // destination ip
remote.sin_port = htons(atoi("3868")); // destination port
memset(packet, 0, 512); // set packet to 0
tcp->source = htons(atoi("6668")); // source port
tcp->dest = htons(atoi("3868")); // destination port
tcp->seq = htons(random()); // inital sequence number
tcp->ack_seq = htons(0); // acknowledgement number
tcp->ack = 0; // acknowledgement flag
tcp->syn = 1; // synchronize flag
tcp->rst = 0; // reset flag
tcp->psh = 0; // push flag
tcp->fin = 0; // finish flag
tcp->urg = 0; // urgent flag
tcp->check = 0; // tcp checksum
tcp->doff = 5; // data offset
int err;
if((err = sendto(sockfd, packet, sizeof(struct iphdr), 0, (struct sockaddr *)&remote, sizeof(struct sockaddr))) < 0)
{ // send packet
printf("Error: Can't send packet : %d %d !\n\n", err, errno);
return -1;
}
Why i am getting the sendto
error -1 and errno 88?
Also, I want to know how to determine the length of data that is to be used in second argument in sendto
function. If I hard code it to size of packet byte buffer i.e. 512 bytes, is it wrong ?
errno
, per documentation, to determine the cause of the problem. Make sure you present it in a human readable form! – Kuba hasn't forgotten Monicasendto
returning -1 simply indicates that it failed. If you follow the documentation on your platform, you'll note that you then have to examineerrno
to check the cause of the error, and you can usestrerror
to translate "errno 88" to English. Do that. – Kuba hasn't forgotten Monica