]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
RPKI: refactore
authorPavel Tvrdík <pawel.tvrdik@gmail.com>
Tue, 22 Dec 2015 15:51:21 +0000 (16:51 +0100)
committerPavel Tvrdík <pawel.tvrdik@gmail.com>
Wed, 23 Dec 2015 14:09:07 +0000 (15:09 +0100)
Move out the translation a PDU header to host byte order to outside of
PDU header correctness checking.

proto/rpki/packets.c

index 9f00ee5fd412791bf5416634434779eaed6b90d7..12785e33147e2291a1ded7f808b89d2d2a402a04 100644 (file)
@@ -486,9 +486,15 @@ static int rtr_send_pdu(struct rpki_cache *cache, const void *pdu, const unsigne
   return RTR_SUCCESS;
 }
 
-/*
+/**
+ * rtr_check_receive_packet - Make a basic validation of received RPKI PDU header:
+ *  - check protocol version
+ *  - check pdu type
+ *  - check size
+ *
+ * @cache cache connection
  * @param len must <= RTR_MAX_PDU_LEN bytes
- * @return RTR_SUCCESS, pdu is converted to host order byte
+ * @return RTR_SUCCESS
  * @return RTR_ERROR, error pdu was sent
  */
 static int
@@ -496,13 +502,6 @@ rtr_check_receive_packet(struct rpki_cache *cache, void *pdu, const size_t len)
 {
   struct rtr_socket *rtr_socket = cache->rtr_socket;
   struct rpki_proto *p = cache->p;
-  //error values:
-  // 0 = no_err
-  // 1 = internal error
-  // 2 = unknown pdu type
-  // 4 = pdu to big
-  // 8 = corrupt data
-  // 16 = unknown pdu version
   int error = RTR_SUCCESS;
 
   // header in hostbyte order, retain original received pdu, in case we need to detach it to an error pdu
@@ -557,15 +556,11 @@ rtr_check_receive_packet(struct rpki_cache *cache, void *pdu, const size_t len)
     goto error;
   }
 
-  memcpy(pdu, &header, sizeof(header)); //copy header in host_byte_order to pdu
-  rtr_pdu_footer_to_host_byte_order(pdu);
-
   if (header.type == IPV4_PREFIX || header.type == IPV6_PREFIX) {
     if (((struct pdu_ipv4 *) pdu)->zero != 0)
       CACHE_TRACE(D_PACKETS, cache, "Warning: Zero field of received Prefix PDU doesn't contain 0");
   }
 
-  rpki_log_packet(cache, pdu, len, RPKI_RECV);
   return RTR_SUCCESS;
 
  error:
@@ -834,6 +829,13 @@ rtr_handle_end_of_data_pdu(struct rpki_cache *cache, void *pdu)
   rtr_schedule_next_expire_check(cache);
 }
 
+static void
+rtr_transform_pdu_to_host_byte_order(byte *pdu)
+{
+  rtr_pdu_header_to_host_byte_order(pdu);
+  rtr_pdu_footer_to_host_byte_order(pdu);
+}
+
 static void
 rpki_rx_packet(struct rpki_cache *cache, byte *pdu, uint len)
 {
@@ -847,7 +849,8 @@ rpki_rx_packet(struct rpki_cache *cache, byte *pdu, uint len)
     return;
   }
 
-  /* the pdu is in host order already */
+  rtr_transform_pdu_to_host_byte_order(pdu);
+  rpki_log_packet(cache, pdu, len, RPKI_RECV);
 
   switch (type)
   {