Example #1
0
    def parse_file(self):
        while True:
            buffer = self.in_file.read(PACKET_SIZE)
            if buffer == '':
                break
            elif len(buffer) != PACKET_SIZE:
                break

            packet = Packet(PACKET_SIZE, buffer)
            bad_sync_byte = packet.parse()

            if not bad_sync_byte:
                continue

            self.demultiplexor.packet_num += 1

            # If there is error in the packet or pid is 0x1fff => skip this packet
            if packet.transport_error_ind or packet.pid == 0x1fff:
                continue

            if packet.pid == self.demultiplexor.PID_PAT and not self.demultiplexor.PAT_analysed:
                self.demultiplexor.parse_PAT(packet)
                # break
            elif packet.pid == self.demultiplexor.PID_NIT and not self.demultiplexor.NIT_analysed:
                self.demultiplexor.parse_NIT(packet)
                # break
            elif packet.pid == self.demultiplexor.PID_SDT and not self.demultiplexor.SDT_analysed:
                self.demultiplexor.parse_SDT(packet)
                # break
            elif self.demultiplexor.PAT_analysed and self.demultiplexor.check_PMT(
                    packet.pid):
                self.demultiplexor.parse_PTM(packet)
    def apply(self):
        timer_up = 0
        timer_down = 0
        buffer_up = Buffer()
        buffer_down = Buffer()
        packet_cur_up = 0
        packet_cur_down = 0
        n_packets = self.trace.getPacketCount()
        n_packets_up = sum(1 for p in self.trace.packets
                           if p.getDirection() == Packet.UP)
        n_packets_down = sum(1 for p in self.trace.packets
                             if p.getDirection() == Packet.DOWN)
        n_sent_up = 0
        n_sent_down = 0
        L = self.params['RUN_PADDING']
        cell_size = self.params['CELL_SIZE']
        t_down = self.params['RECV_INTERVAL']
        t_up = self.params['SEND_INTERVAL']

        while n_sent_up + n_sent_down < n_packets \
                or buffer_up.has_packets() or buffer_down.has_packets():

            # add to buffer: all packets that appeared since last clock
            while packet_cur_down < n_packets \
                    and self.trace.packets[packet_cur_down].getTime() <= timer_down:
                packet = self.trace.packets[packet_cur_down]
                if packet.getDirection() == Packet.DOWN:
                    buffer_down.add(packet)
                    n_sent_down += 1
                packet_cur_down += 1
            while packet_cur_up < n_packets \
                    and self.trace.packets[packet_cur_up].getTime() <= timer_up:
                packet = self.trace.packets[packet_cur_up]
                if packet.getDirection() == Packet.UP:
                    buffer_up.add(packet)
                    n_sent_up += 1
                packet_cur_up += 1

            # check buffer: purge at most Packet.MTU bytes
            pcu = buffer_up.pack_from(cell_size - Packet.HEADER_LENGTH)
            pcd = buffer_down.pack_from(cell_size - Packet.HEADER_LENGTH)

            if n_sent_down < n_packets_down:
                self.add_packet(Packet(Packet.DOWN, timer_down, cell_size))
                timer_down += t_down
                n_sent_down += pcd

            if n_sent_up < n_packets_up:
                self.add_packet(Packet(Packet.UP, timer_up, cell_size))
                timer_up += t_up
                n_sent_up += pcu

        while (timer_up // t_up) % L:
            # print 'PAD UP'
            self.add_packet(Packet(Packet.UP, timer_up, cell_size))
            timer_up += t_up
        while (timer_down // t_down) % L:
            # print 'PAD DOWN'
            self.add_packet(Packet(Packet.DOWN, timer_down, cell_size))
            timer_down += t_down
Example #3
0
 def read_packet(self, data):
     in_packet = Packet(0, self.router_id,
                        {})  # Initialise class as placeholder
     rte_table = in_packet.decode(data)  # Decode the data
     packet_src = in_packet.src
     print("Processing packet from router " + str(packet_src))
     self.update_rt_tbl(packet_src, rte_table)  # Update the routing table
Example #4
0
def copyToDFS(address, fname, path):
    """ Contact the metadata server to ask to copu file fname,
        get a list of data nodes. Open the file in path to read,
        divide in blocks and send to the data nodes.
    """

    # Create a connection to the data server
    metaSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        metaSock.connect(address)
    except:
        exit("Connection to Metadata Server failed, exiting...")

    # Get file size
    fsize = stat(fname).st_size

    # Create a Put packet with the fname and the length of the data,
    # and send it to the metadata server
    p = Packet()
    p.BuildPutPacket(path, fsize)
    metaSock.sendall(p.getEncodedPacket())

    # If no error or file exists
    # Get the list of data nodes.

    message = b""
    while buffer := metaSock.recv(1024):
        message += buffer
Example #5
0
    def __get_target_owner(self, ip: str, port: int):
        owner = ['', Packet('1.1.1.1', 2), ""]
        min_timestamp = datetime.datetime.now().timestamp() * 1000000

        for client in self.__db.collection("Clients").stream():
            name = client.id
            client = client.to_dict()

            if (client["IPDST"] == ip and client["PORTDST"] == port
                    and client["ACTIVE"] == True):
                if ((client["TIMESTAMP"] > 0)
                        and (client["TIMESTAMP"] < min_timestamp)):
                    owner[0] = name
                    owner[1] = Packet(client["IPSRC"], client["PORTSRC"])
                    owner[2] = client["NIC"]
                    min_timestamp = client["TIMESTAMP"]

        try:
            print("!!!!!!!!!!!!!!!!!!OWNER IS " + owner[0] +
                  "!!!!!!!!!!!!!!!!!!")
            self.__db.collection("Clients").document(owner[0]).update(
                {"TIMESTAMP": -1.0})

            if (owner[2] != self.__nic_name):
                owner[1].set(((self.__db.collection("Nics").document(
                    owner[2]).get().to_dict())["IP"]), Packet.Options.IP)

            return owner[1]

        except:
            raise Exception("friking little dicking targets owner")
    def rdt_rcv(self, packt):
        # This function is called by the lower-layer when a packet arrives
        # at the receiver
        if self.state == WAITING_FOR_CALL_0_FROM_BELOW:
            if (packt.seq_num == 0 and packt.corrupted == False):
                # Received correct packet. Send ACK_0 .
                response = Packet(seq_num=0, payload="ACK")
                self.channel.udt_send(response)
                self.receiving_app.deliver_data(packt.payload)
                self.state = WAITING_FOR_CALL_1_FROM_BELOW
            else:
                # Need to resend packet.
                # send ACK_1 and discard this packet.
                response = Packet(seq_num=1, payload="ACK")
                self.channel.udt_send(response)

        elif self.state == WAITING_FOR_CALL_1_FROM_BELOW:
            if (packt.seq_num == 1 and packt.corrupted == False):
                # Received correct packet. Send ACK_1 .
                response = Packet(seq_num=1, payload="ACK")
                self.channel.udt_send(response)
                self.receiving_app.deliver_data(packt.payload)
                self.state = WAITING_FOR_CALL_0_FROM_BELOW
            else:
                # Need to resend packet.
                # send ACK_0 and discard this packet.
                response = Packet(seq_num=0, payload="ACK")
                self.channel.udt_send(response)
Example #7
0
 def receive(self, packet):
     if packet.type == 'ping':
         ping_data = packet.data
         if packet.destination == self.id:
             if not ping_data['receive']:
                 ping_data['receive'] = True
                 ping_data['inf'] = 'ok'
                 ping_data['trace'].append(self.id)
                 packet = Packet(type='ping',
                                 source=self.id,
                                 destination=packet.source,
                                 data=ping_data)
             else:
                 ans = ping_data['trace']
                 print(" ".join(map(str, ans)), end=' ')
                 if ping_data['inf'] == 'ok':
                     print('\n', end='')
                 else:
                     print(ping_data['inf'])
                 return
         else:
             if not ping_data['receive']:
                 ping_data['trace'].append(self.id)
             packet = Packet(type='ping',
                             source=packet.source,
                             destination=packet.destination,
                             data=ping_data)
         self.transfer(packet)
Example #8
0
 def send_packet(self, packet, client):
     seq_num = packet.__get__('seq_num')
     client_timeout_count = CLIENT_TIMEOUT_TRIALS
     while client_timeout_count:
         if randint(1, 100) > LOSS_PROBABILITY:
             client.send(packet.__dumb__())
         else:
             print(colored('Simulating Packet Loss: ' + seq_num, 'red'))
         try:  # Wait for response or timeout
             res = client.recv(PACKET_SIZE)
             if not res:
                 print('Client Disconnected')
                 return 0
             pkt = Packet(pickled=res)
             pkt.__print__()
             if pkt.__get__('ack') == '+':
                 return 1
             else:
                 print(colored('Negative ack, resending : ' + seq_num,
                               color='red'))
         except timeout:
             print(colored('Timeout, resending packet: ' + seq_num,
                           color='red'))
             client_timeout_count -= 1
     return 0
Example #9
0
    def addPacketsFromCellsOld(trace, prevcDirection, prevcTime, cellCtr):
        '''
        cellCtr: number of cells in the same direction (1's of -1's) and same time
        when cellCtr = 1 then add 512
        when cellCtr = 2 then add 1024
        when cellCtr = 3 then add 1024 and 512
        when cellCtr = 4 then add 1024 twice
        when cellCtr = 5 then add 1024 twice and one 512
        and so on
        '''
        cLength = 512  # cell length is always 512 bytes in Tor

        pDirection = Packet.UP
        if (prevcDirection == -1):
            pDirection = Packet.DOWN

        # as in the pcapparser.py
        # delta     = int(round(((ts - start) * 1000),0))
        prevcTime = int(round((float(prevcTime) * 1000), 0))

        while cellCtr >= 1:
            if cellCtr == 1:
                trace.addPacket(Packet(pDirection, prevcTime,
                                       cLength))  # add 512
                break
            else:
                trace.addPacket(Packet(pDirection, prevcTime,
                                       2 * cLength))  # add 1024
                cellCtr = cellCtr - 2
    def __init__(self, byteCodeId=None, comResp=None, parameter=None):
        
        if comResp != None:
            #super().__init__(b"\x55", b"\xAA", byteCodeId)
            Packet.__init__(self, b"\x55", b"\xAA", byteCodeId)
            self.__parameter = parameter
            self.__comResp = comResp
        else:

            length = len(byteCodeId)
            if length == 12:
                if byteCodeId[0]== b"\x55" and byteCodeId[1]== b"\xAA":
                    Packet.__init__(self, byteCodeId[0], byteCodeId[1], byteCodeId[2:4])
                    self.__parameter = byteCodeId[4:8]
                    self.__comResp = byteCodeId[8:10]
                    
                    #print("com1:", self.__commandCode0)
                    #print("com2:", self.__commandCode1)
                    #print("deviceId:", self.__deviceId)
                    #print("parameter:", self.__parameter)
                    #print("comResp:", self.__comResp)
                    #print("check:", receivedByteArray[10:12])
                    #print("checkCalc:",self.getCheckSum())

                    if self.getCheckSum(byteCodeId[0:length-2]) != byteCodeId[length-2:length]:
                        raise SensorException(CHECKSUM_ERROR, COMMAND_PACK_ERROR)
                else:
                    raise SensorException(PACKET_HEADER_ERROR, COMMAND_PACK_ERROR)
            else:
                raise SensorException(LENGTH_ERROR, COMMAND_PACK_ERROR) 
 def send_rdt_protocol(self,socket, data,address):
     pkt= Packet(data=data.encode()).pack()
     socket.sendto(pkt,address)
     ack, add = socket.recvfrom(600)
     ack_p = Packet(pkd_data=ack, type='ack')
     if ack_p.checksum == calc_checksum(str(self.client_port)):
         print('Received protocol ack...')
Example #12
0
 def send_basic_info(self):
     while True:
         self.open_file()
         self.packet_number = len(self.data_list)
         # Send basic information to server
         # msg will include operation type, data size...
         if self.weight == -1:
             msg = "client info" + delimiter + str(self.packet_number)
         else:
             msg = "client info" + delimiter + str(self.packet_number) + delimiter + str(self.weight)
         pkt = self.send_packet(IS_INFO, msg, self.server_address, -1, -1)
         try:
             self.sock.settimeout(10)
             # Receive ACK from server
             buf, address = self.sock.recvfrom(size)
         except socket.timeout:
             print("The connection is closed. Start connecting again...\n")
             self.state = CLOSED
             break
         ack = Packet(0, 0, 0, 0, 0, 0, buf)
         ack.decode_buf()
         if int(ack.msg) == pkt.seq + 1:
             break
         else:
             continue
 def __init__(self, byteCodeId=None, data=None):
     if data != None:
         Packet.__init__(self, b"\x5A", b"\xA5", byteCodeId)
         self.__data = data
     else:
         length = len(byteCodeId)
         if length >= 8:
             if byteCodeId[0]== b"\x5A" and byteCodeId[1]== b"\xA5":
                 Packet.__init__(self, byteCodeId[0], byteCodeId[1], byteCodeId[2:4])
                 self.__data = byteCodeId[4:length-2]
             
                 #print("com1:", self.__commandCode0)
                 #print("com2:", self.__commandCode1)
                 #print("Id:", self.__deviceId)
                 #print("rawData:", self.__data)
                 
                 #checkRec = bytearray(byteCode)[length-2] + bytearray(byteCode)[length-1]
                 #print("checkRec:", checkRec)
                 #print("checkRec:", receivedByteArray[length-2:length])
                 
                 #checkCalc = bytearray(super.getCheckSum())[0]+ bytearray(super.getCheckSum())[1]
                 #print("checkCalc:", checkCalc)
                 #print("checkCalc:", self.getCheckSum())
                 
                 #if self.getCheckSum() != receivedByteArray[length-1:length]:
                 #if checkRec != checkCalc:
                 if self.getCheckSum(byteCodeId[0:length-2]) != byteCodeId[length-2:length]:
                     raise SensorException(CHECKSUM_ERROR, DATA_PACK_ERROR)
             else:
                 raise SensorException(PACKET_HEADER_ERROR,DATA_PACK_ERROR)
         else:
             raise SensorException(LENGTH_ERROR, DATA_PACK_ERROR)
Example #14
0
def listening(rskt):
    rpkt = Packet(0, 0, 0)  #initializing packet
    print(rskt.getsockname())
    while (1):
        data, address = rskt.recvfrom(200)
        rpkt.decode_pkt(data)
        printPkt(rpkt, address)
Example #15
0
def client(ip, port):
    # Contacts the metadata server and ask for list of files.

    # Create socket and connect to meta data server
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        sock.connect((ip, port))
    except:
        exit("Could not connect to Metadata Server. Exiting")

    # Create list packet and encode it
    pack = Packet()
    pack.BuildListPacket()
    encoded_packet = pack.getEncodedPacket()

    # Send list packet
    try:
        sock.send(encoded_packet)
    except Exception as e:
        exit(e)

    # Recieve the response packet as a byte string and decode it into pack
    message = b""
    while buffer := sock.recv(1024):
        message += buffer
    def _send_ack(self):
        packet = Packet()
        packet.set("specification", "setting_ack")
        packet.set("next_index", self._next_index)
        packet.set("sensor_id", self._rf_sensor.id)

        self._rf_sensor.enqueue(packet, to=0)
Example #17
0
    def send(self, flag, message):
        #print ("\nTYPE = " + self.type)

        self.send_buffer = message

        if self.remote == None:
            raise 'must call "bind()" first'

        #client initializes connection


        elif flag == None and self.type == "CLIENT" and self.STATE == "ESTABLISHED":

            buffer = []

            while len(message) > 0:
                buffer.append(message[:self.window_size])
                if len(message) < self.window_size:
                    message = ''
                else:
                    message = message[self.window_size:len(message)]

            for s in buffer:
                pack = Packet(s, self.remote)
                pack.flag = None
                pack.seq_number = self.packet_number
                top_data = pack.seq_number + pack.length
                print ("-> data " + str(pack.seq_number) + " - " + str(top_data))


                self.packet_number = top_data

                self.cache[pack.seq_number + pack.length] = pack, self.current_time()

                self.sched.add(self.current_time(), pack, self.send_data)
Example #18
0
def createAndSendPacket(destinationId,ttl,msgType,timestamp,payload):
	p = Packet()
	p.initialise(0,destinationId,ttl,msgType,timestamp,payload)
	print "Creating and Sending: "
	print p
	global sendingSocket
	sendingSocket.sendData(p.getBytes())
 def send_window_size(self,server_socket, window_size, client_addr):
     pkt = Packet(data=str(window_size))
     server_socket.sendto(pkt.pack(), client_addr)
     ack, add = server_socket.recvfrom(600)
     ack_p = Packet(pkd_data=ack, type='ack')
     if ack_p.checksum == calc_checksum(str(window_size)):
         print('Received file window size ack...')
Example #20
0
    def create_tournament_from_directory(self, dir, reuse_html=False):

        conf_file = os.path.join(dir, 'config.json')

        if not os.path.exists(conf_file):
            conf_file = conf_gen(dir, '*.docx')

        conf_dict = json.load(open(conf_file, 'r'))

        self.tour_name = conf_dict['tournament']
        self.year = conf_dict['year']

        packets = conf_dict['packets']

        for file_entry in packets:
            packet_file = os.path.join(dir, file_entry['filename'])
            packet_author = file_entry['author']

            packet = Packet(packet_author, tournament=self.tour_name)
            try:
                packet.load_packet_from_file(packet_file, reuse_html=reuse_html)
            except PacketParserError as ex:
                self.errors.append(ex)

            self.add_packet(packet)
Example #21
0
    def doCountermeasure(trace):
        # Median trace length in the herrmann dataset is 3500ms
        # Median throughput is 62000 bytes/second
        # 40*1500 = 60000 bytes/second

        newTrace = Trace(trace.getId())

        latency = []
        timer = 0
        bufferUP = Folklore.Buffer()
        bufferDOWN = Folklore.Buffer()
        packetCursor = 0

        # Terminate only if (1) our clock is up, (2) we have no more packets from the source
        # and (3) our buffers are empty
        while timer <= Folklore.MILLISECONDS_TO_RUN \
           or packetCursor < trace.getPacketCount() \
           or bufferUP.hasPackets() \
           or bufferDOWN.hasPackets():

            # calculate max latency
            if bufferUP.hasPackets():
                earliestPacket = bufferUP.queue()[0]
                latency.append(timer - earliestPacket.getTime())
            if bufferDOWN.hasPackets():
                earliestPacket = bufferDOWN.queue()[0]
                latency.append(timer - earliestPacket.getTime())

            # add to buffer: all packets that appeared since last clock
            while packetCursor < trace.getPacketCount()\
              and trace.getPackets()[packetCursor].getTime()<=timer:
                packet = trace.getPackets()[packetCursor]

                if packet.getDirection() == Packet.UP:
                    bufferUP.add(packet)
                elif packet.getDirection() == Packet.DOWN:
                    bufferDOWN.add(packet)

                # increment position in source buffer
                packetCursor += 1

            # check buffer UP: purge at most Packet.MTU bytes
            Folklore.packFromBuffer(
                Folklore.FIXED_PACKET_LEN - Packet.HEADER_LENGTH, bufferUP)

            # check buffer DOWN: purge at most Packet.MTU bytes
            Folklore.packFromBuffer(
                Folklore.FIXED_PACKET_LEN - Packet.HEADER_LENGTH, bufferDOWN)

            # send a byte in both directions
            newTrace.addPacket(
                Packet(Packet.DOWN, timer, Folklore.FIXED_PACKET_LEN))
            newTrace.addPacket(
                Packet(Packet.UP, timer, Folklore.FIXED_PACKET_LEN))

            # go to the next clock cycle
            timer += Folklore.TIMER_CLOCK_SPEED

        return [newTrace, latency]
Example #22
0
    def __delete_client(self, client_prop: Packet):
        for i in self.__db.collection("Clients").stream():
            client = i.to_dict()

            if (client["IPSRC"] == client_prop.get(Packet.Options.IP) and
                    client["PORTSRC"] == client_prop.get(Packet.Options.PORT)):
                self.__db.collection("Clients").document(i.id).delete()
                break
Example #23
0
 def recv_file_len(self):
     pkt, adr = self.socket.recvfrom(600)
     unpkd = Packet(pkd_data=pkt)
     ack = Packet(type='ack', chk_sum=calc_checksum(
         unpkd.data.decode())).pack(type='ack')
     self.socket.send(ack)
     self.file_len = int(unpkd.data)
     print('Required file length = ', self.file_len, ' packets.')
Example #24
0
    def __set_new_fromMe_port(self, update: bool = False):
        port = self.__get_new_port()

        self.__PROPERTIES_FROM_ME = Packet(Network_Properties.local_ip(), port)

        if (update):
            self.__db.collection("Servers").document(
                self.__this_server_name).update({"PORTFROMME": port})
Example #25
0
 def startTransmit(self, nw):
     self.status = "Transmitting"
     self.curReceiver = self.selectReceiver(nw)
     self.curTP = (
         (abs(nw.distance[self.id] - nw.distance[self.curReceiver])) *
         nw.distanceBetweenNodes) / nw.vel
     self.packet = Packet(self.packetCount)
     self.transmissionStartTime = nw.cur_time
Example #26
0
 def callback(self, pck):
     #ipdb.set_trace()
     cont = pck.pop_float_list()
     print "Received new packet: " + repr(cont)
     
     out = Packet()
     out.append_float_list(cont)
     self.conn.sendPacket(out)
 def send_file_len(self, socket, address, data):
     self.get_packets_from_file(data)
     pkt = Packet(seqno=0, data=str(self.file_len).encode()).pack()
     socket.sendto(pkt, address)
     ack, add = socket.recvfrom(600)
     ack_p = Packet(pkd_data=ack, type='ack')
     if ack_p.checksum == calc_checksum(str(self.file_len)):
         print('Received file len ack...')
Example #28
0
 def callback(self, pck):
     print "Received new packet: " + repr(pck.read_float_list())
     raw_input()
     out = Packet()
     out.put_float_list(pck.read_float_list())
     print "Sending packet back "
     self.conn.sendPacket(out)
     print "Done."
Example #29
0
    def run(self):
        """
        Once the instance of a Sender is created run can be called to send the file
        Returns void
        """

        raw_packet = None
        readAmount = 512  #max number of bytes to be read into a file
        try:
            infile = open(self.sendFile, "rb")
        except IOError:
            infile.close()
            self.closePorts()
            sys.exit("File Not Found")
        while self.exitFlag == False:
            data = infile.read(readAmount)
            if not data:
                data = ""
                self.exitFlag = True
            sendPacket = Packet(0x497E, "dataPacket", self.next, len(data),
                                data)
            self.packetsSent += 1
            self.socketOut.send(sendPacket.encode())
            print("packet sent")
            acknowledged = False
            while not acknowledged:
                self.socketIn.settimeout(1.0)
                try:
                    self.socketIn.listen(1)
                    if raw_packet == None:
                        raw_packet, addr = self.socketIn.accept()
                        raw_packet.settimeout(1)
                    packet = raw_packet.recv(self.bufferSize)
                    if packet == "":  #No incoming packets from the Channel.
                        break
                    print("packet Received")
                    ackPacket = Packet.decode(packet)
                    if ackPacket.packetType != "0x497E" and ackPacket.packetType != 1 and ackPacket.seqno != self.next and ackPacket.checksum != ackPacket.getChecksum(
                    ):
                        sendPacket = Packet(0x497E, "dataPacket", self.next,
                                            len(data), data)
                        self.socketOut.send(sendPacket.encode())
                        print("packet resent- corrupted")
                        self.packetsSent += 1
                        continue

                    else:
                        acknowledged = True
                        self.next = 1 - self.next
                except socket.timeout:
                    print("packet resent - timed out")
                    sendPacket = Packet(0x497E, "dataPacket", self.next,
                                        len(data), data)
                    self.socketOut.send(sendPacket.encode())
                    self.packetsSent += 1
        infile.close()
        print("Total packets sent including resends: {}".format(
            self.packetsSent))
Example #30
0
def send_init():
    # Send initialisation information from mesh
    print "Connecting to mesh network..."
    global mesh_listening_socket, mesh_sending_socket, n_players, base_location
    
    mesh_listening_socket = SocketConnection('localhost')
    
    # Perform initialisation actions with mesh: get location and assign addresses.
    while True:
        if mesh_listening_socket.connectAsReceiver():
            mesh_sending_socket = SocketConnection('', 29877)
            if mesh_sending_socket.connectAsSender():
                
                # Start the connection to the mesh
                print "Sending init packet to mesh"
                mesh_sending_socket.sendData('*')
                
                # Get the base station location packet
                firstData = mesh_listening_socket.receiveData()
                print "First Data: " + firstData.encode('hex_codec')
                first_packet = Packet(firstData)
                first_payload = first_packet.getPayload()
                print first_payload
                
                mesh_listening_socket.setTimeout(1.0)
                n_players = 3
                # Assign addresses to the expected number of nodes
                s_time = time.time()
                while (len(id_dict) <= 1):
                    if (time.time() > s_time + 10):
                        break;
                    print "Time remaining: " + str(int(s_time + 10 - time.time() + 0.5))
                    try:
                        data = mesh_listening_socket.receiveData()
                        
                        packet = Packet(data)
                        if packet.isIdentification():
                            speck_id = packet.getPayload().getId()
                            
                            print "Id request from %s" % speck_id
                            id_dict.append(speck_id) 
                            # Respond to all requests: packet may have dropped
                            assign_address(speck_id)
                            print "Address assigned"
                            # Reset the start time so we wait from last receive
                            s_time = time.time()
                            print "Time Reset"
                    except:
                        print "No Data received. Retrying"
                
                break
            # If creating sockets doesn't work, wait and try again
            else: 
                time.sleep(1)
        else:
            time.sleep(1)
    
    assign_names()
Example #31
0
    def request_file(self, server_address, file_name, save_file_name):
        """
        Request file from server using GoBackN protocol
        """
        buffer = []
        last_recieved = -1
        to_be_recieved = 0

        # send request
        self.socket.sendto(Packet(seq_num=0, data=file_name).bytes(), server_address)
        LOGGER.info('File request sent, File name: {}'.format(file_name))
        self.start_time = time.time()
        
        # start queue reciving thread
        recieved_queue = Queue()
        end_event = Event()
        recieve_thread = Thread(target=self.recieve_packets, args=[recieved_queue, end_event])
        recieve_thread.start()


        # keep recieving packets and break if the last packet had a EOT
        LOGGER.info('Starting Recieve Loop.')

        pkt = None
        while True:
            try:
                pkt = Packet(packet_bytes=self.corrupt_or_not(recieved_queue.get()))
                LOGGER.info('Recieved Packet {}'.format(pkt))
            except ValueError:
                LOGGER.info("Packet {} is corrupted".format(pkt))
                self.socket.sendto(Packet(seq_num=last_recieved, data='').bytes(), server_address)

            if pkt.seq_num == to_be_recieved:
                LOGGER.info('Packet is in sequence, adding it to buffer.')
                # Add the packet data to the buffer
                buffer.append(pkt.data)
                # send ack for this packet
                self.socket.sendto(Packet(seq_num=to_be_recieved, data='').bytes(), server_address)

                last_recieved = to_be_recieved
                to_be_recieved = (to_be_recieved + 1) % (2 * WINDOW_SIZE)

                # check if last packet join the thread and exit
                if pkt.is_last_pkt:
                    self.end_time = time.time()
                    LOGGER.info('Last Packet recieved, creating file.')
                    end_event.set()
                    with open('recieved/'+save_file_name, 'w') as f:
                        f.write(''.join(buffer))
                    LOGGER.info('File Recieved Successfully.. Exiting')
                    LOGGER.info("Time elapsed: {}".format(self.end_time-self.start_time))
                    break

            else:
                LOGGER.warning('An out-of-sequence packet recieved. ', pkt)
                # send ack for last recieved packet
                self.socket.sendto(Packet(seq_num=last_recieved, data='').bytes(), server_address)
Example #32
0
 def connect(self):    
     address = "192.168.235.6"
     self.conn.createClient(address, 5000)
     while True:
         pck = Packet()
         dat = [random.random() for _ in range(10)]
         pck.put_float_list(dat)
         print "Sending: " + repr(dat)
         self.conn.sendPacket(pck)
         raw_input()
Example #33
0
def createPackets(chunk):
    global totalSize
    global seqNo
    size = sys.getsizeof(chunk) + sys.getsizeof(seqNo)
    packet = Packet(size, seqNo, chunk)
    toggleSeqNo()
    totalSize += size
    checkSum = generateCheckSum(packet)
    packet.ckSum = checkSum
    return packet
Example #34
0
    def __init__(self):
        super().__init__()

        temp = User()

        packet = Packet()
        packet.receiver = "DataProcessingManager"
        packet.data = temp

        self.send_async_packet(packet, self.callback)
Example #35
0
def create_and_send_packet(destinationId,ttl,msgType,timestamp,payload):
    global mesh_sending_socket
    p = Packet()
    p.initialise(0,destinationId,ttl,msgType,timestamp,payload)
    print "Creating and Sending: " + str(p)
    try:
        mesh_sending_socket.sendData(p.getBytes())
        print "sent"
    except:
        print "A connection error happened. Are you simulating the mesh?"
Example #36
0
    def generatePacket(targetDistribution, packet):
        sample = WrightStyleMorphing.sampleFromDistribution(targetDistribution)
        if sample == None:
            newLen = 1500
        else:
            bits = sample.split('-')
            newLen = int(bits[1])
        packet = Packet(packet.getDirection(), packet.getTime(), newLen)

        return packet
    def generatePacket( targetDistribution, packet ):
        sample       = DirectTargetSampling.sampleFromDistribution( targetDistribution )
        if sample == None:
            newLen       = 1500
        else:
            bits         = sample.split('-')
            newLen       = int(bits[1])
        packet       = Packet( packet.getDirection(), packet.getTime(), newLen )

        return packet
Example #38
0
    def user_state(self, leg, shoulder, elbow, foot):
        user_packet = Packet()
        if leg == 1:
            self.leg_1.set_angles(shoulder, elbow, foot)
        elif leg == 2:
            self.leg_2.set_angles(shoulder, elbow, foot)
        elif leg == 3:
            self.leg_3.set_angles(shoulder, elbow, foot)
        elif leg == 4:
            self.leg_4.set_angles(shoulder, elbow, foot)
        elif leg == 5:
            self.leg_5.set_angles(shoulder, elbow, foot)
        elif leg == 6:
            self.leg_6.set_angles(shoulder, elbow, foot)

        user_packet.set_leg(1, self.leg_1)
        user_packet.set_leg(2, self.leg_2)
        user_packet.set_leg(3, self.leg_3)
        user_packet.set_leg(4, self.leg_4)
        user_packet.set_leg(5, self.leg_5)
        user_packet.set_leg(6, self.leg_6)

        user_packet.make_packet()
        packet_list = [user_packet]
        return packet_list
Example #39
0
    def readEsorics16TorFile( webpageId, traceIndex ):

        if webpageId < 100: # 100 and more is nonMon
            file = os.path.join(config.PCAP_ROOT, str(webpageId)+"-"+str(traceIndex))

        else:
            file = os.path.join(config.PCAP_ROOT, str(webpageId-100)) # as the nonMon id starts from 100 and the file names are 0, 1, 2, 3, ...

        fileList = Utils.readFile(file)

        trace = Trace(webpageId)

        '''
        <time-stamp>\t<directed-packet-length>
        1392354075.204044	-565
        1392354075.20464	565
        1392354075.709575	-565
        1392354075.956634	-565
        1392354075.981538	565
        1392354076.192185	565
        1392354076.36483	-565
        1392354076.647188	-565
        1392354076.685665	-1448
        1392354076.685685	-1448
        ...
        '''
        startTime = 0
        for i in range(0,len(fileList)):
            pArray = fileList[i].split("\t")
            cTime = pArray[0]

            if startTime == 0:
                startTime = float(cTime) # first packet's time as a reference

            cDirection = int(pArray[1])

            pDirection = Packet.UP
            if (cDirection < 0):
                pDirection = Packet.DOWN

            # as in the pcapparser.py
            # delta     = int(round(((ts - start) * 1000),0))

            #pTime = int(round(((float(cTime) - startTime) * 1000),0))
            pTime = int(round(((float(cTime) - startTime) ), 0))

            pLength = abs(int(pArray[1]))

            pkt = Packet(pDirection, pTime, pLength)
            pkt.timeStr = cTime

            trace.addPacket(pkt)


        return trace
    def run(self):
        print("Connection de %s %s" % (
            self.ip,
            self.port,
        ))
        #get informations about client
        try:
            self.cs.settimeout(10)
            info = self.cs.recv(2048)

            self.name = Packet.parse(info)["name"]
            #define port
            lport = settings.base_port + len(settings.cThreads)
            self.cs.send(self.buildReq(lport))
            pAck = self.cs.recv(2048)
            pAck = Packet.parse(pAck)
            if pAck['code'] != 0:
                raise Exception('Error on port attribution')
            self.lport = lport
            self.init = True
            while True:
                logging.debug("Client %s:%s wait for message" %
                              (self.ip, self.port))
                data = None
                while 1:
                    try:
                        data = self.cQueue.get(block=True, timeout=10)
                        break
                    except Exception:
                        logging.debug("Send ping to " + self.name)
                        self.cs.send("ping")
                        pong = self.cs.recv(2048)
                        logging.debug(pong)
                        if "pong" in pong:
                            continue
                        else:
                            raise Exception("Connection error for client " +
                                            self.name)

                logging.debug("Client %s:%s received message" %
                              (self.ip, self.port))
                data = Packet.parse(data)
                logging.debug("Client %s:%s build & send message to client" %
                              (self.ip, self.port))
                self.cs.send(self.buildReq(data["command"]))
                cResp = self.cs.recv(2048)
                cResp = Packet.parse(cResp)
                print(cResp)
                logging.debug("Client %s:%s build & send message to admin" %
                              (self.ip, self.port))
                self.aQueue.put(self.buildResp(cResp['code']))

        except Exception, e:
            logging.error(e)
            self.cs.close()
    def run(self) :
        code = None
        try :
            logging.debug('Send information about client')
            self.cs.send(self.buildInfo())
            #receive port
            logging.debug('receive port...')
            raw_port = self.cs.recv(2048)
            raw_port = Packet.parse(raw_port)
            self.process.port = raw_port['command']
            logging.debug('Set port, send response')
            self.cs.send(self.buildResp(0))
            while True :
                logging.debug("Wait for server...")
                data = self.cs.recv(2048)
                logging.debug(data)
                if "ping" in data:
                    self.cs.send("pong")
                    continue

                data = Packet.parse(data)
                if data["command"] == 'close' :
                    self.cs.send("close")
                    break 
                elif data["command"] == 'status' :
                    if self.process.status() :
                        logging.debug("service is running")
                        code = 0
                    else :
                        logging.debug("service is Stopped")
                        code = 1
                elif data["command"] == 'start' :
                    if self.process.start() :
                        logging.debug("Send Started")
                        code = 0 
                    else :
                        logging.debug("Send Error")
                        code = 2
                elif data["command"] == 'stop' :
                    if self.process.stop() :
                        logging.debug("Send Stopped")
                        code = 0
                    else :
                        logging.debug("Send Error")
                        code = 2
                else :
                    logging.debug("Wrong Command")
                    code=3 
                
                self.cs.send(self.buildResp(code))
        except Exception, e:
            logging.error(e)
            code = 5
            self.cs.send(self.buildResp(code))
Example #42
0
 def data_received(self, data):
     """ 
     incoming data are ready 
     """
     
     # compute all packets
     while (len(data) > 0):
         packet = Packet(data)
         size   = packet.getVarint()
         offset = packet.getVarintSize()
         packet = Packet(data[offset:offset+size])
         
         # compute the packet
         self.protocol.Receive(packet)
         
         # remove the packet
         data = data[offset+size:]
Example #43
0
    def connect(self, connect_handler):
        #create SYN packet
       # print ("I AM A CLIENT")
        self.type = "CLIENT"
        packet = Packet(None, (self.host.address, self.host.port))
        packet.flag = "SYN"

        self.STATE = __SYN_SENT__

        print ("LISTEN <- SYN")

        #put SYN packet on the link
        packet.set_key("SYN")
        self.cache[packet.key] = packet
        self.sched.add(self.current_time(), packet, self.send_data)
        #print "sent: " + str(packet.fla)
        
        self.connect_handler = connect_handler
Example #44
0
def getInput():
	while True:
		print "Enter old ID: "
		oldId = sys.stdin.readline()
		print "Enter new ID: "
		newId = sys.stdin.readline()
		'''if(data == 'quit'):
			break
		if(data == ''):
			continue
			'''	
		payload = PayloadIdentification()
		payload.initialise(int(oldId),int(newId))
		p = Packet()
		p.initialise(0,0xFF,1,0,0,payload)
		print p
		global socketConnection
		socketConnection.sendData(p.getBytes())
Example #45
0
    def run(self):
        wait_counter = 0
        while self.stop_event.is_set() == False:

            # If we already have lots of updates pending, send those.
            if len(self.updates) > 4:# and len(self.updates) != 0:
                self.send_to_server()
                self.updates = []
            try:
                data = self.in_socket.receiveData()
                print "Incoming packet: " + str(data.encode('hex_codec'))
                # If we get something, reset the counter
                wait_counter = 0

                packet = Packet(data)
                sender = packet.getOriginId()
                payload = packet.getPayload()

                location = Point(payload.getDecimalLatitude(), payload.getDecimalLongitude(), payload.getElevation())
                game_coords = loc_translate(location)

                # Only send the message if the position is new
                #if self.loc_dict[sender] != game_coords:
                #self.updates.append([str(sender), game_coords])
                self.updates.append([str(randrange(1,5)), [randrange(0,49), randrange(0,49), randrange(0, 20)]])
                #else:
                    #print "MeshForwarder: Speck is already known at %s. Not sending." % game_coords

            except:
                # If no more data, send what we have
                if len(self.updates) > 0:
                    self.send_to_server()

                wait = 0.1
                print "MeshForwarder: No new data received from mesh. Waiting %s sec." % wait
                time.sleep(wait)

                # Stop the thread if we waited for more than ten seconds
                wait_counter += wait
                if wait_counter > 10:
                    print "No data in 10 seconds. Stopping."
                    self.stop()

        print "Mesh Forwarding thread stopped."
Example #46
0
    def handle(self):
        
        # Read command
        while True:
            
            print('> Ready to handle')
            try:
                (type, code) = Packet.unpack_stream(self.rfile)
                print('type = {}'.format(type))
                print('code = {}'.format(code))
            except NameError as e:
                # TODO: Differentiate closed socket from error
                print('> Socket closed')
                break
            
            if type == 'C':
                (argument_type, function) = self.commands.get(code, '')
                if argument_type == '':
                    argument = tuple()
                else:
                    (type, argument) = Packet.unpack_stream(self.rfile)
                    argument = (argument,)

                print('command = {}'.format(code))
                print('argument = {}'.format(argument))

                # Call function
                message = function(*argument)

            else:
                message = ('S', 
                           "Command expected, '{}' received".format(type))

            if message is not None:
                # Send message back
                print('message = ', *message)
                self.wfile.write(Packet.pack(*message))

            message = ('A', code)
            print("> Acknowledge '{}'\n".format(code))
            self.wfile.write(Packet.pack(*message))
 def __init__(self, callback, auto_reconnect = True, framed = True, start_byte = def_start_byte, end_byte = def_end_byte, 
              escape_byte = def_escape_byte, octet_stuff_byte = def_octet_stuff_byte):
     self.callback = callback
     self.start_byte = start_byte
     self.end_byte = end_byte
     self.escape_byte = escape_byte
     self.octet_stuff_byte = octet_stuff_byte
     self.curr_packet = Packet()
     self.auto_reconnect = auto_reconnect
     self.framed = framed
     self.state = disconnected
     self.server = None   # Says whether this connection is running in server mode.
Example #48
0
    def create_tournament_from_directory(self, dir):

        conf_file = os.path.join(dir, 'config.json')

        if not os.path.exists(conf_file):
            conf_file = conf_gen(dir, '*.docx')

        conf_dict = json.load(open(conf_file, 'r'))

        self.tour_name = conf_dict['tournament']
        self.year = conf_dict['year']

        packets = conf_dict['packets']

        for file_entry in packets:
            packet_file = os.path.join(dir, file_entry['filename'])
            packet_author = file_entry['author']

            packet = Packet(packet_author, tournament=self.tour_name)
            packet.load_packet_from_file(packet_file)

            self.add_packet(packet)
 def byte_callback(self, dat):
     #ipdb.set_trace()
     if dat == "":
         self.connection_reset()
         return
     
     if self.framed:
         if self.state == ready:
             if dat == self.start_byte:
                 self.curr_packet.set_start_time()
                 self.change_state(incoming)
             
         elif self.state == incoming:
             if dat == self.escape_byte:
                 self.change_state(escaping)
             elif dat == self.end_byte:
                 self.curr_packet.set_end_time()
                 self.change_state(received)
             else:
                 self.curr_packet.append_byte(dat)
                 
         elif self.state == escaping:
             dat = chr(ord(dat)^ord(self.octet_stuff_byte))
             self.curr_packet.append_byte(dat)
             self.change_state(incoming)
         
         if self.state == received:
             self.change_state(ready)
             self.callback(Packet(self.curr_packet))
             self.curr_packet = Packet()
             
     # In unframed mode, 1 byte = 1 packet.         
     else:
         self.change_state(incoming)
         self.curr_packet.append_byte(dat)
         self.change_state(ready)
         self.callback(Packet(self.curr_packet))
         self.curr_packet.set_start_time()
         self.curr_packet = Packet()
Example #50
0
    def run(self):
        wait_counter = 0
        while self.stop_event.is_set() == False:
            
            # If we already have lots of updates pending, send those.
            if len(self.updates) > 4:
                self.send_to_server()
                self.updates = []
            try:
                data = self.in_socket.receiveData()
                #print "Incoming packet: " + str(data.encode('hex_codec'))
                # If we get something, reset the counter
                wait_counter = 0
                
                packet = Packet(data)
                sender = packet.getOriginId()
                payload = packet.getPayload()
                
                location = Point(payload.getDecimalLatitude(), payload.getDecimalLongitude(), payload.getElevation()) 
                game_coords = loc_translate(location)
                self.updates.append([str(sender), game_coords])
                
            except:
                # If no more data, send what we have
                self.send_to_server()
                
                wait = 0.1
                print "MeshForwarder: No Data received from mesh. Waiting %s sec." % wait
                time.sleep(wait)

                # Stop the thread if we waited for more than ten seconds
                wait_counter += wait
                if wait_counter > 10:
                    print "No data in 10 seconds. Stopping."
                    self.stop()
        
        print "Mesh Forwarding thread stopped."
Example #51
0
    def home(self):
        home_packet = Packet()

        self.leg_1.set_angles(90, 100, 120)
        self.leg_2.set_angles(90, 100, 120)
        self.leg_3.set_angles(90, 100, 120)
        self.leg_4.set_angles(90, 100, 120)
        self.leg_5.set_angles(90, 100, 120)
        self.leg_6.set_angles(90, 100, 120)

        home_packet.set_leg(1, self.leg_1)
        home_packet.set_leg(2, self.leg_2)
        home_packet.set_leg(3, self.leg_3)
        home_packet.set_leg(4, self.leg_4)
        home_packet.set_leg(5, self.leg_5)
        home_packet.set_leg(6, self.leg_6)

        home_packet.make_packet()
        packet_list = [home_packet]
        return packet_list
Example #52
0
    def storage(self):
        storage_packet = Packet()

        self.leg_1.set_angles(60, 15, 20)
        self.leg_2.set_angles(60, 15, 20)
        self.leg_3.set_angles(60, 15, 20)
        self.leg_4.set_angles(60, 15, 20)
        self.leg_5.set_angles(60, 15, 20)
        self.leg_6.set_angles(60, 15, 20)

        storage_packet.set_leg(1, self.leg_1)
        storage_packet.set_leg(2, self.leg_2)
        storage_packet.set_leg(3, self.leg_3)
        storage_packet.set_leg(4, self.leg_4)
        storage_packet.set_leg(5, self.leg_5)
        storage_packet.set_leg(6, self.leg_6)

        storage_packet.make_packet()
        packet_list = [storage_packet]
        return packet_list
Example #53
0
	def __init__(self,main, data):
		Packet.__init__(self, 06,data)
		self.main = main
def parser_driver(doc_file, mode='json'):

    p = Packet('test')
    p.load_packet_from_file(doc_file)
    p.is_valid()
    parser.add_argument('-d', '--dir', dest='dir')
    parser.add_argument('-o', '--operation', dest='operation')
    parser.add_argument('-s', '--spec', dest='spec')
    parser.add_argument('-m', '--mode', dest='mode', default='json')
    parser.add_argument('-u', '--url', dest='url')
    parser.add_argument('-y', '--year', dest='year', type=int)
    parser.add_argument('--reuse-html', type=bool, default=False)
    parser.add_argument('-op', '--output-file', dest='output_file')
    
    args = parser.parse_args()

    if args.dir is not None and args.operation == 'process':
        parse_directory(args.dir, args.mode, args.reuse_html)

    if args.file is not None and args.operation == 'test':
        packet = Packet('test')
        packet.load_packet_from_file(args.file, test=True)

    if args.file is not None and args.operation is not None:
        if args.operation == 'process':
            parser_driver(args.file)

        if args.operation == 'validate':
            validate_json(args.file)

        if args.operation == 'import':
            import_json_into_mongo(args.file)

    if args.operation == 'conf' and args.dir is not None and args.spec is not None:
        conf_gen(args.dir, args.spec)
Example #56
0
import socket
import sys
from Packet import Packet

HOST, PORT = "localhost", 9999
data = " ".join(sys.argv[1:])

# Create a socket (SOCK_STREAM means a TCP socket)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    # Connect to server and send data
    sock.connect((HOST, PORT))
    sock.sendall(Packet.pack('C', data))

    # Receive data from the server and shut down
    buffer = sock.recv(1024)
finally:
    sock.close()

print("Sent:     {}".format(data))
print("Received: {}".format(str(buffer)))

(type, contents) = Packet.unpack(buffer)
if type == 'M':
    print(contents)

print("\n")
Example #57
0
 def __init__(self, *arguments, **kw):
     if len(arguments) == 0:
         Packet.__init__(self)
     else:
         Packet.__init__(self, self.VERSION, arguments[0], self.__class__._id, 0, *arguments[1:], **kw)
Example #58
0
from Packet import Packet
from payload import *

p = Packet("\xff\x00\x01\x00\xff\xff\xde\xe9\x91\x4f\x01\xb5\x8a\x20\xff\x00\xff\x00\x70\x27\x00\x00\xde\xe9\x07\x27\x00\x00\x8a\x20\xff\x00")
print p
print p.getBytes().encode('hex_codec')