Debian Codegraph @

contrib/mod_snmp/packet.c

raw symbols SCIP
1 /*
2 * ProFTPD - mod_snmp packet routines
3 * Copyright (c) 2008-2015 TJ Saunders
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, TJ Saunders and other respective copyright holders
20 * give permission to link this program with OpenSSL, and distribute the
21 * resulting executable, without including the source code for OpenSSL in the
22 * source distribution.
23 */
24
25 #include "mod_snmp.h"
26 #include "packet.h"
27 #include "db.h"
28
29 static const char *trace_channel = "snmp";
30
31 struct snmp_packet *snmp_packet_create(pool *p) {
32 struct snmp_packet *pkt;
33 pool *sub_pool;
34
35 sub_pool = pr_pool_create_sz(p, 128);
36 pr_pool_tag(sub_pool, "SNMP packet pool");
37
38 pkt = pcalloc(sub_pool, sizeof(struct snmp_packet));
39 pkt->pool = sub_pool;
40
41 /* Allocate the request data buffer for now; leave the response data
42 * buffer to be allocated later.
43 */
44
45 pkt->req_datalen = SNMP_PACKET_MAX_LEN;
46 pkt->req_data = palloc(sub_pool, pkt->req_datalen);
47
48 pkt->resp_datalen = SNMP_PACKET_MAX_LEN;
49 pkt->resp_data = palloc(sub_pool, pkt->resp_datalen);
50
51 return pkt;
52 }
53
54 int snmp_packet_write(pool *p, int sockfd, struct snmp_packet *pkt) {
55 int res;
56 fd_set writefds;
57 struct timeval tv;
58
59 if (sockfd < 0) {
60 errno = EINVAL;
61 return -1;
62 }
63
64 FD_ZERO(&writefds);
65 FD_SET(sockfd, &writefds);
66
67 while (TRUE) {
68 /* XXX Do we really need a timeout, after which we drop this packet? */
69 tv.tv_sec = 15;
70 tv.tv_usec = 0;
71
72 res = select(sockfd + 1, NULL, &writefds, NULL, &tv);
73 if (res < 0) {
74 if (errno == EINTR) {
75 pr_signals_handle();
76 continue;
77 }
78
79 return -1;
80 }
81
82 break;
83 }
84
85 if (res > 0) {
86 if (FD_ISSET(sockfd, &writefds)) {
87 pr_trace_msg(trace_channel, 3,
88 "sending %lu UDP message bytes to %s#%u",
89 (unsigned long) pkt->resp_datalen,
90 pr_netaddr_get_ipstr(pkt->remote_addr),
91 ntohs(pr_netaddr_get_port(pkt->remote_addr)));
92
93 res = sendto(sockfd, pkt->resp_data, pkt->resp_datalen, 0,
94 pr_netaddr_get_sockaddr(pkt->remote_addr),
95 pr_netaddr_get_sockaddr_len(pkt->remote_addr));
96 if (res < 0) {
97 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
98 "error sending %u UDP message bytes to %s#%u: %s",
99 (unsigned int) pkt->resp_datalen,
100 pr_netaddr_get_ipstr(pkt->remote_addr),
101 ntohs(pr_netaddr_get_port(pkt->remote_addr)), strerror(errno));
102
103 } else {
104 pr_trace_msg(trace_channel, 3,
105 "sent %d UDP message bytes to %s#%u", res,
106 pr_netaddr_get_ipstr(pkt->remote_addr),
107 ntohs(pr_netaddr_get_port(pkt->remote_addr)));
108
109 res = snmp_db_incr_value(pkt->pool, SNMP_DB_SNMP_F_PKTS_SENT_TOTAL, 1);
110 if (res < 0) {
111 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
112 "error incrementing SNMP database for "
113 "snmp.packetsSentTotal: %s", strerror(errno));
114 }
115 }
116 }
117
118 } else {
119 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
120 "dropping response after waiting %u secs for available socket space",
121 (unsigned int) tv.tv_sec);
122
123 res = snmp_db_incr_value(pkt->pool, SNMP_DB_SNMP_F_PKTS_DROPPED_TOTAL, 1);
124 if (res < 0) {
125 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
126 "error incrementing snmp.packetsDroppedTotal: %s", strerror(errno));
127 }
128 }
129
130 return res;
131 }