Example #1
0
 def validate(self, payload):
     # Strip off udp header size.
     payload_len = self.total_len - self.LEN
     if payload_len != len(payload):
         raise SCMPBadPktLen(
             "%s: length in header (%dB) does not match "
             "supplied payload (%dB)" %
             (self.NAME, self.total_len, self.LEN + len(payload)), 0)
     checksum = self._calc_checksum(payload)
     if checksum != self._checksum:
         raise SCIONChecksumFailed(
             "%s: checksum in header (%s) does not match "
             "checksum of supplied data (%s)" % (
                 self.NAME, hex_str(self._checksum), hex_str(checksum)))
Example #2
0
 def __str__(self):
     return (
         "%s(%sB): class: %s type: %s "
         "length: %sB checksum: %s timestamp: %s" %
         (self.NAME, self.LEN, SCMPClass.to_str(self.class_),
          scmp_type_name(self.class_, self.type), self.total_len,
          hex_str(self._checksum), iso_timestamp(self.timestamp / 1000000)))
Example #3
0
 def short_desc(self):
     a = []
     a.append("%s: id: %s (owner: %s) Up? %s" %
              (self.NAME, hex_str(self.p.id), self.isd_as(), self.p.up))
     for line in str(self.info).splitlines():
         a.append("  %s" % line)
     return "\n".join(a)
Example #4
0
 def start_element(self, hex_=False):
     """
     Returns the start element of the chain.
     """
     if hex_:
         return hex_str(self._start_ele)
     return self._start_ele
Example #5
0
 def __str__(self):
     s = []
     s.append("%s: From: %s (IF: %s) To: %s (IF: %s) Ingress MTU:%s" %
              (self.NAME, self.isd_as(), self.p.ifID, self.p.mtu))
     s.append("  %s" % self.hof())
     s.append("  ig_rev_token: %s" % hex_str(self.p.igRevToken))
     return "\n".join(s)
Example #6
0
 def __str__(self):
     with self._lock:
         if self.blocks:
             act_info = self.blocks[0].info
         else:
             act_info = "(No active blocks)"
         return "SteadyPath %s to %s: %s" % (hex_str(
             self.id), self.remote, act_info)
Example #7
0
 def _process_reply(self, pkt, ext):
     """Process a reply to a steady path request."""
     path_id = ext.path_ids[0]
     dest = pkt.addrs.src.isd_as
     steady = self.dests[dest].get(path_id, None)
     if not steady:
         logging.error("Unknown path ID: %s:\n%s", hex_str(path_id), pkt)
         return
     steady.process_reply(pkt, ext)
Example #8
0
 def current_element(self, hex_=False):
     """
     Return the currently used element or 'None'.
     """
     if self._next_ele_ptr < 0 or self._next_ele_ptr >= self._length - 1:
         return None
     ele = self.entries[self._next_ele_ptr + 1]
     if hex_:
         return hex_str(ele)
     return ele
Example #9
0
 def next_element(self, hex_=False):
     """
     Return the next element in the hash chain or 'None' if the chain is
     empty.
     """
     if self._next_ele_ptr < 0:
         return None
     ele = self.entries[self._next_ele_ptr]
     if hex_:
         return hex_str(ele)
     return ele
Example #10
0
    def handle_request(self, packet, _, from_local_socket=True, sock=None):
        """
        Main routine to handle incoming SCION packets.

        :param bytes packet: The incoming packet to handle.
        :param tuple sender: Tuple of sender IP, port.
        :param bool from_local_socket:
            True, if the packet was received on the local socket.
        """
        from_local_as = from_local_socket
        pkt = self._parse_packet(packet)
        if not pkt:
            return
        if pkt.ext_hdrs:
            logging.debug("Got packet (from_local_as? %s):\n%s", from_local_as,
                          pkt)
        try:
            flags = self.handle_extensions(pkt, True, from_local_as)
        except SCMPError as e:
            self._scmp_validate_error(pkt, e)
            return
        stop, needs_local = self._process_flags(flags, pkt, from_local_as)
        if stop:
            logging.debug("Stopped processing")
            return
        try:
            needs_local |= self._needs_local_processing(pkt)
        except SCMPError as e:
            self._scmp_validate_error(pkt, e)
            return
        if needs_local:
            try:
                pkt.parse_payload()
            except SCIONBaseError:
                log_exception("Error parsing payload:\n%s" % hex_str(packet))
                return
            handler = self._get_handler(pkt)
        else:
            # It's a normal packet, just forward it.
            handler = self.handle_data
        logging.debug(
            "handle_request (from_local_as? %s):"
            "\n  %s\n  %s\n  handler: %s", from_local_as, pkt.cmn_hdr,
            pkt.addrs, handler)
        if not handler:
            return
        try:
            handler(pkt, from_local_as)
        except SCMPError as e:
            self._scmp_validate_error(pkt, e)
        except SCIONBaseError:
            log_exception("Error handling packet: %s" % pkt)
