- 论坛徽章:
- 0
|
小弟想通过Raw套接字实现双网卡下,一个接受数据包,一个发送数据包,但是现在情况是可以接受,发送出现错误...大家看看什么情况啊?#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <linux/if_ether.h>
#include <net/if.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <netdb.h>
#include <stdlib.h>
#include <assert.h>
#include <netpacket/packet.h>
int main(int argc, char *argv[])
{
int sock_55;
int i,j,k,len,n;
char buffer[1500];
unsigned char src_mac[6]; /*source mac address*/
unsigned char dest_mac[6] = {0x00, 0x01, 0x02, 0x92, 0xb1, 0x2a}; /*dest mac adress*/
//struct sockaddr_in in,from;
unsigned char *iphead,*ethhead,*data;
struct ip *ipp;
struct tcphdr *tcp;
struct udphdr *udp;
struct ifreq ifr;
char *dev;
struct sockaddr_ll sll,tmp;
/*create sock */
if ( (sock_55 = socket(AF_PACKET, SOCK_RAW,htons(ETH_P_ALL)))<0)
{
printf("socket error!");
}
/*retrieve ethernet interface index*/
dev = "eth0";
strncpy ((char *)ifr.ifr_name, dev, sizeof(ifr.ifr_name));
if (ioctl(sock_55, SIOCGIFINDEX, &ifr) == -1)
{
perror("SIOCGIFINDEX");
exit(1);
}
printf("Successfully got interface index: %i\n", ifr.ifr_ifindex);
/*retrieve corresponding MAC*/
if (ioctl(sock_55, 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]);
/*prepare sockaddr_ll*/
memset (&sll, 0, sizeof(sll));
sll.sll_family = PF_PACKET;
sll.sll_protocol = htons(ETH_P_IP);
sll.sll_ifindex = ifr.ifr_ifindex;
sll.sll_hatype = 1;
sll.sll_pkttype = PACKET_OTHERHOST;
sll.sll_halen = ETH_ALEN;
sll.sll_addr[0] = dest_mac[0];
sll.sll_addr[1] = dest_mac[1];
sll.sll_addr[2] = dest_mac[2];
sll.sll_addr[3] = dest_mac[3];
sll.sll_addr[4] = dest_mac[4];
sll.sll_addr[5] = dest_mac[5];
sll.sll_addr[6] = 0x00;
sll.sll_addr[7] = 0x00;
while(1)
{
memset(buffer,sizeof(buffer),0);
n = 0;
n = recvfrom(sock_55,buffer,sizeof(buffer),0,0,0);
if (n<42){
close(sock_55);
exit(0);
}
printf("55 receive %d\n",n);
ipp = (struct ip *)(buffer + 14);
tcp = (struct tcphdr *)(buffer + 4*ipp->ip_hl + 14);
data = buffer + 14 + ipp->ip_hl * 4 + tcp->doff * 4;
memset(&tmp,0,sizeof(struct sockaddr_ll));
tmp.sll_family = sll.sll_family ;
tmp.sll_protocol= sll.sll_protocol ;
tmp.sll_ifindex = sll.sll_ifindex ;
tmp.sll_hatype = sll.sll_hatype ;
tmp.sll_pkttype = sll.sll_pkttype ;
tmp.sll_halen = sll.sll_halen ;
tmp.sll_addr[0] = sll.sll_addr[0] ;
tmp.sll_addr[1] = sll.sll_addr[1] ;
tmp.sll_addr[2] = sll.sll_addr[2] ;
tmp.sll_addr[3] = sll.sll_addr[3] ;
tmp.sll_addr[4] = sll.sll_addr[4] ;
tmp.sll_addr[5] = sll.sll_addr[5] ;
tmp.sll_addr[6] = sll.sll_addr[6] ;
tmp.sll_addr[7] = sll.sll_addr[7] ;
//modify(buffer);
n = 0;
n = sendto(sock_55,buffer,sizeof(buffer),0,(struct sockaddr *)&tmp,sizeof(struct sockaddr_ll));
printf("55 send %d\n",n);
}
}
|
[ 本帖最后由 sillyaboy 于 2009-6-20 16:40 编辑 ] |
|