免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1629 | 回复: 4
打印 上一主题 下一主题

求教: BSD下用C编的sniffer为什么不能接取包? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-09-29 21:15 |只看该作者 |倒序浏览
#include <stdio.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>

#include "headers.h"
#define INTERFACE "ed0"
/*Prototype area*/

int Open_Raw_Socket(void);
void set_promisc(char *interface, int sock);

int main() {
        int sock, bytes_recieved, fromlen;
        char buffer[65535];
        struct sockaddr_in from;
        struct ip *ip;
        struct tcp *tcp;
        sock = Open_Raw_Socket();
        set_promisc(INTERFACE, sock);
        while(1)
        {
                fromlen = sizeof(struct sockaddr_in *);
                bytes_recieved = recvfrom(sock, buffer, sizeof buffer, 0, \
                         (struct sockaddr *)&from, &fromlen);
                printf("\nBytes received ::: %5d\n",bytes_recieved);
                printf("Source address ::: %s\n",inet_ntoa(from.sin_addr));
                ip = (struct ip *)buffer;
                /*See if this is a TCP packet*/
                //if(ip->ip_protocol == 6) {
                printf("IP header length ::: %d\n",ip->ip_length);
                printf("Protocol ::: %d\n",ip->ip_protocol);
                tcp = (struct tcp *)(buffer + (4*ip->ip_length));
                printf("Source port ::: %d\n",ntohs(tcp->tcp_source_port));
                printf("Dest port ::: %d\n",ntohs(tcp->tcp_dest_port));
                //}
        }
}
int Open_Raw_Socket() {
        int sock;
        if((sock = socket(AF_INET, SOCK_RAW, 0)) < 0) {
        /*Then the socket was not created properly and must die*/
                perror("The raw socket was not created");
                exit(1);
        };
        return(sock);
}

void set_promisc(char *interface, int sock ) {
        struct ifreq ifr;
        strncpy(ifr.ifr_name, interface,sizeof(interface)+1);
        if((ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)) {
        /*Could not retrieve flags for the interface*/
                 perror("Could not retrive flags for the interface");
                exit(1);
        }
        printf("The interface is ::: %s\n", interface);
        printf("Retrieved flags from interface successfully");
        ifr.ifr_flags |= IFF_PROMISC;
        if (ioctl (sock, SIOCSIFFLAGS, &ifr) == -1 ) {
        /*Could not set the flags on the interface */
                perror("Could not set the PROMISC flag:");
                exit(1);
        }
        printf("Setting interface ::: %s ::: to promisc", interface);
}

论坛徽章:
0
2 [报告]
发表于 2007-09-29 21:22 |只看该作者
抓包难道不是bpf?
你可以用libnet/libpcap,这样移植性会好一些。

论坛徽章:
0
3 [报告]
发表于 2007-09-29 21:27 |只看该作者
我现在只是想学一下BSD中简单的抓包的原理。

论坛徽章:
0
4 [报告]
发表于 2007-09-29 21:50 |只看该作者
也许 bpf(4) 是你需要的

论坛徽章:
0
5 [报告]
发表于 2007-09-30 08:06 |只看该作者
谢谢楼上
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP