1 /* ircd_client.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/>.
22 #include "IOHandler/IOSockets.h"
23 #include "ircd_parse.h"
25 #include "struct_client.h"
26 #include "struct_auth.h"
27 #include "struct_servermsg.h"
29 typedef int cmd_client_t(struct Client *client, char *argv[], int argc);
30 typedef int cmd_auth_t(struct Auth *auth, char *argv[], int argc);
31 typedef int cmd_server_t(struct Server *server, char *argv[], int argc);
33 static struct ServerMsg parse_static_srvmsg;
35 #define PARSE_CLIFLAG_OPONLY 0x01
37 //include all commands
47 cmd_client_t *function;
48 unsigned int min_argc : 8;
49 unsigned int max_argc : 8;
50 unsigned int flags : 8;
54 unsigned int min_argc : 8;
55 unsigned int max_argc : 8;
56 unsigned int flags : 8;
59 cmd_server_t *function;
60 unsigned int min_argc : 8;
61 unsigned int max_argc : 8;
62 unsigned int flags : 8;
65 } parse_command_list[] = {
66 {{"PING", "P"}, /* Ping Command */
67 {NULL, 0, 1, 0}, /* Client */
68 {NULL, 0, 1, 0}, /* Unauthed */
69 {NULL, 0, 1, 0} /* Server */
71 {{"PASS", NULL}, /* PASS Command */
72 {NULL, 0, 1, 0}, /* Client */
73 {NULL, 0, 1, 0}, /* Unauthed */
74 {NULL, 0, 1, 0} /* Server */
76 {{"NICK", "N"}, /* Nick Command */
77 {cmd_nick_cli, 1, 1, 0}, /* Client */
78 {cmd_nick_auth, 1, 1, 0}, /* Unauthed */
79 {NULL, 0, 1, 0} /* Server */
81 {{"USER", "U"}, /* User Command */
82 {NULL, 0, 1, 0}, /* Client */
83 {NULL, 0, 1, 0}, /* Unauthed */
84 {NULL, 0, 1, 0} /* Server */
87 {{NULL, NULL},{NULL, 0, 0, 0},{NULL, 0, 0, 0},{NULL, 0, 0, 0}}
90 static char *parse_irc_token(char **data) {
94 for(i = 0; *data[i]; i++) {
104 static char **parse_irc_params(char *data, int *argc, int maxargs) {
109 char **argv = calloc(maxargs, sizeof(*argv));
111 //skip leading spaces
115 //the rest is a single parameter
116 argv[*argc++] = data + 1;
119 argv[*argc++] = data;
120 if (*argc >= maxargs)
122 while (*data != ' ' && *data)
128 /* Client Data Format
129 * <TOKEN> <arg1> <arg2>
131 void parse_client_data(struct Client *client, char *data) {
132 char *token = parse_irc_token(&data);
135 for(i = 0; (parse_command_list[i].tokens.client || parse_command_list[i].tokens.server); i++) {
136 if(stricmp(parse_command_list[i].tokens.client, token)) {
141 if(found && !parse_command_list[i].client.function) {
142 // reply 462 You may not reregister
145 char **argv = parse_irc_params(data, &argc, parse_command_list[i].client.max_argc);
146 if(argc < parse_command_list[i].client.min_argc) {
147 // reply 461 Not enough parameters
149 int ret = parse_command_list[i].client.function(client, argv, argc);
157 // reply 421 Unknown command
161 void parse_unauth_data(struct Auth *auth, char *data) {
162 char *token = parse_irc_token(&data);
165 for(i = 0; (parse_command_list[i].tokens.client || parse_command_list[i].tokens.server); i++) {
166 if(stricmp(parse_command_list[i].tokens.client, token)) {
171 if(found && !parse_command_list[i].auth.function) {
172 // reply 451 Register first.
175 char **argv = parse_irc_params(data, &argc, parse_command_list[i].auth.max_argc);
176 if(argc < parse_command_list[i].auth.min_argc) {
177 // reply 461 Not enough parameters
179 int ret = parse_command_list[i].auth.function(auth, argv, argc);
187 // reply 421 Unknown command
191 /* Unicast Multicast Broadcast
192 * <src-srvnum> 4 Bytes <source> 4 Bytes <source> 4 Bytes
193 * <length> 2 Bytes <length> 2 Bytes <length> 2 Bytes
194 * <flags> 1 Byte <flags> 1 Byte <flags> 1 Byte
195 * <token> x Bytes <token> x Bytes <token> x Bytes
196 * " " 1 Byte " " 1 Byte " " 1 Byte
197 * <dst-srvnum> 4 Bytes <servers> 1 Byte <ttl> 1 Byte
198 * <dst-srv01> 4 Bytes
199 * <dst-srv02> 4 Bytes
200 * <args> <args> <args>
202 void parse_server_data(struct Server *server, struct IOSocketBuffer *buffer) {
203 struct ServerMsg *srvmsg = &parse_static_srvmsg;
204 unsigned char *buf = (unsigned char *)buffer->buffer;
205 unsigned int buflen, require_more = 0;
208 while(buffer->bufpos >= 9 && !require_more) { //require at least 9 Bytes
209 srvmsg->source.srvnum = ((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
210 srvmsg->resolved_source = 0;
211 srvmsg->msglen = ((buf[4] << 8) | buf[5]);
212 srvmsg->msgflags = buf[6];
213 srvmsg->msgtype = (buf[6] & SERVERMSG_TYPEMASK);
215 if(buffer->bufpos < srvmsg->msglen) {
220 for(i = 0; buffer->bufpos > i+7; i++) {
221 if(buf[i+7] == ' ') {
222 if(i >= MAXSERVERTOKENLEN)
223 srvmsg->token[MAXSERVERTOKENLEN] = '\0';
225 srvmsg->token[i] = '\0';
228 if(i >= MAXSERVERTOKENLEN)
230 srvmsg->token[i] = buf[i+7];
232 if(buf[i+7] != ' ') {
237 buflen = buffer->bufpos - (i+8);
239 switch(srvmsg->msgtype) {
240 case SERVERMSG_TYPE_UNICAST:
245 srvmsg->destinations = malloc(sizeof(struct ServerMsgDstMap));
246 srvmsg->destinations->dstcount = 1;
247 srvmsg->destinations->dst[0].destination.srvnum = ((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
248 srvmsg->destinations->dst[0].resolved_destination = 0;
250 case SERVERMSG_TYPE_MULTICAST:
255 unsigned int srvcount = buf[0];
258 if(buflen < (srvcount * 4)) {
262 srvmsg->destinations = malloc(sizeof(struct ServerMsgDstMap) + (sizeof(struct ServerMsgDestination) * (srvcount - 1)));
263 srvmsg->destinations->dstcount = srvcount;
264 for(i = 0; i < srvcount; i++) {
265 srvmsg->destinations->dst[i].destination.srvnum = ((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
266 srvmsg->destinations->dst[i].resolved_destination = 0;
271 case SERVERMSG_TYPE_BROADCAST:
276 srvmsg->destinations = NULL;
277 srvmsg->msgttl = buf[0];
283 goto parse_server_data_finish;
286 srvmsg->arglen = srvmsg->msglen - (buf - (unsigned char *)buffer->buffer);
290 for(i = 0; (parse_command_list[i].tokens.client || parse_command_list[i].tokens.server); i++) {
291 if(stricmp(parse_command_list[i].tokens.server, srvmsg->token)) {
297 goto parse_server_data_finish;
299 //exec command function
302 parse_server_data_finish:
303 if(srvmsg->destinations)
304 free(srvmsg->destinations);
306 if(srvmsg->msglen > buffer->bufpos) {
307 memmove(buffer->buffer, buffer->buffer + srvmsg->msglen, srvmsg->msglen - buffer->bufpos);
308 buffer->bufpos -= srvmsg->msglen;