소켓 프로그램중 sendto 에 관하여 질문드립니다.
글쓴이: hahasogang / 작성시간: 목, 2004/08/26 - 11:40오전
이더넷 포트 0에서 바인드를 시켜서 들어오는 프레임들을 받고 이더넷 코트 1로 보내는 프로그램입니다.
테스트해보니... 보내지를 못하는것 같아서 질문드립니다.많은 고수님들의 답변 부탁드립니다
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/ethernet.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
//#include <netinet/ip.h>
//#include <netinet/tcp.h>
//#include <netinet/udp.h>
//#include <netinet/ip_icmp.h>
#include <netinet/ether.h>
#include <netinet/if_ether.h>
#include <linux/if_packet.h>
int
main (int argc, char **argv)
{
int sock, n;
char buffer[2048];
unsigned char *iphead, *ethhead, *ether_addr;
int str_len;
struct sockaddr_ll sll;
//struct sockaddr_in sin;
struct sockaddr_ll dest_sll, test_sll;
int state;
struct ether_addr * test, *dtest;
if ((sock = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0)
{
perror ("socket error");
exit (1);
}
test = ether_aton("00:09:30:28:12:22");
memcpy(sll.sll_addr, test->ether_addr_octet, ETH_ALEN);
sll.sll_family = AF_PACKET;
//sll.sll_addr[8] = ether_aton ("00:09:30:28:12:22");
//sin.sin_family = AF_INET;
//sin.sin_addr.s_addr = inet_addr ("192.168.0.1");
dest_sll.sll_family = AF_PACKET;
// dest_sll.sll_addr[8] = ether_aton ("00:01:30:28:12:24");
dtest = ether_aton("00:01:30:28:12:24");
memcpy(dest_sll.sll_addr, dtest->ether_addr_octet, ETH_ALEN);
state = bind(sock,(struct sockaddr *)&sll, sizeof(sll));
if (state <0)
{
perror ("bind error ");
exit (0);
}
while (1)
{
printf ("----------\n");
n = recvfrom (sock, buffer, 2048, 0,NULL, NULL);
sendto(sock, buffer, str_len, 0, (struct sockaddr *)&dest_sll, sizeof(dest_sll));
printf ("%d bytes read\n", n);
/* Check to see if the packet contains at least
* complete Ethernet (14), IP (20) and TCP/UDP
* (8) headers.
*/
if (n < 42)
{
perror ("recvfrom():");
printf ("Incomplete packet (errno is %d)\n", errno);
close (sock);
exit (0);
}
ethhead = buffer;
printf ("Source MAC address: "
"%02x:%02x:%02x:%02x:%02x:%02x\n",
ethhead[0], ethhead[1], ethhead[2],
ethhead[3], ethhead[4], ethhead[5]);
printf ("Destination MAC address: "
"%02x:%02x:%02x:%02x:%02x:%02x\n",
ethhead[6], ethhead[7], ethhead[8],
ethhead[9], ethhead[10], ethhead[11]);
iphead = buffer + 14; /* Skip Ethernet header */
if (*iphead == 0x45)
{ /* Double check for IPv4
*
* and no options present */
printf ("Source host %d.%d.%d.%d\n",
iphead[12], iphead[13], iphead[14], iphead[15]);
printf ("Dest host %d.%d.%d.%d\n",
iphead[16], iphead[17], iphead[18], iphead[19]);
printf ("Source,Dest ports %d,%d\n",
(iphead[20] << 8) + iphead[21],
(iphead[22] << 8) + iphead[23]);
}
}
}Forums:


댓글 달기