]>
git.ipfire.org Git - oddments/cappie.git/blob - cappie/__init__.py
4152b2fd0385e6b67f11a0c85bb9f9e52acff7c7
2 ###############################################################################
5 # Copyright (C) 2010 Michael Tremer #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
23 import logging
.handlers
27 from ConfigParser
import ConfigParser
28 from threading
import Thread
36 def getAllInterfaces():
37 filters
= ("lo", "any")
39 for dev
in pcapy
.findalldevs():
40 if not dev
in filters
:
46 self
.__interfaces
= []
48 self
.log
= logging
.getLogger("cappie")
49 self
.log
.setLevel(logging
.INFO
)
52 handler
= logging
.StreamHandler()
53 handler
.setFormatter(logging
.Formatter("%(levelname)7s %(message)s"))
54 self
.log
.addHandler(handler
)
57 handler
= logging
.handlers
.SysLogHandler("/dev/log")
58 handler
.setFormatter(logging
.Formatter("cappie: %(message)s"))
59 self
.log
.addHandler(handler
)
61 self
.queue
= queue
.Queue(self
.log
)
63 self
.log
.info("Cappie successfully started")
67 self
.log
.info("Exiting")
69 def setDebug(self
, debug
):
71 self
.log
.setLevel(logging
.DEBUG
)
73 self
.log
.setLevel(logging
.INFO
)
75 def addInterface(self
, dev
, **kwargs
):
76 if not dev
in getAllInterfaces():
77 raise InterfaceError
, "No such interface %s" % dev
79 kwargs
["cappie"] = self
81 iface
= Interface(dev
, **kwargs
)
82 self
.__interfaces
.append(iface
)
85 if not self
.__interfaces
:
86 raise RuntimeError, "No interfaces were configured"
91 # Start a thread for each interface
92 for iface
in self
.__interfaces
:
96 if not self
.queue
.isAlive():
97 self
.log
.critical("Queue thread died unexpectedly.")
100 for iface
in self
.__interfaces
:
101 if not iface
.isAlive():
102 self
.log
.critical("Thread died unexpectedly. %s" % iface
.dev
)
106 def readConfig(self
, configfile
):
107 config
= ConfigParser()
108 config
.read([configfile
])
111 if config
.has_section("global"):
112 for option
, value
in config
.items("global"):
113 global_opts
[option
] = value
115 config
.remove_section("global")
117 for iface
in config
.sections():
119 for option
, value
in config
.items(iface
):
120 options
[option
] = value
121 self
.addInterface(iface
, **options
)
124 for iface
in self
.__interfaces
:
127 self
.queue
.shutdown()
134 class Interface(Thread
):
137 def __init__(self
, dev
, cappie
, promisc
=False, mtu
=1500):
138 Thread
.__init
__(self
)
142 self
.log
= self
.cappie
.log
144 self
.promisc
= promisc
145 self
.queue
= self
.cappie
.queue
147 self
.log
.debug("Created new interface %s" % self
.dev
)
149 self
.__running
= True
151 def _callback(self
, header
, data
):
152 self
.log
.debug("Received packet on %s" % self
.dev
)
154 p
= protocol
.decode_packet(data
)
155 except PacketTypeError
, e
:
156 self
.log
.error("Got unknown packet: %s" % e
)
158 except DecodeError
, e
:
159 self
.log
.warning("Got decoding error: %s" % e
)
162 # Dump packet information
163 for key
, val
in p
.items():
164 self
.log
.debug(" %s: %s" % (key
, val
))
166 self
._handlePacket
(p
)
169 self
.log
.info("Starting interface %s" % self
.dev
)
171 p
= pcapy
.open_live(self
.dev
, self
.mtu
, self
.promisc
, 0)
172 p
.setfilter(self
.filter)
173 #p.loop(0, self._callback)
177 if not self
.__running
:
180 if p
.dispatch(1, self
._callback
):
183 time
.sleep(self
.heartbeat
)
186 if not self
.__running
:
189 self
.log
.debug("Sending shutdown signal to %s" % self
.dev
)
190 self
.__running
= False
196 def addEvent(self
, event
):
197 return self
.cappie
.queue
.add(event
)
199 def _handlePacket(self
, packet
):
200 if packet
.operation
== OPERATION_RESPONSE
:
201 self
.addEvent(EventResponseTrigger(self
, packet
))
202 #self.addEvent(EventCheckDuplicate(self, packet))
204 elif packet
.operation
== OPERATION_REQUEST
:
205 self
.addEvent(EventRequestTrigger(self
, packet
))