Networking Forums

Networking Forums > Computer Networking > Linux Networking > Problem with data-link socket

Reply
Thread Tools Display Modes

Problem with data-link socket

 
 
Marco
Guest
Posts: n/a

 
      08-03-2006, 11:03 AM
Hi all,
I want to send Ethernet Frames over a RAW sockets
(socket(PF_PACKET,...........)), but it doesn't work. I send the frame
to a web server on another host, it arrives but the server ignores it.
That's what i've done so far...

payload = malloc(sizeof(char)*51);
memset((char*)payload, '\0', sizeof(char)*51);
strncpy(payload, "GET /~marco/www/private_resource.html
HTTP/1.1\r\n\r\n",50);


char packet1[ sizeof(struct ethhdr) + sizeof(struct iphdr) +
sizeof(struct tcphdr) + strlen(payload)];
strcpy( (packet1 + sizeof(struct ethhdr) + sizeof(struct iphdr) +
sizeof(struct tcphdr)), payload);

unsigned char src_mac[6]; /*our MAC address*/

unsigned char dst_mac[6]={0xXX,0xXX,0xXX,0xXX,0xXX,0xXX};
/*destination 192.168.0.4

MAC address*/

struct ifreq ifr;
struct sockaddr_ll socket_address;
int ifindex = 0; /*Ethernet Interface index*/
int i;
int length; /*length of received packet*/
int sent, s, r;

struct sockaddr_ll sa;
/*open socket*/
s = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP));
if (s == -1) {
perror("socket():");
exit(1);
}

printf("Successfully opened socket: %i\n", s);

/*retrieve ethernet interface index*/
strncpy(ifr.ifr_name, DEVICE, IFNAMSIZ);
if (ioctl(s, SIOCGIFINDEX, &ifr) == -1) {
perror("SIOCGIFINDEX");
exit(1);
}
ifindex = ifr.ifr_ifindex;
printf("Successfully got interface index: %i\n", ifindex);

/*retrieve corresponding MAC*/
if (ioctl(s, SIOCGIFHWADDR, &ifr) == -1) {
perror("SIOCGIFINDEX");
exit(1);
}
for (i = 0; i < 6; i++) {
src_mac[i] = ifr.ifr_hwaddr.sa_data[i];
}
printf("Successfully got our MAC address:
%02X:%02X:%02X:%02X:%02X:%02X\n",
src_mac[0],src_mac[1],src_mac[2],src_mac[3],src_mac[4],src_mac[5]);


eth = (struct ethhdr *)packet1;

eth_gen((char*)eth, dst_mac, src_mac);

/*prepare sockaddr_ll*/
socket_address.sll_family = PF_PACKET;
socket_address.sll_protocol = htons(ETH_P_IP);
socket_address.sll_ifindex = ifindex;
socket_address.sll_hatype = ARPHRD_ETHER;
socket_address.sll_pkttype = PACKET_OTHERHOST;
socket_address.sll_halen = ETH_ALEN;

memcpy(socket_address.sll_addr, src_mac, ETH_ALEN);

r = bind(s,(struct sockaddr *) &socket_address,
sizeof(socket_address));
if ( r < 0 )
{
perror("bind");
close(s);
return -1;
}

printf(" strlen(payload)=%d\n", strlen(payload));
printf(" sizeof(packet1)= %d\n", sizeof(packet1));

ip = (struct iphdr *)(packet1 + sizeof(struct ethhdr));

frag_off = 0x4000;

ip_gen((char*)ip,IPPROTO_TCP,saddr,daddr,sizeof(pa cket1),id,
frag_off);

tcp = (struct tcphdr *)(packet1 + sizeof(struct ethhdr) +
sizeof(struct iphdr));

fin=0;
syn=0;
ack=1;
psh=0;
urg=0;
tcp_gen((char *)tcp,sport,dport,seq,ack_seq, fin, syn, psh, ack, urg
);


tcp->check = trans_check(IPPROTO_TCP,(char *)tcp, sizeof(struct
tcphdr) + strlen(payload), saddr, daddr);

sent = sendto(s,&packet1,sizeof(packet1), 0x0,(struct sockaddr
*)&socket_address, sizeof(socket_address));
if (sent != sizeof(packet1)){

perror("sendto");
exit(1);
}

free(payload);

Can you help me, please?
Thanks!

 
Reply With Quote
 
 
 
 
Robert Harris
Guest
Posts: n/a

 
      08-03-2006, 02:16 PM
Marco wrote:
> Hi all,
> I want to send Ethernet Frames over a RAW sockets
> (socket(PF_PACKET,...........)), but it doesn't work. I send the frame
> to a web server on another host, it arrives but the server ignores it.
> That's what i've done so far...
>
> [snip]


Well, why not use a packet sniffer (e.g. tcpdump) to see what your code
generates and compare it with what a real HTTP client generates?

As far as I can see (not knowing what your subroutines do), at least you
need to:

1. Insert checksums into headers
2. Perform a three way handshake to establish a TCP connection.

Your application data may not be enough (HTTP servers often want client
hostnames as well). You can check that with:

telnet server.name 80

and sending your data.

Robert
 
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Amount of data written on a socket John Linux Networking 0 04-27-2007 07:21 PM
How to read all data from kernel buffer for a socket fd (which uses TCP ) will_u_tellmemore Linux Networking 0 12-21-2006 01:23 PM
How to get my local ethernet ip on which i receive a socket data?? kernel.lover Linux Networking 1 03-22-2005 08:41 AM
how to capture raw data from socket Christian Bongiorno Linux Networking 1 10-30-2004 08:38 PM
Sending and reading big data by socket cyril Linux Networking 1 08-28-2003 03:58 PM



1 2 3 4 5 6 7 8 9 10 11