LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 939|回复: 0

北京ADSL缺陷

[复制链接]
发表于 2003-11-26 18:15:34 | 显示全部楼层 |阅读模式
北京ADSL缺陷,不监测用户名,使用者利用多接口(网卡)可以获得成倍带宽和ip地址。

可以用下面代码测试:
声明:以下只作测试只用,连接自行建立多重连接,ip需在编译之前填写,不具备断点功能。

带宽测试推荐:vnstat

bitblade.c

  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <string.h>
  4. #include <netinet/in.h>
  5. #include <netdb.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <pthread.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>

  11. #define HTTP_PORT 80
  12. #define FTP_PORT 21
  13. int BLADE;
  14. extern int h_errno;
  15. char host[255],filename_with_path[255],proto[5],filename[128];
  16. unsigned int protocol=0;
  17. char *ui_to_a(unsigned int i);
  18. struct sch{
  19.         unsigned char status;
  20.         unsigned int begin_p;
  21.         unsigned int cur_p;
  22.         unsigned int down_len;
  23. }sch1[255];
  24. void *ana_url(char *p){
  25.         int n;
  26.         /*      judge the protocol e.g:[url]http://www.bit.edu.cn/file/big.zip[/url]
  27.                 the protocol is http
  28.         */

  29.         n=strlen(p)-strlen(strchr(p,':'));
  30.         if(n>4)
  31.                 printf("URL error!You must indicate the Protocol,e.g:[url]http://~[/url]");        strncpy(proto,p,n);
  32.         proto[n]='\0';
  33.         lower(proto);
  34.         if(strncmp(proto,"http",4)==0){
  35.                 protocol=1;
  36.         }else if(strncmp(proto,"ftp",4)==0){
  37.                 protocol=2;
  38.         }else{
  39.                 printf("Protocol Unkown\n");
  40.                 exit(1);
  41.         }
  42.         /* judge the hostname e.g:[url]www.bit.edu.cn*/[/url]
  43.         p=strchr(p,':')+3;
  44.         n=strlen(p)-strlen(strchr(p,'/'));
  45.         strncpy(host,p,n);
  46.         host[n]='\0';
  47.         /* judge the filename_with_path e.g:/file/big.zip*/
  48.         p=strchr(p,'/');
  49.         n=strlen(p);
  50.         strncpy(filename_with_path,p,n);

  51.         filename_with_path[n]='\0';
  52.         /* judge the filename without path*/
  53.         p=strrchr(p,'/');
  54.         n=strlen(p)-1;
  55.         strncpy(filename,p+1,n);
  56.         filename[n]='\0';
  57.         return NULL;
  58. }
  59. /* open the part of file */
  60. int open_t_file(int t_no){
  61.         char t_filename[255];       
  62.         char t_no_char[20];
  63.         char *f;
  64.         int n,ffd;
  65.         n=strlen(filename);
  66.         strncpy(t_filename,filename,n);
  67.         t_filename[n]='_';
  68.         t_filename[n+1]='t';
  69.         t_filename[n+2]='\0';
  70.         f=ui_to_a(t_no);
  71.         printf("\n%s\n",f);
  72.         strncat(t_filename,f,strlen(f));
  73.         ffd=open(t_filename,O_RDWR|O_CREAT);
  74.         return ffd;
  75. }
  76. int bind_sock(int t_cur){
  77.         struct sockaddr_in remote_addr;
  78.         int sockfd;
  79.         struct hostent *h;
  80.         /* create socket,enter ur IP here */
  81.         
  82.         char myhost[]="61.51.105.186";
  83.         char myhost1[]="61.51.105.175";
  84.         char *p;
  85.         if(t_cur%2==0){
  86.                 p=myhost;
  87.         }else{
  88.                 sleep(1);
  89.                 p=myhost1;
  90.         }
  91.        
  92.         if(!(h=gethostbyname(myhost1))){
  93.                 printf("Hostname Unkown\n");
  94.                 exit(1);
  95.         }else{
  96.                 printf("Hostname Converted\n");
  97.         }
  98.         write(1,p,13);
  99.         sockfd=socket(AF_INET,SOCK_STREAM,0);
  100.         bzero(&remote_addr,sizeof(remote_addr));
  101.         remote_addr.sin_family=AF_INET;
  102.         remote_addr.sin_port=htons(12435+t_cur);
  103.         remote_addr.sin_addr.s_addr=inet_addr(p);
  104.        
  105.         printf("p is:%d",remote_addr.sin_addr.s_addr);
  106.         if(bind(sockfd,(struct sockaddr *)&remote_addr,sizeof(remote_addr))<0){
  107.                 printf("Bind Error\n");
  108.                 exit(1);
  109.         }else{
  110.                 printf("Socket Binded\n");

  111.         }

  112.         return sockfd;
  113. }
  114. int creat_sock(int sockfd){
  115.         struct sockaddr_in remote_addr;
  116.         struct hostent *h;
  117.         /* create socket */
  118.         if(!(h=gethostbyname(host))){
  119.                 printf("Hostname Unkown\n");
  120.                 exit(1);
  121.         }else{printf("Hostname Converted\n");}
  122.         bzero(&remote_addr,sizeof(remote_addr));
  123.         remote_addr.sin_family=AF_INET;
  124.         remote_addr.sin_port=htons(HTTP_PORT);
  125.         bcopy(h->h_addr,(char *)&remote_addr.sin_addr,h->h_length);
  126.         if(connect(sockfd,(struct sockaddr *)&remote_addr,sizeof(remote_addr))<0){
  127.                 printf("Connect Error\n");
  128.                 exit(1);
  129.         }else{
  130.                 printf("Socket Created\n");
  131.                
  132.         }
  133.         return sockfd;
  134. }
  135. int
  136. main(int argc,char *argv[]){
  137.         char *p=argv[2];
  138.         BLADE=atoi(argv[1]);
  139.         ana_url(p);
  140.         bzero(&sch1[0],13*255);
  141.         printf("Your required filename is: %s\n",filename);
  142.         bhttp();
  143.         exit(0);               
  144. }
