c - raw socket send two packets on a virtual Network -
i've problem sendto function in code, when try send raw ethernet packet.
i use ubuntu 12.04.01 lts, 2 tap devices connected on 2 vde_switches , dpipe
example:
my send programm create packet below, programm binded socket tap0 , send packet tap1. on tap1 1 receiver wait packets on socket.
my raw ethernet packet looks so:
destination addr ____source addr _________ type/length ___data
00:00:01:00:00:00___00:00:01:00:00:01____ length in byte__some data
example packet send:
00:00:01:00:00:00 00:00:01:00:00:01 (length in byte) (message)test
but programm generate 2 packets, when in wireshark:
first packet ipx packet , [malformed packet] , looks in hex (data = test)
00 04 00 01 00 06 00 00 01 00 00 01 00 00 00 01 74 65 73 74 00 linux cooked capture packet type: sent (4) link-layer address type: 1 link-layer address length: 6 source: 00:00:01:00:00:01 protocol: raw 802.3 (0x0001) [malformed packet: ipx]
second packet unknown protocol
00 00 00 01 00 06 00 00 01 00 00 01 00 00 31 00 74 65 73 74 00 linux cooked capture packet type: unicast (0) link-layer address type: 1 link-layer address length: 6 source: 00:00:01:00:00:01 protocol: unknown (0x3100) data data: 7465737400 [length: 5]
outcut source code
sock_desc = socket(af_packet, sock_raw, htons(eth_p_all)); /*struct sending*/ sock_addr.sll_family = af_packet; sock_addr.sll_protocol = htons(eth_p_802_3); sock_addr.sll_ifindex = if_nametoindex(argv[1]); sock_addr.sll_hatype = arphrd_ether; //ethernet 10mbps sock_addr.sll_pkttype = packet_host; // paket zu irgendjemand sock_addr.sll_halen = eth_alen; //6 oktets in einer ethernet addr /*mac length 8 oktets*/ sock_addr.sll_addr[0] = frame.src_mac[0]; sock_addr.sll_addr[1] = frame.src_mac[1]; sock_addr.sll_addr[2] = frame.src_mac[2]; sock_addr.sll_addr[3] = frame.src_mac[3]; sock_addr.sll_addr[4] = frame.src_mac[4]; sock_addr.sll_addr[5] = frame.src_mac[5]; /*not in use*/ sock_addr.sll_addr[6] = 0x00; sock_addr.sll_addr[7] = 0x00; memset(buffer, '0', sizeof(char)*eth_frame_len); /*set frame header*/ /*build raw ethernet packet*/ buffer[0] = frame.dest_mac[0]; buffer[1] = frame.dest_mac[1]; buffer[2] = frame.dest_mac[2]; buffer[3] = frame.dest_mac[3]; buffer[4] = frame.dest_mac[4]; buffer[5] = frame.dest_mac[5]; buffer[6] = frame.src_mac[0]; buffer[7] = frame.src_mac[1]; buffer[8] = frame.src_mac[2]; buffer[9] = frame.src_mac[3]; buffer[10] = frame.src_mac[4]; buffer[11] = frame.src_mac[5]; while(frame.data[0] != '*'){ printf("input: "); scanf("%s", frame.data); templength = 0; while(frame.data[templength] != '\0'){ templength++; } input = 0; for(sendlen = 14;sendlen <= (14+templength);sendlen++){ buffer[sendlen] = frame.data[input]; input++; } sprintf(convlen,"%x", (14 + input)); buffer[12] = convlen[0]; buffer[13] = convlen[1]; length_in_byte = sendto(sock_desc, buffer, 14+input,0,(struct sockaddr*) &sock_addr,sizeof(struct sockaddr_ll)); if(length_in_byte <= 0){ printf("error beim senden"); }else{ printf("\n"); printf("src: %02x:%02x:%02x:%02x:%02x:%02x\t->\tdest: %02x:%02x:%02x:%02x:%02x:%02x\n",frame.src_mac[0],frame.src_mac[1],frame.src_mac[2],frame.src_mac[3],frame.src_mac[4],frame.src_mac[5],frame.dest_mac[0],frame.dest_mac[1],frame.dest_mac[2],frame.dest_mac[3],frame.dest_mac[4],frame.dest_mac[5]); printf("data: %s\n", frame.data); } }
please need find mistake.
thank forward.
please give lines dealing socket creation operation. assume have created socket :
int socket = socket(af_packet,sock_raw,ipproto_ip)
you right when build ethernet header (by way should use instead struct ethhdr, it's cleaner). after send data, without putting l4 header , l3 header.... it's normal code doesn't work. assume osi model. if it's not case read paper before reading next text.
so when want create raw packet, have many things before. example if want use tcp/ip protocol have next operation
#include <unistd.h> #include <stdio.h> #include <sys/socket.h> #include <netinet/ip.h> #include <netinet/tcp.h> #define sizemax 1000 int main() { char buffer[65535] // mdu // create socket int socket = socket(af_packet,sock_raw,ipproto_raw) ; // can specify opt socket let done own stuffs struct ethhdr * ethdr ; struct iphdr * iph ; struct tcphdr * tcph; // fill fields of these last structures (i can't long). // fill buffer memcpy(buffer,ethdr,sizeof(struct ethhdr)) ; // deplace addr buffer = buffer + sizeof(struct ethhdr) ; memcpy(buffer,iph,sizeof(struct iphdr)) ; buffer = buffer + sizeof(struct iphdr)) ; memcpy(buffer,tcph,sizeof(struct tcphdr)) ; printf("your entry : \n") ; char * entry = malloc(sizemax) ; sncanf("%s",entry,100) ; buffer = buffer+ 100 ; memcpy(buffer,entry,100) ; int size_send = sendto(socket,.......) ; if(size_send =< 0) perror("error sending data") ; return 0 ; }
this pseudo-code tell how send raw packet. paper work : raw socket tcp/ip
hope you
anthony
Comments
Post a Comment