10
10
11
11
from django .contrib .auth .models import User
12
12
13
+ from twisted .internet import reactor
14
+
13
15
from settings import ARP_CACHE_TIMEOUT , MAY_FORWARD_TO_PRIVATE_IPS , WEB_SERVER_ROOT_WWW
14
16
from AddressAllocation import allocate_to_topology
15
17
from DRRQueue import DRRQueue
@@ -256,7 +258,7 @@ def handle_packet_from_client(self, conn, pkt_msg):
256
258
257
259
# failed to find the specified interface
258
260
fmt = 'bad packet request: invalid interface: %s'
259
- return fmt % (n .name , departure_intf_name )
261
+ return fmt % (( n .name , departure_intf_name ), )
260
262
261
263
def create_job_for_incoming_packet (self , packet , rewrite_dst_mac ):
262
264
"""Enqueues a job for handling this packet with handle_incoming_packet()."""
@@ -909,6 +911,8 @@ class WebServer(BasicNode):
909
911
def __init__ (self , topo , name , path_to_serve ):
910
912
BasicNode .__init__ (self , topo , name )
911
913
self .http_server = HTTPServer (TCPServer .ANY_PORT , path_to_serve )
914
+ self .intf = None
915
+ self .pkt = None
912
916
913
917
@staticmethod
914
918
def get_type_str ():
@@ -917,24 +921,32 @@ def get_type_str():
917
921
def handle_non_icmp_ip_packet_to_self (self , intf , pkt ):
918
922
"""If pkt is to an HTTP_PORT, then the packet is handed off to the HTTP
919
923
server. Otherwise, the default superclass implementation is called."""
924
+ reactor .callLater (2 , self .send_packets )
920
925
if pkt .is_valid_tcp ():
921
926
if is_http_port (pkt .tcp_dst_port ):
922
927
self .handle_http_request (intf , pkt )
923
928
return
924
929
BasicNode .handle_non_icmp_ip_packet_to_self (self , intf , pkt )
925
930
926
931
def handle_http_request (self , intf , pkt ):
927
- """Forward the received packet from an HTTP client to the web server."""
932
+ """Forward the rceived packet from an HTTP client to the web server."""
933
+ self .intf = intf
934
+ self .pkt = pkt
928
935
tcp_conn = self .http_server .handle_tcp (pkt )
929
- if tcp_conn :
930
- tcp_pts = tcp_conn .get_packets_to_send ()
931
- if tcp_pts :
932
- for tcp , data in tcp_pts :
933
- eth = pkt .get_reversed_eth ()
934
- ip = pkt .get_reversed_ip (new_ttl = 64 , new_tlen = pkt .ip_hlen + len (tcp )+ len (data ))
935
- pkt_out = eth + ip + Packet .cksum_tcp_hdr (ip , tcp , data ) + data
936
- logging .debug ('%s sending packet from HTTP server: %s' % (self , pktstr (pkt_out )))
937
- intf .link .send_to_other (intf , pkt_out )
936
+ self .send_packets ()
937
+
938
+ def send_packets (self ):
939
+ """Send any waiting packets"""
940
+ if self .intf and self .pkt :
941
+ for (_ , tcp_conn ) in self .http_server .connections .iteritems ():
942
+ if tcp_conn and not tcp_conn .dead :
943
+ tcp_pts = tcp_conn .get_packets_to_send ()
944
+ for tcp , data in tcp_pts :
945
+ eth = self .pkt .get_reversed_eth ()
946
+ ip = self .pkt .get_reversed_ip (new_ttl = 64 , new_tlen = self .pkt .ip_hlen + len (tcp )+ len (data ))
947
+ pkt_out = eth + ip + Packet .cksum_tcp_hdr (ip , tcp , data ) + data
948
+ logging .debug ('%s sending packet from HTTP server: %s' % (self , pktstr (pkt_out )))
949
+ self .intf .link .send_to_other (self .intf , pkt_out )
938
950
939
951
def __str__ (self ):
940
952
ps = ' serving:%s' % self .http_server .get_path_being_served ()
0 commit comments