复制代码


bhttp.c

  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <string.h>
  4. #include <netinet/in.h>
  5. #include <netdb.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <pthread.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>

  11. int BLADE;
  12. char host[255],filename_with_path[255],proto[5],filename[128];
  13. unsigned int http_content_len=0;
  14. pthread_t http_get_t[10];
  15. struct sch{
  16.         unsigned char status;
  17.         unsigned int begin_p;
  18.         unsigned int cur_p;
  19.         unsigned int down_len;
  20. }sch1[255];
  21. char *ui_to_a(unsigned int i);
  22. char *form_http_request(char *p,int begin_p){
  23.         char *q;
  24.         strncat(p,filename_with_path,strlen(filename_with_path));
  25.         strncat(p," HTTP/1.1\r\n",11);
  26.         strncat(p,"Host:",5);
  27.         strncat(p,host,strlen(host));
  28.         strncat(p,"\r\nAccept:*/*\r\n",15);
  29.         strncat(p,"Range:bytes=",13);
  30.         q=ui_to_a(begin_p);
  31.         strncat(p,q,strlen(q));
  32.         strncat(p,"-\r\n\r\n",5);
  33.         return p;
  34. }
  35. void *t_http_get(void *arg){
  36.         char *p;
  37.         int n,t_cur;
  38.         int sockfd,ffd;
  39.         unsigned int need_wr;
  40.         unsigned int header_len,got_p_content_addr=0,got_p_length_addr=0;
  41.         char http_request[1024]="GET ";
  42.         unsigned char retry=0;
  43.         int nn;
  44.         unsigned int need_wr_once=0;
  45.         unsigned int got_len;
  46.         char *q;
  47.         char http_content_len_char[22];
  48.         char recv_data[1024*256];
  49.         t_cur=arg;
  50.         if(sch1[0].down_len==0){
  51.                 nn=BLADE;
  52.         }else{
  53.                 nn=t_cur;       
  54.         }
  55.         sockfd=bind_sock(nn);
  56.         sockfd=creat_sock(sockfd);
  57.         sch1[t_cur].status=1;
  58.         sch1[t_cur].begin_p=(t_cur)*(http_content_len/BLADE);
  59.         p=http_request;
  60.         form_http_request(p,sch1[t_cur].begin_p);
  61.         if(t_cur!=BLADE-1){
  62.         need_wr=sch1[t_cur].down_len=http_content_len/BLADE;
  63.         }else{
  64.         need_wr=sch1[t_cur].down_len=http_content_len/BLADE+http_content_len%BLADE;
  65.         }
  66.         printf("down_len=%d\n",sch1[t_cur].down_len);
  67.         if(sch1[0].down_len!=0)
  68.         ffd=open_t_file(t_cur);
  69.         write(sockfd,p,strlen(p));
  70.         while(1){
  71.                 for(got_len=0;got_len<sizeof(recv_data);got_len+=n){
  72.                         n=read(sockfd,&recv_data[got_len],sizeof(recv_data)-got_len);
  73.                         if(n<0)
  74.                                 printf("socket read error!");
  75.                         if(n==0)
  76.                                 retry+=1;
  77.                         if(retry>=3)
  78.                                 break;
  79.                 }       
  80.                 if(sch1[0].down_len==0&&(p=strstr(recv_data,"\nContent-Length: "))!=NULL&&got_p_length_addr==0){
  81.                         q=strchr(p+1,'\n');
  82.                         got_p_length_addr=1;
  83.                         strncpy(http_content_len_char,p+17,q-p-18);
  84.                         http_content_len_char[q-p-18]='\0';
  85.                         http_content_len=atoi(http_content_len_char);
  86.                         printf("The total http_content_len is:%d",http_content_len);
  87.                         sch1[0].down_len=http_content_len/BLADE;
  88.                         break;
  89.                 }
  90.                
  91.                 if((p=strstr(recv_data,"\r\n\r\n"))!=NULL&&got_p_content_addr!=1){
  92.                         header_len=p-&recv_data[0]+4;
  93.                         got_p_content_addr=1;
  94.                         got_len-=header_len;
  95.                         p+=4;
  96.                 }else{
  97.                         p=recv_data;
  98.                 }
  99.                 if(got_len<=need_wr){
  100.                         need_wr_once=got_len;
  101.                 }else{
  102.                         need_wr_once=need_wr;
  103.                 }
  104.                 printf("got_len==%d\n",got_len);
  105.                 nn=write(ffd,p,need_wr_once);
  106.                 sch1[t_cur].cur_p+=nn;
  107.                 need_wr-=nn;
  108.                 if(need_wr<=0)
  109.                         break;       
  110.         }
  111.         return NULL;
  112. }
  113. void bhttp(){
  114.         int i;
  115.         if(pthread_create(&http_get_t[0],NULL,t_http_get,0)){
  116.                         printf("error creating thread.");
  117.                         exit(1);
  118.         }else{
  119.                 printf("thread created.\n");
  120.         }
  121.         while(sch1[0].down_len==0)
  122.                 sleep(1);
  123.         for(i=0;i<BLADE;i++){
  124.                 if(pthread_create(&http_get_t[i+1],NULL,t_http_get,i)){
  125.                                 printf("error creating thread.");
  126.                                 exit(1);
  127.                 }else{
  128.                         printf("thread created.\n");
  129.                 }
  130.         }
  131.         for(i=0;i<BLADE+1;i++){       
  132.                 if(pthread_join(http_get_t[i],NULL)){
  133.                         printf("error joining thread.");
  134.                         exit (1);
  135.                 }
  136.         }
  137. }
