0
votes

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

  1. using a byte buffer to include TCP Header + Payload
  2. 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 ?

2
"Can anyone help me in knowing the reason behind sendto error ?" No, but you can help yourself. Check the errno, per documentation, to determine the cause of the problem. Make sure you present it in a human readable form!Kuba hasn't forgotten Monica
edited my questionSubbu
The "errno 88" is not helpful. sendto returning -1 simply indicates that it failed. If you follow the documentation on your platform, you'll note that you then have to examine errno to check the cause of the error, and you can use strerror to translate "errno 88" to English. Do that.Kuba hasn't forgotten Monica
strerror(errno) resulted in segmentation fault.Subbu

2 Answers

0
votes

Here I am not giving the solution but you can check the meaning of your error and can correct your error by and also I can not comment because I haven't much reputation.. I just want to help...

int err = sendto(sockfd, packet, sizeof(struct iphdr), 0, (struct sockaddr *)&remote, sizeof(struct sockaddr)));
int saved_error = errno;  // Save errno flag that is set by sendto.
if(err < 0) {
    fprintf(stderr, "Send Error: %s\n", strerror(saved_error));
}

I think it may help...But this is not the answer...This will give the meaning of error so that you can correct your error..

0
votes

For the SOCK_RAW you can use AF_ATMPVC: Access to raw ATM PVCs

In your code:

if((sockfd = socket(AF_ATMPVC, SOCK_RAW, IPPROTO_TCP)) < 0)

I hope it might be helpful