]>
git.ipfire.org Git - oddments/cappie.git/blob - cappie/__init__.py
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
35 def getAllInterfaces():
36 filters
= ("lo", "any")
38 for dev
in pcapy
.findalldevs():
39 if not dev
in filters
:
45 self
.__interfaces
= []
47 self
.log
= logging
.getLogger("cappie")
48 self
.log
.setLevel(logging
.INFO
)
51 handler
= logging
.StreamHandler()
52 handler
.setFormatter(logging
.Formatter("%(levelname)7s %(message)s"))
53 self
.log
.addHandler(handler
)
56 handler
= logging
.handlers
.SysLogHandler("/dev/log")
57 handler
.setFormatter(logging
.Formatter("cappie: %(message)s"))
58 self
.log
.addHandler(handler
)
60 self
.queue
= queue
.Queue(self
.log
)
62 self
.log
.info("Cappie successfully started")
66 self
.log
.info("Exiting")
68 def setDebug(self
, debug
):
70 self
.log
.setLevel(logging
.DEBUG
)
72 self
.log
.setLevel(logging
.INFO
)
74 def addInterface(self
, dev
, **kwargs
):
75 if not dev
in getAllInterfaces():
76 raise InterfaceError
, "No such interface %s" % dev
78 kwargs
["cappie"] = self
80 iface
= Interface(dev
, **kwargs
)
81 self
.__interfaces
.append(iface
)
84 if not self
.__interfaces
:
85 raise RuntimeError, "No interfaces were configured"
90 # Start a thread for each interface
91 for iface
in self
.__interfaces
:
95 if not self
.queue
.isAlive():
96 self
.log
.critical("Queue thread died unexpectedly.")
99 for iface
in self
.__interfaces
:
100 if not iface
.isAlive():
101 self
.log
.critical("Thread died unexpectedly. %s" % iface
.dev
)
105 def readConfig(self
, configfile
):
106 config
= ConfigParser()
107 config
.read([configfile
])
110 if config
.has_section("global"):
111 for option
, value
in config
.items("global"):
112 global_opts
[option
] = value
114 config
.remove_section("global")
116 for iface
in config
.sections():
118 for option
, value
in config
.items(iface
):
119 options
[option
] = value
120 self
.addInterface(iface
, **options
)
123 for iface
in self
.__interfaces
:
126 self
.queue
.shutdown()
129 class Interface(Thread
):
132 def __init__(self
, dev
, cappie
, promisc
=False, mtu
=1500):
133 Thread
.__init
__(self
)
137 self
.log
= self
.cappie
.log
139 self
.promisc
= promisc
140 self
.queue
= self
.cappie
.queue
142 self
.log
.debug("Created new interface %s" % self
.dev
)
144 self
.__running
= True
146 def _callback(self
, header
, data
):
147 self
.log
.debug("Received packet on %s" % self
.dev
)
149 p
= protocol
.decode_packet(data
)
150 except PacketTypeError
, e
:
151 self
.log
.error("Got unknown packet: %s" % e
)
153 except DecodeError
, e
:
154 self
.log
.warning("Got decoding error: %s" % e
)
157 # Dump packet information
158 for key
, val
in p
.items():
159 self
.log
.debug(" %s: %s" % (key
, val
))
162 self
.log
.info("Starting interface %s" % self
.dev
)
164 p
= pcapy
.open_live(self
.dev
, self
.mtu
, self
.promisc
, 0)
165 p
.setfilter(self
.filter)
166 #p.loop(0, self._callback)
170 if not self
.__running
:
173 if p
.dispatch(1, self
._callback
):
176 time
.sleep(self
.heartbeat
)
179 if not self
.__running
:
182 self
.log
.debug("Sending shutdown signal to %s" % self
.dev
)
183 self
.__running
= False