def call_dts(sock, msg_obj, msg_type):
    header = dts_pb2.ControlRequest()
    header.type = msg_type
    header.id = sock.id_counter.next()

    # The chosen generic address to DTS
    addrSend = "DTS\x00\x00\x00" + getHwAddr(sock.getsockname()[0])
    #invert
    addrRcv = addrSend[6:] + addrSend[:6]

    #print 'addrSend:', addrSend.encode('string-escape'), '\naddrRecv:', addrRcv.encode('string-escape')    # borrar

    header_serial = header.SerializeToString()
    msg_serial = msg_obj.SerializeToString()
    sent = sock.send(addrSend, struct.pack("<H",
                                           len(header_serial)), header_serial,
                     struct.pack("<H", len(msg_serial)), msg_serial)
    if sent != 0:
        # TODO: deal with the case the message to DTS doesn't
        # fit the ethernet frame
        pass

    # Wait for response
    resp_obj = dts_pb2.ControlResponse()
    found_resp = False
    while not found_resp:
        data = sock.recv_filter(addrRcv)
        for msg_buffer in buffer_splitter(data):
            resp_obj.ParseFromString(msg_buffer)
            print resp_obj.request_id
            print header.id
            if resp_obj.request_id == header.id:
                found_resp = True
                break
    return resp_obj
Esempio n. 2
0
    def _handle_PacketIn(self, event):
        if event.data[:14] != self.HEADER:
            # Unknown title and ethertype identifier; discard...
            log.info('Discarding nos-DTSish message...')
            return

        # If the full message was not sent, but instead kept
        # in switch's buffer, request it entirely...
        if event.ofp.total_len != len(event.data):
            log.info('Partial package received. Requesting it fully...')
            msg = of.ofp_packet_out(in_port=of.OFPP_NONE)
            msg.actions.append(of.ofp_action_output(port=of.OFPP_CONTROLLER))
            msg.data = event.ofp

            event.connection.send(msg)
            return

        buff = event.data[14:]
        request = dts_pb2.ControlRequest()
        buff_iter = buffer_splitter(buff)
        while True:
            try:
                request.ParseFromString(buff_iter.next())
            except StopIteration:
                break

            msg_buff = buff_iter.next()
            try:
                (handler, msg_class) = self.MSG_HANDLER[request.type]
                msg = msg_class()
                msg.MergeFromString(msg_buff)
                handler(self, msg, request.id, event)
            except KeyError:
                # TODO: deal with unknown message type
                pass
Esempio n. 3
0
def call_dts(sock, msg_obj, msg_type):
    header = dts_pb2.ControlRequest()
    header.type = msg_type
    header.id = sock.id_counter.next()

    # The chosen generic address to DTS
    addr = "DTS\x00\x00\x00\x00\x00\x00\x00\x00\x00"

    header_serial = header.SerializeToString()
    msg_serial = msg_obj.SerializeToString()
    sent = sock.send(addr, struct.pack("<H",
                                       len(header_serial)), header_serial,
                     struct.pack("<H", len(msg_serial)), msg_serial)
    if sent != 0:
        # TODO: deal with the case the message to DTS doesn't
        # fit the ethernet frame
        pass

    # Wait for response
    resp_obj = dts_pb2.ControlResponse()
    found_resp = False
    while not found_resp:
        data = sock.recv_filter(addr)
        for msg_buffer in buffer_splitter(data):
            resp_obj.ParseFromString(msg_buffer)
            #print resp_obj.request_id, header.id
            if resp_obj.request_id == header.id:
                found_resp = True
                break
    return resp_obj
Esempio n. 4
0
    def _handle_PacketIn(self, event):
        def cleanBuffer(buff):          # for properlly working with certain type of user-space switches
           while ord(buff[-1]) == 0:
               buff = buff[:-1]
           return buff
    
        if not self.wifi:
            if event.data[:14] != self.HEADER:
                # Unknown title and ethertype identifier; discard...
                log.debug('Discarding nos-DTSish message...')
                return
        else:
            if event.data[:6] != self.HEADER[:6] or event.data[:14][-2:] != self.HEADER[-2:]:
                # Unknown title and ethertype identifier; discard...
                log.debug('Discarding nos-DTSish message...')
                return

        # If the full message was not sent, but instead kept in switch's buffer, request it entirely
        if event.ofp.total_len != len(event.data):
            log.info('Partial package received. Requesting it fully...')
            msg = of.ofp_packet_out(in_port=of.OFPP_NONE)
            msg.actions.append(of.ofp_action_output(port = of.OFPP_CONTROLLER))
            msg.data = event.ofp

            event.connection.send(msg)
            return

        if self.wifi:
            addrResp = event.data[:12][6:] + event.data[:12][:6] + '\x08\x80'
        else:
            addrResp = None
        buff = cleanBuffer(event.data[14:])
        request = dts_pb2.ControlRequest()
        buff_iter = buffer_splitter(buff)
        while True:
            try:
                request.ParseFromString(buff_iter.next())
            except StopIteration:
                break

            try:
                msg_buff = buff_iter.next()
            except StopIteration:
                log.info('Problem detected ... activating detonation in 3 .. 2 ..')
                pass

            try:
                (handler, msg_class) = self.MSG_HANDLER[request.type]
                msg = msg_class()
                msg.MergeFromString(msg_buff)
                handler(self, msg, request.id, event, addrResp)
            except KeyError:
                # TODO: deal with unknown message type
                pass