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

📄 dhcp-align.c

📁 this is sample about DHCP-agent
💻 C
字号:
/* $Header: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-align.c,v 1.3 2002/11/16 00:23:42 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. * * Since structures may be padded. We simply can't guarantee * alignment unless we copy each member out individually. * Yes I know this is extreme since most packet structures * are already aligned and shouldn't need padding. *  * The question you should ask yourself is: "do you feel lucky?" * I'd rather not chance a compiler with its own padding issues. *  */#define MODULE_NAME "dhcp-align"#include "dhcp-local.h"#include "dhcp-libutil.h"#include "dhcp-librawnet.h"void align_ip(const unsigned char *data, struct ip_hdr *iphdr){    unsigned char vhl;    vhl = *data;    iphdr->ip_hl = (vhl & 0x0f);    iphdr->ip_v = ((vhl & 0xf0) >> 4);    data++; /* ip_vhl is one octet. */    /* TOS */    memcpy(&iphdr->ip_tos, data, sizeof(iphdr->ip_tos));    data++; /* TOS is one octet. */    /* len */    memcpy(&iphdr->ip_len, data, sizeof(iphdr->ip_len));    data += sizeof(iphdr->ip_len); /* len is two octets. */    /* ID */    memcpy(&iphdr->ip_id, data, sizeof(iphdr->ip_id));    data += sizeof(iphdr->ip_id); /* ID is two octets. */    /* offset */    memcpy(&iphdr->ip_off, data, sizeof(iphdr->ip_off));    data += sizeof(iphdr->ip_off); /* offset is two octets. */    /* ttl */    memcpy(&iphdr->ip_ttl, data, sizeof(iphdr->ip_ttl));    data++; /* ttl is one octet. */    /* protocol */    memcpy(&iphdr->ip_p, data, sizeof(iphdr->ip_p));    data++; /* protocol is one octet. */    /* checksum */    memcpy(&iphdr->ip_sum, data, sizeof(iphdr->ip_sum));    data += sizeof(iphdr->ip_sum); /* checksum is one octet. */    /* source address */    memcpy(&iphdr->ip_src, data, IP_ADDR_LEN);    data += IP_ADDR_LEN; /* source addr is four octets. */    /* dest address */    memcpy(&iphdr->ip_dst, data, IP_ADDR_LEN);    return;}void align_udp(const unsigned char *data, struct udp_hdr *udp){    /* all 2 octets long. */    memcpy(&udp->uh_sport, data, sizeof(udp->uh_sport));    data += sizeof(udp->uh_sport);    memcpy(&udp->uh_dport, data, sizeof(udp->uh_dport));    data += sizeof(udp->uh_dport);    memcpy(&udp->uh_ulen, data, sizeof(udp->uh_ulen));    data += sizeof(udp->uh_ulen);    memcpy(&udp->uh_sum, data, sizeof(udp->uh_sum));    return;}void align_dhcphdr(const unsigned char *data, dhcphdr * dhcp){    memcpy(&dhcp->op, data, sizeof(dhcp->op));    data++;    memcpy(&dhcp->htype, data, sizeof(dhcp->htype));    data++;    memcpy(&dhcp->hlen, data, sizeof(dhcp->hlen));    data++;    memcpy(&dhcp->hops, data, sizeof(dhcp->hops));    data++;    memcpy(&dhcp->xid, data, sizeof(dhcp->xid));    data += sizeof(dhcp->xid);    memcpy(&dhcp->secs, data, sizeof(dhcp->secs));    data += sizeof(dhcp->secs);    memcpy(&dhcp->flags, data, sizeof(dhcp->flags));    data += sizeof(dhcp->flags);    memcpy(&dhcp->ciaddr, data, IP_ADDR_LEN);    data += IP_ADDR_LEN;    memcpy(&dhcp->yiaddr, data, IP_ADDR_LEN);    data += IP_ADDR_LEN;    memcpy(&dhcp->siaddr, data, IP_ADDR_LEN);    data += IP_ADDR_LEN;    memcpy(&dhcp->giaddr, data, IP_ADDR_LEN);    data += IP_ADDR_LEN;    memcpy(&dhcp->chaddr, data, sizeof(dhcp->chaddr));    data += sizeof(dhcp->chaddr);    memcpy(&dhcp->sname, data, sizeof(dhcp->sname));    data += sizeof(dhcp->sname);    memcpy(&dhcp->file, data, sizeof(dhcp->file));    return;}void align_eth(const unsigned char *data, struct eth_hdr *header){    memcpy(&header->eth_dst, data, ETH_ADDR_LEN);    data += ETH_ADDR_LEN;    memcpy(&header->eth_src, data, ETH_ADDR_LEN);    data += ETH_ADDR_LEN;    memcpy(&header->eth_type, data, ETH_TYPE_LEN);    return;}void align_icmp(const unsigned char *data, struct icmp_hdr *header){    memcpy(&header->icmp_type, data, sizeof(header->icmp_type));    data += sizeof(header->icmp_type);    memcpy(&header->icmp_code, data, sizeof(header->icmp_code));    data += sizeof(header->icmp_code);    memcpy(&header->icmp_cksum, data, sizeof(header->icmp_cksum));    return;}void align_icmp_mask(const unsigned char *data, struct icmp_msg_mask *header){    memcpy(&header->icmp_id, data, sizeof(header->icmp_id));    data += sizeof(header->icmp_id);    memcpy(&header->icmp_seq, data, sizeof(header->icmp_seq));    data += sizeof(header->icmp_seq);    memcpy(&header->icmp_mask, data, sizeof(header->icmp_mask));    return;}void align_icmp_echo(const unsigned char *data, struct icmp_msg_echo *header){    memcpy(&header->icmp_id, data, sizeof(header->icmp_id));    data += sizeof(header->icmp_id);    memcpy(&header->icmp_seq, data, sizeof(header->icmp_seq));    return;}void align_arp_header(const unsigned char *data, struct arp_hdr *header){    memcpy(&header->ar_hrd, data, sizeof(header->ar_hrd));    data += sizeof(header->ar_hrd);    memcpy(&header->ar_pro, data, sizeof(header->ar_pro));    data += sizeof(header->ar_pro);    memcpy(&header->ar_hln, data, sizeof(header->ar_hln));    data += sizeof(header->ar_hln);    memcpy(&header->ar_pln, data, sizeof(header->ar_pln));    data += sizeof(header->ar_pln);    memcpy(&header->ar_op, data, sizeof(header->ar_op));    return;}void align_arp_data(const unsigned char *data, struct arp_ethip *arp_data){    memcpy(&arp_data->ar_sha, data, sizeof(arp_data->ar_sha));    data += sizeof(arp_data->ar_sha);    memcpy(&arp_data->ar_spa, data, sizeof(arp_data->ar_spa));    data += sizeof(arp_data->ar_spa);    memcpy(&arp_data->ar_tha, data, sizeof(arp_data->ar_tha));    data += sizeof(arp_data->ar_tha);    memcpy(&arp_data->ar_tpa, data, sizeof(arp_data->ar_tpa));}

⌨️ 快捷键说明

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