真开始写东西才发现编程似乎已经离我很远了,也可能是脚本语言写的多把老本都忘了。今天用C写东西被个字符串指针,字符串数组,结构体数组搞得个焦头烂额,虽然最终搞定,但是似乎感觉就像打乒乓球一样—-该练练基本功了。
在网络上面有着许多类型的机器,这些机器在表示数据的字节顺序是不同的, 比如i386芯片是低字节在内存地址的低端,高字节在高端,而alpha芯片却相反. 为了统一起来,在Linux下面,有专门的字节转换函数.
unsigned long int htonl(unsigned long int hostlong)
unsigned short int htons(unisgned short int hostshort)
unsigned long int ntohl(unsigned long int netlong)
unsigned short int ntohs(unsigned short int netshort)
在这四个转换函数中,h 代表host, n 代表 network.s 代表short l 代表long 第一个函数的意义是将本机器上的long数据转化为网络上的long. 其他几个函数的意义也差不多.
3.2 IP和域名的转换
在网络上标志一台机器可以用IP或者是用域名.那么我们怎么去进行转换呢?
struct hostent *gethostbyname(const char *hostname)
struct hostent *gethostbyaddr(const char *addr,int len,int type)
在中有struct hostent的定义
struct hostent{
char *h_name; /* […]
绝对是至理名言。
本人为了晚上不被工作电话吵醒,刻苦攻关月余,终于能够实现一个能够联动路由器的流量监控系统,离成功就差几步了,坚持住。
今天研究了下pcap,简单写了段代码,能嗅探到混杂模式网卡接收到的所有包了,贴一下以备以后使用:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
char *dev, errbuf[PCAP_ERRBUF_SIZE];
pcap_if_t * alldevsp;
dev = pcap_lookupdev(errbuf);
printf(”Device used: %sn”, dev);
int x=pcap_findalldevs(&alldevsp,errbuf);
pcap_if_t * ifdev;
for(ifdev=alldevsp;ifdev;ifdev=ifdev->next)
{
printf(”Device: %sn”,ifdev->name);
}
struct bpf_program filter;
char filter_app[] = “host 10.0.0″;
bpf_u_int32 mask;
bpf_u_int32 net;
pcap_t* adhandle;
adhandle = pcap_open_live(dev,100,1,1000,errbuf);
pcap_lookupnet(dev, &net, &mask, errbuf);
pcap_compile(adhandle, &filter, filter_app, 0, net);
pcap_setfilter(adhandle, &filter);
pcap_dumper_t* dumpfile;
dumpfile=pcap_dump_open(adhandle, “packet.dat”);
int re;
struct pcap_pkthdr* header;
u_char* pkt_data;
while((re=pcap_next_ex(adhandle,&header,(const u_char**)&pkt_data))>=0)
{
pcap_dump((unsigned char*)dumpfile,header,pkt_data);
}
pcap_freealldevs(alldevsp);
return(0);
}