arp 작성중 질문입니다.
네트 워크 및 C언어를 시작한지 이제 2달 되었습니다.
회사에 들어와서 수습 기간 동안 과제를 부여 받았는데
하다 보면 되겠지 하고 멘땅에 해딩 중입니다. 그것이 어언 2달째네요 ㅠㅠ
Socket 통신부터 시작 해서 지금까지 socket 통신만 하고 있습니다.
회사 툴이 libnet이 없고 linux 또한 없습니다.
오로지 unix와 알고리즘으로 풀어야 되는 상황인것 같습니다.
사수에게 물어 보지만 돌아오는 답변은 많이 답답하지? 그러다 해답 찾으면 니꺼야
라고 대답입니다. 안가르쳐 줍니다. ㅠㅠ
일단 arp request 를 만들고 싶습니다. raw_socket 이 아닌 raw_packet를 사용 하고 싶은데
없습니다. raw_packet가요 ㅠㅠ
급한맘에 TCP/IP 일러스트 볼륨 1,2 를 원서로 구입하고 UNP(unix network programming)도
원서로 구입하였습니다. 확실히 기초 지식이 부족해서 그런지 살짝살짝 감은 있지만
전혀 이해가 가지 않습니다.
구글링도 해보았는데 raw_socket로는 IP 레이어까지만 조작이 가능 하다고 공부 했습니다.
ip 레이어 조작으로 arp request를 구현 가능 한가요?
부족한 코딩도 하였는데 wireshark로 확인 결과 dst = broadcast, protocol = ip로 나옵니다.
현재 까지 코딩한 부분인데 주석 부분이 ip header와 udp header 입니다.
당연히 ip를 풀면 wccp 프로토콜이 잡히고 udp를 풀면 udp 프로토콜로 잡힙니다.
여기서 부터 1주일째 제자리 걸음이라 답답한 맘에 글을 올립니다.
조업 부탁드립니다.
이건 wireshark 캡쳐 결과입니다. ㅠ
SunMicro_22:8a:57 Broadcast IP Bogus IP length (0, less than header length 20)
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
unsigned short in_cksum(unsigned short *addr, int len)
{
int nleft = len;
int sum = 0;
unsigned short *w = addr;
unsigned short answer = 0;
while (nleft > 1) {
sum += *w++;
nleft -= 2;
}
if (nleft == 1) {
*(unsigned char *) (&answer) = *(unsigned char *) w;
sum += answer;
}
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
answer = ~sum;
return (answer);
}
main()
{
int fd=socket(AF_INET,SOCK_RAW,IPPROTO_RAW);
int val=1;
//int ret=setsockopt(fd,SOL_SOCKET,SO_BROADCAST,&val,sizeof(val));
char buf[8192];
struct sockaddr sa;
/* create a IP header */
/*
struct ip *iphdr=(struct ip*)buf;//(struct iphdr*) malloc(sizeof(struct iphdr));
iphdr->ip_v=4;
iphdr->ip_hl=5;
iphdr->ip_tos=0;
iphdr->ip_id=0;
iphdr->ip_off=0;
iphdr->ip_ttl=255;
iphdr->ip_p=IPPROTO_UDP;
iphdr->ip_sum=0;
iphdr->ip_src.s_addr=inet_addr("192.168.20.253");
iphdr->ip_dst.s_addr=inet_addr("192.168.20.75");
*/
struct arpreq *arpreq = (struct arpreq*)(buf+sizeof(struct ip));
struct ether_header *eh = (struct ether_header*)(buf+sizeof(struct ip));
struct ether_arp *ea = (struct ether_arp*)(buf+sizeof(struct ip));
struct arphdr *arphdr = (struct arphdr*)(buf+sizeof(struct ip));
eh = (struct ether_header *)sa.sa_data;
/*
ether_vlan_header->ether_type = ETHERTYPE_ARP;
*/
//ether net header
arpreq->arp_pa.sa_data[0] = 0x1;
arpreq->arp_pa.sa_data[1] = 0x9;
arpreq->arp_pa.sa_data[2] = 0x2;
arpreq->arp_pa.sa_data[3] = 0x1;
arpreq->arp_pa.sa_data[4] = 0x6;
arpreq->arp_pa.sa_data[5] = 0x8;
arpreq->arp_pa.sa_data[6] = 0x2;
arpreq->arp_pa.sa_data[7] = 0x0;
arpreq->arp_pa.sa_data[8] = 0x2;
arpreq->arp_pa.sa_data[9] = 0x5;
arpreq->arp_pa.sa_data[10] = 0x3;
arpreq->arp_ha.sa_data[0] = 0x0;
arpreq->arp_ha.sa_data[1] = 0x0;
arpreq->arp_ha.sa_data[2] = 0x1;
arpreq->arp_ha.sa_data[3] = 0x3;
arpreq->arp_ha.sa_data[4] = 0x4;
arpreq->arp_ha.sa_data[5] = 0xf;
arpreq->arp_ha.sa_data[6] = 0x2;
arpreq->arp_ha.sa_data[7] = 0x2;
arpreq->arp_ha.sa_data[8] = 0x8;
arpreq->arp_ha.sa_data[9] = 0xa;
arpreq->arp_ha.sa_data[10] = 0x5;
arpreq->arp_ha.sa_data[11] = 0x7;
arpreq->arp_flags = ATF_USETRAILERS;
ea->arp_sha[0] = (0x00);
ea->arp_sha[1] = (0x14);
ea->arp_sha[2] = (0x4f);
ea->arp_sha[3] = (0x22);
ea->arp_sha[4] = (0x8a);
ea->arp_sha[5] = (0x57);
ea->arp_tha[0] = 0xff;
ea->arp_tha[1] = 0xff;
ea->arp_tha[2] = 0xff;
ea->arp_tha[3] = 0xff;
ea->arp_tha[4] = 0xff;
ea->arp_tha[5] = 0xff;
ea->arp_spa[0] = (192);
ea->arp_spa[1] = (168);
ea->arp_spa[2] = (20);
ea->arp_spa[3] = (253);
ea->arp_tpa[0] = (192);
ea->arp_tpa[1] = (168);
ea->arp_tpa[2] = (20);
ea->arp_tpa[3] = (75);
/*
struct udphdr* udp=(struct udphdr*)(buf+sizeof(struct ip));
(struct udphdr*) malloc(sizeof(struct udphdr));
udp->uh_sport=htons(40000);
udp->uh_dport=htons(50000);
udp->uh_sum=0;
char* data=(char*)buf+sizeof(struct ip)+sizeof(struct udphdr);
strcpy(data,"Harry Potter and the Philosopher's Stone");
udp->uh_ulen=htons(sizeof(struct udphdr)+strlen(data));
udp->uh_sum=in_cksum((unsigned short*) udp,8+strlen(data));
iphdr->ip_len=htons(sizeof(struct ip)+sizeof(struct arphdr)+strlen(data));
*/
char* data=(char*)buf+sizeof(struct arphdr)+sizeof(struct arpreq);
ea->arp_hrd = ARPHRD_ETHER;
ea->arp_pro = ETHERTYPE_IP;
ea->arp_hln = (sizeof(ea->arp_sha) + strlen(data));
ea->arp_pln = (sizeof(ea->arp_spa) + strlen(data));
ea->arp_op = ARPOP_REQUEST;
struct sockaddr_in d;
bzero(&d,sizeof(d));
d.sin_family=AF_INET;
d.sin_port=htons(50000);
int ret=setsockopt(fd,SOL_SOCKET,SO_BROADCAST,&val,sizeof(val));
//inet_pton(AF_INET,"localhost",&d.sin_addr.s_addr);
while(1){
// setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &d, sizeof(d));
sendto(fd,buf,//sizeof(struct ip)
//+sizeof(struct udphdr)
+sizeof(struct ether_header)
+sizeof(struct ether_arp)
+sizeof(struct arphdr)
+strlen(data),0,(struct sockaddr*) &d,sizeof(d));
}
}
댓글 달기