Librería para enchufes en C.

feat: First commit.

stau.space 17bac48c

+213
.build/client

This is a binary file and will not be displayed.

.build/main

This is a binary file and will not be displayed.

.build/server

This is a binary file and will not be displayed.

+10
LICENSE
··· 1 + This is free and unencumbered software released into the public domain. 2 + 3 + Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. 4 + 5 + In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and 6 + successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. 7 + 8 + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 9 + 10 + For more information, please refer to <http://unlicense.org/>
+19
Makefile
··· 1 + LIB=src/socket.c src/log.c 2 + CFLAGS=-g 3 + 4 + .build/server: 5 + gcc $(GFLAGS) -o .build/server src/server.c $(LIB) 6 + 7 + run-server: .build/server 8 + ./.build/server 9 + 10 + .build/client: 11 + gcc $(CFLAGS) -o .build/client src/client.c $(LIB) 12 + 13 + run-client: .build/client 14 + ./.build/client 15 + 16 + clean: 17 + rm .build/* 18 + 19 + .PHONY: .build/server .build/client
+48
src/client.c
··· 1 + #include "log.h" 2 + #include "socket.h" 3 + #include "utils.h" 4 + #include <stdio.h> 5 + #include <unistd.h> 6 + #include <arpa/inet.h> 7 + 8 + 9 + int main() { 10 + int sock = socket(PF_INET, SOCK_STREAM, 0); 11 + try (sock); 12 + log_info("Created socket."); 13 + 14 + struct sockaddr_in server_addr = { 15 + .sin_family = AF_INET, 16 + .sin_port = htons(42069), 17 + }; 18 + 19 + try (inet_pton(AF_INET, "100.96.176.98", &server_addr.sin_addr)); 20 + 21 + 22 + try (connect(sock, (const struct sockaddr*)&server_addr, sizeof(server_addr))); 23 + log_info("Connected socket to server."); 24 + 25 + /* 26 + * Time to write :3 27 + */ 28 + char buf[0x1000] = {0}; 29 + char msg[0x100] = {0}; 30 + 31 + try (read(sock, buf, 0x1000 - 1)); 32 + printf("[INFO]: Received a message.\n%s\n", buf); 33 + 34 + while (1) { 35 + printf("[INFO]: Sending message:\n>\t"); 36 + fgets(msg, 0x100 - 1, stdin); 37 + msg[strlen(msg) - 1] = '\0'; 38 + send(sock, msg, strlen(msg), 0); 39 + 40 + size_t len = read(sock, buf, 0x1000 - 1); 41 + try (len); 42 + buf[len] = '\0'; 43 + printf("[INFO]: Received a message:\n>\t%s\n", buf); 44 + } 45 + 46 + close(sock); 47 + return 0; 48 + }
+5
src/log.c
··· 1 + #include <stdio.h> 2 + 3 + void log_info(const char* str) { printf("[INFO]: %s\n", str); } 4 + void log_error(const char* str) { printf("[ERROR]: %s\n", str); } 5 + void log_WARN(const char* str) { printf("[WARN]: %s\n", str); }
+7
src/log.h
··· 1 + #ifndef LOG_H_ 2 + #define LOG_H_ 3 + 4 + void log_info(const char* str); 5 + void log_error(const char* str); 6 + void log_WARN(const char* str); 7 + #endif // LOG_H_ header
+7
src/main.c
··· 1 + #include <stdio.h> 2 + #include <sys/socket.h> 3 + 4 + int main() { 5 + printf("Hello there.\n"); 6 + return 0; 7 + }
+50
src/server.c
··· 1 + #include "log.h" 2 + #include "socket.h" 3 + #include "utils.h" 4 + #include <unistd.h> 5 + 6 + 7 + int main() { 8 + int sock = socket(PF_INET, SOCK_STREAM, 0); 9 + 10 + struct sockaddr_in name = { 11 + .sin_family = AF_INET, 12 + .sin_addr.s_addr = htonl(INADDR_LOOPBACK), 13 + .sin_port = htons(42069), 14 + }; 15 + socklen_t addrlen = sizeof(name); 16 + 17 + try (bind(sock, (const struct sockaddr*)&name, sizeof(name))); 18 + 19 + try (listen(sock, 3)); 20 + log_info("Listening on socket."); 21 + 22 + int client = accept(sock, (struct sockaddr*)&name, &addrlen); 23 + log_info("Accepted client connection."); 24 + 25 + if (client == -1) { 26 + fprintf(stderr, "[ERROR]: Client connection: %s\n", strerror(errno)); 27 + return 1; 28 + } 29 + 30 + char msg[] = "[ECHO]: You have succesfully connected to the echo server.\n"; 31 + send(client, msg, strlen(msg), 0); 32 + 33 + /* 34 + * Time to read :3 35 + */ 36 + char buf[0x100] = {0}; 37 + while (1) { 38 + try (read(client, buf, 0x100 - 1)); 39 + if (buf[0] == 'q' && strlen(buf) == 1) break; 40 + send(client, buf, strlen(buf), 0); 41 + size_t len = strlen(buf); 42 + for (size_t i = 0; i < len; ++i) buf[i] = '\0'; 43 + } 44 + 45 + 46 + 47 + close(client); 48 + close(sock); 49 + return 0; 50 + }
+34
src/socket.c
··· 1 + #include <sys/socket.h> 2 + #include <netinet/in.h> 3 + #include "socket.h" 4 + #include "utils.h" 5 + 6 + Socket get_socket(enum SocketType type, uint32_t addr, uint16_t port) { 7 + struct sockaddr_in name = { 8 + .sin_family = AF_INET, 9 + .sin_addr.s_addr = htonl(addr), 10 + .sin_port = htons(port), 11 + }; 12 + 13 + int sock; 14 + switch (type) { 15 + case TCP: { 16 + try (sock = socket(PF_INET, SOCK_STREAM, 0)); 17 + } break; 18 + case UDP: { 19 + try (sock = socket(PF_INET, SOCK_DGRAM, 0)); 20 + } break; 21 + default: { 22 + fprintf(stderr, "[ERROR]: Bad SocketType.\n"); 23 + exit (EXIT_FAILURE); 24 + }; 25 + }; 26 + 27 + try (bind(sock, (const struct sockaddr*)&name, sizeof(name))); 28 + 29 + return (Socket){ 30 + .socket = sock, 31 + .addr = name, 32 + .addrlen = sizeof(name) 33 + }; 34 + }
+18
src/socket.h
··· 1 + #ifndef SOCKET_H_ 2 + #define SOCKET_H_ 3 + #include <stdint.h> 4 + #include <netinet/in.h> 5 + 6 + enum SocketType { 7 + TCP, 8 + UDP, 9 + }; 10 + 11 + typedef struct { 12 + int socket; 13 + struct sockaddr_in addr; 14 + socklen_t addrlen; 15 + } Socket; 16 + 17 + Socket get_socket(enum SocketType type, uint32_t addr, uint16_t port); 18 + #endif // SOCKET_H_ header
+15
src/utils.h
··· 1 + #ifndef UTILS_H_ 2 + #define UTILS_H_ 3 + #include <stdio.h> 4 + #include <stdlib.h> 5 + #include <string.h> 6 + #include <errno.h> 7 + 8 + #define try(a) do { \ 9 + if ((a) < 0) { \ 10 + fprintf(stderr, "[ERROR]: Error connecting socket: %s\n", strerror(errno)); \ 11 + exit (EXIT_FAILURE); \ 12 + } \ 13 + } while(0) 14 + 15 + #endif // UTILS_H_ header