Last active
January 10, 2019 15:29
-
-
Save PanagiotisPtr/c558b2561b58a7410378f2c19864e5a5 to your computer and use it in GitHub Desktop.
Simple sockets server wrapper in C
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> | |
#define DEBUG | |
#include "server.h" | |
int main(int argc, char **argv) { | |
server s; | |
server_init(&s, AF_INET, SOCK_STREAM,\ | |
IPPROTO_TCP, 1313, INADDR_ANY); | |
server_listen(&s, 15); | |
while(1){ | |
server_accept_connection(&s); | |
server_close_connection(&s); | |
} | |
return 0; | |
} |
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
/* | |
MIT License | |
Copyright (c) 2018 Panagiotis Petridis | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
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 OR COPYRIGHT HOLDERS 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. | |
*/ | |
/* | |
Todo: remove unnecessery headers | |
*/ | |
#include <stdio.h> | |
#include <string.h> | |
#include <sys/types.h> | |
#include <netinet/in.h> | |
#include <netdb.h> | |
#include <fcntl.h> | |
#include <unistd.h> | |
#include <errno.h> | |
#include <sys/socket.h> | |
#include <errno.h> | |
#include <stdlib.h> | |
#define handle_error(msg) \ | |
do { perror(msg); exit(EXIT_FAILURE); } while(0) | |
typedef struct server { | |
int server_conn; | |
int client_conn; | |
struct sockaddr_in server_sock_address; | |
struct sockaddr_in client_sock_address; | |
} server; | |
/* | |
HELPER FUNCTIONS | |
*/ | |
int safe_socket(int domain, int type, int protocol) { | |
int rv = socket(domain, type, protocol); | |
if(rv == -1) | |
handle_error("socket"); | |
return rv; | |
} | |
int safe_bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { | |
int rv = bind(sockfd, addr, addrlen); | |
if(rv == -1) | |
handle_error("bind"); | |
return rv; | |
} | |
int safe_listen(int sockfd, int backlog) { | |
int rv = listen(sockfd, backlog); | |
if(rv == -1) | |
handle_error("listen"); | |
return rv; | |
} | |
int safe_accept(int sockfd, struct sockaddr* addr, socklen_t *addrlen) { | |
int rv = accept(sockfd, addr, addrlen); | |
if(rv == -1) | |
handle_error("accept"); | |
return rv; | |
} | |
int safe_close(int sockfd) { | |
int rv = close(sockfd); | |
if(rv == -1) | |
handle_error("safe_close"); | |
return rv; | |
} | |
/* | |
INTERFACE | |
*/ | |
/* | |
load address using inet or pass INADDR_ANY to accept all connecitons | |
*/ | |
void server_init(server* s, int domain, int type, int protocol,\ | |
unsigned port, unsigned long address) { | |
s->server_conn = safe_socket(domain, type, protocol); | |
memset(&s->server_sock_address, 0, sizeof(struct sockaddr_in)); | |
s->server_sock_address.sin_family = domain; | |
s->server_sock_address.sin_port = htons(port); | |
s->server_sock_address.sin_addr.s_addr = address; | |
safe_bind(s->server_conn, (struct sockaddr*)&(s->server_sock_address), \ | |
sizeof(struct sockaddr_in)); | |
} | |
void server_listen(server* s, int backlog) { | |
safe_listen(s->server_conn, backlog); | |
} | |
/* | |
Accepts next connection. | |
If there is no connection it halts execution | |
*/ | |
void server_accept_connection(server* s) { | |
unsigned size = sizeof(struct sockaddr_in); | |
s->client_conn = safe_accept(s->server_conn,\ | |
(struct sockaddr*)&(s->client_sock_address),\ | |
&size); | |
#ifdef DEBUG | |
printf("Conenction from %x port %d\n",\ | |
ntohl(s->client_sock_address.sin_addr.s_addr),\ | |
ntohs(s->client_sock_address.sin_port)); | |
#endif | |
} | |
void server_close_connection(server* s) { | |
safe_close(s->client_conn); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment