1 /* ircd_sock.c - NextIRCd
2 * Copyright (C) 2012-2013 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/>.
18 #include "ircd_config.h"
19 #include "ircd_sock.h"
20 #include "ircd_client.h"
21 #include "struct_connection.h"
23 #include "IOHandler/IOSockets.h"
27 #include <stdio.h> // @debug
29 static struct Connection *sockets_listening = NULL;
30 static struct Connection *sockets_first = NULL;
31 static struct Connection *sockets_last = NULL;
33 static CONFRELOAD_CALLBACK(sockets_config_reload);
34 static IOSOCKET_CALLBACK(sockets_iohandler_callback);
35 static void sockets_free_connection(struct Connection *sock);
38 reload_config_callback(sockets_config_reload);
39 sockets_config_reload(); /* open all listening sockets */
43 static void sockets_append_list(struct Connection *conn) {
45 conn->next = sockets_listening;
46 sockets_listening = conn;
49 conn->prev = sockets_last;
51 sockets_last->next = conn;
58 static void sockets_remove_list(struct Connection *conn) {
60 struct Connection *prev;
61 if(sockets_listening == conn)
62 sockets_listening = conn->next;
64 for(prev = sockets_listening; prev; prev = prev->next) {
65 if(prev->next == conn)
69 prev->next = conn->next;
73 conn->next->prev = conn->prev;
75 sockets_last = conn->prev;
77 conn->prev->next = conn->next;
79 sockets_first = conn->next;
83 static void sockets_free_connection(struct Connection *conn) {
87 static void sockets_close_listening() {
88 struct Connection *conn;
89 while((conn = sockets_listening)) {
90 sockets_remove_list(conn);
91 iosocket_close(conn->socket);
92 sockets_free_connection(conn);
96 static int sockets_add_listening(struct ConfigPortObject *conf_port, char *bind_addr, char *certfile, char *keyfile, int socket_flags) {
100 struct IOSocket *iosock;
101 if(!conf_port->secure)
102 iosock = iosocket_listen_flags(bind_addr, conf_port->port, sockets_iohandler_callback, socket_flags);
103 else if(certfile && keyfile)
104 iosock = iosocket_listen_ssl_flags(bind_addr, conf_port->port, certfile, keyfile, sockets_iohandler_callback, socket_flags);
111 printf("Added Listener Socket to Port %d.\n", conf_port->port); // @debug
113 struct Connection *connection;
114 connection = calloc(1, sizeof(*connection));
115 connection->listening = 1;
116 connection->socket = iosock;
117 connection->server = conf_port->server;
118 iosock->data = connection;
119 sockets_append_list(connection);
123 static void sockets_accept_client(struct IOSocket *new_client, struct Connection *listener) {
124 struct Connection *connection;
125 connection = calloc(1, sizeof(*connection));
126 connection->socket = new_client;
127 connection->ssl = new_client->ssl;
129 new_client->parse_delimiter = 1;
130 new_client->parse_empty = 0;
131 strcpy((char*) new_client->delimiters, "\r\n");
133 connection->parent = listener;
134 new_client->data = connection;
135 sockets_append_list(connection);
137 client_connected(connection);
140 static CONFRELOAD_CALLBACK(sockets_config_reload) {
141 struct ConfigPortObject *conf_port;
142 char *certfile, *keyfile;
144 sockets_close_listening();
146 certfile = NULL; /* to be continued */
149 for(conf_port = global_config.ports; conf_port; conf_port = conf_port->next) {
150 printf("port: %d\n", conf_port->port); // @debug
152 //add 2 listeners (IPv4 & IPv6) here
153 if(!conf_port->ip4only) {
154 char *bind_addr = conf_port->bind_addr;
157 sockets_add_listening(conf_port, bind_addr, certfile, keyfile, IOSOCKET_ADDR_IPV6);
160 if(!conf_port->ip6only) {
161 char *bind_addr = conf_port->bind_addr;
163 bind_addr = "0.0.0.0";
164 sockets_add_listening(conf_port, bind_addr, certfile, keyfile, IOSOCKET_ADDR_IPV4);
169 static IOSOCKET_CALLBACK(sockets_iohandler_callback) {
170 struct Connection *connection = event->socket->data;
172 switch(event->type) {
173 case IOSOCKETEVENT_ACCEPT:
174 sockets_accept_client(event->data.accept_socket, connection);
177 case IOSOCKETEVENT_NOTCONNECTED:
178 case IOSOCKETEVENT_CLOSED:
179 sockets_remove_list(connection);
180 if(connection->server) {
183 client_disconnected(connection);
185 sockets_free_connection(connection);
188 case IOSOCKETEVENT_DNSFAILED:
189 if(connection->listening) {
190 // listening socket could not be opened
192 sockets_remove_list(connection);
193 client_disconnected(connection);
194 sockets_free_connection(connection);
198 case IOSOCKETEVENT_RECV:
199 if(connection->server) {
202 client_recv(connection, event->data.recv_str);
211 void socket_send(struct Connection *conn, char *data, int len) {
212 iosocket_send(conn->socket, data, len);