3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
6 * Author: Harti Brandt <harti@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $Begemot: bsnmp/snmpd/trans_udp.c,v 1.5 2005/10/04 08:46:56 brandt_h Exp $
33 #include <sys/types.h>
34 #include <sys/queue.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
47 #include "trans_udp.h"
51 static int udp_start(void);
52 static int udp_stop(int);
53 static void udp_close_port(struct tport *);
54 static int udp_init_port(struct tport *);
55 static ssize_t udp_send(struct tport *, const u_char *, size_t,
56 const struct sockaddr *, size_t);
59 const struct transport_def udp_trans = {
61 OIDX_begemotSnmpdTransUdp,
68 static struct transport *my_trans;
73 return (trans_register(&udp_trans, &my_trans));
77 udp_stop(int force __unused)
80 if (trans_unregister(my_trans) != 0)
81 return (SNMP_ERR_GENERR);
82 return (SNMP_ERR_NOERROR);
89 udp_input(int fd __unused, void *udata)
91 struct udp_port *p = udata;
93 p->input.peerlen = sizeof(p->ret);
94 snmpd_input(&p->input, &p->tport);
98 * Create a UDP socket and bind it to the given port
101 udp_init_port(struct tport *tp)
103 struct udp_port *p = (struct udp_port *)tp;
104 struct sockaddr_in addr;
108 if ((p->input.fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
109 syslog(LOG_ERR, "creating UDP socket: %m");
110 return (SNMP_ERR_RES_UNAVAIL);
112 ip = (p->addr[0] << 24) | (p->addr[1] << 16) | (p->addr[2] << 8) |
114 memset(&addr, 0, sizeof(addr));
115 addr.sin_addr.s_addr = htonl(ip);
116 addr.sin_port = htons(p->port);
117 addr.sin_family = AF_INET;
118 addr.sin_len = sizeof(addr);
119 if (addr.sin_addr.s_addr == INADDR_ANY &&
120 setsockopt(p->input.fd, IPPROTO_IP, IP_RECVDSTADDR, &on,
122 syslog(LOG_ERR, "setsockopt(IP_RECVDSTADDR): %m");
125 return (SNMP_ERR_GENERR);
127 if (bind(p->input.fd, (struct sockaddr *)&addr, sizeof(addr))) {
128 if (errno == EADDRNOTAVAIL) {
131 return (SNMP_ERR_INCONS_NAME);
133 syslog(LOG_ERR, "bind: %s:%u %m", inet_ntoa(addr.sin_addr),
137 return (SNMP_ERR_GENERR);
139 if ((p->input.id = fd_select(p->input.fd, udp_input,
143 return (SNMP_ERR_GENERR);
145 return (SNMP_ERR_NOERROR);
149 * Create a new SNMP Port object and start it, if we are not
150 * in initialization mode. The arguments are in host byte order.
153 udp_open_port(u_int8_t *addr, u_int32_t udp_port, struct udp_port **pp)
155 struct udp_port *port;
158 if (udp_port > 0xffff)
159 return (SNMP_ERR_NO_CREATION);
160 if ((port = malloc(sizeof(*port))) == NULL)
161 return (SNMP_ERR_GENERR);
162 memset(port, 0, sizeof(*port));
164 /* initialize common part */
165 port->tport.index.len = 5;
166 port->tport.index.subs[0] = addr[0];
167 port->tport.index.subs[1] = addr[1];
168 port->tport.index.subs[2] = addr[2];
169 port->tport.index.subs[3] = addr[3];
170 port->tport.index.subs[4] = udp_port;
172 port->addr[0] = addr[0];
173 port->addr[1] = addr[1];
174 port->addr[2] = addr[2];
175 port->addr[3] = addr[3];
176 port->port = udp_port;
179 port->input.id = NULL;
180 port->input.stream = 0;
181 port->input.cred = 0;
182 port->input.peer = (struct sockaddr *)&port->ret;
183 port->input.peerlen = sizeof(port->ret);
185 trans_insert_port(my_trans, &port->tport);
187 if (community != COMM_INITIALIZE &&
188 (err = udp_init_port(&port->tport)) != SNMP_ERR_NOERROR) {
189 udp_close_port(&port->tport);
193 return (SNMP_ERR_NOERROR);
200 udp_close_port(struct tport *tp)
202 struct udp_port *port = (struct udp_port *)tp;
204 snmpd_input_close(&port->input);
205 trans_remove_port(tp);
213 udp_send(struct tport *tp, const u_char *buf, size_t len,
214 const struct sockaddr *addr, size_t addrlen)
216 struct udp_port *p = (struct udp_port *)tp;
218 return (sendto(p->input.fd, buf, len, 0, addr, addrlen));
225 op_snmp_port(struct snmp_context *ctx, struct snmp_value *value,
226 u_int sub, u_int iidx, enum snmp_op op)
228 asn_subid_t which = value->var.subs[sub-1];
235 case SNMP_OP_GETNEXT:
236 if ((p = (struct udp_port *)trans_next_port(my_trans,
237 &value->var, sub)) == NULL)
238 return (SNMP_ERR_NOSUCHNAME);
239 index_append(&value->var, sub, &p->tport.index);
243 if ((p = (struct udp_port *)trans_find_port(my_trans,
244 &value->var, sub)) == NULL)
245 return (SNMP_ERR_NOSUCHNAME);
249 p = (struct udp_port *)trans_find_port(my_trans,
251 ctx->scratch->int1 = (p != NULL);
253 if (which != LEAF_begemotSnmpdPortStatus)
255 if (!TRUTH_OK(value->v.integer))
256 return (SNMP_ERR_WRONG_VALUE);
258 ctx->scratch->int2 = TRUTH_GET(value->v.integer);
260 if (ctx->scratch->int2) {
261 /* open an SNMP port */
263 /* already open - do nothing */
264 return (SNMP_ERR_NOERROR);
266 if (index_decode(&value->var, sub, iidx, addr, &port))
267 return (SNMP_ERR_NO_CREATION);
268 return (udp_open_port(addr, port, &p));
271 /* close SNMP port - do in commit */
273 return (SNMP_ERR_NOERROR);
275 case SNMP_OP_ROLLBACK:
276 p = (struct udp_port *)trans_find_port(my_trans,
278 if (ctx->scratch->int1 == 0) {
280 if (ctx->scratch->int2 == 1) {
283 udp_close_port(&p->tport);
286 return (SNMP_ERR_NOERROR);
289 p = (struct udp_port *)trans_find_port(my_trans,
291 if (ctx->scratch->int1 == 1) {
293 if (ctx->scratch->int2 == 0) {
296 udp_close_port(&p->tport);
299 return (SNMP_ERR_NOERROR);
306 * Come here to fetch the value
310 case LEAF_begemotSnmpdPortStatus:
311 value->v.integer = 1;
318 return (SNMP_ERR_NOERROR);