def send_packet(self, packet, **kwargs): """ Sending a packet to an host does not require any connection or any call to connect. So if a packet is the first for a destination host. Associated rules are added in iptables. Then every fields are setup in order to call the transfer it to the lowest layer """ try: ip = self.remoteIP if self.remoteIP else kwargs["IP"]["dst"] except KeyError: raise Exception("[Errno 89] Destination address required") if not re.match(self.ipregex, ip): #Then this is a dn realip = transversal_layer_access["DNS"].nslookup(ip) if realip: ip = realip else: raise Exception("[Errno -5] No address associated with hostname") if not self.connectionID: block_icmp_port_unreachable() self.connectionID = (self.localIP, self.localPort) self.lowerLayers['default'].register_upper_layer(self.connectionID, self) if not kwargs.has_key("UDP"): kwargs["UDP"] = {} kwargs["UDP"]["sport"] = self.localPort kwargs["UDP"]["dport"] = self.remotePort if self.remotePort else kwargs["UDP"]["dport"] if not kwargs.has_key("IP"): kwargs["IP"] = {} kwargs["IP"]["src"] = self.localIP kwargs["IP"]["dst"] = ip self.transfer_packet(packet, **kwargs)
def bind(self, port, app=None, fork=None): #App and fork are just here to be generic with the tcp bind from the pysocket point of view """ Bind like connect will register a handler in the UDP layer. But it will also prevent the host to send ICMP host port unreachable """ self.localPort = port block_icmp_port_unreachable() #block_outgoing_packets("udp", self.localIP, self.localPort, None, None) self.connectionID = (self.localIP, self.localPort) self.lowerLayers['default'].register_upper_layer(self.connectionID, self)