a nice connection demo from an irc user
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Dec 2008 17:58:03 +0100
changeset 10 f19f44e2e863
parent 9 c3fecc82ade6
child 11 0a8be4eac87e
a nice connection demo from an irc user committer: Markus Bröker <mbroeker@largo.homelinux.org>
Makefile
connection.c
ddos/client.c
ddos/server.c
ddos/set_limit.c
--- a/Makefile
+++ b/Makefile
@@ -38,7 +38,8 @@
 	database \
 	gauss \
 	mem2swap \
-	prog_limit
+	prog_limit \
+	connection
 
 .SUFFIXES: .c .cc .asm
 
@@ -210,6 +211,10 @@
 	@echo Linking $< ...
 	@$(CPP) -o $@ $<
 
+connection: connection.o
+	@echo Linking $< ...
+	@$(CPP) -o $@ $<
+
 .PHONY: clean uninstall
 
 clean:
new file mode 100644
--- /dev/null
+++ b/connection.c
@@ -0,0 +1,71 @@
+/**
+ * test/demos/connection.c
+ * Copyright (C) 2008 mussolini
+ * Copyright (C) 2008 mbroeker
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+#include <netdb.h>
+#include <arpa/inet.h>
+
+int connection (char *ip, unsigned short port)
+{
+    struct hostent *hs;
+    struct sockaddr_in sock;
+    int sockfd;
+
+    memset (&sock, 0, sizeof (sock));
+    sock.sin_family = AF_INET;
+    sock.sin_port = htons (port);
+
+    if ((sock.sin_addr.s_addr = inet_addr (ip)) == -1) {
+        if ((hs = gethostbyname (ip)) == NULL) {
+            perror ("[-] Error");
+            return -1;
+        }
+        sock.sin_family = hs->h_addrtype;
+        memcpy (&sock.sin_addr.s_addr, hs->h_addr, hs->h_length);
+    }
+
+    if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
+        perror ("[-] Error");
+        return -1;
+    }
+
+    if (connect (sockfd, (struct sockaddr *)&sock, sizeof (sock)) < 0) {
+        perror ("[-] Error ");
+        return -1;
+    }
+
+    return (sockfd);
+}
+
+int main (int argc, char **argv)
+{
+    char buffer[1024];
+    int sockfd;
+    int num;
+
+    if (argc != 3) {
+        printf ("Usage: %s <ipaddr> <port>\n", argv[0]);
+        return EXIT_SUCCESS;
+    }
+
+    if ((sockfd = connection (argv[1], atoi (argv[2]))) < 0) {
+        printf ("Connection error: %s:%d\n", argv[1], atoi (argv[2]));
+        return EXIT_FAILURE;
+    }
+
+    write (sockfd, "GET /\r\n", 7);
+
+    while ((num = read (sockfd, buffer, 1023)) != 0) {
+        buffer[num] = 0;
+        printf ("%s", buffer);
+    }
+
+    return close (sockfd);
+}
--- a/ddos/client.c
+++ b/ddos/client.c
@@ -46,6 +46,5 @@
         }
     }
 
-    close (client_socket);
-    return 0;
+    return close (client_socket);
 }
--- a/ddos/server.c
+++ b/ddos/server.c
@@ -100,6 +100,6 @@
             printf ("Starting LINK [%04d] %04d\n", pid, links++);
         }
     }
-    close (server_socket);
-    return 0;
+
+    return close (server_socket);
 }
--- a/ddos/set_limit.c
+++ b/ddos/set_limit.c
@@ -11,5 +11,6 @@
 
     rlim.rlim_cur = processes;
     rlim.rlim_max = processes + 24;
+
     return setrlimit (RLIMIT_NPROC, &rlim);
 }