def buildResponse(req, packet, serverMac, serverIp, **kwargs): resp = None typ = extractMsgType(packet) if typ == Dhcp.DISCOVER_MSG: dhcpPkt = buildOfferMsg(packet, **kwargs) elif typ == Dhcp.REQUEST_MSG: dhcpPkt = buildAckMsg(packet, **kwargs) else: return None udpPkt = udp.buildUdpResponse(req, dhcpPkt) ipKwargs = {} ipKwargs['srcip'] = serverIp if (isinstance(dhcpPkt.yiaddr, (int, long))): ipKwargs['dstip'] = IPAddr(dhcpPkt.yiaddr) else: ipKwargs['dstip'] = dhcpPkt.yiaddr ipKwargs['protocol'] = Ipv4.UDP_PROTOCOL ipPkt = ipv4.buildIpResponse(req, udpPkt, **ipKwargs) udpPkt.csum = udp.checksum(udpPkt) resp = ethernet.buildResponse(req, ipPkt, src=serverMac) return resp
def buildUnreachResp(req, packet, unreachCode): icmpPkt = Icmp() icmpPkt.type = TYPE_DEST_UNREACH icmpPkt.code = unreachCode icmpPkt.set_payload(packet) icmpPkt.parsed = True ipPkt = ipv4.buildIpResponse(req, icmpPkt, protocol=Ipv4.ICMP_PROTOCOL, tos=0xC0) dummy = DummyHdr("\x00\x00\x00\x00") icmpPkt.set_payload(dummy) dummy.set_payload(packet) ipLen = 0 a = ipPkt while a is not None: m = getattr(a, "hdr", None) if m: n = len(a.hdr()) else: n = len(a) ipLen += n a = getattr(a, "next", None) ipPkt.iplen = ipLen icmpPkt.prev = ipPkt ethPkt = ethernet.buildResponse(req, ipPkt) icmpPkt.csum = 0 icmpPkt.csum = checksum(icmpPkt) return ethPkt
def buildResponse(req, packet, typ): resp = None if typ == Echo: icmpPkt = _buildEcho(packet) else: return None if icmpPkt is None: return None ipPkt = ipv4.buildIpResponse(req, icmpPkt, protocol=Ipv4.ICMP_PROTOCOL) resp = ethernet.buildResponse(req, ipPkt) return resp
def buildStandardTcpResponse(req, tcpPkt, payload="", ipId=None): tcpPkt.next = payload offres = 20 * 4 # header length = 20 tcpPkt.off = offres >> 4 tcpPkt.res = offres & 0x0F tcpPkt.parsed = True hdr = tcpPkt.hdr(payload, calc_checksum=False) tcpPkt.arr = hdr tcpPkt.tcplen = len(hdr) kwargs = {} if ipId is None and isSyn(tcpPkt) and isAck(tcpPkt): kwargs["id"] = 0 kwargs["protocol"] = Ipv4.TCP_PROTOCOL ipPkt = ipv4.buildIpResponse(req, tcpPkt, **kwargs) if ipId is not None: ipPkt.id = ipId ipPkt.iplen = len(hdr) + tcpPkt.tcplen tcpPkt.csum = tcpPkt.checksum() resp = ethernet.buildResponse(req, ipPkt) return resp