⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dhcp-print.c

📁 this is sample about DHCP-agent
💻 C
📖 第 1 页 / 共 2 页
字号:
/* $Header: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-print.c,v 1.8 2003/06/28 02:08:24 actmodern Exp $ * * Copyright 2002 Thamer Alharbash * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. The names of the authors may not be used to endorse or promote * products derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. * * Print functions. * */#define MODULE_NAME "dhcp-print"#include "dhcp-local.h"#include "dhcp-limits.h"#include "dhcp-libutil.h"#include "dhcp-librawnet.h"#include "dhcp-option.h"#include "dhcp-print.h"/* Print the fixed header briefly.*/void print_dhcphdr_brief(dhcp_obj * dhcp){    uint8_t op;    struct in_addr in;    uint8_t *hw_ptr;    int i;    /* op */    op = dhcp_get_op(dhcp);    printf("op:");    switch (op) {    case DHCP_BOOTP_REQUEST:        printf("req");        break;    case DHCP_BOOTP_REPLY:        printf("rpy");        break;    default:        printf("%-3d", op);    }    printf(" ");    /* Hardware Type field. */    printf("hd:");    switch (dhcp_get_htype(dhcp)) {    case DLT_NULL:        printf("%s", "nul");        break;    case DLT_EN10MB:    case DLT_EN3MB:        printf("%s", "eth");        break;    case DLT_AX25:        printf("%s", "ax25");        break;    case DLT_PRONET:        printf("%s", "pnet");        break;    case DLT_CHAOS:        printf("%s", "chas");        break;    case DLT_ARCNET:        printf("%s", "slip");        break;    case DLT_PPP:        printf("%s", "ppp");        break;    case DLT_FDDI:        printf("%s", "fddi");        break;    default:        printf("%s", "unkn");        break;    }    printf(" ");    printf("hlen:%d ", dhcp_get_hlen(dhcp));    printf("hops:%d ", dhcp_get_hops(dhcp));    printf("xid:%x ", dhcp_get_xid(dhcp));    printf("secs:%d ", dhcp_get_secs(dhcp));    if(dhcp_get_flags(dhcp))        printf("f:brdcst");    else        printf("f:none");    printf(" ");    in.s_addr = dhcp_get_ciaddr(dhcp);    printf("ciaddr: %s ", inet_ntoa(in));    in.s_addr = dhcp_get_yiaddr(dhcp);    printf("yiaddr: %s ", inet_ntoa(in));    in.s_addr = dhcp_get_siaddr(dhcp);    printf("siaddr: %s ", inet_ntoa(in));    in.s_addr = dhcp_get_giaddr(dhcp);    printf("agaddr: %s ", inet_ntoa(in));    printf("chdr: ");    hw_ptr = dhcp_get_chaddr(dhcp);    for(i = 0; i < 16; i++) {        printf("%.2x", *hw_ptr);        hw_ptr++;    }    printf(" ");    fflush(stdout);}/* Print the fixed header verbosely. *  * We know that if a dhcp packet is read successfully into a dhcp * object we'll always have the fixed header in its completeness, * although it may be malformed. So we can just print out here * as best we can without any worries about length. *  */void print_dhcphdr_verbose(dhcp_obj * dhcp){    struct in_addr in;    uint8_t *hw_ptr;    int i;    uint8_t op;    printf("----------------------------------DHCP Info--------------------------\n");    /* Op field. */    printf("[Operation: ");    op = dhcp_get_op(dhcp);    switch (op) {    case DHCP_BOOTP_REQUEST:        printf("%-12s] ", "BootRequest");        break;    case DHCP_BOOTP_REPLY:        printf("%-12s] ", "BootReply");        break;    default:        printf("%-12d] ", op);    }    /* Hardware Type field. */    printf("[Hardware Type: ");    switch (dhcp_get_htype(dhcp)) {    case DLT_NULL:        printf("%-8s ]", "Null");        break;    case DLT_EN10MB:    case DLT_EN3MB:        printf("%-8s ] ", "Ethernet");        break;    case DLT_AX25:        printf("%-8s ] ", "AX25");        break;    case DLT_PRONET:        printf("%-8s ] ", "ProNet");        break;    case DLT_CHAOS:        printf("%-8s ] ", "Chaos");        break;    case DLT_ARCNET:        printf("%-8s ] ", "SLIP");        break;    case DLT_PPP:        printf("%-8s ] ", "PPP");        break;    case DLT_FDDI:        printf("%-8s ] ", "FDDI");        break;    default:        printf("%-7s ] ", "Unknown");        break;    }    printf("\n");    printf("[Hardware Addr Len: %-3d ] ", dhcp_get_hlen(dhcp));    printf("[Hops: %-17d ] ", dhcp_get_hops(dhcp));    printf("[XID: %-8X ]", dhcp_get_xid(dhcp));    printf("\n");    printf("[Seconds: %-14d] ", dhcp_get_secs(dhcp));    if(dhcp_get_flags(dhcp))        printf("[Flags: Broadcast]");    else        printf("[Flags: ]");    printf("\n\n");    printf("    ");    in.s_addr = dhcp_get_ciaddr(dhcp);    printf("[Client Addr: %-15s]  ", inet_ntoa(in));    in.s_addr = dhcp_get_yiaddr(dhcp);    printf("[Your Addr: %-16s]", inet_ntoa(in));    printf("\n");    printf("    ");    in.s_addr = dhcp_get_siaddr(dhcp);    printf("[Server Addr: %-15s]  ", inet_ntoa(in));    in.s_addr = dhcp_get_giaddr(dhcp);    printf("[Agent Addr: %-15s]", inet_ntoa(in));    printf("\n\n");    printf("         ");    printf("[Client HW Addr: ");    hw_ptr = dhcp_get_chaddr(dhcp);    for(i = 0; i < 16; i++) {        printf("%.2x", *hw_ptr);        hw_ptr++;    }    printf("]\n");    /* Ignoring name and file field for now. Too long.     * I'll need to find a way to display it properly. */    printf("---------------------------------------------------------------------\n");    fflush(stdout);    return;}void print_request_options(const uint8_t *data, int len, int printval){    int i;    printf("\n---------------------------------------------------------------------\n");    printval = 0;    for(i = 0; i < len; i++) {        if(data[i] > MAX_OPTIONS_HANDLED)            continue;        printval += printf("%s", option_handlers[data[i]].prefix);        if(i == (len - 1))            printf(".");        else            printf(", ");        if(printval >= 50) {            printf("\n");            printval = 0;        }    }    printf("\n---------------------------------------------------------------------\n");    return;}void print_dhcp_type_message(const uint8_t *data, int len, int printval){    uint8_t dhcp_type = *data;    if(len != 1) {        printf("<Malformed Data>");        return;    }    switch (dhcp_type) {    case DHCP_DISCOVER_TM:        printf("DISCOVER");        break;    case DHCP_OFFER_TM:        printf("OFFER");        break;    case DHCP_REQUEST_TM:        printf("REQUEST");        break;    case DHCP_DECLINE_TM:        printf("DECLINE");        break;    case DHCP_DHCPNAK_TM:        printf("DHCPNAK");        break;    case DHCP_DHCPACK_TM:        printf("DHCPACK");        break;    case DHCP_RELEASE_TM:        printf("RELEASE");        break;    default:        break;    }    return;}/* Just print a 32bit inet address. */static int print_address(const uint8_t *data){    struct in_addr in;    memcpy(&in.s_addr, data, 4);    return (printf("%s", inet_ntoa(in)));}/* Print a list of addresses. Works on single * addresses too. */void print_address_list(const uint8_t *data, int len, int ini_val){    int i, j, line_val;    if(len <= 0 || len % 4 != 0) {        printf("<Malformed data>");        return;    }    line_val = ini_val;    for(i = 0; i < len; i += 4) {        line_val += print_address(data);        data += 4;        if(i >= (len - 4))            printf(".");        else            printf(", ");        if(line_val > 60) {            printf("\n");            line_val = ini_val;            for(j = 0; j < line_val; j++)                printf(" ");        }    }    return;}/* Same as print_address_list only we add a netmask in between. */void print_address_mask_list(const uint8_t *data, int len, int ini_val){    int i, j, line_val;    if(len <= 0 || len % 8 != 0) {        printf("<Malformed data>");        return;    }    line_val = ini_val;    for(i = 0; i < len; i += 4) {        line_val += print_address(data);        data += 4;        if(i != 0 && (i % 8 == 0)) {            /* We're at a mask. */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -