tcp_portmon: drop useless function pointer cast, use OBJ_ARG
[monky] / src / tcp-portmon.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * tcp-portmon.c - libtcp-portmon hooks
5  *
6  * Copyright (C) 2008 Phil Sutter <Phil@nwl.cc>
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 #include "conky.h"
22 #include "logging.h"
23 #include "tcp-portmon.h"
24 #include "libtcp-portmon.h"
25
26 static tcp_port_monitor_collection_t *pmc = NULL;
27 static tcp_port_monitor_args_t pma;
28
29 int tcp_portmon_init(const char *arg, struct tcp_port_monitor_data *pmd)
30 {
31         int argc, port_begin, port_end, item, connection_index;
32         char itembuf[32];
33
34         memset(itembuf, 0, sizeof(itembuf));
35         connection_index = 0;
36         /* massive argument checking */
37         argc = sscanf(arg, "%d %d %31s %d", &port_begin, &port_end, itembuf,
38                         &connection_index);
39         if ((argc != 3) && (argc != 4)) {
40                 CRIT_ERR(NULL, NULL, "tcp_portmon: requires 3 or 4 arguments");
41         }
42         if ((port_begin < 1) || (port_begin > 65535) || (port_end < 1)
43                         || (port_end > 65535)) {
44                 CRIT_ERR(NULL, NULL, "tcp_portmon: port values must be from 1 to 65535");
45         }
46         if (port_begin > port_end) {
47                 CRIT_ERR(NULL, NULL, "tcp_portmon: starting port must be <= ending port");
48         }
49         if (strncmp(itembuf, "count", 31) == EQUAL) {
50                 item = COUNT;
51         } else if (strncmp(itembuf, "rip", 31) == EQUAL) {
52                 item = REMOTEIP;
53         } else if (strncmp(itembuf, "rhost", 31) == EQUAL) {
54                 item = REMOTEHOST;
55         } else if (strncmp(itembuf, "rport", 31) == EQUAL) {
56                 item = REMOTEPORT;
57         } else if (strncmp(itembuf, "rservice", 31) == EQUAL) {
58                 item = REMOTESERVICE;
59         } else if (strncmp(itembuf, "lip", 31) == EQUAL) {
60                 item = LOCALIP;
61         } else if (strncmp(itembuf, "lhost", 31) == EQUAL) {
62                 item = LOCALHOST;
63         } else if (strncmp(itembuf, "lport", 31) == EQUAL) {
64                 item = LOCALPORT;
65         } else if (strncmp(itembuf, "lservice", 31) == EQUAL) {
66                 item = LOCALSERVICE;
67         } else {
68                 CRIT_ERR(NULL, NULL, "tcp_portmon: invalid item specified");
69         }
70         if ((argc == 3) && (item != COUNT)) {
71                 CRIT_ERR(NULL, NULL, "tcp_portmon: 3 argument form valid only for \"count\" "
72                                 "item");
73         }
74         if ((argc == 4) && (connection_index < 0)) {
75                 CRIT_ERR(NULL, NULL, "tcp_portmon: connection index must be non-negative");
76         }
77         /* ok, args looks good. save the text object data */
78         pmd->port_range_begin = (in_port_t) port_begin;
79         pmd->port_range_end = (in_port_t) port_end;
80         pmd->item = item;
81         pmd->connection_index = connection_index;
82
83         /* if the port monitor collection hasn't been created,
84          * we must create it */
85         if (!pmc) {
86                 pmc = create_tcp_port_monitor_collection();
87                 if (!pmc) {
88                         CRIT_ERR(NULL, NULL, "tcp_portmon: unable to create port monitor "
89                                         "collection");
90                 }
91         }
92
93         /* if a port monitor for this port does not exist,
94          * create one and add it to the collection */
95         if (find_tcp_port_monitor(pmc, port_begin, port_end) == NULL) {
96                 tcp_port_monitor_t *p_monitor = create_tcp_port_monitor(port_begin,
97                                 port_end, &pma);
98
99                 if (!p_monitor) {
100                         CRIT_ERR(NULL, NULL, "tcp_portmon: unable to create port monitor");
101                 }
102                 /* add the newly created monitor to the collection */
103                 if (insert_tcp_port_monitor_into_collection(pmc, p_monitor) != 0) {
104                         CRIT_ERR(NULL, NULL, "tcp_portmon: unable to add port monitor to "
105                                         "collection");
106                 }
107         }
108         return 0;
109 }
110
111 int tcp_portmon_action(char *p, int p_max_size, struct tcp_port_monitor_data *pmd)
112 {
113         tcp_port_monitor_t *p_monitor;
114
115         /* grab a pointer to this port monitor */
116         p_monitor = find_tcp_port_monitor(pmc, pmd->port_range_begin,
117                                           pmd->port_range_end);
118
119         if (!p_monitor) {
120                 snprintf(p, p_max_size, "monitor not found");
121                 return 1;
122         }
123
124         /* now grab the text of interest */
125         if (peek_tcp_port_monitor(p_monitor, pmd->item,
126                                 pmd->connection_index, p, p_max_size) != 0) {
127                 snprintf(p, p_max_size, "monitor peek error");
128                 return 1;
129         }
130         return 0;
131 }
132
133 void tcp_portmon_update(void)
134 {
135         update_tcp_port_monitor_collection(pmc);
136 }
137
138 int tcp_portmon_clear(void)
139 {
140         destroy_tcp_port_monitor_collection(pmc);
141         pmc = NULL;
142         return 0;
143 }
144
145 int tcp_portmon_set_max_connections(int max)
146 {
147         if (max <= 0) {
148                 pma.max_port_monitor_connections =
149                         MAX_PORT_MONITOR_CONNECTIONS_DEFAULT;
150         } else {
151                 pma.max_port_monitor_connections = max;
152         }
153         return (max < 0) ? 1 : 0;
154 }
155