00001 # Copyright (c) 2008, 2009 Axel Wachtler 00002 # All rights reserved. 00003 # 00004 # Redistribution and use in source and binary forms, with or without 00005 # modification, are permitted provided that the following conditions 00006 # are met: 00007 # 00008 # * Redistributions of source code must retain the above copyright 00009 # notice, this list of conditions and the following disclaimer. 00010 # * Redistributions in binary form must reproduce the above copyright 00011 # notice, this list of conditions and the following disclaimer in the 00012 # documentation and/or other materials provided with the distribution. 00013 # * Neither the name of the authors nor the names of its contributors 00014 # may be used to endorse or promote products derived from this software 00015 # without specific prior written permission. 00016 # 00017 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00021 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00022 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00023 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00024 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00025 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00026 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00027 # POSSIBILITY OF SUCH DAMAGE. 00028 00029 # $Id: ieee802154__socket_8py-source.html,v 1.1.1.2 2010/01/21 21:36:45 awachtler Exp $ 00030 ## 00031 # @file 00032 # @ingroup grpContribCapture 00033 # @brief Wireshark related socket class 00034 # 00035 # (see @ref grpContribCapture "here" for related files) 00036 # 00037 00038 # === import ================================================================== 00039 import sys, threading, socket, os, glob, subprocess, struct 00040 from ieee802154_base import * 00041 00042 # === globals ================================================================= 00043 00044 # === functions =============================================================== 00045 00046 # === classes ================================================================= 00047 00048 ## Socket class 00049 # this class umplements the interface to the 00050 # libpcap/wireshark application. 00051 class SocketOut(PcapBase): 00052 ## conctructor 00053 def open(self, sockname, devin): 00054 self.sockname = sockname 00055 self.InputDev = devin 00056 if os.path.exists(self.sockname): 00057 self.message(1, "remove old socket") 00058 os.remove(self.sockname) 00059 self.TxSocket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) 00060 self.TxSocket.bind(self.sockname) 00061 self.TxThread=threading.Thread(target = self.__tx__) 00062 self.TxThread.setDaemon(1) 00063 self.TxThread.start() 00064 00065 ## closing the socket 00066 def close(self): 00067 self.TxSocket.close() 00068 if self.TxThread != None: 00069 self.TxThread.join(self.TMO) 00070 self.TxThread = None 00071 00072 ## periodically send data retrived from the input device 00073 # (a instance of @ref PcapFile or @ref PcapFile). 00074 def __tx__(self): 00075 run = 1 00076 cnt = 0 00077 while run: 00078 self.TxSocket.listen(1) 00079 conn, addr = self.TxSocket.accept() 00080 self.message(1,"accepted connection sock=%s addr=%s", conn, addr) 00081 while 1: 00082 try: 00083 d = self.InputDev.read_packet() 00084 if d: 00085 conn.sendall(d,socket.MSG_EOR) 00086 self.message(1,"cnt=%d pack=%d len=%d", cnt, self.InputDev.FCNT, len(d)) 00087 if self.VERBOSE > 1: 00088 self.message(self.VERBOSE,":%08d:P:% 3d:%s", 00089 self.InputDev.FCNT,len(d),str(self.InputDev)) 00090 self.message(self.VERBOSE,":".join(map(hex,map(ord,d)))) 00091 cnt += 1 00092 except socket.error: 00093 si = sys.exc_info() 00094 if si[1].args[0]!=32: 00095 self.exc_handler("SocketError: %s" % str(si[1].args)) 00096 break 00097 except: 00098 self.exc_handler("End Thread cnt=%d" % cnt) 00099 run = 0 00100 break 00101 self.message(1,"Packets: fcnt=%d", self.InputDev.FCNT) 00102 00103 # === EOF ====================================================================