| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
import socket,time |
|---|
| 7 |
from config import conf |
|---|
| 8 |
from data import * |
|---|
| 9 |
|
|---|
| 10 |
class _SuperSocket_metaclass(type): |
|---|
| 11 |
def __repr__(self): |
|---|
| 12 |
if self.desc is not None: |
|---|
| 13 |
return "<%s: %s>" % (self.__name__,self.desc) |
|---|
| 14 |
else: |
|---|
| 15 |
return "<%s>" % self.__name__ |
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
class SuperSocket: |
|---|
| 19 |
__metaclass__ = _SuperSocket_metaclass |
|---|
| 20 |
desc = None |
|---|
| 21 |
closed=0 |
|---|
| 22 |
def __init__(self, family=socket.AF_INET,type=socket.SOCK_STREAM, proto=0): |
|---|
| 23 |
self.ins = socket.socket(family, type, proto) |
|---|
| 24 |
self.outs = self.ins |
|---|
| 25 |
self.promisc=None |
|---|
| 26 |
def send(self, x): |
|---|
| 27 |
sx = str(x) |
|---|
| 28 |
x.sent_time = time.time() |
|---|
| 29 |
return self.outs.send(sx) |
|---|
| 30 |
def recv(self, x): |
|---|
| 31 |
return conf.raw_layer(self.ins.recv(x)) |
|---|
| 32 |
def fileno(self): |
|---|
| 33 |
return self.ins.fileno() |
|---|
| 34 |
def close(self): |
|---|
| 35 |
if self.closed: |
|---|
| 36 |
return |
|---|
| 37 |
self.closed=1 |
|---|
| 38 |
if self.ins != self.outs: |
|---|
| 39 |
if self.outs and self.outs.fileno() != -1: |
|---|
| 40 |
self.outs.close() |
|---|
| 41 |
if self.ins and self.ins.fileno() != -1: |
|---|
| 42 |
self.ins.close() |
|---|
| 43 |
def bind_in(self, addr): |
|---|
| 44 |
self.ins.bind(addr) |
|---|
| 45 |
def bind_out(self, addr): |
|---|
| 46 |
self.outs.bind(addr) |
|---|
| 47 |
|
|---|
| 48 |
class L3RawSocket(SuperSocket): |
|---|
| 49 |
desc = "Layer 3 using Raw sockets (PF_INET/SOCK_RAW)" |
|---|
| 50 |
def __init__(self, type = ETH_P_IP, filter=None, iface=None, promisc=None, nofilter=0): |
|---|
| 51 |
self.outs = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW) |
|---|
| 52 |
self.outs.setsockopt(socket.SOL_IP, socket.IP_HDRINCL, 1) |
|---|
| 53 |
self.ins = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(type)) |
|---|
| 54 |
def recv(self, x): |
|---|
| 55 |
return Ether(self.ins.recv(x)).payload |
|---|
| 56 |
def send(self, x): |
|---|
| 57 |
try: |
|---|
| 58 |
sx = str(x) |
|---|
| 59 |
x.sent_time = time.time() |
|---|
| 60 |
self.outs.sendto(sx,(x.dst,0)) |
|---|
| 61 |
except socket.error,msg: |
|---|
| 62 |
log_runtime.error(msg) |
|---|
| 63 |
|
|---|
| 64 |
class SimpleSocket(SuperSocket): |
|---|
| 65 |
desc = "wrapper arround a classic socket" |
|---|
| 66 |
def __init__(self, sock): |
|---|
| 67 |
self.ins = sock |
|---|
| 68 |
self.outs = sock |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
class StreamSocket(SimpleSocket): |
|---|
| 72 |
desc = "transforms a stream socket into a layer 2" |
|---|
| 73 |
def __init__(self, sock, basecls=None): |
|---|
| 74 |
if basecls is None: |
|---|
| 75 |
basecls = conf.raw_layer |
|---|
| 76 |
SimpleSocket.__init__(self, sock) |
|---|
| 77 |
self.basecls = basecls |
|---|
| 78 |
|
|---|
| 79 |
def recv(self, x=MTU): |
|---|
| 80 |
pkt = self.ins.recv(x, socket.MSG_PEEK) |
|---|
| 81 |
x = len(pkt) |
|---|
| 82 |
if x == 0: |
|---|
| 83 |
raise socket.error((100,"Underlying stream socket tore down")) |
|---|
| 84 |
pkt = self.basecls(pkt) |
|---|
| 85 |
pad = pkt[Padding] |
|---|
| 86 |
if pad is not None and pad.underlayer is not None: |
|---|
| 87 |
del(pad.underlayer.payload) |
|---|
| 88 |
while pad is not None and not isinstance(pad, NoPayload): |
|---|
| 89 |
x -= len(pad.load) |
|---|
| 90 |
pad = pad.payload |
|---|
| 91 |
self.ins.recv(x) |
|---|
| 92 |
return pkt |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
if conf.L3socket is None: |
|---|
| 97 |
conf.L3socket = L3RawSocket |
|---|