Example #11
0
 def _process_req(self, pkt, ext):
     """Process a steady path request."""
     path_id = ext.path_ids[0]
     self.srcs.setdefault(ext.src_ia, {})
     if ext.setup and path_id in self.srcs[ext.src_ia]:
         logging.error("Setup request for existing path id: %s\n%s",
                       hex_str(path_id), pkt)
         return
     elif not ext.setup and path_id not in self.srcs[ext.src_ia]:
         logging.error("Renewal request for non-existant path id: %s\n%s",
                       hex_str(path_id), pkt)
         return
     ifid = find_last_ifid(pkt, ext)
     if ifid not in self.link_states:
         logging.error("Packet came from unknown interface '%s':\n%s", ifid,
                       pkt)
         return
     if not ext.accepted:
         # Request was already rejected, so just send the packet back.
         pkt.reverse()
         self.sendq.put(pkt)
         return
     state = self.link_states[ifid]
     req_info = ext.req_block.info
     bwsnap = req_info.bw.to_snap()
     bwhint = state.add_steady(path_id, req_info.index, bwsnap,
                               req_info.exp_tick, True, ext.setup)
     if bwhint is not None:
         # This shouldn't happen - if the local BR accepted the reservation,
         # then there should be enough bandwidth available for it. This means
         # our state is out of sync.
         logging.critical("Requested: %s Available bandwidth: %s\n%s",
                          bwsnap, bwhint, pkt)
         return
     self.srcs[ext.src_ia][path_id] = None
     # All is good, return the packet to the requestor.
     pkt.reverse()
     self.sendq.put(pkt)
Example #12
0
 def _get_msg(self):
     if len(self._buf) < 4:
         return None
     msg_len = struct.unpack("!I", self._buf[:4])[0]
     if msg_len + 4 > len(self._buf):
         return None
     msg = self._buf[4:4 + msg_len]
     self._buf = self._buf[4 + msg_len:]
     try:
         return msg_from_raw(msg)
     except SCIONBaseError:
         log_exception("Error parsing message: %s" % hex_str(msg),
                       level=logging.ERROR)
         return None
Example #13
0
 def _parse_end(self, data, req):
     """
     Parse the request block/offer block at the end of the header, if
     present.
     """
     if req:
         if self.accepted:
             self.req_block = self._parse_block(data, self.total_hops,
                                                self.steady)
         else:
             self.req_block = self._parse_offers_block(data)
     if len(data):
         raise SCIONParseError("%s bytes left when parsing %s: %s" %
                               (len(data), self.NAME, hex_str(data.get())))
     self._parse_src_ia()
Example #14
0
 def _parse_packet(self, packet):
     try:
         pkt = SCIONL4Packet(packet)
     except SCMPError as e:
         self._scmp_parse_error(packet, e)
         return None
     except SCIONBaseError:
         log_exception("Error parsing packet: %s" % hex_str(packet),
                       level=logging.ERROR)
         return None
     try:
         pkt.validate(len(packet))
     except SCMPError as e:
         self._scmp_validate_error(pkt, e)
         return None
     except SCIONChecksumFailed:
         logging.debug("Dropping packet due to failed checksum:\n%s", pkt)
     return pkt
