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 "ircd_parse.h"
22 #include "struct_connection.h"
24 #include "IOHandler/IOSockets.h"
28 #include <stdio.h> // @debug
30 static struct Connection *sockets_listening = NULL;
31 static struct Connection *sockets_first = NULL;
32 static struct Connection *sockets_last = NULL;
34 static CONFRELOAD_CALLBACK(sockets_config_reload);
35 static IOSOCKET_CALLBACK(sockets_iohandler_callback);
36 static void sockets_free_connection(struct Connection *sock);
39 reload_config_callback(sockets_config_reload);
40 sockets_config_reload(); /* open all listening sockets */
44 static void sockets_append_list(struct Connection *conn) {
46 conn->next = sockets_listening;
47 sockets_listening = conn;
50 conn->prev = sockets_last;
52 sockets_last->next = conn;
59 static void sockets_remove_list(struct Connection *conn) {
61 struct Connection *prev;
62 if(sockets_listening == conn)
63 sockets_listening = conn->next;
65 for(prev = sockets_listening; prev; prev = prev->next) {
66 if(prev->next == conn)
70 prev->next = conn->next;
74 conn->next->prev = conn->prev;
76 sockets_last = conn->prev;
78 conn->prev->next = conn->next;
80 sockets_first = conn->next;
84 static void sockets_free_connection(struct Connection *conn) {
88 static void sockets_close_listening() {
89 struct Connection *conn;
90 while((conn = sockets_listening)) {
91 sockets_remove_list(conn);
92 iosocket_close(conn->socket);
93 sockets_free_connection(conn);
97 static int sockets_add_listening(struct ConfigPortObject *conf_port, char *bind_addr, char *certfile, char *keyfile, int socket_flags) {
101 struct IOSocket *iosock;
102 if(!conf_port->secure)
103 iosock = iosocket_listen_flags(bind_addr, conf_port->port, sockets_iohandler_callback, socket_flags);
104 else if(certfile && keyfile)
105 iosock = iosocket_listen_ssl_flags(bind_addr, conf_port->port, certfile, keyfile, sockets_iohandler_callback, socket_flags);
112 printf("Added Listener Socket to Port %d.\n", conf_port->port); // @debug
114 struct Connection *connection;
115 connection = calloc(1, sizeof(*connection));
116 connection->listening = 1;
117 connection->socket = iosock;
118 connection->server = conf_port->server;
119 iosock->data = connection;
120 sockets_append_list(connection);
124 static void sockets_accept_client(struct IOSocket *new_client, struct Connection *listener) {
125 struct Connection *connection;
126 connection = calloc(1, sizeof(*connection));
127 connection->socket = new_client;
128 connection->ssl = new_client->ssl;
130 new_client->parse_delimiter = 1;
131 new_client->parse_empty = 0;
132 strcpy((char*) new_client->delimiters, "\r\n");
134 connection->parent = listener;
135 new_client->data = connection;
136 sockets_append_list(connection);
138 client_connected(connection);
141 static CONFRELOAD_CALLBACK(sockets_config_reload) {
142 struct ConfigPortObject *conf_port;
143 char *certfile, *keyfile;
145 sockets_close_listening();
147 certfile = NULL; /* to be continued */
150 for(conf_port = global_config.ports; conf_port; conf_port = conf_port->next) {
151 printf("port: %d\n", conf_port->port); // @debug
153 //add 2 listeners (IPv4 & IPv6) here
154 if(!conf_port->ip4only) {
155 char *bind_addr = conf_port->bind_addr;
158 sockets_add_listening(conf_port, bind_addr, certfile, keyfile, IOSOCKET_ADDR_IPV6);
161 if(!conf_port->ip6only) {
162 char *bind_addr = conf_port->bind_addr;
164 bind_addr = "0.0.0.0";
165 sockets_add_listening(conf_port, bind_addr, certfile, keyfile, IOSOCKET_ADDR_IPV4);
170 static IOSOCKET_CALLBACK(sockets_iohandler_callback) {
171 struct Connection *connection = event->socket->data;
173 switch(event->type) {
174 case IOSOCKETEVENT_ACCEPT:
175 sockets_accept_client(event->data.accept_socket, connection);
178 case IOSOCKETEVENT_NOTCONNECTED:
179 case IOSOCKETEVENT_CLOSED:
180 sockets_remove_list(connection);
181 if(connection->server) {
184 client_disconnected(connection);
186 sockets_free_connection(connection);
189 case IOSOCKETEVENT_DNSFAILED:
190 if(connection->listening) {
191 // listening socket could not be opened
193 sockets_remove_list(connection);
194 client_disconnected(connection);
195 sockets_free_connection(connection);
199 case IOSOCKETEVENT_RECV:
200 if(!connection->authed)
201 parse_unauth_data(connection->data.auth, event->data.recv_str);
202 else if(connection->server)
203 parse_server_data(connection->data.server, event->data.recv_buf);
205 parse_client_data(connection->data.client, event->data.recv_str);
213 void socket_send(struct Connection *conn, char *data, int len) {
214 iosocket_send(conn->socket, data, len);
217 void socket_set_server(struct Connection *conn) {
218 struct IOSocket *iosock = conn->socket;
219 iosock->parse_delimiter = 0;