SOCK_RAW Problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hallo everybody,
i'm writing a user program that read data from a
device interface e.g. /dev/ker2usr and send it direct
via the network card interface using raw socket. But i
have the problem that the data i receive, isn't what i
expect. I capture the packet exchange with tcpdump
(see below).
Below i give the source code and the output of
tcpdump. 
It would be great if someone could tell me why it
doesn't function.
Thank for the help.

The source code:

int main(int argc, char **argv)
{
   int fd_ker2usr, ret_read, sockfd;
   FILE *fd_log;
   unsigned char message[576];
   int i = 0, j = 1, k = 0;
   unsigned long hex_tab[576];
   unsigned char buffer[576];
   int on = 1;
   struct sockaddr_ll mysocket;
   unsigned char hex[2];
   unsigned char dest_mac_addr[6] = {0x00, 0xe0, 0x2b,
0x6f, 0x1c, 0x00},
                 src_mac_addr[6] = {0x00, 0x10, 0x4b,
0x42, 0x2d, 0xd3};
   unsigned short h_proto = ETH_P_IP;

   if (argc < 1) {
      fprintf(stdout, "usage: %s saddr\n", argv[0]);
      exit(-1);
   }

   /* Open the device /dev/ker2usr, and read data
inside */
   fd_ker2usr = open("/dev/ker2usr", O_RDONLY);
   if (fd_ker2usr < 0) {
      perror("fopen ker2usr");
      exit(-1);
   }

   if ((sockfd = socket(AF_PACKET,
SOCK_RAW,htons(ETH_P_IP))) < 0) {
      perror("socket");
      exit(-1);
   }

   /* Form the destination address for sendto */
   mysocket.sll_family = AF_PACKET;
   mysocket.sll_protocol = 2048;       /* Correspond
to 0x0800 for ETH_P_IP*/
   mysocket.sll_ifindex = 2;           /* Correspond
to eth0 device */
   mysocket.sll_hatype = ARPHRD_ETHER;
   mysocket.sll_halen = ETH_ALEN;
   mysocket.sll_addr[0] = 0x00;
   mysocket.sll_addr[1] = 0x0e;
   mysocket.sll_addr[2] = 0x2b;
   mysocket.sll_addr[3] = 0x6f;
   mysocket.sll_addr[4] = 0x1c;
   mysocket.sll_addr[5] = 0x00;
   mysocket.sll_addr[6] = 0x00;
   mysocket.sll_addr[7] = 0x00;


   while ((ret_read = read(fd_ker2usr, message, 576))
> 0) {
      k = 14;
      j = 1;
      for (i = 0; i < ret_read; i+=2) {
         sprintf(hex, "%c%c", message[i], message[j]);
         hex_tab[k++] = strtoul(hex, NULL, 16);
         j+=2;
      }
      /* Set the destination and source mac_addresses
*/
      for ( i = 0; i < 6; i++) {
         hex_tab[i] = dest_mac_addr[i];
      }
      for ( i = 6; i < 12; i++) {
         hex_tab[i] = src_mac_addr[i-6];
      }
      hex_tab[12] = 0x08;
      hex_tab[13] = 0x00;

      for (i = 0; i < ret_read/2+14; i++) {
         buffer[i] = (unsigned char)(hex_tab[i]);
         fprintf(stdout, "%02x%s", buffer[i],
               i == ret_read/2 + 13? "\n": " ");
      }

      /* Send the prepared buffer into the network
layer 
       * Where it will be routed to the corresponding 
       * netdevice interface
       */
      if(sendto(sockfd, buffer,
ret_read/2+14,0x0,(struct sockaddr *)&mysocket,
                   sizeof(mysocket)) < 0) {
         perror("sendto");
         exit(-1);
      }
      printf("send done\n");
   }
   close(fd_ker2usr);
   close(sockfd);
}

- What is wrong in this code? 
- Why does it give this output ?

Output of tcpdump:
18:33:03.800401 > 0:0:0:0:0:0 0:10:4b:42:2d:d3 0008
98: null > sap 45 I (s=0,r=42,P) len=80
0000 4000 3e01 f355 c0a8 6401 c0a8 6401
0800 ce53 ff03 0a00 4f37 ba3a 2033 0c00
0809 0a0b 0c0d 0e0f 1011 1213 1415 1617
1819 1a1b 1c1d 1e1f 2021 2223 2425 2627
2829 2a2b 2c2d 2e2f 3031 3233 3435 3637
4500 0054 0000 4000 3e01 f355 c0a8 6401
c0a8 6401 0800 ce53 ff03 0a00 4f37 ba3a
2033 0c00 0809 0a0b 0c0d 0e0f 1011 1213
1415 1617 1819 1a1b 1c1d 1e1f 2021 2223
2425 2627 2829 2a2b 2c2d 2e2f 3031 3233
3435 3637


As it seems to be two packets were concatened.
Why?

Thanks. 



__________________________________________________________________
Do You Yahoo!?
Gesendet von Yahoo! Mail - http://mail.yahoo.de
-
: send the line "unsubscribe linux-net" in
the body of a message to majordomo@vger.kernel.org


[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux 802.1Q VLAN]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Git]     [Bugtraq]     [Yosemite News and Information]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux PCI]     [Linux Admin]     [Samba]

  Powered by Linux