1 /* ServerSocket.c - TransparentIRC 0.1
2 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "ServerSocket.h"
18 #include "IOHandler.h"
19 #include "UserClient.h"
21 static struct ServerSocket *serversockets = NULL;
23 static void serversocket_callback(struct IOEvent *event);
25 static struct ServerSocket *serversocket_create(struct IODescriptor *iofd) {
26 struct ServerSocket *sock = malloc(sizeof(*sock));
28 sock->clientcount = 0;
30 //add ServerSocket to the list
32 sock->next = serversockets;
33 serversockets->prev = sock;
37 static void serversocket_delete(struct ServerSocket *server) {
39 server->prev->next = server->next;
41 serversockets = server->next;
43 server->next->prev = server->prev;
47 struct ServerSocket *serversocket_listen(char *hostname, int port) {
48 struct IODescriptor *iofd = iohandler_listen(hostname, port, serversocket_callback);
50 struct ServerSocket *server = serversocket_create(iofd);
56 void serversocket_close(struct ServerSocket *server, int keep_clients) {
57 userclient_close_server(server, keep_clients);
58 iohandler_close(server->iofd);
59 serversocket_delete(server);
62 static void serversocket_callback(struct IOEvent *event) {
65 struct ServerSocket *server = NULL;
66 for(server = serversockets; server; server = server->next) {
67 if(server->iofd == event->iofd)
71 userclient_accepted(server, event->accept_fd)