]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - contrib/bsnmp/snmpd/trans_udp.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / contrib / bsnmp / snmpd / trans_udp.c
1 /*
2  * Copyright (c) 2003
3  *      Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4  *      All rights reserved.
5  *
6  * Author: Harti Brandt <harti@freebsd.org>
7  * 
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  * 
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
27  * SUCH DAMAGE.
28  *
29  * $Begemot: bsnmp/snmpd/trans_udp.c,v 1.5 2005/10/04 08:46:56 brandt_h Exp $
30  *
31  * UDP transport
32  */
33 #include <sys/types.h>
34
35 #include <stdlib.h>
36 #include <syslog.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <unistd.h>
40
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
43
44 #include "snmpmod.h"
45 #include "snmpd.h"
46 #include "trans_udp.h"
47 #include "tree.h"
48 #include "oid.h"
49
50 static int udp_start(void);
51 static int udp_stop(int);
52 static void udp_close_port(struct tport *);
53 static int udp_init_port(struct tport *);
54 static ssize_t udp_send(struct tport *, const u_char *, size_t,
55     const struct sockaddr *, size_t);
56
57 /* exported */
58 const struct transport_def udp_trans = {
59         "udp",
60         OIDX_begemotSnmpdTransUdp,
61         udp_start,
62         udp_stop,
63         udp_close_port,
64         udp_init_port,
65         udp_send
66 };
67 static struct transport *my_trans;
68
69 static int
70 udp_start(void)
71 {
72         return (trans_register(&udp_trans, &my_trans));
73 }
74
75 static int
76 udp_stop(int force __unused)
77 {
78         if (my_trans != NULL)
79                 if (trans_unregister(my_trans) != 0)
80                         return (SNMP_ERR_GENERR);
81         return (SNMP_ERR_NOERROR);
82 }
83
84 /*
85  * A UDP port is ready
86  */
87 static void
88 udp_input(int fd __unused, void *udata)
89 {
90         struct udp_port *p = udata;
91
92         p->input.peerlen = sizeof(p->ret);
93         snmpd_input(&p->input, &p->tport);
94 }
95
96 /*
97  * Create a UDP socket and bind it to the given port
98  */
99 static int
100 udp_init_port(struct tport *tp)
101 {
102         struct udp_port *p = (struct udp_port *)tp;
103         struct sockaddr_in addr;
104         u_int32_t ip;
105
106         if ((p->input.fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
107                 syslog(LOG_ERR, "creating UDP socket: %m");
108                 return (SNMP_ERR_RES_UNAVAIL);
109         }
110         ip = (p->addr[0] << 24) | (p->addr[1] << 16) | (p->addr[2] << 8) |
111             p->addr[3];
112         memset(&addr, 0, sizeof(addr));
113         addr.sin_addr.s_addr = htonl(ip);
114         addr.sin_port = htons(p->port);
115         addr.sin_family = AF_INET;
116         addr.sin_len = sizeof(addr);
117         if (bind(p->input.fd, (struct sockaddr *)&addr, sizeof(addr))) {
118                 if (errno == EADDRNOTAVAIL) {
119                         close(p->input.fd);
120                         p->input.fd = -1;
121                         return (SNMP_ERR_INCONS_NAME);
122                 }
123                 syslog(LOG_ERR, "bind: %s:%u %m", inet_ntoa(addr.sin_addr),
124                     p->port);
125                 close(p->input.fd);
126                 p->input.fd = -1;
127                 return (SNMP_ERR_GENERR);
128         }
129         if ((p->input.id = fd_select(p->input.fd, udp_input,
130             p, NULL)) == NULL) {
131                 close(p->input.fd);
132                 p->input.fd = -1;
133                 return (SNMP_ERR_GENERR);
134         }
135         return (SNMP_ERR_NOERROR);
136 }
137
138 /*
139  * Create a new SNMP Port object and start it, if we are not
140  * in initialization mode. The arguments are in host byte order.
141  */
142 static int
143 udp_open_port(u_int8_t *addr, u_int32_t udp_port, struct udp_port **pp)
144 {
145         struct udp_port *port;
146         int err;
147
148         if (udp_port > 0xffff)
149                 return (SNMP_ERR_NO_CREATION);
150         if ((port = malloc(sizeof(*port))) == NULL)
151                 return (SNMP_ERR_GENERR);
152         memset(port, 0, sizeof(*port));
153
154         /* initialize common part */
155         port->tport.index.len = 5;
156         port->tport.index.subs[0] = addr[0];
157         port->tport.index.subs[1] = addr[1];
158         port->tport.index.subs[2] = addr[2];
159         port->tport.index.subs[3] = addr[3];
160         port->tport.index.subs[4] = udp_port;
161
162         port->addr[0] = addr[0];
163         port->addr[1] = addr[1];
164         port->addr[2] = addr[2];
165         port->addr[3] = addr[3];
166         port->port = udp_port;
167
168         port->input.fd = -1;
169         port->input.id = NULL;
170         port->input.stream = 0;
171         port->input.cred = 0;
172         port->input.peer = (struct sockaddr *)&port->ret;
173         port->input.peerlen = sizeof(port->ret);
174
175         trans_insert_port(my_trans, &port->tport);
176
177         if (community != COMM_INITIALIZE &&
178             (err = udp_init_port(&port->tport)) != SNMP_ERR_NOERROR) {
179                 udp_close_port(&port->tport);
180                 return (err);
181         }
182         *pp = port;
183         return (SNMP_ERR_NOERROR);
184 }
185
186 /*
187  * Close an SNMP port
188  */
189 static void
190 udp_close_port(struct tport *tp)
191 {
192         struct udp_port *port = (struct udp_port *)tp;
193
194         snmpd_input_close(&port->input);
195         trans_remove_port(tp);
196         free(port);
197 }
198
199 /*
200  * Send something
201  */
202 static ssize_t
203 udp_send(struct tport *tp, const u_char *buf, size_t len,
204     const struct sockaddr *addr, size_t addrlen)
205 {
206         struct udp_port *p = (struct udp_port *)tp;
207
208         return (sendto(p->input.fd, buf, len, 0, addr, addrlen));
209 }
210
211 /*
212  * Port table
213  */
214 int
215 op_snmp_port(struct snmp_context *ctx, struct snmp_value *value,
216     u_int sub, u_int iidx, enum snmp_op op)
217 {
218         asn_subid_t which = value->var.subs[sub-1];
219         struct udp_port *p;
220         u_int8_t addr[4];
221         u_int32_t port;
222
223         switch (op) {
224
225           case SNMP_OP_GETNEXT:
226                 if ((p = (struct udp_port *)trans_next_port(my_trans,
227                     &value->var, sub)) == NULL)
228                         return (SNMP_ERR_NOSUCHNAME);
229                 index_append(&value->var, sub, &p->tport.index);
230                 break;
231
232           case SNMP_OP_GET:
233                 if ((p = (struct udp_port *)trans_find_port(my_trans,
234                     &value->var, sub)) == NULL)
235                         return (SNMP_ERR_NOSUCHNAME);
236                 break;
237
238           case SNMP_OP_SET:
239                 p = (struct udp_port *)trans_find_port(my_trans,
240                     &value->var, sub);
241                 ctx->scratch->int1 = (p != NULL);
242
243                 if (which != LEAF_begemotSnmpdPortStatus)
244                         abort();
245                 if (!TRUTH_OK(value->v.integer))
246                         return (SNMP_ERR_WRONG_VALUE);
247
248                 ctx->scratch->int2 = TRUTH_GET(value->v.integer);
249
250                 if (ctx->scratch->int2) {
251                         /* open an SNMP port */
252                         if (p != NULL)
253                                 /* already open - do nothing */
254                                 return (SNMP_ERR_NOERROR);
255
256                         if (index_decode(&value->var, sub, iidx, addr, &port))
257                                 return (SNMP_ERR_NO_CREATION);
258                         return (udp_open_port(addr, port, &p));
259
260                 } else {
261                         /* close SNMP port - do in commit */
262                 }
263                 return (SNMP_ERR_NOERROR);
264
265           case SNMP_OP_ROLLBACK:
266                 p = (struct udp_port *)trans_find_port(my_trans,
267                     &value->var, sub);
268                 if (ctx->scratch->int1 == 0) {
269                         /* did not exist */
270                         if (ctx->scratch->int2 == 1) {
271                                 /* created */
272                                 if (p != NULL)
273                                         udp_close_port(&p->tport);
274                         }
275                 }
276                 return (SNMP_ERR_NOERROR);
277
278           case SNMP_OP_COMMIT:
279                 p = (struct udp_port *)trans_find_port(my_trans,
280                     &value->var, sub);
281                 if (ctx->scratch->int1 == 1) {
282                         /* did exist */
283                         if (ctx->scratch->int2 == 0) {
284                                 /* delete */
285                                 if (p != NULL)
286                                         udp_close_port(&p->tport);
287                         }
288                 }
289                 return (SNMP_ERR_NOERROR);
290
291           default:
292                 abort();
293         }
294
295         /*
296          * Come here to fetch the value
297          */
298         switch (which) {
299
300           case LEAF_begemotSnmpdPortStatus:
301                 value->v.integer = 1;
302                 break;
303
304           default:
305                 abort();
306         }
307
308         return (SNMP_ERR_NOERROR);
309 }