当前位置: 技术问答>linux和unix
帮忙看下,我找了很久没发现错误在哪里?
来源: 互联网 发布时间:2016-11-01
本文导语: 我自己构建一个ip包,放入某段内存,再读出来。 有三个文件: #1 : ip_packet.h #2 : ip_packet.c #3 : main.c 编译命令为: gcc -g -ansi -Wall -Werror -o2 ip_packet.c main.c -o main.out 运行 ./main.out 之后有错误,Segmentation...
我自己构建一个ip包,放入某段内存,再读出来。
有三个文件:
#1 : ip_packet.h
#2 : ip_packet.c
#3 : main.c
编译命令为: gcc -g -ansi -Wall -Werror -o2 ip_packet.c main.c -o main.out
运行 ./main.out 之后有错误,Segmentation fault
gdb main.out
再run,提示在ip_hdr_init()函数,
可是没发现什么错误啊。
代码如下:
ip_packet.h
#ifndef __IP_PACKET_H
#define __IP_PACKET_H
#if !defined( LITTLE_ENDIAN)
#define LITTLE_ENDIAN 0
#else
#undef LITTLE_ENDIAN
#define LITTLE_ENDIAN 0
#endif
#if !defined(BIG_ENDIAN)
#define BIG_ENDIAN 1
#else
#undef BIG_ENDIAN
#define BIG_ENDIAN 1
#endif
#if !defined(BYTE_ORDER)
#define BYTE_ORDER LITTLE_ENDIAN
#else
#undef BYTE_ORDER
#define BYTE_ORDER LITTLE_ENDIAN
#endif
#define MAX_OCTETS_NUM 576
#define MIN_HDR_WORD_SIZE 5 /* in word */
#define MIN_HDR_BYTE_SIZE (5 MIN_HDR_SIZE ? 1 :0)
#define option_size(ip_hdr_p) ((ip_hdr_p)->(ip_ihl) ^ 5)
struct in_addr
{
unsigned long s_addr;
};
typedef struct ip_hdr
{
#ifdef _IP_VHL
unsigned char ip_vhl; /* version and ivh */
#else
#if BYTE_ORDER == LITTLE_ENDIAN
unsigned char ip_ihl; /* internet heder length */
unsigned char ip_v; /* IP Version */
#endif
#if BYTE_ORDER == BIG_ENDIAN
unsigned char ip_v;
unsigned char ip_ihl;
#endif
#endif
unsigned char ip_tos; /* type of service */
unsigned short ip_len; /* total length */
unsigned short ip_id; /* identification */
unsigned short ip_off; /* fragment offset */
unsigned char ip_ttl; /* time to live */
unsigned char ip_p; /* protocol */
unsigned short ip_sum; /* ip header checksum */
struct in_addr ip_src; /* ip source address */
struct in_addr ip_dst; /* ip dest address */
}IP_HDR;
typedef IP_HDR *IP_HDR_ID;
#ifdef _IP_VHL
#define IP_MAKE_VHL(v, hl) ((v) 4)
#endif
/*
int inet_aton(char *cp, struct in_addr *inp);
struct in_addr inet_addr(char *cp);
struct in_addr inet_network(char *cp);
char *inet_ntoa(struct in_addr in);
struct in_addr inet_makeaddr(int net, int host);
struct in_addr inet_lnaof(struct in_addr in);
struct in_addr inet_netof(struct in_addr in);
*/
char itoa(int digit);
IP_HDR_ID ip_hdr_init(IP_HDR_ID ip_hdr_p);
char* ip_cons(char* pbuf, IP_HDR_ID ip_hdr_p);
void print(char* pbuf, IP_HDR_ID ip_hdr_p);
#endif
ip_packet.c
#include
#include
#include
#include "ip_packet.h"
char itoa(int digit)
{
return (digit + '0');
}
IP_HDR_ID ip_hdr_init(IP_HDR_ID ip_hdr_p)
{
ip_hdr_p->ip_ihl = 15;
ip_hdr_p->ip_v = 4;
ip_hdr_p->ip_tos = 10;
ip_hdr_p->ip_len = 556;
ip_hdr_p->ip_id = 1121;
ip_hdr_p->ip_off = 128;
ip_hdr_p->ip_ttl = 16;
ip_hdr_p->ip_p = 80;
ip_hdr_p->ip_sum = 16388;
ip_hdr_p->ip_src.s_addr =101519663;
ip_hdr_p->ip_dst.s_addr =101619664;
printf(" why notn");
return ip_hdr_p;
}
char* ip_cons(char* pbuf, IP_HDR_ID ip_hdr_p)
{
IP_HDR_ID tmp_ip_hdr_p;
char* pTupBuf;
pTupBuf = pbuf;
tmp_ip_hdr_p = ip_hdr_p;
unsigned int padding_word_num;
unsigned int data_byte_len;
unsigned int pos = 0;
unsigned int digit = 0;
char *str, *tmp_str;
tmp_str = NULL;
padding_word_num = tmp_ip_hdr_p->ip_ihl - MIN_HDR_WORD_SIZE;
data_byte_len = tmp_ip_hdr_p->ip_len - (tmp_ip_hdr_p->ip_ihl ip_v);
printf(" ip: ihl = %hun", rcv_ip_hdr_p->ip_ihl);
printf(" ip: tos = %hun", rcv_ip_hdr_p->ip_tos);
printf(" ip: len = %hun", rcv_ip_hdr_p->ip_len);
printf(" ip: id = %hun", rcv_ip_hdr_p->ip_id);
printf(" ip: off = %hun", rcv_ip_hdr_p->ip_off);
printf(" ip: ttl = %hun", rcv_ip_hdr_p->ip_ttl);
printf(" ip: p = %hun", rcv_ip_hdr_p->ip_p);
printf(" ip: sum = %hun", rcv_ip_hdr_p->ip_sum);
printf(" ip: src = %lun", rcv_ip_hdr_p->ip_src.s_addr);
printf(" ip: dst = %lun", rcv_ip_hdr_p->ip_dst.s_addr);
padding_word_num = rcv_ip_hdr_p->ip_ihl - MIN_HDR_WORD_SIZE;
data_byte_len = rcv_ip_hdr_p->ip_len - (rcv_ip_hdr_p->ip_ihl
有三个文件:
#1 : ip_packet.h
#2 : ip_packet.c
#3 : main.c
编译命令为: gcc -g -ansi -Wall -Werror -o2 ip_packet.c main.c -o main.out
运行 ./main.out 之后有错误,Segmentation fault
gdb main.out
再run,提示在ip_hdr_init()函数,
可是没发现什么错误啊。
代码如下:
ip_packet.h
#ifndef __IP_PACKET_H
#define __IP_PACKET_H
#if !defined( LITTLE_ENDIAN)
#define LITTLE_ENDIAN 0
#else
#undef LITTLE_ENDIAN
#define LITTLE_ENDIAN 0
#endif
#if !defined(BIG_ENDIAN)
#define BIG_ENDIAN 1
#else
#undef BIG_ENDIAN
#define BIG_ENDIAN 1
#endif
#if !defined(BYTE_ORDER)
#define BYTE_ORDER LITTLE_ENDIAN
#else
#undef BYTE_ORDER
#define BYTE_ORDER LITTLE_ENDIAN
#endif
#define MAX_OCTETS_NUM 576
#define MIN_HDR_WORD_SIZE 5 /* in word */
#define MIN_HDR_BYTE_SIZE (5 MIN_HDR_SIZE ? 1 :0)
#define option_size(ip_hdr_p) ((ip_hdr_p)->(ip_ihl) ^ 5)
struct in_addr
{
unsigned long s_addr;
};
typedef struct ip_hdr
{
#ifdef _IP_VHL
unsigned char ip_vhl; /* version and ivh */
#else
#if BYTE_ORDER == LITTLE_ENDIAN
unsigned char ip_ihl; /* internet heder length */
unsigned char ip_v; /* IP Version */
#endif
#if BYTE_ORDER == BIG_ENDIAN
unsigned char ip_v;
unsigned char ip_ihl;
#endif
#endif
unsigned char ip_tos; /* type of service */
unsigned short ip_len; /* total length */
unsigned short ip_id; /* identification */
unsigned short ip_off; /* fragment offset */
unsigned char ip_ttl; /* time to live */
unsigned char ip_p; /* protocol */
unsigned short ip_sum; /* ip header checksum */
struct in_addr ip_src; /* ip source address */
struct in_addr ip_dst; /* ip dest address */
}IP_HDR;
typedef IP_HDR *IP_HDR_ID;
#ifdef _IP_VHL
#define IP_MAKE_VHL(v, hl) ((v) 4)
#endif
/*
int inet_aton(char *cp, struct in_addr *inp);
struct in_addr inet_addr(char *cp);
struct in_addr inet_network(char *cp);
char *inet_ntoa(struct in_addr in);
struct in_addr inet_makeaddr(int net, int host);
struct in_addr inet_lnaof(struct in_addr in);
struct in_addr inet_netof(struct in_addr in);
*/
char itoa(int digit);
IP_HDR_ID ip_hdr_init(IP_HDR_ID ip_hdr_p);
char* ip_cons(char* pbuf, IP_HDR_ID ip_hdr_p);
void print(char* pbuf, IP_HDR_ID ip_hdr_p);
#endif
ip_packet.c
#include
#include
#include
#include "ip_packet.h"
char itoa(int digit)
{
return (digit + '0');
}
IP_HDR_ID ip_hdr_init(IP_HDR_ID ip_hdr_p)
{
ip_hdr_p->ip_ihl = 15;
ip_hdr_p->ip_v = 4;
ip_hdr_p->ip_tos = 10;
ip_hdr_p->ip_len = 556;
ip_hdr_p->ip_id = 1121;
ip_hdr_p->ip_off = 128;
ip_hdr_p->ip_ttl = 16;
ip_hdr_p->ip_p = 80;
ip_hdr_p->ip_sum = 16388;
ip_hdr_p->ip_src.s_addr =101519663;
ip_hdr_p->ip_dst.s_addr =101619664;
printf(" why notn");
return ip_hdr_p;
}
char* ip_cons(char* pbuf, IP_HDR_ID ip_hdr_p)
{
IP_HDR_ID tmp_ip_hdr_p;
char* pTupBuf;
pTupBuf = pbuf;
tmp_ip_hdr_p = ip_hdr_p;
unsigned int padding_word_num;
unsigned int data_byte_len;
unsigned int pos = 0;
unsigned int digit = 0;
char *str, *tmp_str;
tmp_str = NULL;
padding_word_num = tmp_ip_hdr_p->ip_ihl - MIN_HDR_WORD_SIZE;
data_byte_len = tmp_ip_hdr_p->ip_len - (tmp_ip_hdr_p->ip_ihl ip_v);
printf(" ip: ihl = %hun", rcv_ip_hdr_p->ip_ihl);
printf(" ip: tos = %hun", rcv_ip_hdr_p->ip_tos);
printf(" ip: len = %hun", rcv_ip_hdr_p->ip_len);
printf(" ip: id = %hun", rcv_ip_hdr_p->ip_id);
printf(" ip: off = %hun", rcv_ip_hdr_p->ip_off);
printf(" ip: ttl = %hun", rcv_ip_hdr_p->ip_ttl);
printf(" ip: p = %hun", rcv_ip_hdr_p->ip_p);
printf(" ip: sum = %hun", rcv_ip_hdr_p->ip_sum);
printf(" ip: src = %lun", rcv_ip_hdr_p->ip_src.s_addr);
printf(" ip: dst = %lun", rcv_ip_hdr_p->ip_dst.s_addr);
padding_word_num = rcv_ip_hdr_p->ip_ihl - MIN_HDR_WORD_SIZE;
data_byte_len = rcv_ip_hdr_p->ip_len - (rcv_ip_hdr_p->ip_ihl