【求助】怎么用原始套接字自定义协议,实现数据包的收发,求大神帮忙。。跪谢。。。。

weq9685742310 2012-06-30 04:59:34
如题,自己写了个小程序发包,但是sendto的时候老出错
#include "client.h"
#define SERVPORT 0x8808

void sendpack(int, struct sockaddr_ll *);

ne_u8 recvbuf[1024] = {0};
ne_u8 sendbuf[1024] = {0};

int main(int argc, char *argv[])
{
int sockfd;
struct sockaddr_ll my_addr;
struct sockaddr_ll de_addr;
if (argc <2)
{
fprintf(stderr,"please enter the hostname!\n");
exit(1);
}

if ((sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) == -1)
{
perror("socket error!");
exit(1);
}
memset(&my_addr,0,sizeof(my_addr));
my_addr.sll_family = AF_PACKET;
my_addr.sll_protocol = htons(ETH_P_ALL);
my_addr.sll_ifindex = 0;
// my_addr.sll_pkttype = PACKET_BROADCAST;
// my_addr.sll_htype =
// my_addr.sin_addr.s_addr = INADDR_ANY;
// bzero(&(my_addr.sin_zero),8);

if(bind(sockfd, (struct sockaddr * )&my_addr,sizeof(my_addr)) !=0 )
{
perror("errno");
exit(1);
}
sendpack(sockfd,&my_addr);
close(sockfd);
return 0;
}

void sendpack(int sockfd, struct sockaddr_ll *my_addr)
{
int head_len = 0;
bzero(sendbuf , 1024);
struct ether_header *ether_header;
struct protocol *optl;
ne_u8 s_mac[6] = {0x00,0x0c,0x29,0xe4,0x15,0x32};
ne_u8 d_mac[6] = {0xff,0xff,0xff,0xff,0xff,0xff};

ether_header = (struct ether_header*)sendbuf;
ether_header->ether_type = 0x8808;
memcpy(ether_header->ether_shost, s_mac, 6);
memcpy(ether_header->ether_dhost, d_mac, 6);

head_len = sizeof(struct ether_header *) + sizeof(struct protocol *);

optl = (struct protocol *)(sendbuf + 14);
memcpy(optl->so_mac, s_mac, 6);
memcpy(optl->de_mac, d_mac, 6);
optl->port = SERVPORT;
optl->flags = 55;
if (sendto(sockfd, sendbuf, head_len, 0, (struct sockaddr *)my_addr, sizeof(struct sockaddr)) ==-1)
{
perror("send error!");
printf("%d\n",errno);
exit(1);
}
/*
if (send(sockfd, sendbuf, head_len, 0) ==-1 )
{
perror("send error!");
printf("%d\n",errno);
exit(1);
}
*/
}
完整代码如上,求帮助,不胜感激!!
...全文
223 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ningto.com 2012-07-02
  • 打赏
  • 举报
回复
要封包啊。。。
qq120848369 2012-07-02
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

哥们玩私服不,玩的话一起啊?引用 1 楼 的回复:
救不了你.
[/Quote]

玩啊,加我QQ.
weq9685742310 2012-07-02
  • 打赏
  • 举报
回复
哥们玩私服不,玩的话一起啊?[Quote=引用 1 楼 的回复:]
救不了你.
[/Quote]
weq9685742310 2012-07-02
  • 打赏
  • 举报
回复
哥们玩私服不,玩的话一起啊?[Quote=引用楼主 的回复:]
如题,自己写了个小程序发包,但是sendto的时候老出错
#include "client.h"
#define SERVPORT 0x8808

void sendpack(int, struct sockaddr_ll *);

ne_u8 recvbuf[1024] = {0};
ne_u8 sendbuf[1024] = {0};

int main(int argc, ……
[/Quote]
weq9685742310 2012-07-02
  • 打赏
  • 举报
回复
好吧。。已经搞定了
Gloveing 2012-06-30
  • 打赏
  • 举报
回复
先把常规协议的编程先搞懂再说吧
W170532934 2012-06-30
  • 打赏
  • 举报
回复
建议楼主还是去看下套接字编程的吧。
qq120848369 2012-06-30
  • 打赏
  • 举报
回复
救不了你.

69,373

社区成员

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

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