summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--hacking.h25
-rw-r--r--simple_server.c58
2 files changed, 83 insertions, 0 deletions
diff --git a/hacking.h b/hacking.h
index ef8d2c1..2684b6c 100644
--- a/hacking.h
+++ b/hacking.h
@@ -17,3 +17,28 @@ void *ec_malloc(unsigned int size)
fatal("in ec_malloc() on memory allocation");
return ptr;
}
+
+void dump(const char *data_buffer, const unsigned int length) {
+ unsigned char byte;
+ unsigned int i, j;
+
+ for (int i = 0; i < length; i++) {
+ byte = data_buffer[i];
+ printf("%02x ", data_buffer[i]);
+ if ( ((i%16) == 15) || (i==length-1) ) {
+ for (int j = 0; j < 15 - (i%16); j++) {
+ printf(" ");
+ }
+ printf("| ");
+ for (j=(i-(i%16)); j <= i; j++) {
+ byte = data_buffer[j];
+ if ((byte > 31) && (byte < 127)) {
+ printf("%c", byte);
+ } else {
+ printf(".");
+ }
+ }
+ printf("\n");
+ }
+ }
+}
diff --git a/simple_server.c b/simple_server.c
new file mode 100644
index 0000000..574f4ea
--- /dev/null
+++ b/simple_server.c
@@ -0,0 +1,58 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+#include "hacking.h"
+
+#define PORT 7890
+
+int main()
+{
+ int sockfd, new_sockfd;
+ struct sockaddr_in host_addr, client_addr;
+ socklen_t sin_size;
+ int recv_length=1, yes=1;
+ char buffer[1024];
+
+ if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
+ fatal("in socket");
+
+ if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
+ fatal("setting socket option SO_REUSEADDR");
+ }
+
+ host_addr.sin_family = AF_INET;
+ host_addr.sin_port = htons(PORT);
+ host_addr.sin_addr.s_addr = 0;
+ memset(&(host_addr.sin_zero), '\0', 8);
+
+ if (bind(sockfd, (struct sockaddr *)&host_addr, sizeof(struct sockaddr)) == -1) {
+ fatal("binding to socket");
+ }
+
+ if (listen(sockfd, 5) == -1) {
+ fatal("listening on socket");
+ }
+
+ while(1) {
+ sin_size = sizeof(struct sockaddr_in);
+ new_sockfd = accept(sockfd, (struct sockaddr *)&client_addr, &sin_size);
+ if (new_sockfd == -1) {
+ fatal("accepting connection");
+ }
+ printf("server got connection from %s port %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
+ send(new_sockfd, "Hello, world!\n", 13, 0);
+ recv_length = recv(new_sockfd, &buffer, 1024, 0);
+ while(recv_length > 0){
+ printf("RECV: %d bytes\n", recv_length);
+ dump(buffer, recv_length);
+ recv_length = recv(new_sockfd, &buffer, 1024, 0);
+ }
+ close(new_sockfd);
+ }
+
+ return 0;
+}