复制代码

itoa.c

  1. /* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2002
  2.      Free Software Foundation, Inc.
  3.      Written by James Clark (jjc@jclark.com)

  4. This file is part of groff.

  5. groff is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 2, or (at your option) any later
  8. version.

  9. groff is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12. for more details.

  13. You should have received a copy of the GNU General Public License along
  14. with groff; see the file COPYING.  If not, write to the Free Software
  15. Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

  16. #define INT_DIGITS 19                /* enough for 64 bit integer */
  17. #define UINT_DIGITS 20

  18. char *i_to_a(i)
  19.      int i;
  20. {
  21.   /* Room for INT_DIGITS digits, - and '\0' */
  22.   static char buf[INT_DIGITS + 2];
  23.   char *p = buf + INT_DIGITS + 1;        /* points to terminating '\0' */
  24.   if (i >= 0) {
  25.     do {
  26.       *--p = '0' + (i % 10);
  27.       i /= 10;
  28.     } while (i != 0);
  29.     return p;
  30.   }
  31.   else {                        /* i < 0 */
  32.     do {
  33.       *--p = '0' - (i % 10);
  34.       i /= 10;
  35.     } while (i != 0);
  36.     *--p = '-';
  37.   }
  38.   return p;
  39. }

  40. char *ui_to_a(i)
  41.      unsigned int i;
  42. {
  43.   /* Room for UINT_DIGITS digits and '\0' */
  44.   static char buf[UINT_DIGITS + 1];
  45.   char *p = buf + UINT_DIGITS;        /* points to terminating '\0' */
  46.   do {
  47.     *--p = '0' + (i % 10);
  48.     i /= 10;
  49.   } while (i != 0);
  50.   return p;
  51. }
复制代码

  1. /* make every char lowercase */
  2. void lower(char abc[]){
  3.         int ii;
  4.         for(ii=0;ii<=strlen(abc);ii++)
  5.                 abc[ii]=tolower(abc[ii]);
  6. }
复制代码

makefile

  1. bb:bitblade.c ast.c bhttp.c itoa.c
  2.         gcc -o bb bitblade.c ast.c bhttp.c itoa.c -lpthread

复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表