chiark / gitweb /
sd-event: fix typo
[elogind.git] / src / libsystemd-network / test-lldp.c
index 288aac5961fa62976e22399aaa1afdecb80efa93..06545aee59e56cae67c9aaafe77e04a2c1d4d202 100644 (file)
 ***/
 
 #include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
-#include <sys/socket.h>
-#include <linux/if.h>
-#include <linux/if_ether.h>
 #include <net/ethernet.h>
-#include <sys/types.h>
 #include <arpa/inet.h>
 
 #include "macro.h"
@@ -45,13 +40,13 @@ static struct ether_addr mac_addr = {
 static int lldp_build_tlv_packet(tlv_packet **ret) {
         _cleanup_tlv_packet_free_ tlv_packet *m = NULL;
         const uint8_t lldp_dst[] = LLDP_MULTICAST_ADDR;
-        struct ether_header ether;
+        struct ether_header ether = {
+                .ether_type = htons(ETHERTYPE_LLDP),
+        };
 
         /* Append ethernet header */
-        memset(&ether, 0, sizeof(ether));
         memcpy(&ether.ether_dhost, lldp_dst, ETHER_ADDR_LEN);
         memcpy(&ether.ether_shost, &mac_addr, ETHER_ADDR_LEN);
-        ether.ether_type = htons(ETHERTYPE_LLDP);
 
         assert_se(tlv_packet_new(&m) >= 0);
 
@@ -131,7 +126,8 @@ static int lldp_parse_chassis_tlv(tlv_packet *m, uint8_t *type) {
 }
 
 static int lldp_parse_port_id_tlv(tlv_packet *m) {
-        char *str = NULL, *p;
+        _cleanup_free_ char *p = NULL;
+        char *str = NULL;
         uint16_t length;
         uint8_t subtype;
 
@@ -143,9 +139,8 @@ static int lldp_parse_port_id_tlv(tlv_packet *m) {
         case LLDP_PORT_SUBTYPE_INTERFACE_NAME:
                 assert_se(tlv_packet_read_string(m, &str, &length) >= 0);
 
-                p = malloc0(length + 1);
+                p = strndup(str, length-1);
                 assert_se(p);
-                strncpy(p, str, length-1);
 
                 assert_se(streq(p, TEST_LLDP_PORT) == 1);
                 break;
@@ -159,14 +154,15 @@ static int lldp_parse_port_id_tlv(tlv_packet *m) {
 }
 
 static int lldp_parse_system_name_tlv(tlv_packet *m) {
-        char *str = NULL, *p;
+        _cleanup_free_ char *p = NULL;
+        char *str = NULL;
         uint16_t length;
 
         assert_se(lldp_tlv_packet_enter_container(m, LLDP_TYPE_SYSTEM_NAME) >= 0);
         assert_se(tlv_packet_read_string(m, &str, &length) >= 0);
 
-        p = malloc0(length + 1);
-        strncpy(p, str, length);
+        p = strndup(str, length);
+        assert_se(p);
 
         assert_se(streq(p, TEST_LLDP_TYPE_SYSTEM_NAME) == 1);
 
@@ -176,15 +172,15 @@ static int lldp_parse_system_name_tlv(tlv_packet *m) {
 }
 
 static int lldp_parse_system_desc_tlv(tlv_packet *m) {
-        char *str = NULL, *p;
+        _cleanup_free_ char *p = NULL;
+        char *str = NULL;
         uint16_t length;
 
         assert_se(lldp_tlv_packet_enter_container(m, LLDP_TYPE_SYSTEM_DESCRIPTION) >= 0);
         assert_se(tlv_packet_read_string(m, &str, &length) >= 0);
 
-        p = malloc0(length + 1);
+        p = strndup(str, length);
         assert_se(p);
-        strncpy(p, str, length);
 
         assert_se(streq(p, TEST_LLDP_TYPE_SYSTEM_DESC) == 1);