Created
May 6, 2012 10:37
-
-
Save pstiasny/2621562 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <arpa/inet.h> | |
#include <unistd.h> | |
char* logo = | |
" ____ ____ ____ ____\n" | |
" /\\ \\/\\ \\/\\ \\/\\ \\\n" | |
" / \\___\\ \\___\\ \\___\\ \\___\\\n" | |
" \\ / __/_/ / / / / /\n" | |
" \\/_/\\ \\__/\\/___/\\/___/\n" | |
" / \\___\\ / \\___\\\n" | |
" \\ / __/_ _\\ / /\n" | |
" \\/_/\\ \\/\\ \\/___/\n" | |
" / \\__/ \\___\\\n" | |
" \\ / _\\ / /\n" | |
" \\/_/\\ \\/___/\n" | |
" / \\___\\\n" | |
" \\ / /\n" | |
" \\/___/\n"; | |
void telnet_session(FILE* in, FILE* out) { | |
fputs("\e[0;44m\e[2J\e[H", out); | |
fputs("\e[30m", out); | |
fputs(logo, out); | |
fputs("\e[3;40HLogin:\e[4;40H", out); | |
fflush(out); | |
char buf[256]; | |
fscanf(in, "%s", buf); | |
fprintf(out, "\e[4;40H\e[K\e[3;40H\e[0KHello %s!", buf); | |
fflush(out); | |
sleep(3); | |
fprintf(out, "\e[0m\e[2J\e[HGoodbye!\n"); | |
fflush(out); | |
fclose(out); | |
fclose(in); | |
} | |
int main() { | |
struct sockaddr_in sa, sr; | |
socklen_t addrsize = sizeof(sr); | |
memset(&sa, 0, sizeof(sa)); | |
int sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); | |
if (sock == -1) return 1; | |
int yes = 1; | |
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) { | |
perror("setsockopt"); return 4; } | |
sa.sin_family = AF_INET; | |
sa.sin_port = htons(23000); | |
sa.sin_addr.s_addr = inet_addr("127.0.0.1"); | |
if (bind(sock, (struct sockaddr*)&sa, sizeof(sa)) == -1) { perror("bind"); return 2; } | |
if (listen(sock, 5) == -1) return 3; | |
for(;;) { | |
int in_sock = accept(sock, (struct sockaddr*)&sr, &addrsize); | |
if (in_sock == -1) { perror("accept"); return 7; } | |
int pid = fork(); | |
if (pid == 0) { | |
FILE *sinfile, *soutfile; | |
sinfile = fdopen(in_sock, "r"); | |
soutfile = fdopen(in_sock, "w"); | |
if (sinfile == 0 || soutfile == 0) { perror("fdopen"); return 5; } | |
telnet_session(sinfile, soutfile); | |
close(in_sock); | |
return 0; | |
} else | |
close(in_sock); | |
} | |
close(sock); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment