def server(interface, port): server_stack = PyStack(interface) # Create a stack print '1' server_app = TCPApplication() # Create a TCPApplication server_stack.register_tcp_application(server_app) server_stack.run( doreactor=False ) # Run the stack to start listening using a thread to make it non-blocking server_app.bind(port, server_app, False) print '2' server_app.listen(3) # the maximum tcp clients to establish connections print '3' flow_one = server_app.accept( ) #will return the same tcp_application if newinstace is set to false in bind(port,app,newinstance) #print type(flow_one) print 'accept client request' while True: time.sleep(1) #flow_one.send_packet('ok') data = flow_one.fetch_data() if data != None: print data
def bind(self, port, app=None, newinstance=False): """ The bind method is quite ligthweight. It justs register itself to the TCP protocol as a handler and an entry is added to iptables to prevent the Hosting host to reply with RST. Note app and newinstance define on which TCPApplication client connections should be redirected and if the TCPApplication should be forked for every client or not. """ self.app = app if app else TCPApplication() self.newinstance = newinstance self.localPort = port block_outgoing_packets("tcp", self.localIP, self.localPort, None, None) self.connectionID = (self.localIP, self.localPort) self.lowerLayers['default'].register_upper_layer( self.connectionID, self)
def client(interface, server_ip, server_port, **kwargs): full_stack = PyStack(interface) #Create a stack client_app = TCPApplication() #Create a TCPApplication full_stack.register_tcp_application( client_app ) #Register the application to the stack which will manage to create the TCPSession etc. full_stack.run( doreactor=False ) #Run the stack to start listening using a thread to make it non-blocking if client_app.connect(server_ip, server_port): #Connect to the given server print 'connect to server' i = 0 while True: print 'send packet' i += 1 mpls_info = {} mpls_info["cos"] = 0L mpls_info["ttl"] = 64 mpls_info["s"] = 1L mpls_info["label"] = 1L kwargs["MPLS"] = mpls_info client_app.send_packet("{0}:changjiang!".format(i), **kwargs) #Send the request to the server time.sleep(1) client_app.close() print 'can not connect to server' full_stack.stop() #Stop the stack
kwargs["IP"]["flags"] = 2 if t1 or t2: t1 = t1 if t1 else 0 import time #t1 = time.time() t2 = t2 if t2 else 0 #t2 = 0 if kwargs["TCP"].has_key("options"): kwargs["TCP"]["options"].append(("Timestamp", (t1, t2))) else: kwargs["TCP"]["options"] = [("Timestamp", (t1, t2))] if res: kwargs["IP"]["flags"] = 2 return packet, kwargs if __name__ == "__main__": stack = PyStack() steganoapp = SteganoApplication() server = TCPApplication() stack.register_tcp_application(server) server.bind(7777, steganoapp, True) server.listen(5) stack.run(doreactor=True) #stack.stop() called when Ctrl+C
arp = ARPProtocol(interface) eth.register_layer(arp) #Layer 4 tcp = TCPProtocol() ip.register_layer(tcp) #Layer 5 tcpsession = TCPSession(interface) tcp.register_layer(tcpsession) #Layer 6 #Nothing for now #Layer 7 conn = TCPApplication() tcpsession.register_layer(conn) #conn.connect("192.168.1.48",7777) conn.bind(7777) #conn.send_packet("Hello world !") #packet = IP(dst="192.168.1.48") #ip.send_packet(packet) ''' def test(): global ip packet = IP(dst="192.168.1.48") ip.send_packet(packet) reactor.callWhenRunning(test)