trying out stuff
This commit is contained in:
		
							parent
							
								
									a90401ddeb
								
							
						
					
					
						commit
						507cfe2140
					
				
					 8 changed files with 118 additions and 49 deletions
				
			
		|  | @ -8,7 +8,46 @@ | |||
| #include "Filter.h" | ||||
| 
 | ||||
| Filter::Filter() { | ||||
|     // TODO Auto-generated constructor stub
 | ||||
| 
 | ||||
| } | ||||
| Filter::Filter(macAddr a) { | ||||
|     switchMac = a; | ||||
| } | ||||
| Filter::Filter(Packet::OpCode o) { | ||||
|     oc = o; | ||||
| } | ||||
| 
 | ||||
| Filter Filter::opcode(Packet::OpCode o) { | ||||
|     oc = o; | ||||
|     return *this; | ||||
| } | ||||
| Filter Filter::mac(macAddr a) { | ||||
|     switchMac = a; | ||||
|     return *this; | ||||
| } | ||||
| Filter Filter::tokenid(short i) { | ||||
|     tokenId = i; | ||||
|     return *this; | ||||
| } | ||||
| 
 | ||||
| bool Filter::pass(const Packet p) const { | ||||
|     macAddr none { 0, 0, 0, 0, 0, 0 }; | ||||
|     if (this->oc != Packet::NONE && this->oc != p.getOpCode()) | ||||
|         return false; | ||||
|     if (this->switchMac != none && this->switchMac != p.getSwitchMac()) | ||||
|         return false; | ||||
|     if (this->tokenId != -1 && this->tokenId != p.getTokenId()) | ||||
|         return false; | ||||
|     /*    if (fragmentOffset != p.getF && fragmentOffset != -1)
 | ||||
|      return false;*/ | ||||
|     return true; | ||||
| } | ||||
| 
 | ||||
| bool Filter::operator<(const Filter f) const { | ||||
|     if (this->oc != f.oc) | ||||
|     return this->oc < f.oc; | ||||
|     if (this->switchMac != f.switchMac) | ||||
|         return this->switchMac < f.switchMac; | ||||
|     if (this->tokenId < f.tokenId) | ||||
|         return this->tokenId < f.tokenId; | ||||
|     return false; | ||||
| } | ||||
|  |  | |||
							
								
								
									
										14
									
								
								src/Filter.h
									
										
									
									
									
								
							
							
						
						
									
										14
									
								
								src/Filter.h
									
										
									
									
									
								
							|  | @ -8,9 +8,23 @@ | |||
| #ifndef FILTER_H_ | ||||
| #define FILTER_H_ | ||||
| 
 | ||||
| #include "Packet.h" | ||||
| 
 | ||||
| class Filter { | ||||
| public: | ||||
|     Filter(); | ||||
|     Filter(Packet::OpCode); | ||||
|     Filter(macAddr); | ||||
|     Filter opcode(Packet::OpCode); | ||||
|     Filter mac(macAddr); | ||||
|     Filter tokenid(short); | ||||
|     bool pass(const Packet) const; | ||||
|     bool operator<(const Filter) const; | ||||
| private: | ||||
|     Packet::OpCode oc = Packet::NONE; | ||||
|     macAddr switchMac = { 0, 0, 0, 0, 0, 0 }; | ||||
|     short tokenId = -1; | ||||
|     short fragmentOffset = -1; | ||||
| }; | ||||
| 
 | ||||