Example #15
0
def serve_forever(soc, bridge_mode, scion_mode, kbase, source_isd_as,
                  target_isd_as):
    """
    Serve incoming HTTP requests until a KeyboardInterrupt is received.
    :param soc: Socket object that belongs to the server.
    :type soc: socket
    :param handler: The type of class to be instantiated as the
    connection handler.
    :type handler: ConnectionHandler or ForwardingProxyConnectionHandler
    :param scion_soc: Use SCION multi-path sockets.
    :type scion_soc: boolean
    """
    while True:
        con, addr = soc.accept()
        conn_id = hex_str(os.urandom(CONN_ID_BYTES))
        if bridge_mode:
            params = (ForwardingProxyConnectionHandler, con, addr, conn_id,
                      scion_mode, kbase, source_isd_as, target_isd_as)
        else:
            params = ConnectionHandler, con, addr, conn_id

        threading.Thread(target=thread_safety_net, args=params,
                         daemon=True).start()
Example #16
0
 def __str__(self):
     flags = self._pack_flags()
     return ("%s(%dB): flags: %s, exp_time: %s, "
             "ingress: %s, egress: %s, mac: %s" %
             (self.NAME, len(self), HopOFFlags.to_str(flags), self.exp_time,
              self.ingress_if, self.egress_if, hex_str(self.mac)))
Example #17
0
 def __str__(self):
     tmp = ["%s(%dB):" % (self.NAME, len(self))]
     tmp.append("Ingress: %s" % self.ingress)
     tmp.append("Egress: %s" % self.egress)
     tmp.append("Mac: %s" % hex_str(self.mac))
     return " ".join(tmp)
Example #18
0
 def hdr_str(name, val):
     if not val:
         return
     ret.append("  %s(%dB): %s" % (name, len(val), hex_str(val)))
Example #19
0
 def log_recv(self, handler, data):
     logging.log(self.level, "Received: %s <%d> : %s",
                 self._common_prefix(handler, data), len(data),
                 hex_str(data))
Example #20
0
 def __str__(self):
     return ("%s(%sB): Height: %s Order: %s\n\tSignature: %s\n\tHashes: %s" % (
              self.NAME, len(self), self.height, hex_str(self.order),
              hex_str(self.signature), hex_str(self.hashes)))
Example #21
0
 def log_send(self, handler, data):
     logging.log(self.level, "Sent: %s <%d> : %s",
                 self._common_prefix(handler, data), len(data),
                 hex_str(data))
Example #22
0
 def __str__(self):
     return "UDP hdr (%sB): sport: %s dport: %s length: %sB checksum: %s" \
         % (self.LEN, self.src_port, self.dst_port,
            self.total_len, hex_str(self._checksum))
Example #23
0
 def __str__(self):
     return "%s(%dB): class: %s payload: %s" % (self.NAME, len(
         self), ExtensionClass.to_str(self.EXT_CLASS), hex_str(self.pack()))
Example #24
0
 def __str__(self):
     s = "%s(%dB): %s" % (self.NAME, len(
         self._raw), hex_str(self._raw[:self.SNIPPET_LEN]))
     if len(self._raw) > self.SNIPPET_LEN:
         s += "[...]"
     return s
Example #25
0
 def __str__(self):
     return ("%s(%dB): IOF offset:%sB HOF offset: %sB "
             "IF id: %s Ingress: %s Rev token: %s" %
             (self.NAME, len(self), self.iof_off, self.hof_off, self.if_id,
              self.ingress, hex_str(self.rev_info)))
Example #26
0
 def __str__(self):
     return "%s(%sB): Direction: %s MAC: %s" % (
         self.NAME, len(self), SCMPAuthDRKeyDirections.to_str(self.direction), hex_str(self.mac))
Example #27
0
 def __str__(self):
     return "%s(%sB):\n\tMeta: %s\n\tAuth: %s" % (self.NAME, len(
         self), hex_str(self.metadata), hex_str(self.authenticator))
Example #28
0
 def __str__(self):
     s = "PayloadRaw(%dB): %s" % (len(
         self._raw), hex_str(self._raw[:self.SNIPPET_LEN]))
     if len(self._raw) > self.SNIPPET_LEN:
         s += "[...]"
     return s
Example #29
0
 def __str__(self):
     return "%s(%dB): id:%s seq:%s" % (self.NAME, self.LEN, hex_str(
         self.id), self.seq)
Example #30
0
 def _id_with_owner(self, id_):  # pragma: no cover
     return "%s (Owner: %s)" % (hex_str(id_), ISD_AS(id_[:ISD_AS.LEN]))