breakwww.im arts.com有玩家群吗

1. 下载安装版本go 1.8
2. 按照提示下一步下一步安装好 go 后,应该是在C:\Go 目录
3. 可以在cmd 命令行$go 试试看能不能答应出usag
4. mac 可以使用homebrew 安装,安装完后应该是在homebrew 下的Celler 下面
5. 配置gopath 目录指向一个你用于存放go 源码、包、可执行文件的目录,
并且go 强制要求在此目录下简历src bin pkg目录存储相关的go 源码文件和包文件
当你使用go /urlshor…..之类的包的时候就会存储在这些src 目录下了
6. 下载安装intellj IDEA , 地址:/idea/
激活可以使用licenseServer 激活,server可以使用:/key.php
7. 启动idea ,并在config中找到plugins -&browse repositories中输入go 安装go language
8. 新建项目 配置go sdk 选择你安装go的目录,也就是那个C:\Go 目录
9. 编写go 代码
#10. 关于包的管理,IDEA 强制包名同目录名,如果你想建立一个package在本地,然后main 包引用
那么你需要建立相应的目录在src 目录中,然后通过import “./packagename” 引入
QDBM 基本调用API规范
(此文档非正式文档,仅仅是我自己为了便于自己理解qdbm api 接口调用顺便翻译。转载请注明出处!--lgm)
QDBM 是一个常规的管理型数据库调用库。这个数据库是一种简单的记录型数据库,每一条记录是一个包含key 和value的键值对.每一对key和value是一个可变长的字节序.二进制形式和字符串形式都可以作为他的key和value.它是一个没有表格也没有数据类型的一种数据库形式。每条记录以hash表或者B+ 树的形式组织。
关于这种hash表形式的数据库,每一个key在数据库中必须是唯一,因此不可能存在两个或多个key 完全一样的记录.以下我们提供了一系列的方法来访问数据库,比如:通过一个key和value存储一条记录,通过一个key删除一条记录,通过一个key检索一条记录,此外,遍历每一条记录,当然顺序是随意的我们不进行排序。在linux中访问数据库的方法和大多数的DBM (NDBM ,GDBM)具有类似的接口定义。QDBM 是一个好的方式去替代其它数据库,因为它具有较好的性能表现。
相对于hash 表,B+树形式存储,它是可以存储重复的key值记录的。同样的增加,删除,遍历,查找的我们也提供类似hash表的API调用。记录可以通过用户定义排序方法顺序的存储。我们可以通过一个游标以升序或者降序的形式访问,根据这种机制呢,我们可以做一个字符串的匹配的查询,或者实现一个整数范围内的查询。此外,遍历数据库记录在B+ 树也是可用的。
QDBM是用C写的一个数据库,提供有一系列语言的API,其中包括有C,C++.JAVA,Perl, 和Ruby。QDBM在所有符合POSIX标准的平台上这些API都是可用的。QDBM 是一个在LGPL License 授权下试用的免费使用的软件。
Depot 是QDBM的一个基础API,实现了几乎所有用于管理QDBM数据库功能的API调用。其它的API调用基本上只是把Depot API调用做了一次包装。Depot是所有API调用中最快的。为了你能正常的使用Depot , 你需要在你的源文件中包含”depot.h”头文件和”stdlib.h”头文件。通常情况下这两个文件我们应该把它放在源文件的最开始的位置。
#include &depot.h&
#include &stdlib.h&
一个用于指向数据库操作的句柄指针 “DEPOT”,这也就是好比如我们需要操作文件时使用的”stdio.h”里面用的”FILE” 文件指针。数据库可以通过“dpopen”函数调用打开,通过“dpclose”函数调用关闭。你不应该直接手动去操作数据库句柄的成员,如果出现了致命错误,除了关闭操作”dpclose”方法,任何的访问方法都会不正常并且会返回一个错误状态代码。同一个进程可以同时使用两个数据库操作句柄,但是同时操作同一个数据库文件我们并不建议你这样去操作。
extern const char *
“dpversion”全局外部变量是一个字符串变量,定义了版本相关的信息。
extern int dpcode
“dpcode”全局外部变量定义了最后的错误状态码,所有的错误状态码的详细信息我们都可以在“depot.h”头文件中查到。初始变量值是“DP_ENOERR”,其它定义有的值还有以下值:DP_EFATAL', DP_EMODE’, DP_EBROKEN', DP_EKEEP’, DP_ENOITEM', DP_EALLOC’, DP_EMAP', DP_EOPEN’, DP_ECLOSE', DP_ETRUNC’, DP_ESYNC', DP_ESTAT’, DP_ESEEK', DP_EREAD’, DP_EWRITE', DP_ELOCK’, DP_EUNLINK', DP_EMKDIR’, DP_ERMDIR', and DP_EMISC’.
const char *dperrmsg(int code)
“ecode”指一个特定的错误代码,函数的返回值是一个不可写的错误代码对应的错误信息字符串。
DEPOT *dpopen(const char *name, int omode, int bnum);
“name”是指定的数据库名称。“omode”是指定连接数据库所要进行的操作:“DP_OWRITER”指定写,“DP_OREADER”指定读,如果指定的操作模式是“DP_OWRITER”,那么你可能需要对操作模式操作做一个与的操作,进行“DP_OCREAT”,也就是说当数据库不存在的时候创建新的数据库文件。“DP_OTRUNC”操作意味着不管数据库存在或者不存在我们都创建一个新的数据库文件。对于“DP_OREADER”读操作和“DP_OWRITER”写操作,我们可以与上一个“DP_ONOLCK”操作,这意味着我们操作数据库的时候将不对数据文件上锁,或者我们可以与上一个“DP_OLCKNB“操作,这就意味着我们执行非阻塞式打开。对于“DP_OCREAT”
操作可以或上“DP_OSPARSE”操作,这代表创建一个松散的数据库文件。“bnum”代表元素阵列的数组元素个数,如果不大于0,则使用默认值,这一个阵列值是在创建数据库时就决定了的,并且通常情况下是不能改变的。建议的阵列数组大小设置为0.5到4的范围内。函数的返回值是一个数据库的操作句柄,如果返回NULL则代表数据库打开不成功,当以写方式打开数据库文件时,我们会对数据库执行独占写锁的调用,当以读方式打开时,我们会对数据库文件执行共享读锁调用,打开线程会阻塞,直到数据库被锁定,如果使用了“DP_ONOLCK”模式操作,则应用程序应该负责控制数据库文件的锁定。
Int dpclose(DEPOT *depot)
“depot”代表一个数据库操作句柄。如果函数调用成功,返回值为true否则返回false。关闭数据库操作句柄后,我们就不应该再继续使用此句柄。如果是更新数据,我们应该在数据库句柄关闭前写入数据。如果是以写方式打开数据库,但是又没有关闭数据库,则这个数据库将会被破坏。
Int dpput(DEPOT *depot)
“depot”是一个写操作的数据库句柄。“kbuf”是一个指向需要写的数据域的Key指针,”ksiz”是一个表示key数据域长度大小值,如果这个数是一个负数,这个大小通过“strlen(kbuf)”分配。“vbuf”是一个表示value数据域的指针,“vsiz”是一个表示value数据域长度的值,如果是负数,这个大小通过“strlen(vbuf)”分配,“dmode”表示如果出现重复key的时候所要进行的操作,可以有以下值:”DP_DOVER”表示覆盖现有的存在的值,“DP_DKEEP”表示保持现有数据库的值,“DP_DCAT”表示指定的值连接到现有值的末尾,如果函数执行成功返回true否则返回false。
Int dpout(DEPOT *depot, const char *kbuf, int ksiz)
“dpout”函数表示用于删除一条记录,“depot”表示一个写操作的数据库句柄,“kbuf”表示一个指向key数据域的一个指针,”ksiz”表示key数据域的大小。如果为负数,则大小通过“strlen(kbuf)”计算分配。如果函数执行成功,返回true否则返货false。 返回false表明函数执行失败,数据库中不存在这这要上出的key 的数据库记录。
char *dpget(DEPOT *depot, const char*kbuf, int ksiz, int start, int max, int *sp);
“dpget”用于检索获取一条数据库记录。“depot”表示数据库操作句柄,“kbuf”表示一个key数据域指针,“ksiz”表示key数据域指针的大小,如果为负数,则key数据域指针使用“strlen(kbuf)”计算分配大小,“start”表示检索的起始数据库记录偏移,”max”表示读取的最大的数据库记录数,如果为负数,则表示无限制。“sp”表示一个用户返回数据库记录的变量的指针,如果为NULL,表示不适用此指针。如果函数执行成功,返回指向相应数据记录的指针,否则返回NULL。如果返回NULL,表明没有相应的key 值的记录存在数据库中,或者是size的值小于start的起始的值。由于返回的记录值会在末尾追加一个结束符‘\0’,所以返回值可以当做一个字符串进行处理。由于返回值的指针区域是使用malloc 函数动态分配的,所以在使用完后应该做release释放处理。
int dpgetwb(DEPOT *depot, const char *kbuf, int ksiz, int start, int max, char *vbuf);
“dpgetwb” 是用于检索一条数据库记录,并存入指定的指针区域。“depot”指向需要操作的数据库句柄。“kbuf”是一个指向key数据域的指针。“ksiz”表示key数据域的大小。如果为负数,则大小使用“strlen(kbuf)”计算。”start”表示开始检索的数据库记录的起始偏移地址。
“max”表示检索的最大长度。最大应该小于或等于缓冲区的大小。“vbuf”表示检索出的数据库记录的value值存储的buffer 地址。如果函数调用成功,返回写入”vbuf”的数据大小,否则返回“-1”.返回“-1”还有可能是数据库没有指定的key值的记录,或者查询的size大小小于开始start偏移量。注意查询的结果写入进“vbuf”是不带结束符‘\0’的。
以下内容是从我个人角度出发分析:
话说作为一个农村人以前我也是很不懂也不愿意花钱在保险上,但是经过一次家庭的事故,我现在觉得其实可以考虑买一些基本的保险。
但现在我研究了一下保险这东西,其实也算是金融类的一大块,其实跟我们存钱一样,就是一种储蓄,只不过他很多时候存储的
是一份安心。
先说总体原则;买保险一般越年轻越便宜,因为得病概率小,要买的年数多。
买保险原则;意外险->医疗(意外医疗和住院医疗)->重大疾病->人寿险
报销原则:无论买了多少保险,正常是报销加起来不能高于实际花费。
首先,第一点大家肯定很关心社保这东西。
社保三不报:
(一)社保只报销因疾病引起的医疗费用,因意外伤害导致的医疗费用不能报销;
(二)社保不对非工作期间发生的意外伤害和意外医疗责任进行赔付;
(三)无论意外身故还是疾病身故,社保都没有身故赔偿,身故后也只返还当时个人账户的金额,而这部分金额很少。
还有一点就是,社保是先支出,后报销,也就是说自己垫着先治好,之后再拿发票报销,
所以,有些时候就是,我特么自己就没那么多钱治病,我怎么治好然后拿去报销,我就只能等死。
养老保险累计缴费年限满15年,就可以正常领取养老保险,理论上应该是你缴费越多然后每个月领的就越多
基本医疗保险,一旦中断缴费(一般这东西好像会有一个月左右的缓冲期),断保期间将不再享受医疗保险待遇,医疗费用不能报销。另外,医疗保险累计缴费年限男满25年、女满20年,就可以享受退休医疗待遇。
第二点说意外险:意外伤害,意外医疗
意外险是纯属于消费类的东西,就是买一份自己的安心,作用就是当发生意外的时候能得到一定的理赔。
意外险一般也比较便宜,不用你话很多钱。
意外险伤害险,一般只是赔付那些意外被烧伤呀,受伤残废或者发生意外突然就gameover那种,然后这样你可以拿到一份理赔。
注意它是不赔付你的医疗费的。
如果要赔付你的医疗费用,则需要买的应该是意外医疗险,这样你因为发生了意外,然后住院的话,他才是给你报销的。
意外险要注意一点就是买的时候你要确定什么属于意外,比如说这种中暑啊,怀孕二保一啊,猝死啊,这些都不算意外的!!!
这里提到一点就是,关于你老婆怀孕生娃的保险,这个的话可以考虑一个剩余保险,包括妊娠特定疾病保障、分娩身故保障、先天性疾病保障及新生儿身故保障,涵盖了孕妇妊娠及分娩两个阶段的关键保障需求
住院医疗这个会有一个住院津贴的东西,这个需要自己去详细了解,我目前也没找,这个就是你住院的时候每天会贴给你多少多少钱,让你住院。
第三点是重疾险,
这里我觉得要跟拿他跟商业保险一起说,这个似乎是具有提前给付的,也就是不管你得病了治疗不治疗,好像有医院证明就可以提前理赔。然后让你选择到底怎么治。
重大疾病这里会有明确的指出什么算重大疾病,你去买的时候要注意看协议,而且还要填个人健康信息,就是说有没有什么先天性遗传呀这些东西。
如果说,你觉得你现在很胖,然后比较当心今后自己会会得高血压或者心脏病,然后当心自己挂了家里老人没人养,那么这个时候你就可以考虑了。
重大疾病这个东西我举得得看自己的嗅觉,而且比较贵。
最后说人寿:
寿险简单说就是买生或者买死
买死呢就是,一般来说比如你中年以后背着房贷车贷,如果gameover了,那么整个家庭就垮了包括老婆孩子父母等都没有了依靠,死亡险就是应对这种情况的,让你可以放心的走,死了以后还有一笔钱给他们。
买生呢就是你觉得你可以活很久,然后活到约定的年龄没有gameover就给你一笔钱恭喜你,或者是说活到一定的年龄没有死,那么你每个月或者每年就一直零钱。
当然保险也会有很多属于投资或者理财的,这个需要大家自己去了解,我只想了解大事故或者得病这些。
结语:总之就是买保险就是买一份放心吧,你别想着有啥用途,而且保险公司会有精算师,他们是不会亏本的了。
我觉得就是如果有钱,可以考虑拿一点带你钱,买一些你可能会预期的保险,意外险是便宜最好买的了。
有说得不对的,大家尽情拍砖。
traceroute 通过逐渐变更的TTL值发送UDP包(我见busybox 也有icmp 类型的),然后等待ttl 值变为0的那一跳回复icmp time exceeded 包来测试网络的连通性。首先,traceroute送出一个TTL是1的IP数据包到目的地,当路径上的第一个路由器收到IP数据包时,将TTL减1。此时,TTL变为0,所以将该路由器会将此IP数据包丢掉,并返回一个ICMP time exceeded数据包,当traceroute收到这个消息后,接着继续发生TTL为2的IP数据包给第二个路由器。以此类推,直到IP数据包达到最后一台路由器时,会返回一个ICMP echo reply 或者 地址不可达的数据包。我们通过建立原始套接字来接收返回的icmp 数据包,然后通过解析数据包的字段,析出地址,然后给出连通性的信息。
以下代码存储测试,写完可以跑了就没理了,没考虑过释放内存,关闭套接字等一系列细节,就一测试程序,跑完就结束了。
#include &stdio.h&
#include &stdlib.h&
#include &sys/param.h&
#include &sys/file.h&
#include &sys/ioctl.h&
#include &sys/socket.h&
#include &sys/time.h&
#include &netinet/in_systm.h&
#include &netinet/in.h&
#include &netinet/ip.h&
#include &netinet/ip_icmp.h&
#include &netinet/udp.h&
#include &arpa/inet.h&
#include &ctype.h&
#include &errno.h&
#include &fcntl.h&
#include &malloc.h&
#include &memory.h&
#include &netdb.h&
#include &string.h&
#include &unistd.h&
//udp packet data payload
struct outdata{
typedef struct traceInfo{
char host[128];
}traceInfo_t;
typedef struct globalInfo{
struct sockaddr_
struct sockaddr_in loc_
int packet[1024];
}globalInfo_t;
struct sockaddr_in *
struct udphdr *
struct outdata *
globalInfo_t g_
void tvsub(struct timeval *out, struct timeval *in)
if((out-&tv_usec -= in-&tv_usec) & 0)
out-&tv_sec = out-&tv_sec -1;
out-&tv_usec += 1000000;
out-&tv_sec = out-&tv_sec - in-&tv_
double deltaT(struct timeval *t1p, struct timeval *t2p)
dt = (double)(t2p-&tv_sec - t1p-&tv_sec)*1000.0 +
(double)(t2p-&tv_usec - t1p-&tv_usec)/1000.0;
int getsockaddr_by_host(struct sockaddr_in *sin, char *host)
struct hostent *
struct in_
if(!inet_aton(host,&addr))
if(!(hent = gethostbyname(host)))
printf("gethostbyname error!\n");
return -1;
addr = *(struct in_addr *)hent-&h_
memset(&(sin-&sin_addr),0x00,sizeof(sin-&sin_addr));
sin-&sin_addr =
void init_globalinfo(globalInfo_t *ptr, traceInfo_t *traceInfo)
if(ptr == NULL || traceInfo == NULL)
memset(ptr,0,sizeof(*ptr));
ptr-&min_ttl = traceInfo-&min_
ptr-&max_ttl = traceInfo-&max_
ptr-&waittime = traceInfo-&
ptr-&queries = traceInfo-&
ptr-&packlen = traceInfo-&
ptr-&port = traceInfo-&
ptr-&ident = (getpid() & 0xffff)|0x8000;
//ptr-&haddr = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in));
//memset(ptr-&haddr, 0x00, sizeof(struct sockaddr_in));
getsockaddr_by_host(&(ptr-&haddr), traceInfo-&host);
ptr-&haddr.sin_family = AF_INET;
ptr-&haddr.sin_port = ptr-&
void send_probe(globalInfo_t *ptr, int seq, int ttl, struct timeval *tp)
outdata = (struct outdata *)malloc(sizeof(ptr-&packlen));
memset(outdata,0,sizeof(ptr-&packlen));
ptr-&haddr.sin_port = htons(ptr-&port+seq);
outdata-&seq =
outdata-&ttl =
outdata-&tv = *
if(setsockopt(ptr-&sendsock, IPPROTO_IP, IP_TTL,(char *)&ttl,sizeof(ttl)) & 0)
printf("set ttl error!\n");
cc = sendto(ptr-&sendsock, (char *)outdata, ptr-&packlen,0,(struct sockaddr *)&(ptr-&haddr),sizeof(ptr-&haddr));
if(cc & 0 || cc != ptr-&packlen)
printf("send packlen error!\n");
fflush(NULL);
int wait_for_reply(globalInfo_t *ptr, struct sockaddr_in *from, struct timeval *tp)
struct timeval now,
int cc = 0;
int sock = ptr-&
int fromlen = sizeof(*from);
FD_ZERO(&fds);
FD_SET(sock, &fds);
wait.tv_sec = tp-&tv_sec + ptr-&
wait.tv_usec = tp-&tv_
gettimeofday(&now,&tz);
tvsub(&wait,&now);
if(select(sock+1, &fds, NULL, NULL,&wait) & 0)
cc = recvfrom(sock, (char *)ptr-&packet,sizeof(ptr-&packet),0,(struct sockaddr *)from, &fromlen);
int packet_ok(globalInfo_t *ptr,char *buf, int cc, struct sockaddr_in *from,int seq)
struct icmp *
char type,
struct ip *
ip = (struct ip *)
hlen = ip-&ip_hl && 2;
if(cc & hlen + ICMP_MINLEN)
printf("packet too short!!\n");
return -1;
icmp = (struct icmp *)(buf+hlen);
type = icmp-&icmp_
code = icmp-&icmp_
if((type == ICMP_TIMXCEED && code == ICMP_TIMXCEED_INTRANS) ||
type == ICMP_UNREACH || type == ICMP_ECHOREPLY)
struct ip *
struct udphdr *
struct icmp *
hip = &(icmp-&icmp_ip);
hlen = hip-&ip_hl&&2;
hudp = (struct udphdr *)((char *)hip + hlen);
printf("this is init!" );
printf("hlen=%d---&cc=%d\n",hlen,cc);
printf("hip-&ip_p=%d----&IPPROTO_UDP=%d\n",hip-&ip_p,IPPROTO_UDP);
printf("hudp-&dest=%d----&htons(ptr-&port+seq)=%d\n",
hudp-&dest,htons(ptr-&port + seq));
if(hlen +12 &= cc && hip-&ip_p == IPPROTO_UDP &&
/*hudp-&source == htons(ptr-&ident) &&*/ hudp-&dest == htons(ptr-&port + seq))
return (type == ICMP_TIMXCEED?-1:code+1);
void print(char *buf, int cc, struct sockaddr_in *from )
struct ip *
ip = (struct ip *)
hlen = ip-&ip_hl && 2;
printf(" %s", inet_ntoa(from-&sin_addr));
int getByDefaultValue(int value,int min,int max)
if( min&value && value & max)
else if(value & max)
else if(value & min)
void trace(traceInfo_t *traceInfo)
int on = 1;
struct sockaddr_in *
int seq = 0;
int minipacket,
if(traceInfo == NULL)
init_globalinfo(&g_info,traceInfo);
minipacket = sizeof(*outip) + sizeof(outdata);
packlen = minipacket + sizeof(*outudp);
g_info.packlen = getByDefaultValue(g_info.packlen,minipacket,32*1024);
from = malloc(sizeof(struct sockaddr_in));
memset(from,0x00,sizeof(struct sockaddr_in));
//setlinebuf(stdout);
outip = (struct ip *)malloc((unsigned)packlen);
if(outip == NULL)
printf("outip is null!\n");
memset((char *)outip,0,packlen);
outip-&ip_v = IPVERSION;
outip-&ip_len =
outip-&ip_off = 0;
if((g_info.sendsock=socket(AF_INET, SOCK_DGRAM, 0)) & 0)
printf("create sendsock error!\n");
if((g_info.recvsock = socket(AF_INET,SOCK_RAW,IPPROTO_ICMP)) & 0)
printf("create recvsock error!\n");
if(setsockopt(g_info.sendsock, IPPROTO_IP, IP_HDRINCL,(char *)&on,sizeof(on)) & 0)
printf("setopt error!\n");
printf("traceroute to %s (%s)\n",traceInfo-&host,inet_ntoa(g_info.haddr.sin_addr));
fflush(NULL);
for(ttl=g_info.min_ ttl & g_info.max_ ++ttl)
u_int32_t lastaddr = 0;
int gotlastaddr = 0;
int got_there = 0;
int unreachable = 0;
int sentfirst = 0;
printf("%2d\t",ttl);
for(probe = 0; probe&g_info. ++probe)
struct timeval t1,t2;
struct ip *
gettimeofday(&t1,&tz);
send_probe(&g_info,++seq,ttl,&t1);
while((cc = wait_for_reply(&g_info,from,&t1)) != 0)
gettimeofday(&t2,&tz);
i = packet_ok(&g_info,(char *)g_info.packet,cc,from,seq);
if(i == 0)
if(!gotlastaddr || from-&sin_addr.s_addr != lastaddr)
print((char *)g_info.packet,cc,from);
lastaddr = from-&sin_addr.s_
%.3f ms",deltaT(&t1,&t2));
if(i == -1)
code = i -1;
switch(code)
case ICMP_UNREACH_PORT:
case ICMP_UNREACH_NET:
printf(" !N");
case ICMP_UNREACH_HOST:
printf(" !H");
case ICMP_UNREACH_PROTOCOL:
printf(" !P");
printf("!&%d&",code);
if(cc == 0)
printf(" *");
fflush(NULL);
printf("\n");
if(got_there || unreachable & 0 && (unreachable &= g_info.queries -1))
int main(void)
traceInfo_t traceI
strcpy(traceInfo.host,"");
traceInfo.min_ttl = 1;
traceInfo.max_ttl = 30;
traceInfo.waittime = 5;
traceInfo.queries = 3;
traceInfo.packsize = 50;
traceInfo.port = 32758 + 666;
trace(&traceInfo);
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
#include &stdio.h&#include &stdlib.h&#include &sys/param.h&#include &sys/file.h&#include &sys/ioctl.h&#include &sys/socket.h&#include &sys/time.h&#include &netinet/in_systm.h&#include &netinet/in.h&#include &netinet/ip.h&#include &netinet/ip_icmp.h&#include &netinet/udp.h&#include &arpa/inet.h&#include &ctype.h&#include &errno.h&#include &fcntl.h&#include &malloc.h&#include &memory.h&#include &netdb.h&#include &string.h&#include &unistd.h&&//udp packet data payloadstruct outdata{ u_char seq; u_char ttl; struct timeval tv;};&typedef struct traceInfo{ char host[128]; int min_ttl; int max_ttl; int waittime; int queries; int packsize; int port;}traceInfo_t;&typedef struct globalInfo{ struct sockaddr_in haddr; struct sockaddr_in loc_addr; int sendsock; int recvsock; int min_ttl; int max_ttl; int waittime; int queries; int packlen; int port; int ident; int packet[1024];}globalInfo_t;&&struct sockaddr_in *outip;struct udphdr *outudp;struct outdata *outdata;&globalInfo_t g_info;&void tvsub(struct timeval *out, struct timeval *in){ if((out-&tv_usec -= in-&tv_usec) & 0) {
out-&tv_sec = out-&tv_sec -1;
out-&tv_usec += 1000000; } out-&tv_sec = out-&tv_sec - in-&tv_sec;}&double deltaT(struct timeval *t1p, struct timeval *t2p){ double dt; dt = (double)(t2p-&tv_sec - t1p-&tv_sec)*1000.0 +
(double)(t2p-&tv_usec - t1p-&tv_usec)/1000.0;& return dt;}&int getsockaddr_by_host(struct sockaddr_in *sin, char *host){ struct hostent *hent; struct in_addr addr;
if(!inet_aton(host,&addr)) {
if(!(hent = gethostbyname(host)))
printf("gethostbyname error!\n");
return -1;
addr = *(struct in_addr *)hent-&h_addr; }& memset(&(sin-&sin_addr),0x00,sizeof(sin-&sin_addr)); sin-&sin_addr = addr;&&}&void init_globalinfo(globalInfo_t *ptr, traceInfo_t *traceInfo){ if(ptr == NULL || traceInfo == NULL)
return ;& memset(ptr,0,sizeof(*ptr)); ptr-&min_ttl = traceInfo-&min_ttl; ptr-&max_ttl = traceInfo-&max_ttl; ptr-&waittime = traceInfo-&waittime; ptr-&queries = traceInfo-&queries; ptr-&packlen = traceInfo-&packsize; ptr-&port = traceInfo-&port; ptr-&ident = (getpid() & 0xffff)|0x8000;& //ptr-&haddr = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in)); //memset(ptr-&haddr, 0x00, sizeof(struct sockaddr_in)); getsockaddr_by_host(&(ptr-&haddr), traceInfo-&host); ptr-&haddr.sin_family = AF_INET; ptr-&haddr.sin_port = ptr-&port;}&void send_probe(globalInfo_t *ptr, int seq, int ttl, struct timeval *tp){ int cc;& outdata = (struct outdata *)malloc(sizeof(ptr-&packlen)); memset(outdata,0,sizeof(ptr-&packlen));& ptr-&haddr.sin_port = htons(ptr-&port+seq); outdata-&seq = seq; outdata-&ttl = ttl; outdata-&tv = *tp;
if(setsockopt(ptr-&sendsock, IPPROTO_IP, IP_TTL,(char *)&ttl,sizeof(ttl)) & 0) {
printf("set ttl error!\n");
return ; }& cc = sendto(ptr-&sendsock, (char *)outdata, ptr-&packlen,0,(struct sockaddr *)&(ptr-&haddr),sizeof(ptr-&haddr)); if(cc & 0 || cc != ptr-&packlen) {
printf("send packlen error!\n");
fflush(NULL);
return ; }&}&int wait_for_reply(globalInfo_t *ptr, struct sockaddr_in *from, struct timeval *tp){ fd_set fds; struct timeval now,wait; struct timezone tz; int cc = 0; int sock = ptr-&recvsock;
int fromlen = sizeof(*from);& FD_ZERO(&fds); FD_SET(sock, &fds);& wait.tv_sec = tp-&tv_sec + ptr-&waittime; wait.tv_usec = tp-&tv_usec; gettimeofday(&now,&tz);& tvsub(&wait,&now);& if(select(sock+1, &fds, NULL, NULL,&wait) & 0)
cc = recvfrom(sock, (char *)ptr-&packet,sizeof(ptr-&packet),0,(struct sockaddr *)from, &fromlen);
return cc;}&int packet_ok(globalInfo_t *ptr,char *buf, int cc, struct sockaddr_in *from,int seq){ struct icmp *icmp; char type, code; int hlen;& struct ip *ip; ip = (struct ip *)buf;& hlen = ip-&ip_hl && 2; if(cc & hlen + ICMP_MINLEN) {
printf("packet too short!!\n");
return -1; } cc = cc -hlen;& icmp = (struct icmp *)(buf+hlen);& type = icmp-&icmp_type; code = icmp-&icmp_code;& if((type == ICMP_TIMXCEED && code == ICMP_TIMXCEED_INTRANS) ||
type == ICMP_UNREACH || type == ICMP_ECHOREPLY) {
struct ip *hip;
struct udphdr *hudp;
struct icmp *hicmp;&
hip = &(icmp-&icmp_ip);
hlen = hip-&ip_hl&&2;
hudp = (struct udphdr *)((char *)hip + hlen);#if 0
printf("this is init!" );
printf("hlen=%d---&cc=%d\n",hlen,cc);
printf("hip-&ip_p=%d----&IPPROTO_UDP=%d\n",hip-&ip_p,IPPROTO_UDP);
printf("hudp-&dest=%d----&htons(ptr-&port+seq)=%d\n",
hudp-&dest,htons(ptr-&port + seq));#endif&&
if(hlen +12 &= cc && hip-&ip_p == IPPROTO_UDP &&
/*hudp-&source == htons(ptr-&ident) &&*/ hudp-&dest == htons(ptr-&port + seq))
return (type == ICMP_TIMXCEED?-1:code+1); } return 0; }&void print(char *buf, int cc, struct sockaddr_in *from ){ struct ip *ip; int hlen;& ip = (struct ip *)buf; hlen = ip-&ip_hl && 2; cc -= hlen;& printf(" %s", inet_ntoa(from-&sin_addr));}&&int getByDefaultValue(int value,int min,int max){ if( min&value && value & max)
return value; else if(value & max)
return max; else if(value & min)
return min; else
return min;}&&&void trace(traceInfo_t *traceInfo){& int on = 1; int ttl,probe; struct sockaddr_in *from; int seq = 0; int code; int i=0; int minipacket,packlen;& if(traceInfo == NULL)
init_globalinfo(&g_info,traceInfo);&& minipacket = sizeof(*outip) + sizeof(outdata); packlen = minipacket + sizeof(*outudp); g_info.packlen = getByDefaultValue(g_info.packlen,minipacket,32*1024);& from = malloc(sizeof(struct sockaddr_in)); memset(from,0x00,sizeof(struct sockaddr_in));& //setlinebuf(stdout);#if 0 outip = (struct ip *)malloc((unsigned)packlen); if(outip == NULL) {
printf("outip is null!\n");
return ; } memset((char *)outip,0,packlen);& outip-&ip_v = IPVERSION; outip-&ip_len = packlen; outip-&ip_off = 0;& outp#endif& if((g_info.sendsock=socket(AF_INET, SOCK_DGRAM, 0)) & 0) {
printf("create sendsock error!\n");
return ; } if((g_info.recvsock = socket(AF_INET,SOCK_RAW,IPPROTO_ICMP)) & 0) {
printf("create recvsock error!\n");
return ; }#if 0 if(setsockopt(g_info.sendsock, IPPROTO_IP, IP_HDRINCL,(char *)&on,sizeof(on)) & 0) {
printf("setopt error!\n");
return ; }#endif& printf("traceroute to %s (%s)\n",traceInfo-&host,inet_ntoa(g_info.haddr.sin_addr)); & fflush(NULL); for(ttl=g_info.min_ttl; ttl & g_info.max_ttl; ++ttl) {
u_int32_t lastaddr = 0;
int gotlastaddr = 0;
int got_there = 0;
int unreachable = 0;
int sentfirst = 0;&
printf("%2d\t",ttl);
for(probe = 0; probe&g_info.queries; ++probe)
struct timeval t1,t2;
struct timezone tz;
struct ip *ip;&
gettimeofday(&t1,&tz);&
send_probe(&g_info,++seq,ttl,&t1);&
++sentfirst;&
while((cc = wait_for_reply(&g_info,from,&t1)) != 0)
gettimeofday(&t2,&tz);
i = packet_ok(&g_info,(char *)g_info.packet,cc,from,seq);
if(i == 0)
if(!gotlastaddr || from-&sin_addr.s_addr != lastaddr)
print((char *)g_info.packet,cc,from);
lastaddr = from-&sin_addr.s_addr;
++gotlastaddr;
printf("&& %.3f ms",deltaT(&t1,&t2));&
if(i == -1)
code = i -1;
switch(code)
case ICMP_UNREACH_PORT:
++got_there;
case ICMP_UNREACH_NET:
++unreachable;
printf(" !N");
case ICMP_UNREACH_HOST:
++unreachable;
printf(" !H");
case ICMP_UNREACH_PROTOCOL:
++got_there;
printf(" !P");
++unreachable;
printf("!&%d&",code);
if(cc == 0)
printf(" *");
fflush(NULL);
printf("\n");
if(got_there || unreachable & 0 && (unreachable &= g_info.queries -1))
} }}&&&int main(void){ traceInfo_t traceInfo; strcpy(traceInfo.host,""); traceInfo.min_ttl = 1; traceInfo.max_ttl = 30; traceInfo.waittime = 5; traceInfo.queries = 3; traceInfo.packsize = 50; traceInfo.port = 32758 + 666;& trace(&traceInfo); return 0;&}
12131415161718
19202122232425
2627282930
Recent Posts
Categories

我要回帖

更多关于 euroarts 的文章

 

随机推荐