-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtinyserv.c
More file actions
45 lines (38 loc) · 1.58 KB
/
Copy pathtinyserv.c
File metadata and controls
45 lines (38 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Servidor TCP mínimo — escucha en puerto 31337, saluda y hace eco.
// Ctrl+C para parar.
#include "../0x260/0x265_hacking.h"
#include <netinet/in.h>
#define PORT 31337
int main(void) {
int sockfd, new_sockfd, yes = 1, recv_length;
struct sockaddr_in host_addr, client_addr;
socklen_t sin_size;
char buffer[1024];
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) fatal("socket");
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
host_addr.sin_family = AF_INET;
host_addr.sin_port = htons(PORT);
host_addr.sin_addr.s_addr = INADDR_ANY;
memset(&(host_addr.sin_zero), 0, 8);
if (bind(sockfd, (struct sockaddr *) &host_addr, sizeof(struct sockaddr)) == -1)
fatal("bind");
if (listen(sockfd, 5) == -1) fatal("listen");
printf("[*] Escuchando en puerto %d...\n", PORT);
while (1) {
sin_size = sizeof(struct sockaddr_in);
new_sockfd = accept(sockfd, (struct sockaddr *) &client_addr, &sin_size);
if (new_sockfd == -1) { perror("accept"); continue; }
printf("[+] Conexión de %s:%d\n",
inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
send(new_sockfd, "Hola desde tinyserv\n", 20, 0);
recv_length = recv(new_sockfd, buffer, sizeof(buffer), 0);
while (recv_length > 0) {
printf("[recv] %.*s", recv_length, buffer);
send(new_sockfd, buffer, recv_length, 0); // eco
recv_length = recv(new_sockfd, buffer, sizeof(buffer), 0);
}
close(new_sockfd);
}
close(sockfd);
return 0;
}