不同版本GCC编译的问题!

crossfire_sli 2010-07-26 04:03:35
一程序,在Ubuntu 10.04下用GCC编译有warning(涉及到类型转换的, 没问题),运行时就提示如下:
*** stack smashing detected ***: ./a.out terminated


还是那同样的程序,放到redhat 9.0上用GCC编译,运行OK。

真不知道为什么。错误提示不懂。
恳请指个方向。
...全文
163 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
skyworth98 2010-07-26
  • 打赏
  • 举报
回复
parp.ar_hrd = htons(ARPHRD_ETHER); //这个需要转网络字节序,为何ip,mac不转

ipone是RISC芯片,mac之前用的是power芯片,两者都是bigendian cpu,而tcp/ip协议栈最早是实现在big endian系统上的,所以不需要转换。

而x86系列的是little endian芯片,所以在x86系列上的都需要转换,新的基于intel芯片的mac也需要转换。
crossfire_sli 2010-07-26
  • 打赏
  • 举报
回复
#include <stdio.h> //perror
#include <sys/types.h> //socket
#include <sys/socket.h> //socket
#include <string.h> //memcpy
#include <unistd.h> //usleep
#include <stdlib.h>

#include <net/if_arp.h> //struct arphdr parp&struct ether_header
#include <net/ethernet.h> //同上

#define ARP_LEN 30


void mac_str(char * mac, char * buf)
{
sscanf(mac, "%x:%x:%x:%x:%x:%x", buf, buf+1, buf+2, buf+3, buf+4, buf+5);
}

void ip_str(char * ip, char * buf)
{
sscanf(ip, "%d.%d.%d.%d", buf+0, buf+1, buf+2, buf+3);
}

void encapsulate_frame( char * dest_mac, char * source_mac, unsigned int type, char * buf)
{
mac_str( dest_mac, buf );
mac_str( source_mac, buf+6 );
*(short *)( buf+12) = htons(type); //the first * is missing...
}

void encapsulate_arp( unsigned short ar_op, char * source_mac, char * source_ip, char * dest_mac, char * dest_ip, char * buf)
{
struct arphdr parp;

parp.ar_hrd = htons(ARPHRD_ETHER); //这个需要转网络字节序,为何ip,mac不转
parp.ar_pro = htons(ETHERTYPE_IP);
parp.ar_hln = 6;
parp.ar_pln = 4;
parp.ar_op = htons(ar_op);
memcpy(buf, &parp, sizeof(struct arphdr));

char addr_buf[20];
mac_str(source_mac, addr_buf);
ip_str(source_ip, addr_buf+6);
mac_str(dest_mac, addr_buf+10);
ip_str(dest_ip, addr_buf+16);
memcpy(buf+sizeof(struct arphdr), addr_buf, 20);
}

int open_packet_socket(void)
{
int sock;
if( (sock = socket(AF_INET, SOCK_PACKET, htons(ETH_P_RARP))) < 0 ) //AF_INET,采用TCP/IP|SOCK_PACKET原始套接口,自己封装|
{
perror("The RAW socket was not created.");
exit(-1);
}; //missing ;
return sock;
}

int main()
{
char * source_ip = "210.41.192.1";
char * source_mac = "00:25:64:7A:B6:2E";
char * dest_ip = "210.41.196.93";
char * dest_mac = "00:0A:E6:E2:EF:E2";
char * buf = malloc( sizeof(struct ether_header)+ARP_LEN ); //以太网头的长度+arp头的长度
encapsulate_frame( dest_mac, source_mac, ETHERTYPE_ARP, buf );
encapsulate_arp( ARPOP_REPLY, source_mac, source_ip, dest_mac, dest_ip, buf+sizeof(struct ether_header) );

int sock_fd = open_packet_socket();
struct sockaddr to; //创建一个结构,表示发送给谁
bzero( &to, sizeof(struct sockaddr) );
to.sa_family = AF_INET;
strcpy( to.sa_data, "eth0" ); //通过eth0发送出去

while(1)
{
sendto( sock_fd, buf, sizeof(struct ether_header)+ARP_LEN, 0, &to, sizeof(struct sockaddr));
//0,采用默认方式发送|&to通过eth0网卡发送|
usleep( 100 );
}
}
skyworth98 2010-07-26
  • 打赏
  • 举报
回复
根据错误提示,貌似你有什么地方有栈缓冲区溢出,建议检查局部变量的使用

[Quote=引用楼主 crossfire_sli 的回复:]
一程序,在Ubuntu 10.04下用GCC编译有warning(涉及到类型转换的, 没问题),运行时就提示如下:
*** stack smashing detected ***: ./a.out terminated


还是那同样的程序,放到redhat 9.0上用GCC编译,运行OK。

真不知道为什么。错误提示不懂。
恳请指个方向。
[/Quote]
ilwmin 2010-07-26
  • 打赏
  • 举报
回复
上代码

69,364

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