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

📄 dhcp-ip.c

📁 this is sample about DHCP-agent
💻 C
字号:
/* $Header: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-ip.c,v 1.5 2002/12/27 02:53:45 actmodern Exp $ *  * Copyright 2002 Thamer Alharbash <tmh@whitefang.com> *  * 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. *  * IP object. *  */#define MODULE_NAME "dhcp-ip"#include "dhcp-local.h"#include "dhcp-libutil.h"#include "dhcp-librawnet.h"#include "dhcp-align.h"ip_obj *ip_create(void){    ip_obj *ip;    ip = xmalloc(sizeof(ip_obj));    ip->header.ip_v = 4; /* this won't ever be changed. */    return ip;}void ip_destroy(ip_obj * ip){    xfree(ip);}ip_addr_t ip_get_src_addr(ip_obj * ip){    return (ip->header.ip_src);}ip_addr_t ip_get_dst_addr(ip_obj * ip){    return (ip->header.ip_dst);}unsigned char ip_get_hl(ip_obj * ip){    return (ip->header.ip_hl * 4);}uint8_t ip_get_proto(ip_obj * ip){    return (ip->header.ip_p);}void ip_set_src_addr(ip_obj * ip, uint32_t ip_src){    ip->header.ip_src = ip_src;    return;}void ip_set_dst_addr(ip_obj * ip, uint32_t ip_dst){    ip->header.ip_dst = ip_dst;    return;}void ip_set_hl(ip_obj * ip, uint16_t header_len){    unsigned char ip_hl;    ip_hl = (header_len / 4); /* this is expected to fit. */    ip->header.ip_hl = ip_hl;    return;}void ip_set_proto(ip_obj * ip, unsigned char ip_proto){    ip->header.ip_p = ip_proto;    return;}void ip_set_tos(ip_obj * ip, unsigned char ip_tos){    ip->header.ip_tos = ip_tos;    return;}void ip_set_len(ip_obj * ip, uint16_t ip_len){    ip->header.ip_len = htons(ip_len);    return;}void ip_set_id(ip_obj * ip, uint16_t ip_id){    ip->header.ip_id = htons(ip_id);    return;}void ip_set_off(ip_obj * ip, uint16_t ip_off){    ip->header.ip_off = htons(ip_off);    return;}void ip_set_ttl(ip_obj * ip, uint8_t ip_ttl){    ip->header.ip_ttl = ip_ttl;    return;}int ip_read_packet_image(ip_obj * ip, const unsigned char *packet, int len){    if(len < IP_HDR_LEN)        return -1;    align_ip(packet, &ip->header);    return 0;}void ip_write_packet_image(ip_obj * ip, unsigned char *packet){    /* struct may pad. memcpy() each member. */    unsigned char ipvhl;    unsigned char hl, v;    /* grr! */    hl = ip->header.ip_hl;    v = ip->header.ip_v;    ipvhl = (v << 4 | hl);    *packet = ipvhl;    packet += sizeof(ipvhl);    memcpy(packet, &ip->header.ip_tos, sizeof(ip->header.ip_tos));    packet += sizeof(ip->header.ip_tos);    memcpy(packet, &ip->header.ip_len, sizeof(ip->header.ip_len));    packet += sizeof(ip->header.ip_len);    memcpy(packet, &ip->header.ip_id, sizeof(ip->header.ip_id));    packet += sizeof(ip->header.ip_id);    memcpy(packet, &ip->header.ip_off, sizeof(ip->header.ip_off));    packet += sizeof(ip->header.ip_off);    memcpy(packet, &ip->header.ip_ttl, sizeof(ip->header.ip_ttl));    packet += sizeof(ip->header.ip_ttl);    memcpy(packet, &ip->header.ip_p, sizeof(ip->header.ip_p));    packet += sizeof(ip->header.ip_p);    /* skip checksum field. */    packet += 2;    memcpy(packet, &ip->header.ip_src, IP_ADDR_LEN);    packet += IP_ADDR_LEN;    memcpy(packet, &ip->header.ip_dst, IP_ADDR_LEN);    return;}

⌨️ 快捷键说明

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