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