先在liunx中安装GTK图形库
然后直接代码
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>
#include <net/if_arp.h>
#include <gtk/gtk.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <netinet/tcp.h>
#include <netinet/ip_icmp.h>
#define ETH_P_REVARP 0x8035
struct arp_packet
{
unsigned char mac_target[ETH_ALEN];
unsigned char mac_source[ETH_ALEN];
unsigned short ethertype;
unsigned short hw_type;
unsigned short proto_type;
unsigned char mac_addr_len;
unsigned char ip_addr_len;
unsigned short operation_code;
unsigned char mac_sender[ETH_ALEN];
unsigned char ip_sender[4];
unsigned char mac_receiver[ETH_ALEN];
unsigned char ip_receiver[4];
unsigned char padding[18];
};
/* 定义tpc/ip数据包的数据结构 */
struct ip_tcp {
struct iphdr *iph;
struct tcphdr *tcph;
unsigned char buffer[500];
};
/* 定义udp/ip数据包的数据结构 */
struct ip_udp {
struct iphdr *iph;
struct udphdr *udph;
unsigned char buffer[500];
};
/* 定义tpc/ip数据包的数据结构 */
struct ip_icmp1 {
struct iphdr *iph;
struct icmp *icmph;
unsigned char buffer[500];
};
static GtkWidget* createwindow;
void die(const char*pre)
{
perror(pre);
exit(1);
}
/* 创建一个"信息"列表 */
GtkWidget *create_list_arp( void )
{
GtkWidget *scrolled_window;
GtkWidget *tree_view;
GtkListStore *model;
GtkTreeIter iter;
GtkCellRenderer *cell;
GtkTreeViewColumn *column;
int i;
/* 创建一个新的滚动窗口(scrolled window),只有需要时,滚动条才出现 */
scrolled_window = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),GTK_POLICY_AUTOMATIC,GTK_POLICY_AUTOMATIC);
model = gtk_list_store_new (1, G_TYPE_STRING);
tree_view = gtk_tree_view_new();
gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window),tree_view);
gtk_tree_view_set_model (GTK_TREE_VIEW (tree_view), GTK_TREE_MODEL (model));
gtk_widget_show (tree_view);
/* 在窗口中添加一些消息 */
int sfd;
struct sockaddr_ll my_etheraddr;
struct arp_packet rcvBuffer;
sfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ARP)); //发送接收以太网数据帧 htons将主机字节顺序转换为网络字节顺序
if(-1 == sfd)
{
die("socket");
}
memset(&my_etheraddr, 0, sizeof(my_etheraddr));//内存空间初始化
//目的物理地址结构
my_etheraddr.sll_family = AF_PACKET;
my_etheraddr.sll_protocol = htons(ETH_P_ARP);
my_etheraddr.sll_ifindex = IFF_BROADCAST;
//使用bind将套接字绑定到配置好的本地物理地址结构
if(-1 == bind(sfd, (struct sockaddr *)&my_etheraddr, sizeof(my_etheraddr))){
die("bind");
}
recv(sfd, &rcvBuffer, sizeof(rcvBuffer), 0);
for (i = 0; i <= 23; i++)
{
if(i==0){
gchar *msg = g_strdup_printf ("MAC target:");
gtk_list_store_append (GTK_LIST_STORE (model), &iter);
gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
g_free (msg);}
if(i==1){
int i1=0;
gchar *msg = g_strdup_printf ("0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x", rcvBuffer.mac_target[i1],rcvBuffer.mac_target[i1+1],rcvBuffer.mac_target[i1+2],rcvBuffer.mac_target[i1+3],rcvBuffer.mac_target[i1+4],rcvBuffer.mac_target[i1+5]);
gtk_list_store_append (GTK_LIST_STORE (model), &iter);
gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
g_free (msg);}
if(i==2){
gchar *msg = g_strdup_printf ("MAC source:");
gtk_list_store_append (GTK_LIST_STORE (model), &iter);
gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
g_free (msg);}
if(i==3){
int i2=0;for(i2=0;i2<ETH_ALEN;i2++){
gchar *msg = g_strdup_printf ("0x%.2x", rcvBuffer.mac_source[i2]);
gtk_list_store_append (GTK_LIST_STORE (model), &iter);
gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
g_free (msg);}}
if(i==4){
char *msg = g_strdup_printf ("ethertype:");
gtk_list_store_append (GTK_LIST_STORE (model), &iter);
gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
g_free (msg);}
if(i==5){
char *msg = g_strdup_printf ("0x%x", ntohs(rcvBuffer.ethertype));
gtk_list_store_append (GTK_LIST_STORE (model), &iter);
gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
g_free (msg);}
if(i==6){
char *msg = g_strdup_printf ("hw_type:");
gtk_list_store_append (GTK_LIST_STORE (model), &iter);
gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
g_free (msg);}
if(i==7){
char *msg = g_strdup_printf ("0x%x", ntohs(rcvBuffer.hw_type));
gtk_list_store_append (GTK_LIST_STORE (model), &iter);
gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
g_free (msg);}
if(i==8){
char *msg = g_strdup_printf ("proto_type:");
gtk_list_store_append (GTK_LIST_STORE (model), &iter);
gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
g_free (msg);}
if(i==9){
char *msg = g_strdup_printf ("0x%x", ntohs(rcvBuffer.proto_type));
gtk_list_store_append (GTK_LIST_STORE (model), &iter);
gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
g_free (msg);}
if(i==10){
char *msg = g_strdup_printf ("mac_addrlen:");
gtk_list_store_append (GTK_LIST_STORE (model), &iter);
gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
g_free (msg);}
if(i==11){
char *msg = g_strdup_printf ("0x%x", rcvBuffer.mac_addr_len);
gtk_list_store_append (GTK_LIST_STORE (model), &iter);
gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
g_free (msg);}
if(i==12){
char *msg = g_strdup_printf ("ip_add_len:");
gtk_list_store_append (GTK_LIST_STORE (model), &iter);
gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
g_free (msg);}
if(i==13){
char *msg = g_strdup_printf ("0x%x", ntohs(rcvBuffer.ip_addr_len));
gtk_list_store_append (GTK_LIST_STORE (model), &iter);
gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
g_free (msg);}
if(i==14){
char *msg = g_strdup_printf ("operation_code:");
gtk_list_store_append (GTK_LIST_STORE (model), &iter);
gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
g_free (msg);}
if(i==15){
char *msg = g_strdup_printf ("0x%x", ntohs(rcvBuffer.operation_code));
gtk_list_store_append (GTK_LIST_STORE (model), &iter);
gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
g_free (msg);}
if(i==16){
char *msg = g_strdup_printf ("mac_sender:");
gtk_list_store_append (GTK_LIST_STORE (model), &iter);
gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
g_free (msg);}
if(i==17){
int i3=0;
for( i3 = 0; i3 < ETH_ALEN; i3++)
{
gchar *msg = g_strdup_printf ("0x%.2x", rcvBuffer.mac_sender[i3]);
gtk_list_store_append (GTK_LIST_STORE (model), &iter);
gtk_list_store_set (GTK_LIST_STORE (model)

最低0.47元/天 解锁文章
3522

被折叠的 条评论
为什么被折叠?



