fix typo that made an assignment instead of comparison
[rofl0r-microsocks.git] / server.c
bloba71de3d5111895a8f4f0f04b4e16236178d0a765
1 #include "server.h"
2 #include <stdio.h>
3 #include <unistd.h>
5 int resolve(const char *host, unsigned short port, struct addrinfo** addr) {
6 struct addrinfo hints = {
7 .ai_family = AF_UNSPEC,
8 .ai_socktype = SOCK_STREAM,
9 .ai_flags = AI_PASSIVE,
11 char port_buf[8];
12 snprintf(port_buf, sizeof port_buf, "%u", port);
13 return getaddrinfo(host, port_buf, &hints, addr);
16 int server_waitclient(struct server *server, struct client* client) {
17 socklen_t clen = sizeof client->addr;
18 return ((client->fd = accept(server->fd, (void*)&client->addr, &clen)) == -1)*-1;
21 int server_setup(struct server *server, const char* listenip, unsigned short port) {
22 struct addrinfo *ainfo = 0;
23 if(resolve(listenip, port, &ainfo)) return -1;
24 struct addrinfo* p;
25 int listenfd;
26 for(p = ainfo; p; p = p->ai_next) {
27 if((listenfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) < 0)
28 continue;
29 int yes = 1;
30 setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
31 if(bind(listenfd, p->ai_addr, p->ai_addrlen) < 0) {
32 close(listenfd);
33 listenfd = -1;
34 continue;
36 break;
38 if(listenfd < 0) return -2;
39 freeaddrinfo(ainfo);
40 if(listen(listenfd, SOMAXCONN) < 0) {
41 close(listenfd);
42 return -3;
44 server->fd = listenfd;
45 return 0;