⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 144.htm

📁 unix高级编程原吗
💻 HTM
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>CTerm非常精华下载</title>
</head>
<body bgcolor="#FFFFFF">
<table border="0" width="100%" cellspacing="0" cellpadding="0" height="577">
<tr><td width="32%" rowspan="3" height="123"><img src="DDl_back.jpg" width="300" height="129" alt="DDl_back.jpg"></td><td width="30%" background="DDl_back2.jpg" height="35"><p align="center"><a href="http://apue.dhs.org"><font face="黑体"><big><big>123</big></big></font></a></td></tr>
<tr>
<td width="68%" background="DDl_back2.jpg" height="44"><big><big><font face="黑体"><p align="center">               ● UNIX网络编程                       (BM: clown)                </font></big></big></td></tr>
<tr>
<td width="68%" height="44" bgcolor="#000000"><font face="黑体"><big><big><p   align="center"></big></big><a href="http://cterm.163.net"><img src="banner.gif" width="400" height="60" alt="banner.gif"border="0"></a></font></td>
</tr>
<tr><td width="100%" colspan="2" height="100" align="center" valign="top"><br><p align="center">[<a href="index.htm">回到开始</a>][<a href="135.htm">上一层</a>][<a href="145.htm">下一篇</a>]
<hr><p align="left"><small>发信人: ysqcn (岁月无声), 信区: UNP <br>

标  题: 基于Linux的网络数据帧捕获方法与思考 <br>

发信站: UNIX编程 (2001年08月15日00:06:58 星期三), 站内信件 <br>

  <br>

http://www.netguard.com.cn/LibList.php?pageno=2 <br>

  <br>

www.netguard.com.cn----[ <br>

 基于Linux的网络数据帧捕获方法与思考 <br>

( 2001/07/23) <br>

作 者: 魏星 <br>

· 魏星 浦仕超·CPCW <br>

Linux的本地化应该以提高大量应用程序的使用和分析为突破口。 <br>

  目前,国内推出了许多的Linux的发行版本,其重点集中在中文平台上,方便了国内 <br>

用户对Linux的使用,但是有一个不是太好的迹象就是把汉化作为Linux操作系统的主要 <br>

功能,实际上汉字处理尽管非常重要,但是把Linux作为桌面系统进行推广,其价值不是 <br>

非常大,并且在推出的发行版本中,应用程序的源代码包多被删除,而选择了一些不是 <br>

太有价值的X-Windows程序包,而许多应用程序(如PHP3)必须要源代码的支持才可进行 <br>

功能的扩展,GNU/Linux的优秀主要是给了我们非常丰富的软件资源,和享受资源的充分 <br>

自由,应用程序的分析难度远小于内核,并且能够带来比较明显的效果,实际上许多的 <br>

应用程序都提供了多平台的支持。Linux目前可能作为对抗Windows NT的工具是非常合适 <br>

的。 <br>

附源程序: <br>

/* <br>

/* <br>

* This program demonstrate SOCK_PACK call. <br>

* Thanks Linux. Thanks Alan Cox <br>

* derived from/usr/src/redhat/SOURCES/dosemu-0.66.7/src/dosext/net/net/libpa <br>

cket.c <br>

*  compile method: cc capturer.c -o capturer <br>

*/ <br>

/* <br>

* Alan Cox raw code <br>

*/ <br>

/* <br>

* SOCK_PACKET support. <br>

* Placed under the GNU LGPL. <br>

* <br>

* First cut at a library of handy support routines. Comments, additions <br>

* and bug fixes greatfully received. <br>

* <br>

* (c) 1994 Alan Cox iiitac@pyr.swan.ac.uk GW4PTS@GB7SWN <br>

*/ <br>

#include <stdio.h> <br>

#include <features.h> <br>

#include <unistd.h> <br>

#include <stdlib.h> <br>



#include <ctype.h> <br>

#include <getopt.h> <br>

#include <string.h> <br>

#include <fcntl.h> <br>

#include <asm/types.h> <br>

#include <sys/socket.h> <br>

#include <sys/ioctl.h> <br>

/*#if __GLIBC__ > 1*/ <br>

#include <asm/sockios.h> <br>

#include <net/if.h> <br>

/*#else <br>

#include <linux/sockios.h> <br>

#include <linux/if.h> <br>

#endif*/ <br>

#include <netinet/in.h> <br>

#include <asm/checksum.h> <br>

/* <br>

* Obtain a file handle on a raw ethernet type. In actual fact <br>

* you can also request the dummy types for AX.25 or 802.3 also <br>

* <br>

* -1 indicates an error <br>

* 0 or higher is a file descriptor which we have set non blocking <br>



* <br>

* WARNING: It is ok to listen to a service the system is using (eg arp) <br>

* but don try and run a user mode stack on the same service or all <br>

* hell will break loose. <br>

*/ <br>

int <br>

OpenNetworkType(unsigned short netid) <br>

{ <br>

int s = socket(AF_INET, SOCK_PACKET, htons(netid)); <br>

if (s == -1) <br>

return -1; <br>

fcntl(s, F_SETFL, O_NDELAY); <br>

return s; <br>

} <br>

/* <br>

* Close a file handle to a raw packet type. <br>

*/ <br>

void <br>

CloseNetworkLink(int sock) <br>

{ <br>

close(sock); <br>

} <br>

} <br>

/* <br>

* Write a packet to the network. You have to give a device to <br>

* this function. This is a device name (eg eth0 for the first <br>

* ethernet card). Please don assume eth0, make it configurable <br>

* - plip is ethernet like but not eth0, ditto for the de600s. <br>

* <br>

* Return: -1 is an error <br>

* otherwise bytes written. <br>

*/ <br>

int <br>

WriteToNetwork(int sock, const char *device, const char *data, int len) <br>

{ <br>

struct sockaddr sa; <br>

sa.sa_family = AF_INET; <br>

strcpy(sa.sa_data, device); <br>

return (sendto(sock, data, len, 0, &sa, sizeof(sa))); <br>

} <br>

/* <br>

* Read a packet from the network. The device parameter will <br>

* be filled in by this routine (make it 32 bytes or more). <br>

* If you wish to work with one interface only you must filter <br>

* yourself. Remember to make your buffer big enough for your <br>



* data. Oversized packets will be truncated. <br>

* <br>

* Return: <br>

* -1 Error <br>

* otherwise Size of packet received. <br>

*/ <br>

int <br>

ReadFromNetwork(int sock, char *device, char *data, int len) <br>

{ <br>

struct sockaddr sa; <br>

int sz = sizeof(sa); <br>

int error; <br>

error = recvfrom(sock, data, len, 0, &sa, &sz); <br>

if (error == -1) <br>

return -1; <br>

strcpy(device, sa.sa_data); <br>

return error; /* Actually size of received packet */ <br>

} <br>

/* <br>

* Handy support routines. <br>

*/ <br>

/* <br>

/* <br>

* Obtain the hardware address of an interface. <br>

* addr should be a buffer of 8 bytes or more. <br>

* <br>

* Return: <br>

* 0 Success, buffer holds data. <br>

* -1 Error. <br>

*/ <br>

/* <br>

* NET2 or NET3 - work for both. <br>

*/ <br>

#if defined(OLD_SIOCGIFHWADDR) || (KERNEL_VERSION >= 1003038) <br>

#define NET3 <br>

#endif <br>

int <br>

GetDeviceHardwareAddress(char *device, char *addr) <br>

{ <br>

int s = socket(AF_INET, SOCK_DGRAM, 0); <br>

struct ifreq req; <br>

int err; <br>

strcpy(req.ifr_name, device); <br>

err = ioctl(s, SIOCGIFHWADDR, &req); <br>

close(s); /* Thanks Rob. for noticing this */ <br>



if (err == -1) <br>

return err; <br>

memcpy(addr, req.ifr_hwaddr.sa_data,8); <br>

return 0; <br>

} <br>

/* <br>

* Obtain the maximum packet size on an interface. <br>

* <br>

* Return: <br>

* >0 Return is the mtu of the interface <br>

* -1 Error. <br>

*/ <br>

int <br>

GetDeviceMTU(char *device) <br>

{ <br>

int s = socket(AF_INET, SOCK_DGRAM, 0); <br>

struct ifreq req; <br>

int err; <br>

strcpy(req.ifr_name, device); <br>

err = ioctl(s, SIOCGIFMTU, &req); <br>

close(s); /* So Ill add this one as well. Ok Alan? - Rob */ <br>

if (err == -1) <br>

if (err == -1) <br>

return err; <br>

return req.ifr_mtu; <br>

} <br>

#define data_packet_len 1514 <br>

int <br>

main(int argc ,char *argv[]) <br>

{ <br>

char devicename_rec[32]; <br>

unsigned char data[data_packet_len]; <br>

int netid=0x03,sock_h=0,i=0,count_rec=0; <br>

if ((sock_h=OpenNetworkType(netid))<0) <br>

{ <br>

printf("Can open net_dectype %d n",netid); <br>

return -1; <br>

} <br>

printf("Ready to receive 0x%x data packet...n",netid); <br>

for(;;) { <br>

if (ReadFromNetwork(sock_h,devicename_rec,data,data_packet_len)>0) { <br>

printf("Received Packet = %dn",++count_rec) ; <br>

for (i=0;i<100;i++) <br>

printf("%2x|",data[i]); <br>

printf("n"); <br>



} <br>

} <br>

} <br>

/*以上程序在Redhat 5.1下编译通过,运行良好。*/ <br>

摘自:http://www.linuxbyte.net <br>

备    注: <br>

关  闭 <br>

---------------------------------------------------------------------------- <br>

---- <br>

北京华泰网安信息技术有限公司 版权所有 <br>

Copyright (C) 2001 www.netguard.com.cn. All Rights Reserved <br>

  <br>

-- <br>

一万年太久,只争朝夕... <br>

※ 来源:·UNIX编程 www.tiaozhan.com/unixbbs/·[FROM: 211.69.197.81] <br>

</small><hr>
<p align="center">[<a href="index.htm">回到开始</a>][<a href="135.htm">上一层</a>][<a href="145.htm">下一篇</a>]
<p align="center"><a href="http://cterm.163.net">欢迎访问Cterm主页</a></p>
</table>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -