Created
February 17, 2018 05:29
-
-
Save tjhv/9e87210b1270ff4c6af97980822ea71e to your computer and use it in GitHub Desktop.
Old snippet from back in the day.
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
/* | |
* @Author tjhv | |
* @Version 1.0.1 | |
* @year 2009 | |
**/ | |
#include <arpa/inet.h> | |
#include <netinet/in.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <sys/param.h> | |
#include <sys/socket.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
#include <stdarg.h> | |
struct ServerEngine | |
{ | |
int sockfd; | |
struct sockaddr_in s; | |
int port; | |
int eid; | |
int backlog; | |
signed char isActive; | |
}; | |
struct ClientEngine | |
{ | |
int sockfd; | |
struct sockaddr_in s; | |
int eid; | |
size_t len; | |
char *ip; | |
}; | |
int create_sockfd(struct ServerEngine serverEngine) | |
{ | |
serverEngine.sockfd = socket(AF_INET, SOCK_STREAM, 0); | |
return serverEngine.sockfd; | |
} | |
struct sockaddr_in create_struct(struct ServerEngine serverEngine) | |
{ | |
serverEngine.s.sin_family = AF_INET; | |
serverEngine.s.sin_port = htons(serverEngine.port); | |
serverEngine.s.sin_addr.s_addr = htonl(INADDR_ANY); | |
return serverEngine.s; | |
} | |
int _bind(struct ServerEngine serverEngine) | |
{ | |
serverEngine.eid = bind(serverEngine.sockfd, (struct sockaddr *) &serverEngine.s, sizeof(serverEngine.s)); | |
return serverEngine.eid; | |
} | |
int _listen(struct ServerEngine serverEngine) | |
{ | |
serverEngine.eid = listen(serverEngine.sockfd, serverEngine.backlog); | |
return serverEngine.eid; | |
} | |
int _send(struct ClientEngine clientEngine, char *buf, ...) | |
{ | |
char buff[1024]; | |
va_list ap; | |
va_start(ap, buf); | |
vsprintf(buf, buf, ap); | |
va_end(ap); | |
clientEngine.eid = send(clientEngine.sockfd, buff, strlen(buff), 0); | |
return clientEngine.eid; | |
} | |
void _run(struct ServerEngine serverEngine) | |
{ | |
while (serverEngine.isActive) | |
{ | |
struct ClientEngine clientEngine; | |
clientEngine.len = sizeof(clientEngine.s); | |
clientEngine.eid = accept(serverEngine.sockfd, (struct sockaddr *) &clientEngine.s, &clientEngine.len); | |
if (clientEngine.eid >= 0) | |
{ | |
clientEngine.sockfd = clientEngine.eid; | |
clientEngine.ip = inet_ntoa(clientEngine.s.sin_addr); | |
_send(clientEngine, "Your IP Address is %s.\r\n", clientEngine.ip); | |
/* Check if the IP is banned using CIDR. If so, close the connection. */ | |
/* Assign a slot for the client and pass it to the Engine. */ | |
close(clientEngine.sockfd); | |
} | |
} | |
} | |
int main(void) | |
{ | |
/* Need to make a better method for this? Perhaps it shouldn't be in the main function.. */ | |
struct ServerEngine s; | |
s.port = 40000; | |
s.backlog = 3; | |
s.isActive = 1; | |
create_sockfd(s); | |
create_struct(s); | |
_bind(s); | |
_listen(s); | |
_run(s); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment