Raw socket and sendto()

This is a discussion on Raw socket and sendto() within the Networking/Device Communication forums, part of the General Programming Boards category; Okay here we go: With raw sockets you are suppose to be able to access the hole packet. without any ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    4

    Raw socket and sendto()

    Okay here we go:

    With raw sockets you are suppose to be able to access the hole packet. without any adding data from the kernel.
    But why do then have to give the destination and port, when calling sendto().
    Code:
    sendto(int sockfd, const void *msg, int len, unsigned int flags,
    const struct sockaddr *to, socklen_t tolen);
    
    /* As you can see, this call is basically the same as the call to send() with the addition
    of two other pieces of information. to is a pointer to a struct sockaddr (which you'll
    probably have as a struct sockaddr_in and cast it at the last minute) which contains the
    destination IP address and port. tolen, an int deep-down, can simply be set to sizeof *to or
    sizeof(struct sockaddr). */
    Thanks in advance!
    Last edited by matB; 07-08-2008 at 10:55 AM.

  2. #2
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    sendto(2) is not confined to sending data on raw sockets. It is also used for UDP sockets which require a port number. While using it for raw sockets the port number need not be set.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    4
    Quote Originally Posted by mageshd84 View Post
    sendto(2) is not confined to sending data on raw sockets. It is also used for UDP sockets which require a port number. While using it for raw sockets the port number need not be set.
    What about the ip?

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    Raw sockets only reduces the work done by the kernel. There is an option IP_HDRINCL for the socket, which when NOT set, the kernel will add an IP header before sending the data on the network. If the option is set then the user process can create an IP header for the data to be sent.

  5. #5
    FOSS Enthusiast
    Join Date
    Jun 2008
    Location
    Germany
    Posts
    64
    Quote Originally Posted by matB View Post
    What about the ip?
    Look at the 5th argument of sendto(), its const struct sockaddr *to.

    You pass a struct sockaddr_in to it by typecast. The struct sockaddr_in looks like this:

    Code:
    struct sockaddr_in {
        short            sin_family;
        unsigned short   sin_port;
        struct in_addr   sin_addr;
        char             sin_zero[8];
    };
    struct in_addr looks like this:
    Code:
    struct in_addr {
        unsigned long s_addr;
    };
    So to store the ip 127.0.0.1, the port 1234, and AF_INET on the struct sockaddr_in, you'd do the following:

    Code:
    struct sockaddr_in self;
    self.sin_family = AF_INET;
    self.sin_addr.s_addr = inet_addr("127.0.0.1"); // you can also use inet_aton()
    self.sin_port = htons(1234);
    memset(self.sin_zero, '\0', sizeof(self.sin_zero));
    Now, to send data to that ip, do the following:
    (I'm assuming sock is already created and everything)

    Code:
    char data[16] = "Hello, world!";
    if(sendto(sock, data, strlen(data), 0, (struct sockaddr*)&self, sizeof(self)) == -1)
        fprintf(stderr, "sendto() failed\n");
    Edit: Imho raw-sockets aren't very useful because they increase the complexity of your code and make it more prone to vulnerabilities and errors. You should use TCP or UDP sockets, they are more convenient to use and more stable, reliable. The chance that your code is faster than the kernel's ip stack is by the way very low.
    Last edited by mkruk; 07-10-2008 at 06:12 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Raw socket
    By like_no_other in forum Networking/Device Communication
    Replies: 4
    Last Post: 03-28-2009, 03:05 PM
  2. SOcket programming trouble
    By sodja in forum Networking/Device Communication
    Replies: 1
    Last Post: 01-11-2008, 10:20 AM
  3. sendto Socket packet size
    By bj00 in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-27-2007, 12:05 PM
  4. Datagram Unix socket problem
    By karas in forum C Programming
    Replies: 9
    Last Post: 07-27-2006, 11:08 AM
  5. Socket Datagram info sendto(), recvfrom()
    By siluro in forum C Programming
    Replies: 1
    Last Post: 02-20-2005, 12:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21