| #endif /* FILTER_H_ */ | ||||
|  |  | |||
|  | @ -22,14 +22,8 @@ using namespace std; | |||
| int Interactive::loop() { | ||||
|     string cmd; | ||||
|     vector<string> v; | ||||
|     //vector<char const*> vc;
 | ||||
|     //const char** argv;
 | ||||
|     //int argc;
 | ||||
| 
 | ||||
|     Program p = Program(); | ||||
| 
 | ||||
|     p.init(); | ||||
| 
 | ||||
|     while (1) { | ||||
|         cmd = readline("smrtlink> "); | ||||
|         if (!cmd.compare("quit") || !cmd.compare("q")) | ||||
|  | @ -37,27 +31,16 @@ int Interactive::loop() { | |||
|         if (!cmd.empty()) { | ||||
|             add_history(cmd.c_str()); | ||||
|             v = boost::program_options::split_unix(cmd); | ||||
| 
 | ||||
|             //vc = vector<char const*>(v.size());
 | ||||
|             //std::transform(begin(v), end(v), begin(vc), [](std::string const &s) { return s.c_str(); });
 | ||||
|             //argv = &vc[0];
 | ||||
|             //argc = v.size();
 | ||||
|             p.run(v); | ||||
| 
 | ||||
|         } | ||||
| 
 | ||||
|     } | ||||
|     return 0; | ||||
| } | ||||
| 
 | ||||
| int Interactive::single(vector<string> v) { | ||||
| 
 | ||||
|     Program p = Program(); | ||||
| 
 | ||||
|     p.init(); | ||||
| 
 | ||||
|     p.run(v); | ||||
| 
 | ||||
|     return 0; | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -15,10 +15,11 @@ | |||
| 
 | ||||
| static short sequenceId = 0; | ||||
| 
 | ||||
| class Packet | ||||
| { | ||||
| class Packet { | ||||
| public: | ||||
|     enum OpCode {DISCOVERY, GET, REPLY, SET, CONFIRM, NONE}; | ||||
|     enum OpCode { | ||||
|         DISCOVERY, GET, REPLY, SET, CONFIRM, NONE | ||||
|     }; | ||||
|     Packet(OpCode); | ||||
|     static void encode(bytes&); | ||||
|     bytes getBytes(); | ||||
|  |  | |||
|  | @ -17,6 +17,7 @@ | |||
| #include "Socket.h" | ||||
| #include "Switch.h" | ||||
| #include "Packet.h" | ||||
| #include "Filter.h" | ||||
| #include "Types.h" | ||||
| #include "lookup.h" | ||||
| #include "table.h" | ||||
|  | @ -220,18 +221,16 @@ int Program::sniff() { | |||
|         Socket s(io_service); | ||||
|         s.setHostIp(host.getIp()); | ||||
|         s.init(DST_PORT, SRC_PORT); | ||||
|         s.callback = [](Packet p) { | ||||
|         s.listen([](Packet p) { | ||||
|             cout << p.opCodeToString() << "\n"; | ||||
|             printHeader(p); | ||||
|             printPacket(p); | ||||
|             return 0; | ||||
|         }; | ||||
|         s.receive(); | ||||
|         }); | ||||
|         io_service.run(); | ||||
|     } catch (exception& e) { | ||||
|         cerr << "Exception: " << e.what() << "\n"; | ||||
|     } | ||||
| 
 | ||||
|     return 0; | ||||
| } | ||||
| 
 | ||||
|  | @ -406,7 +405,7 @@ int Program::discover(function<int(Packet)> c) { | |||
|     Packet p = Packet(Packet::DISCOVERY); | ||||
|     p.setHostMac(host.getMac()); | ||||
|     p.setPayload( { }); | ||||
|     sock->callback = c; | ||||
|     sock->listen(c,Filter(Packet::REPLY)); | ||||
|     sock->send(p); | ||||
|     return 0; | ||||
| } | ||||
|  | @ -416,7 +415,7 @@ int Program::get(Packet l, datasets t, function<int(Packet)> c) { | |||
|     p.setSwitchMac(l.getSwitchMac()); | ||||
|     p.setHostMac(host.getMac()); | ||||
|     p.setPayload(t); | ||||
|     sock->callback = c; | ||||
|     sock->listen(c,Filter(Packet::REPLY)); | ||||
|     sock->send(p); | ||||
|     return 0; | ||||
| } | ||||
|  | @ -431,7 +430,7 @@ int Program::set(Packet l, datasets t, function<int(Packet)> c) { | |||
|     datasets ld = { { LOGIN_USER, (short) (n.size()), n }, { LOGIN_PASSWORD, | ||||
|             (short) (w.size()), w } }; | ||||
|     p.setPayload(ld + t); | ||||
|     sock->callback = c; | ||||
|     sock->listen(c,Filter(Packet::CONFIRM)); | ||||
|     sock->send(p); | ||||
|     return 0; | ||||
| } | ||||
|  |  | |||
|  | @ -5,11 +5,14 @@ | |||
|  *      Author: jdi | ||||
|  */ | ||||
| #include <cstdlib> | ||||
| #include <array> | ||||
| 
 | ||||
| #include <map> | ||||
| 
 | ||||
| #include <unistd.h> | ||||
| #include "Socket.h" | ||||
| #include "Packet.h" | ||||
| #include "Constant.h" | ||||
| #include "Filter.h" | ||||
| #include "Host.h" | ||||
| #include "Types.h" | ||||
| 
 | ||||
|  | @ -17,6 +20,10 @@ Socket::Socket(boost::asio::io_service& io_service) : | |||
|         send_socket_(io_service), receive_socket_(io_service), timer(io_service) { | ||||
| } | ||||
| //, resolver(				io_service)
 | ||||
| 
 | ||||
| Socket::~Socket() { | ||||
| } | ||||
| 
 | ||||
| void Socket::init(short dst_port, short src_port) { | ||||
|     if (options.flags.REVERSE) { | ||||
|         short p = dst_port; | ||||
|  | @ -50,6 +57,15 @@ void Socket::setHostIp(ipAddr ip) { | |||
|     local_ip = ip; | ||||
| } | ||||
| 
 | ||||
| void Socket::listen(Listener l, Filter f) { | ||||
|     if (callback.find(f) == callback.end()) { | ||||
|         callback.insert(ListenerPair(f, l)); | ||||
|     } else { | ||||
|         callback[f] = l; | ||||
|     } | ||||
|     receive(); | ||||
| } | ||||
| 
 | ||||
| void Socket::send(Packet p) { | ||||
|     bytes data = p.getBytes(); | ||||
|     p.encode(data); | ||||
|  | @ -76,11 +92,15 @@ void Socket::receive() { | |||
|                     data.resize(bytes_recvd); | ||||
|                     Packet p = Packet(Packet::NONE); | ||||
|                     p.encode(data); | ||||
|                     // std::cout << "err" << p.getErrorCode() <<std::endl;
 | ||||
|                     p.parse(data); | ||||
|                     //std::cout << "err" << p.getErrorCode() <<std::endl;
 | ||||
|                     if(!callback(p)) { | ||||
|                         //TODO do something
 | ||||
|                     for(auto r : callback) { | ||||
|                         if(r.first.pass(p)) { | ||||
|                             r.second(p); | ||||
|                             // std::cout<<"pass"<<std::endl;
 | ||||
|                             break; | ||||
|                         } else { | ||||
|                             // std::cout<<"no pass"<<std::endl;
 | ||||
|                         } | ||||
|                     } | ||||
|                     receive(); | ||||
|                     settimeout(); | ||||
|  |  | |||
							
								
								
									
										15
									
								
								src/Socket.h
									
										
									
									
									
								
							
							
						
						
									
										15
									
								
								src/Socket.h
									
										
									
									
									
								
							|  | @ -8,8 +8,10 @@ | |||
| #ifndef SOCKET_H_ | ||||
| #define SOCKET_H_ | ||||
| 
 | ||||
| #include <map> | ||||
| #include <boost/asio.hpp> | ||||
| #include "Packet.h" | ||||
| #include "Filter.h" | ||||
| #include "Types.h" | ||||
| 
 | ||||
| #define SRC_PORT 29809 | ||||
|  | @ -18,22 +20,19 @@ | |||
| #define MAX_LENGTH 1024 | ||||
| 
 | ||||
| typedef std::function<int(Packet)> Listener; | ||||
| typedef std::pair<Filter, Listener> ListenerPair; | ||||
| 
 | ||||
| class Socket { | ||||
| public: | ||||
|     Socket(boost::asio::io_service&); | ||||
|     virtual ~Socket() { | ||||
|     } | ||||
|     virtual ~Socket(); | ||||
|     void init(short, short); | ||||
|     void send(Packet); | ||||
|     void setHostIp(ipAddr); | ||||
|     void listen(); | ||||
|     Listener callback = [](Packet a) { | ||||
|         return 0; | ||||
|     }; | ||||
|     void receive(); | ||||
|     void listen(Listener l, Filter f = Filter()); | ||||
| 
 | ||||
| private: | ||||
|     void receive(); | ||||
|     void settimeout(); | ||||
|     boost::asio::ip::udp::socket send_socket_; | ||||
|     boost::asio::ip::udp::socket receive_socket_; | ||||
|  | @ -44,7 +43,7 @@ private: | |||
|     boost::asio::deadline_timer timer; | ||||
|     bytes data = bytes(MAX_LENGTH); | ||||
|     ipAddr local_ip; | ||||
|     int initialized = 0; | ||||
|     std::map<Filter, Listener> callback = { }; | ||||
| 
 | ||||
| }; | ||||
| 
 | ||||
|  |  | |||
							
								
								
									
										14
									
								
								src/Types.h
									
										
									
									
									
								
							
							
						
						
									
										14
									
								
								src/Types.h
									
										
									
									
									
								
							|  | @ -50,6 +50,20 @@ public: | |||
|             else break; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     bool operator==(const macAddr &A) { | ||||
|         for (unsigned i = 0; i < 6; i++) { | ||||
|             if(A[i]!=(*this)[i])return false; | ||||
|         } | ||||
|         return true; | ||||
|     } | ||||
| 
 | ||||
|     bool operator!=(const macAddr &A) { | ||||
|         for (unsigned i = 0; i < 6; i++) { | ||||
|             if(A[i]!=(*this)[i])return true; | ||||
|         } | ||||
|         return false; | ||||
|     } | ||||
| }; | ||||
| 
 | ||||
| /*
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue