]> jfr.im git - irc/rqf/shadowircd.git/blob - servlink/control.c
[svn] - the new plan:
[irc/rqf/shadowircd.git] / servlink / control.c
1 /************************************************************************
2 * IRC - Internet Relay Chat, servlink/servlink.c
3 *
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 1, or (at your option)
7 * any later version.
8 *
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.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 *
18 * $Id: control.c 1285 2006-05-05 15:03:53Z nenolod $
19 */
20
21 #include "setup.h"
22
23 #include <sys/types.h>
24
25 #include <assert.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <string.h>
29 #ifdef HAVE_LIBZ
30 #include <zlib.h>
31 #endif
32
33 #include "servlink.h"
34 #include "io.h"
35 #include "control.h"
36
37 static cmd_handler cmd_set_zip_out_level;
38 static cmd_handler cmd_start_zip_out;
39 static cmd_handler cmd_start_zip_in;
40 static cmd_handler cmd_init;
41
42 struct command_def command_table[] = {
43 {CMD_SET_ZIP_OUT_LEVEL, cmd_set_zip_out_level, COMMAND_FLAG_DATA},
44 {CMD_START_ZIP_OUT, cmd_start_zip_out, 0},
45 {CMD_START_ZIP_IN, cmd_start_zip_in, 0},
46 {CMD_INJECT_RECVQ, process_recvq, COMMAND_FLAG_DATA},
47 {CMD_INJECT_SENDQ, process_sendq, COMMAND_FLAG_DATA},
48 {CMD_INIT, cmd_init, 0},
49 {CMD_ZIPSTATS, send_zipstats, 0},
50 {0, 0, 0}
51 };
52
53 void
54 cmd_set_zip_out_level(struct ctrl_command *cmd)
55 {
56 #ifdef HAVE_LIBZ
57 out_state.zip_state.level = *cmd->data;
58 if((out_state.zip_state.level < -1) || (out_state.zip_state.level > 9))
59 send_error("invalid compression level %d", out_state.zip_state.level);
60 #else
61 send_error("can't set compression level - no libz support!");
62 #endif
63 }
64
65 void
66 cmd_start_zip_out(struct ctrl_command *cmd)
67 {
68 #ifdef HAVE_LIBZ
69 int ret;
70
71 if(out_state.zip)
72 send_error("can't start compression - already started!");
73
74 out_state.zip_state.z_stream.total_in = 0;
75 out_state.zip_state.z_stream.total_out = 0;
76 out_state.zip_state.z_stream.zalloc = (alloc_func) 0;
77 out_state.zip_state.z_stream.zfree = (free_func) 0;
78 out_state.zip_state.z_stream.data_type = Z_ASCII;
79
80 if(out_state.zip_state.level <= 0)
81 out_state.zip_state.level = Z_DEFAULT_COMPRESSION;
82
83 if((ret = deflateInit(&out_state.zip_state.z_stream, out_state.zip_state.level)) != Z_OK)
84 send_error("deflateInit failed: %s", zError(ret));
85
86 out_state.zip = 1;
87 #else
88 send_error("can't start compression - no libz support!");
89 #endif
90 }
91
92 void
93 cmd_start_zip_in(struct ctrl_command *cmd)
94 {
95 #ifdef HAVE_LIBZ
96 int ret;
97
98 if(in_state.zip)
99 send_error("can't start decompression - already started!");
100
101 in_state.zip_state.z_stream.total_in = 0;
102 in_state.zip_state.z_stream.total_out = 0;
103 in_state.zip_state.z_stream.zalloc = (alloc_func) 0;
104 in_state.zip_state.z_stream.zfree = (free_func) 0;
105 in_state.zip_state.z_stream.data_type = Z_ASCII;
106 if((ret = inflateInit(&in_state.zip_state.z_stream)) != Z_OK)
107 send_error("inflateInit failed: %s", zError(ret));
108 in_state.zip = 1;
109 #else
110 send_error("can't start decompression - no libz support!");
111 #endif
112 }
113
114
115 void
116 cmd_init(struct ctrl_command *cmd)
117 {
118 if(in_state.active || out_state.active)
119 send_error("CMD_INIT sent twice!");
120
121 in_state.active = 1;
122 out_state.active = 1;
123 CONTROL.read_cb = read_ctrl;
124 CONTROL.write_cb = NULL;
125 LOCAL.read_cb = read_data;
126 LOCAL.write_cb = NULL;
127 REMOTE.read_cb = read_net;
128 REMOTE.write_cb = NULL;
129 }