HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/libsystemd-network/dhcp-message-dump.c

44,270 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 0314522231108df8fb85fd240c71e594b921f4aa

Lines 1001–1181 of 1,181

                        uint8_t code = SD_DHCP_OPTION_PAD;           /* avoid false maybe-uninitialized warning */
                        DHCPOptionType type = DHCP_OPTION_TYPE_AUTO; /* avoid false maybe-uninitialized warning */
                        r = parse_arg(*arg, &code, &type);
                        if (r < 0)
                                return r;
                        assert(!IN_SET(code, SD_DHCP_OPTION_PAD, SD_DHCP_OPTION_END));
 
                        r = dump_dhcp_option_one(table, message, code, type);
                        if (r < 0)
                                return r;
 
                        if (type == DHCP_OPTION_TYPE_AUTO)
                                switch (code) {
                                case SD_DHCP_OPTION_VENDOR_SPECIFIC_INFORMATION:
                                        has_vendor_specific_information = true;
                                        break;
                                case SD_DHCP_OPTION_VENDOR_IDENTIFYING_VENDOR_CLASS:
                                        has_vendor_identifying_vendor_class = true;
                                        break;
                                case SD_DHCP_OPTION_VENDOR_IDENTIFYING_VENDOR_SPECIFIC_INFORMATION:
                                        has_vendor_identifying_vendor_specific_information = true;
                                        break;
                                }
                }
 
        if (FLAGS_SET(flags, DUMP_DHCP_MESSAGE_LEGEND))
                printf("%s%sOptions:%s\n", ansi_highlight(), ansi_add_underline(), ansi_normal());
 
        r = table_print_or_warn(table);
        if (r < 0)
                return r;
 
        if (has_vendor_specific_information) {
                r = dump_dhcp_option_vendor_specific_information(message, flags);
                if (r < 0)
                        return r;
        }
 
        if (has_vendor_identifying_vendor_class) {
                r = dump_dhcp_option_vendor_identifying_vendor_class(message, flags);
                if (r < 0)
                        return r;
        }
 
        if (has_vendor_identifying_vendor_specific_information) {
                r = dump_dhcp_option_vendor_identifying_vendor_specific_information(message, flags);
                if (r < 0)
                        return r;
        }
 
        return 0;
}
 
static int dump_buffer(Table *table, const char *field, uint8_t *buf, size_t len) {
        int r;
 
        assert(table);
        assert(field);
        assert(buf);
        assert(len > 0);
 
        uint8_t *nul = memchr(buf, 0, len);
        if (nul)
                len = nul - buf;
 
        if (len == 0)
                return 0;
 
        _cleanup_free_ char *str = NULL;
        r = make_cstring(buf, len, MAKE_CSTRING_REFUSE_TRAILING_NUL, &str);
        if (r < 0)
                return log_error_errno(r, "Failed to parse buffer for field '%s': %m", field);
 
        if (isempty(str))
                return 0;
 
        r = table_add_many(
                        table,
                        TABLE_FIELD, field,
                        TABLE_STRING, str);
        if (r < 0)
                return table_log_add_error(r);
 
        return 0;
}
 
static int dump_dhcp_header(sd_dhcp_message *message, DumpDHCPMessageFlag flags) {
        int r;
 
        assert(message);
 
        _cleanup_(table_unrefp) Table *table = table_new_vertical();
        if (!table)
                return log_oom();
 
        table_apply_flags(table, flags);
 
        TableCell *cell = table_get_cell(table, 0, 0);
        if (!cell)
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to get table cell.");
 
        (void) table_set_align_percent(table, cell, 100);
        (void) table_set_ellipsize_percent(table, cell, 100);
 
        struct hw_addr_data hw_addr;
        r = dhcp_message_get_hw_addr(message, &hw_addr);
        if (r < 0)
                return log_error_errno(r, "Failed to get hardware address from DHCP message: %m");
 
        struct in_addr yiaddr = { .s_addr = message->header.yiaddr };
 
        r = table_add_many(
                        table,
                        TABLE_FIELD, "Hardware Type",
                        TABLE_STRING, arphrd_to_name(message->header.htype),
                        TABLE_FIELD, "Hardware Address",
                        TABLE_STRING, HW_ADDR_TO_STR(&hw_addr),
                        TABLE_FIELD, "Client Address",
                        TABLE_IN_ADDR, &yiaddr);
        if (r < 0)
                return table_log_add_error(r);
 
        if (message->header.siaddr != INADDR_ANY) {
                struct in_addr siaddr = { .s_addr = message->header.siaddr };
 
                r = table_add_many(
                                table,
                                TABLE_FIELD, "Server Address",
                                TABLE_IN_ADDR, &siaddr);
                if (r < 0)
                        return table_log_add_error(r);
        }
 
        if (message->header.giaddr != INADDR_ANY) {
                struct in_addr giaddr = { .s_addr = message->header.giaddr };
 
                r = table_add_many(
                                table,
                                TABLE_FIELD, "Relay Agent Address",
                                TABLE_IN_ADDR, &giaddr);
                if (r < 0)
                        return table_log_add_error(r);
        }
 
        uint8_t overload = DHCP_OVERLOAD_NONE;
        (void) dhcp_message_get_option_u8(message, SD_DHCP_OPTION_OVERLOAD, &overload);
 
        if (!FLAGS_SET(overload, DHCP_OVERLOAD_SNAME)) {
                r = dump_buffer(table, "Server Host Name", message->header.sname, sizeof(message->header.sname));
                if (r < 0)
                        return r;
        }
 
        if (!FLAGS_SET(overload, DHCP_OVERLOAD_FILE)) {
                r = dump_buffer(table, "Boot File Name", message->header.file, sizeof(message->header.file));
                if (r < 0)
                        return r;
        }
 
        if (FLAGS_SET(flags, DUMP_DHCP_MESSAGE_LEGEND))
                printf("%s%sHeader:%s\n", ansi_highlight(), ansi_add_underline(), ansi_normal());
 
        return table_print_or_warn(table);
}
 
int dump_dhcp_message(sd_dhcp_message *m, char * const *args, DumpDHCPMessageFlag flags) {
        int r;
 
        assert(m);
 
        if (strv_isempty(args)) {
                r = dump_dhcp_header(m, flags);
                if (r < 0)
                        return r;
 
                putchar('\n');
        }
 
        return dump_dhcp_options(m, args, flags);
}