猫型エンジニアのブログ

プログラム/ネットワーク系の技術関連をまとめたページです 

RAWソケットプログラミング(L3編)

下書き中…
L3でヘッダチェックサムの処理が必要となります。

#!/usr/bin/env python
from socket import *
rawSocket = socket(AF_PACKET, SOCK_RAW)
rawSocket.bind(("eth1", 0))

class ether_flame:
     def __init__(self, src_mac, dst_mac, ethertype):
           self.src_mac = src_mac
           self.dst_mac = dst_mac
           self.ethertype = ethertype

     def string(self):
           return self.dst_mac + self.src_mac + self.ethertype

class arp_reply_packet:
     def __init__(self, src_mac, src_ip, dst_mac, dst_ip, hw_type="\x00\x01", proto="\x08\x00", h_len="\x06", p_len="\x04", opcode="\x00\x02"):

         self.hw_type = hw_type
         self.proto = proto
         self.h_len = h_len
         self.p_len = p_len
         self.opcode = opcode
         self.src_mac = src_mac
         self.src_ip = src_ip
         self.dst_mac = dst_mac
         self.dst_ip = dst_ip

     def string(self):
          return self.hw_type + self.proto + self.h_len + self.p_len + self.opcode + self.src_mac + self.src_ip + self.dst_mac + self.dst_ip

class ip:
     def __init__(self, version=4, header_length, tos, toatal_length, id, flags, offset, ttl, proto, csum, ip_src, ip_dst, option):

        self.version = version
        self.header_length = header_length
        self.tos = tos
        self.total_length = total_length
        self.id = id
        self.flags = flags
        self.offset = offset
        self.ttl = ttl
        self.proto = proto
        self.csum = csum
        self.ip_src = ip_src
        self.ip_dst = ip_dst
        self.option = option

     def calculate_csum(self)

     def string(self)
          return self.version + self.header_length + self.tos + self.total_length + self.id + self.flags + self.offset + self.ttl + self.proto + self.csum + self.ip_src + self.ip_dst + self.option