Permalink
Browse files

Initial Import

  • Loading branch information...
nakkaya committed May 26, 2009
0 parents commit 5658f2af53ef950fa07ad3e5d00fcb927c5fc578
Showing with 722 additions and 0 deletions.
  1. +63 −0 ArpFactory.cpp
  2. +28 −0 ArpFactory.h
  3. +7 −0 Host.h
  4. +26 −0 Makefile
  5. +183 −0 NetworkInterface.cpp
  6. +33 −0 NetworkInterface.h
  7. +247 −0 makeWay.cpp
  8. +41 −0 network.h
  9. +80 −0 util.cpp
  10. +14 −0 util.h
@@ -0,0 +1,63 @@
+#include "ArpFactory.h"
+
+void ArpFactory::setArpOpCode( uint16_t oc ){
+ opcode = oc;
+ }
+
+ void ArpFactory::setArpIP( uint8_t src_ip[4] , uint8_t tgt_ip[4] ){
+ for(int i = 0 ; i<4 ; i++ ){
+ arp_src.src_ip[i] = src_ip[i];
+ arp_src.tgt_ip[i] = tgt_ip[i];
+ }
+ }
+
+ void ArpFactory::setArpMAC( uint8_t src_mac[6] , uint8_t tgt_mac[6] ){
+ for(int i = 0 ; i<6 ; i++ ){
+ arp_src.src_mac[i] = src_mac[i];
+ arp_src.tgt_mac[i] = tgt_mac[i];
+ }
+ }
+
+ void ArpFactory::setEthMAC( uint8_t src_mac[6] , uint8_t tgt_mac[6] ){
+ for(int i = 0 ; i<6 ; i++ ){
+ eth_src.h_source[i] = src_mac[i];
+ eth_src.h_dest[i] = tgt_mac[i];
+ }
+ }
+
+ char* ArpFactory::create(){
+ packet = (char*)malloc(sizeof(struct ether_hdr) + sizeof(struct arp_hdr));
+
+ struct ether_hdr eth_frame;
+ struct arp_hdr arp_frame;
+
+ memcpy(eth_frame.h_dest, eth_src.h_dest, 6);
+ memcpy(eth_frame.h_source, eth_src.h_source, 6);
+
+// printMAC("eth_frame.h_dest" , eth_src.h_dest );
+// printMAC("eth_frame.h_source" , eth_src.h_source );
+
+ eth_frame.h_proto = htons(ARP_PROTO);
+
+ arp_frame.htype = htons(HW_ETHER);
+ arp_frame.proto = htons(IP_PROTO);
+ arp_frame.hsize = HW_SIZE;
+ arp_frame.psize = PROTO_SIZE;
+ arp_frame.opcode = htons(opcode);
+
+ memcpy(arp_frame.src_mac, arp_src.src_mac, 6);
+ memcpy(arp_frame.src_ip, arp_src.src_ip, 4);
+ memcpy(arp_frame.tgt_mac, arp_src.tgt_mac, 6);
+ memcpy(arp_frame.tgt_ip, arp_src.tgt_ip, 4);
+
+// printIP("arp_src.src_ip" , arp_src.src_ip );
+// printMAC("arp_src.src_mac" , arp_src.src_mac );
+
+// printIP("arp_src.tgt_ip" , arp_src.tgt_ip );
+// printMAC("arp_src.tgt_mac" , arp_src.tgt_mac );
+
+ memcpy(packet, &eth_frame, sizeof(struct ether_hdr));
+ memcpy(packet + sizeof(struct ether_hdr), &arp_frame, sizeof(struct arp_hdr));
+
+ return packet;
+ }
@@ -0,0 +1,28 @@
+#include <stdlib.h>
+#include <string.h>
+
+#include <iostream>
+#include <vector>
+
+//htons linux
+#include <sys/types.h>
+#include <netinet/in.h>
+
+#include <pcap.h>
+#include "Host.h"
+#include "network.h"
+
+class ArpFactory{
+private:
+ struct arp_hdr arp_src;
+ struct ether_hdr eth_src;
+ uint16_t opcode;
+ char *packet;
+
+public:
+ void setArpOpCode( uint16_t oc );
+ void setArpIP( uint8_t src_ip[4] , uint8_t tgt_ip[4] );
+ void setArpMAC( uint8_t src_mac[6] , uint8_t tgt_mac[6] );
+ void setEthMAC( uint8_t src_mac[6] , uint8_t tgt_mac[6] );
+ char* create();
+};
7 Host.h
@@ -0,0 +1,7 @@
+using namespace std;
+
+class Host{
+ public:
+ string ip;
+ string mac;
+};
@@ -0,0 +1,26 @@
+objects = build/util.o build/NetworkInterface.o build/ArpFactory.o build/makeWay.o
+CC = g++ -Wall
+
+darwin : util NetworkInterfaceDarwin ArpFactory driverDarwin
+ $(CC) -lpcap $(objects) -o build/makeWay
+linux : util NetworkInterfaceLinux ArpFactory driverLinux
+ $(CC) -lpcap $(objects) -o build/makeWay
+
+driverDarwin: makeWay.cpp
+ $(CC) -DOS_DARWIN -c makeWay.cpp -o build/makeWay.o
+driverLinux: makeWay.cpp
+ $(CC) -DOS_LINUX -c makeWay.cpp -o build/makeWay.o
+
+NetworkInterfaceDarwin: NetworkInterface.cpp
+ $(CC) -DOS_DARWIN -c NetworkInterface.cpp -o build/NetworkInterface.o
+NetworkInterfaceLinux: NetworkInterface.cpp
+ $(CC) -DOS_LINUX -c NetworkInterface.cpp -o build/NetworkInterface.o
+
+util: util.cpp
+ $(CC) -c util.cpp -o build/util.o
+
+ArpFactory: ArpFactory.cpp
+ $(CC) -c ArpFactory.cpp -o build/ArpFactory.o
+
+clean:
+ rm -rf build/*o build/makeWay
@@ -0,0 +1,183 @@
+#include "NetworkInterface.h"
+
+NetworkInterface::NetworkInterface( const char* dv ){
+ dev = dv;
+}
+
+#ifdef OS_DARWIN
+std::string NetworkInterface::getLocalGateway( ){
+
+ struct in_addr gatewayaddr;
+ in_addr_t * addr = &(gatewayaddr.s_addr);
+
+ //in_addr_t * addr;
+ int mib[] = {CTL_NET, PF_ROUTE, 0, AF_INET,
+ NET_RT_FLAGS, RTF_GATEWAY};
+ size_t l;
+ char * buf, * p;
+ struct rt_msghdr * rt;
+ struct sockaddr * sa;
+ struct sockaddr * sa_tab[RTAX_MAX];
+ int i;
+ if(sysctl(mib, sizeof(mib)/sizeof(int), 0, &l, 0, 0) < 0) {
+ return std::string("Unknown");
+ }
+
+ if(l>0) {
+ buf = (char*)malloc(l);
+ if(sysctl(mib, sizeof(mib)/sizeof(int), buf, &l, 0, 0) < 0) {
+ free(buf);
+ return std::string("Unknown");
+ }
+ for(p=buf; p<buf+l; p+=rt->rtm_msglen) {
+ rt = (struct rt_msghdr *)p;
+ sa = (struct sockaddr *)(rt + 1);
+ for(i=0; i<RTAX_MAX; i++) {
+ if(rt->rtm_addrs & (1 << i)) {
+ sa_tab[i] = sa;
+ sa = (struct sockaddr *)((char *)sa + sa->sa_len);
+ } else {
+ sa_tab[i] = NULL;
+ }
+ }
+ if( ((rt->rtm_addrs & (RTA_DST|RTA_GATEWAY)) == (RTA_DST|RTA_GATEWAY))
+ && sa_tab[RTAX_DST]->sa_family == AF_INET
+ && sa_tab[RTAX_GATEWAY]->sa_family == AF_INET) {
+ if(((struct sockaddr_in *)sa_tab[RTAX_DST])->sin_addr.s_addr == 0) {
+ *addr = ((struct sockaddr_in *)(sa_tab[RTAX_GATEWAY]))->sin_addr.s_addr;
+ }
+ }
+ }
+
+ free(buf);
+ }
+
+ return IPtoString((uint8_t*)addr);
+}
+#endif
+
+#ifdef OS_LINUX
+std::string NetworkInterface::getLocalGateway( ){
+ struct in_addr gatewayaddr;
+ in_addr_t * addr = &(gatewayaddr.s_addr);
+
+ long d, g;
+ char buf[256];
+ int line = 0;
+ FILE * f;
+ char * p;
+ f = fopen("/proc/net/route", "r");
+ if(!f)
+ return "Unknown";
+ while(fgets(buf, sizeof(buf), f)) {
+ if(line > 0) {
+ p = buf;
+ while(*p && !isspace(*p))
+ p++;
+ while(*p && isspace(*p))
+ p++;
+ if(sscanf(p, "%lx%lx", &d, &g)==2) {
+ if(d == 0) { /* default */
+ *addr = g;
+ fclose(f);
+ return IPtoString((uint8_t*)addr);
+ }
+ }
+ }
+ line++;
+ }
+ /* default route not found ! */
+ if(f)
+ fclose(f);
+ return "Unknown";
+}
+#endif
+
+
+/* Given a ifreq structure this function returns its IP address */
+std::string NetworkInterface::getLocalIP( ){
+
+ struct ifreq ifr;
+ char netaddr[INET_ADDRSTRLEN];
+ int sd;
+ sd=socket(AF_INET,SOCK_DGRAM,0);
+ strcpy(ifr.ifr_name, dev );
+ if((ioctl(sd,SIOCGIFADDR,(caddr_t)&ifr,sizeof(struct ifreq)))<0)
+ perror("Error : ");
+
+
+ struct sockaddr *sa;
+ sa=(struct sockaddr *)&(ifr.ifr_addr);
+ switch(sa->sa_family){
+ case AF_INET6:
+ inet_ntop(AF_INET6,(struct in6_addr *)&(((struct sockaddr_in6 *)sa)->sin6_addr),
+ netaddr,INET6_ADDRSTRLEN);
+ break;
+ default : strcpy(netaddr,inet_ntoa(((struct sockaddr_in *)sa)->sin_addr));
+ }
+
+ std::string ip(netaddr);
+ return ip;
+}
+
+#ifdef OS_DARWIN
+std::string NetworkInterface::getLocalMAC(){
+ int mib[6];
+ size_t len;
+ char *buf;
+ unsigned char *ptr;
+ struct if_msghdr *ifm;
+ struct sockaddr_dl *sdl;
+
+ mib[0] = CTL_NET;
+ mib[1] = AF_ROUTE;
+ mib[2] = 0;
+ mib[3] = AF_LINK;
+ mib[4] = NET_RT_IFLIST;
+
+ if ((mib[5] = if_nametoindex(dev)) == 0) {
+ perror("if_nametoindex error");
+ exit(2);
+ }
+
+ if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
+ perror("sysctl 1 error");
+ exit(3);
+ }
+
+ if ((buf = (char *)malloc(len)) == NULL) {
+ perror("malloc error");
+ exit(4);
+ }
+
+ if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
+ perror("sysctl 2 error");
+ exit(5);
+ }
+
+ ifm = (struct if_msghdr *)buf;
+ sdl = (struct sockaddr_dl *)(ifm + 1);
+ ptr = (unsigned char *)LLADDR(sdl);
+
+ return MACtoString( ptr );
+}
+#endif
+
+#ifdef OS_LINUX
+std::string NetworkInterface::getLocalMAC(){
+
+ int fd;
+ struct ifreq ifr;
+
+ fd = socket(AF_INET, SOCK_DGRAM, 0);
+
+ ifr.ifr_addr.sa_family = AF_INET;
+ strncpy(ifr.ifr_name, "eth0", IFNAMSIZ-1);
+
+ ioctl(fd, SIOCGIFHWADDR, &ifr);
+
+ close(fd);
+
+ return MACtoString((uint8_t*)ifr.ifr_hwaddr.sa_data);
+}
+#endif
@@ -0,0 +1,33 @@
+#include <errno.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <iostream>
+#include <vector>
+#include <math.h>
+#include <sys/ioctl.h>
+#include <net/if.h>
+#include <sys/sysctl.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include "util.h"
+#include <string.h>
+
+#ifdef OS_DARWIN
+#include <net/if_dl.h>
+#include <stdlib.h>
+#include <sys/sysctl.h>
+#include <sys/socket.h>
+#include <net/route.h>
+#endif
+
+class NetworkInterface{
+private:
+ const char* dev;
+public:
+ NetworkInterface(const char* dv);
+ std::string getLocalGateway();
+ std::string getLocalIP();
+ std::string getLocalMAC();
+};
Oops, something went wrong.

0 comments on commit 5658f2a

Please sign in to comment.