| 1 |
/* |
| 2 |
* Smux module authored by Rohit Dube. |
| 3 |
* Rewritten by Nick Amato <[email protected]>. |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <net-snmp/net-snmp-config.h> |
| 7 |
#include <net-snmp/net-snmp-features.h> |
| 8 |
#include <sys/types.h> |
| 9 |
#include <ctype.h> |
| 10 |
|
| 11 |
#ifdef HAVE_IO_H |
| 12 |
#include <io.h> |
| 13 |
#endif |
| 14 |
#include <stdio.h> |
| 15 |
#ifdef HAVE_STDLIB_H |
| 16 |
#include <stdlib.h> |
| 17 |
#endif |
| 18 |
#ifdef HAVE_STRING_H |
| 19 |
#include <string.h> |
| 20 |
#else |
| 21 |
#include <strings.h> |
| 22 |
#endif |
| 23 |
#ifdef HAVE_UNISTD_H |
| 24 |
#include <unistd.h> |
| 25 |
#endif |
| 26 |
#ifdef HAVE_ERR_H |
| 27 |
#include <err.h> |
| 28 |
#endif |
| 29 |
#ifdef TIME_WITH_SYS_TIME |
| 30 |
# include <sys/time.h> |
| 31 |
# include <time.h> |
| 32 |
#else |
| 33 |
# ifdef HAVE_SYS_TIME_H |
| 34 |
# include <sys/time.h> |
| 35 |
# else |
| 36 |
# include <time.h> |
| 37 |
# endif |
| 38 |
#endif |
| 39 |
#include <errno.h> |
| 40 |
#ifdef HAVE_NETDB_H |
| 41 |
#include <netdb.h> |
| 42 |
#endif |
| 43 |
|
| 44 |
#include <sys/stat.h> |
| 45 |
#ifdef HAVE_SYS_SOCKET_H |
| 46 |
#include <sys/socket.h> |
| 47 |
#endif |
| 48 |
#ifdef HAVE_SYS_FILIO_H |
| 49 |
#include <sys/filio.h> |
| 50 |
#endif |
| 51 |
|
| 52 |
#ifdef HAVE_NETINET_IN_H |
| 53 |
#include <netinet/in.h> |
| 54 |
#endif |
| 55 |
|
| 56 |
#ifdef HAVE_ARPA_INET_H |
| 57 |
#include <arpa/inet.h> |
| 58 |
#endif |
| 59 |
|
| 60 |
#ifdef HAVE_SYS_IOCTL_H |
| 61 |
#include <sys/ioctl.h> |
| 62 |
#endif |
| 63 |
|
| 64 |
#include <net-snmp/net-snmp-includes.h> |
| 65 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 66 |
#include <net-snmp/library/tools.h> |
| 67 |
|
| 68 |
#include "smux.h" |
| 69 |
#include "snmpd.h" |
| 70 |
|
| 71 |
netsnmp_feature_require(snprint_objid); |
| 72 |
|
| 73 |
long smux_long; |
| 74 |
u_long smux_ulong; |
| 75 |
struct sockaddr_in smux_sa; |
| 76 |
struct counter64 smux_counter64; |
| 77 |
oid smux_objid[MAX_OID_LEN]; |
| 78 |
u_char smux_str[SMUXMAXSTRLEN]; |
| 79 |
int smux_listen_sd = -1; |
| 80 |
|
| 81 |
static struct timeval smux_rcv_timeout; |
| 82 |
static long smux_reqid; |
| 83 |
|
| 84 |
static u_char *smux_open_process(int, u_char *, size_t *, int *); |
| 85 |
static u_char *smux_rreq_process(int, u_char *, size_t *); |
| 86 |
static u_char *smux_close_process(int, u_char *, size_t *); |
| 87 |
static u_char *smux_trap_process(u_char *, size_t *); |
| 88 |
static u_char *smux_parse(u_char *, oid *, size_t *, size_t *, u_char *); |
| 89 |
static u_char *smux_parse_var(u_char *, size_t *, oid *, size_t *, |
| 90 |
size_t *, u_char *); |
| 91 |
static void smux_send_close(int, int); |
| 92 |
static void smux_list_detach(smux_reg **, smux_reg *); |
| 93 |
static void smux_replace_active(smux_reg *, smux_reg *); |
| 94 |
static void smux_peer_cleanup(int); |
| 95 |
static int smux_auth_peer(oid *, size_t, char *, int); |
| 96 |
static int smux_build(u_char, long, oid *, |
| 97 |
size_t *, u_char, u_char *, size_t, u_char *, |
| 98 |
size_t *); |
| 99 |
static int smux_list_add(smux_reg **, smux_reg *); |
| 100 |
static int smux_pdu_process(int, u_char *, size_t); |
| 101 |
static int smux_send_rrsp(int, int); |
| 102 |
static smux_reg *smux_find_match(smux_reg *, int, oid *, size_t, long); |
| 103 |
static smux_reg *smux_find_replacement(oid *, size_t); |
| 104 |
u_char *var_smux_get(oid *, size_t, oid *, size_t *, int, size_t *, |
| 105 |
u_char *); |
| 106 |
int var_smux_write(int, u_char *, u_char, size_t, oid *, size_t); |
| 107 |
|
| 108 |
static smux_reg *ActiveRegs; /* Active registrations */ |
| 109 |
static smux_reg *PassiveRegs; /* Currently unused registrations */ |
| 110 |
|
| 111 |
static smux_peer_auth *Auths[SMUX_MAX_PEERS]; /* Configured peers */ |
| 112 |
static int nauths, npeers = 0; |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
void |
| 117 |
smux_parse_smux_socket(const char *token, char *cptr) |
| 118 |
{ |
| 119 |
DEBUGMSGTL(("smux", "port spec: %s\n", cptr)); |
| 120 |
netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_SMUX_SOCKET, cptr); |
| 121 |
} |
| 122 |
|
| 123 |
void |
| 124 |
smux_parse_peer_auth(const char *token, char *cptr) |
| 125 |
{ |
| 126 |
smux_peer_auth *aptr; |
| 127 |
char *password_cptr; |
| 128 |
int rv; |
| 129 |
|
| 130 |
if ((aptr = |
| 131 |
(smux_peer_auth *) calloc(1, sizeof(smux_peer_auth))) == NULL) { |
| 132 |
snmp_log_perror("smux_parse_peer_auth: malloc"); |
| 133 |
return; |
| 134 |
} |
| 135 |
if (nauths == SMUX_MAX_PEERS) { |
| 136 |
config_perror("Too many smuxpeers"); |
| 137 |
free(aptr); |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
password_cptr = strchr(cptr, ' '); |
| 142 |
if (password_cptr) |
| 143 |
*(password_cptr++) = '\0'; |
| 144 |
|
| 145 |
/* |
| 146 |
* oid |
| 147 |
*/ |
| 148 |
aptr->sa_active_fd = -1; |
| 149 |
aptr->sa_oid_len = MAX_OID_LEN; |
| 150 |
rv = read_objid( cptr, aptr->sa_oid, &aptr->sa_oid_len ); |
| 151 |
DEBUGMSGTL(("smux_conf", "parsing registration for: %s\n", cptr)); |
| 152 |
if (!rv) |
| 153 |
config_perror("Error parsing smux oid"); |
| 154 |
|
| 155 |
if (password_cptr != NULL) { /* Do we have a password or not? */ |
| 156 |
DEBUGMSGTL(("smux_conf", "password is: %s\n", |
| 157 |
SNMP_STRORNULL(password_cptr))); |
| 158 |
|
| 159 |
/* |
| 160 |
* password |
| 161 |
*/ |
| 162 |
if (*password_cptr) |
| 163 |
strlcpy(aptr->sa_passwd, password_cptr, sizeof(aptr->sa_passwd)); |
| 164 |
} else { |
| 165 |
/* |
| 166 |
* null passwords OK |
| 167 |
*/ |
| 168 |
DEBUGMSGTL(("smux_conf", "null password\n")); |
| 169 |
} |
| 170 |
|
| 171 |
Auths[nauths++] = aptr; |
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
void |
| 176 |
smux_free_peer_auth(void) |
| 177 |
{ |
| 178 |
int i; |
| 179 |
|
| 180 |
for (i = 0; i < nauths; i++) { |
| 181 |
free(Auths[i]); |
| 182 |
Auths[i] = NULL; |
| 183 |
} |
| 184 |
nauths = 0; |
| 185 |
} |
| 186 |
|
| 187 |
void |
| 188 |
init_smux(void) |
| 189 |
{ |
| 190 |
snmpd_register_config_handler("smuxpeer", smux_parse_peer_auth, |
| 191 |
smux_free_peer_auth, |
| 192 |
"OID-IDENTITY PASSWORD"); |
| 193 |
snmpd_register_config_handler("smuxsocket", |
| 194 |
smux_parse_smux_socket, NULL, |
| 195 |
"SMUX bind address"); |
| 196 |
} |
| 197 |
|
| 198 |
void |
| 199 |
real_init_smux(void) |
| 200 |
{ |
| 201 |
struct sockaddr_in lo_socket; |
| 202 |
char *smux_socket; |
| 203 |
int one = 1; |
| 204 |
|
| 205 |
if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE) == SUB_AGENT) { |
| 206 |
smux_listen_sd = -1; |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
/* |
| 211 |
* Reqid |
| 212 |
*/ |
| 213 |
smux_reqid = 0; |
| 214 |
smux_listen_sd = -1; |
| 215 |
|
| 216 |
/* |
| 217 |
* Receive timeout |
| 218 |
*/ |
| 219 |
smux_rcv_timeout.tv_sec = 0; |
| 220 |
smux_rcv_timeout.tv_usec = 500000; |
| 221 |
|
| 222 |
/* |
| 223 |
* Get ready to listen on the SMUX port |
| 224 |
*/ |
| 225 |
memset(&lo_socket, (0), sizeof(lo_socket)); |
| 226 |
lo_socket.sin_family = AF_INET; |
| 227 |
|
| 228 |
smux_socket = netsnmp_ds_get_string(NETSNMP_DS_APPLICATION_ID, |
| 229 |
NETSNMP_DS_SMUX_SOCKET); |
| 230 |
#ifdef NETSNMP_ENABLE_LOCAL_SMUX |
| 231 |
if (!smux_socket) |
| 232 |
smux_socket = "127.0.0.1"; /* By default, listen on localhost only */ |
| 233 |
#endif |
| 234 |
netsnmp_sockaddr_in( &lo_socket, smux_socket, SMUXPORT ); |
| 235 |
|
| 236 |
if ((smux_listen_sd = (int) socket(AF_INET, SOCK_STREAM, 0)) < 0) { |
| 237 |
snmp_log_perror("[init_smux] socket failed"); |
| 238 |
return; |
| 239 |
} |
| 240 |
#ifdef SO_REUSEADDR |
| 241 |
/* |
| 242 |
* At least on Linux, when the master agent terminates, any |
| 243 |
* TCP connections for SMUX peers are put in the TIME_WAIT |
| 244 |
* state for about 60 seconds. If the master agent is started |
| 245 |
* during this time, the bind for the listening socket will |
| 246 |
* fail because the SMUX port is in use. |
| 247 |
*/ |
| 248 |
if (setsockopt(smux_listen_sd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, |
| 249 |
sizeof(one)) < 0) { |
| 250 |
snmp_log_perror("[init_smux] setsockopt(SO_REUSEADDR) failed"); |
| 251 |
} |
| 252 |
#endif /* SO_REUSEADDR */ |
| 253 |
|
| 254 |
if (bind(smux_listen_sd, (struct sockaddr *) &lo_socket, |
| 255 |
sizeof(lo_socket)) < 0) { |
| 256 |
snmp_log_perror("[init_smux] bind failed"); |
| 257 |
close(smux_listen_sd); |
| 258 |
smux_listen_sd = -1; |
| 259 |
return; |
| 260 |
} |
| 261 |
#ifdef SO_KEEPALIVE |
| 262 |
if (setsockopt(smux_listen_sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, |
| 263 |
sizeof(one)) < 0) { |
| 264 |
snmp_log_perror("[init_smux] setsockopt(SO_KEEPALIVE) failed"); |
| 265 |
close(smux_listen_sd); |
| 266 |
smux_listen_sd = -1; |
| 267 |
return; |
| 268 |
} |
| 269 |
#endif /* SO_KEEPALIVE */ |
| 270 |
|
| 271 |
if (listen(smux_listen_sd, SOMAXCONN) == -1) { |
| 272 |
snmp_log_perror("[init_smux] listen failed"); |
| 273 |
close(smux_listen_sd); |
| 274 |
smux_listen_sd = -1; |
| 275 |
return; |
| 276 |
} |
| 277 |
|
| 278 |
DEBUGMSGTL(("smux_init", |
| 279 |
"[smux_init] done; smux listen sd is %d, smux port is %d\n", |
| 280 |
smux_listen_sd, ntohs(lo_socket.sin_port))); |
| 281 |
} |
| 282 |
|
| 283 |
static int |
| 284 |
smux_handler(netsnmp_mib_handler *handler, |
| 285 |
netsnmp_handler_registration *reginfo, |
| 286 |
netsnmp_agent_request_info *reqinfo, |
| 287 |
netsnmp_request_info *requests) |
| 288 |
{ |
| 289 |
u_char *access = NULL; |
| 290 |
size_t var_len; |
| 291 |
int exact = 1; |
| 292 |
int status = 0; |
| 293 |
u_char var_type; |
| 294 |
static long old_reqid = -1; |
| 295 |
static long old_sessid = -1; |
| 296 |
long new_reqid, new_sessid; |
| 297 |
|
| 298 |
/* Increment the reqid of outgoing SMUX messages only when processing |
| 299 |
* new incoming SNMP message, i.e. when reqid or session id chamges */ |
| 300 |
new_reqid = reqinfo->asp->pdu->reqid; |
| 301 |
new_sessid = reqinfo->asp->session->sessid; |
| 302 |
DEBUGMSGTL(("smux", "smux_handler: incoming reqid=%ld, sessid=%ld\n", |
| 303 |
new_reqid, new_sessid)); |
| 304 |
if (old_reqid != new_reqid || old_sessid != new_sessid) { |
| 305 |
smux_reqid++; |
| 306 |
old_reqid = new_reqid; |
| 307 |
old_sessid = new_sessid; |
| 308 |
} |
| 309 |
|
| 310 |
switch (reqinfo->mode) { |
| 311 |
case MODE_GETNEXT: |
| 312 |
case MODE_GETBULK: |
| 313 |
exact = 0; |
| 314 |
} |
| 315 |
|
| 316 |
for (; requests; requests = requests->next) { |
| 317 |
switch(reqinfo->mode) { |
| 318 |
case MODE_GET: |
| 319 |
case MODE_GETNEXT: |
| 320 |
case MODE_SET_RESERVE1: |
| 321 |
access = var_smux_get(reginfo->rootoid, |
| 322 |
reginfo->rootoid_len, |
| 323 |
requests->requestvb->name, |
| 324 |
&requests->requestvb->name_length, |
| 325 |
exact, |
| 326 |
&var_len, |
| 327 |
&var_type); |
| 328 |
if (access) |
| 329 |
if (reqinfo->mode != MODE_SET_RESERVE1) |
| 330 |
snmp_set_var_typed_value(requests->requestvb, |
| 331 |
var_type, access, var_len); |
| 332 |
if (reqinfo->mode != MODE_SET_RESERVE1) |
| 333 |
break; |
| 334 |
/* fall through if MODE_SET_RESERVE1 */ |
| 335 |
/* FALL THROUGH */ |
| 336 |
|
| 337 |
default: |
| 338 |
/* SET processing */ |
| 339 |
status = var_smux_write(reqinfo->mode, |
| 340 |
requests->requestvb->val.string, |
| 341 |
requests->requestvb->type, |
| 342 |
requests->requestvb->val_len, |
| 343 |
requests->requestvb->name, |
| 344 |
requests->requestvb->name_length); |
| 345 |
if (status != SNMP_ERR_NOERROR) { |
| 346 |
netsnmp_set_request_error(reqinfo, requests, status); |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
return SNMP_ERR_NOERROR; |
| 351 |
} |
| 352 |
|
| 353 |
u_char * |
| 354 |
var_smux_get(oid *root, size_t root_len, |
| 355 |
oid * name, size_t * length, |
| 356 |
int exact, size_t * var_len, u_char *var_type) |
| 357 |
{ |
| 358 |
u_char *valptr; |
| 359 |
smux_reg *rptr; |
| 360 |
|
| 361 |
/* |
| 362 |
* search the active registration list |
| 363 |
*/ |
| 364 |
for (rptr = ActiveRegs; rptr; rptr = rptr->sr_next) { |
| 365 |
if (0 >= snmp_oidtree_compare(root, root_len, rptr->sr_name, |
| 366 |
rptr->sr_name_len)) |
| 367 |
break; |
| 368 |
} |
| 369 |
if (rptr == NULL) |
| 370 |
return NULL; |
| 371 |
else if (exact && (*length < rptr->sr_name_len)) |
| 372 |
return NULL; |
| 373 |
|
| 374 |
valptr = smux_snmp_process(exact, name, length, |
| 375 |
var_len, var_type, rptr->sr_fd); |
| 376 |
|
| 377 |
if (valptr == NULL) |
| 378 |
return NULL; |
| 379 |
|
| 380 |
if ((snmp_oidtree_compare(name, *length, rptr->sr_name, |
| 381 |
rptr->sr_name_len)) != 0) { |
| 382 |
/* |
| 383 |
* the peer has returned a value outside |
| 384 |
* * of the registered tree |
| 385 |
*/ |
| 386 |
return NULL; |
| 387 |
} else { |
| 388 |
return valptr; |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
int |
| 393 |
var_smux_write(int action, |
| 394 |
u_char * var_val, |
| 395 |
u_char var_val_type, |
| 396 |
size_t var_val_len, |
| 397 |
oid * name, size_t name_len) |
| 398 |
{ |
| 399 |
smux_reg *rptr; |
| 400 |
u_char buf[SMUXMAXPKTSIZE], *ptr, sout[3], type; |
| 401 |
int reterr; |
| 402 |
size_t var_len, datalen, name_length, packet_len; |
| 403 |
size_t len; |
| 404 |
ssize_t tmp_len; |
| 405 |
long reqid, errsts, erridx; |
| 406 |
u_char *dataptr; |
| 407 |
|
| 408 |
DEBUGMSGTL(("smux", "[var_smux_write] entering var_smux_write\n")); |
| 409 |
|
| 410 |
len = SMUXMAXPKTSIZE; |
| 411 |
reterr = SNMP_ERR_NOERROR; |
| 412 |
var_len = var_val_len; |
| 413 |
name_length = name_len; |
| 414 |
|
| 415 |
/* |
| 416 |
* XXX find the descriptor again |
| 417 |
*/ |
| 418 |
for (rptr = ActiveRegs; rptr; rptr = rptr->sr_next) { |
| 419 |
if (!snmp_oidtree_compare(name, name_len, rptr->sr_name, |
| 420 |
rptr->sr_name_len)) |
| 421 |
break; |
| 422 |
} |
| 423 |
|
| 424 |
if (!rptr) { |
| 425 |
DEBUGMSGTL(("smux", "[var_smux_write] unknown registration\n")); |
| 426 |
return SNMP_ERR_GENERR; |
| 427 |
} |
| 428 |
|
| 429 |
switch (action) { |
| 430 |
case RESERVE1: |
| 431 |
DEBUGMSGTL(("smux", "[var_smux_write] entering RESERVE1\n")); |
| 432 |
|
| 433 |
/* |
| 434 |
* length might be long |
| 435 |
*/ |
| 436 |
var_len += (*(var_val + 1) & ASN_LONG_LEN) ? |
| 437 |
var_len + ((*(var_val + 1) & 0x7F) + 2) : 2; |
| 438 |
|
| 439 |
switch (var_val_type) { |
| 440 |
case ASN_INTEGER: |
| 441 |
case ASN_OCTET_STR: |
| 442 |
case ASN_COUNTER: |
| 443 |
case ASN_GAUGE: |
| 444 |
case ASN_TIMETICKS: |
| 445 |
case ASN_UINTEGER: |
| 446 |
case ASN_COUNTER64: |
| 447 |
case ASN_IPADDRESS: |
| 448 |
case ASN_OPAQUE: |
| 449 |
case ASN_NSAP: |
| 450 |
case ASN_OBJECT_ID: |
| 451 |
case ASN_BIT_STR: |
| 452 |
datalen = var_val_len; |
| 453 |
dataptr = var_val; |
| 454 |
break; |
| 455 |
case SNMP_NOSUCHOBJECT: |
| 456 |
case SNMP_NOSUCHINSTANCE: |
| 457 |
case SNMP_ENDOFMIBVIEW: |
| 458 |
case ASN_NULL: |
| 459 |
default: |
| 460 |
DEBUGMSGTL(("smux", |
| 461 |
"[var_smux_write] variable not supported\n")); |
| 462 |
return SNMP_ERR_GENERR; |
| 463 |
break; |
| 464 |
} |
| 465 |
|
| 466 |
if ((smux_build((u_char) SMUX_SET, smux_reqid, |
| 467 |
name, &name_length, var_val_type, dataptr, |
| 468 |
datalen, buf, &len)) < 0) { |
| 469 |
DEBUGMSGTL(("smux", "[var_smux_write] smux build failed\n")); |
| 470 |
return SNMP_ERR_GENERR; |
| 471 |
} |
| 472 |
|
| 473 |
if (sendto(rptr->sr_fd, (void *) buf, len, 0, NULL, 0) < 0) { |
| 474 |
DEBUGMSGTL(("smux", "[var_smux_write] send failed\n")); |
| 475 |
return SNMP_ERR_GENERR; |
| 476 |
} |
| 477 |
|
| 478 |
while (1) { |
| 479 |
/* |
| 480 |
* peek at what's received |
| 481 |
*/ |
| 482 |
if ((len = recvfrom(rptr->sr_fd, (void *) buf, |
| 483 |
SMUXMAXPKTSIZE, MSG_PEEK, NULL, NULL)) <= 0) { |
| 484 |
if ((len == -1) && ((errno == EINTR) || (errno == EAGAIN))) |
| 485 |
{ |
| 486 |
continue; |
| 487 |
} |
| 488 |
DEBUGMSGTL(("smux", |
| 489 |
"[var_smux_write] peek failed or timed out\n")); |
| 490 |
/* |
| 491 |
* do we need to do a peer cleanup in this case?? |
| 492 |
*/ |
| 493 |
smux_peer_cleanup(rptr->sr_fd); |
| 494 |
smux_snmp_select_list_del(rptr->sr_fd); |
| 495 |
return SNMP_ERR_GENERR; |
| 496 |
} |
| 497 |
|
| 498 |
DEBUGMSGTL(("smux", "[var_smux_write] Peeked at %" NETSNMP_PRIz |
| 499 |
"d bytes\n", len)); |
| 500 |
DEBUGDUMPSETUP("var_smux_write", buf, len); |
| 501 |
|
| 502 |
/* |
| 503 |
* determine if we received more than one packet |
| 504 |
*/ |
| 505 |
packet_len = len; |
| 506 |
ptr = asn_parse_header(buf, &packet_len, &type); |
| 507 |
if (ptr == NULL) |
| 508 |
return SNMP_ERR_GENERR; |
| 509 |
packet_len += (ptr - buf); |
| 510 |
if (len > (ssize_t)packet_len) { |
| 511 |
/* |
| 512 |
* set length to receive only the first packet |
| 513 |
*/ |
| 514 |
len = packet_len; |
| 515 |
} |
| 516 |
|
| 517 |
/* |
| 518 |
* receive the first packet |
| 519 |
*/ |
| 520 |
tmp_len = len; |
| 521 |
do |
| 522 |
{ |
| 523 |
len = tmp_len; |
| 524 |
len = recvfrom(rptr->sr_fd, (void *) buf, len, 0, NULL, NULL); |
| 525 |
} |
| 526 |
while((len == -1) && ((errno == EINTR) || (errno == EAGAIN))); |
| 527 |
|
| 528 |
if (len <= 0) { |
| 529 |
DEBUGMSGTL(("smux", |
| 530 |
"[var_smux_write] recv failed or timed out\n")); |
| 531 |
smux_peer_cleanup(rptr->sr_fd); |
| 532 |
smux_snmp_select_list_del(rptr->sr_fd); |
| 533 |
return SNMP_ERR_GENERR; |
| 534 |
} |
| 535 |
|
| 536 |
DEBUGMSGTL(("smux", "[var_smux_write] Received %" NETSNMP_PRIz |
| 537 |
"d bytes\n", len)); |
| 538 |
|
| 539 |
if (buf[0] == SMUX_TRAP) { |
| 540 |
DEBUGMSGTL(("smux", "[var_smux_write] Received trap\n")); |
| 541 |
DEBUGMSGTL(("smux", "Got trap from peer on fd %d\n", |
| 542 |
rptr->sr_fd)); |
| 543 |
ptr = asn_parse_header(buf, &len, &type); |
| 544 |
if (ptr == NULL) |
| 545 |
return SNMP_ERR_GENERR; |
| 546 |
smux_trap_process(ptr, &len); |
| 547 |
|
| 548 |
|
| 549 |
/* |
| 550 |
* go and peek at received data again |
| 551 |
*/ |
| 552 |
/* |
| 553 |
* we could receive the reply or another trap |
| 554 |
*/ |
| 555 |
} else { |
| 556 |
ptr = buf; |
| 557 |
ptr = asn_parse_header(ptr, &len, &type); |
| 558 |
if ((ptr == NULL) || type != SNMP_MSG_RESPONSE) |
| 559 |
return SNMP_ERR_GENERR; |
| 560 |
|
| 561 |
ptr = |
| 562 |
asn_parse_int(ptr, &len, &type, &reqid, sizeof(reqid)); |
| 563 |
if ((ptr == NULL) || type != ASN_INTEGER) |
| 564 |
return SNMP_ERR_GENERR; |
| 565 |
|
| 566 |
ptr = |
| 567 |
asn_parse_int(ptr, &len, &type, &errsts, |
| 568 |
sizeof(errsts)); |
| 569 |
if ((ptr == NULL) || type != ASN_INTEGER) |
| 570 |
return SNMP_ERR_GENERR; |
| 571 |
|
| 572 |
if (errsts) { |
| 573 |
DEBUGMSGTL(("smux", |
| 574 |
"[var_smux_write] errsts returned\n")); |
| 575 |
return (errsts); |
| 576 |
} |
| 577 |
|
| 578 |
ptr = |
| 579 |
asn_parse_int(ptr, &len, &type, &erridx, |
| 580 |
sizeof(erridx)); |
| 581 |
if ((ptr == NULL) || type != ASN_INTEGER) |
| 582 |
return SNMP_ERR_GENERR; |
| 583 |
|
| 584 |
reterr = SNMP_ERR_NOERROR; |
| 585 |
break; |
| 586 |
} |
| 587 |
} /* while (1) */ |
| 588 |
break; /* case Action == RESERVE1 */ |
| 589 |
|
| 590 |
case RESERVE2: |
| 591 |
DEBUGMSGTL(("smux", "[var_smux_write] entering RESERVE2\n")); |
| 592 |
reterr = SNMP_ERR_NOERROR; |
| 593 |
break; /* case Action == RESERVE2 */ |
| 594 |
|
| 595 |
case FREE: |
| 596 |
case COMMIT: |
| 597 |
ptr = sout; |
| 598 |
*(ptr++) = (u_char) SMUX_SOUT; |
| 599 |
*(ptr++) = (u_char) 1; |
| 600 |
if (action == FREE) { |
| 601 |
*ptr = (u_char) 1; /* rollback */ |
| 602 |
DEBUGMSGTL(("smux", |
| 603 |
"[var_smux_write] entering FREE - sending RollBack \n")); |
| 604 |
} else { |
| 605 |
*ptr = (u_char) 0; /* commit */ |
| 606 |
DEBUGMSGTL(("smux", |
| 607 |
"[var_smux_write] entering FREE - sending Commit \n")); |
| 608 |
} |
| 609 |
|
| 610 |
if ((sendto(rptr->sr_fd, (void *) sout, 3, 0, NULL, 0)) < 0) { |
| 611 |
DEBUGMSGTL(("smux", |
| 612 |
"[var_smux_write] send rollback/commit failed\n")); |
| 613 |
return SNMP_ERR_GENERR; |
| 614 |
} |
| 615 |
|
| 616 |
reterr = SNMP_ERR_NOERROR; |
| 617 |
break; /* case Action == COMMIT */ |
| 618 |
|
| 619 |
default: |
| 620 |
break; |
| 621 |
} |
| 622 |
return reterr; |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
int |
| 627 |
smux_accept(int sd) |
| 628 |
{ |
| 629 |
u_char data[SMUXMAXPKTSIZE], *ptr, type; |
| 630 |
struct sockaddr_in in_socket; |
| 631 |
struct timeval tv; |
| 632 |
int fail, fd; |
| 633 |
socklen_t alen; |
| 634 |
int length; |
| 635 |
size_t len; |
| 636 |
|
| 637 |
alen = sizeof(struct sockaddr_in); |
| 638 |
/* |
| 639 |
* this may be too high |
| 640 |
*/ |
| 641 |
tv.tv_sec = 5; |
| 642 |
tv.tv_usec = 0; |
| 643 |
|
| 644 |
/* |
| 645 |
* connection request |
| 646 |
*/ |
| 647 |
DEBUGMSGTL(("smux", "[smux_accept] Calling accept()\n")); |
| 648 |
errno = 0; |
| 649 |
if ((fd = (int) accept(sd, (struct sockaddr *) &in_socket, &alen)) < 0) { |
| 650 |
snmp_log_perror("[smux_accept] accept failed"); |
| 651 |
return -1; |
| 652 |
} else { |
| 653 |
DEBUGMSGTL(("smux", "[smux_accept] accepted fd %d from %s:%d\n", |
| 654 |
fd, inet_ntoa(in_socket.sin_addr), |
| 655 |
ntohs(in_socket.sin_port))); |
| 656 |
if (npeers + 1 == SMUXMAXPEERS) { |
| 657 |
snmp_log(LOG_ERR, |
| 658 |
"[smux_accept] denied peer on fd %d, limit %d reached", |
| 659 |
fd, SMUXMAXPEERS); |
| 660 |
close(fd); |
| 661 |
return -1; |
| 662 |
} |
| 663 |
|
| 664 |
/* |
| 665 |
* now block for an OpenPDU |
| 666 |
*/ |
| 667 |
do |
| 668 |
{ |
| 669 |
length = recvfrom(fd, (char *) data, SMUXMAXPKTSIZE, 0, NULL, NULL); |
| 670 |
} |
| 671 |
while((length == -1) && ((errno == EINTR) || (errno == EAGAIN))); |
| 672 |
|
| 673 |
if (length <= 0) { |
| 674 |
DEBUGMSGTL(("smux", |
| 675 |
"[smux_accept] peer on fd %d died or timed out\n", |
| 676 |
fd)); |
| 677 |
close(fd); |
| 678 |
return -1; |
| 679 |
} |
| 680 |
/* |
| 681 |
* try to authorize him |
| 682 |
*/ |
| 683 |
ptr = data; |
| 684 |
len = length; |
| 685 |
if ((ptr = asn_parse_header(ptr, &len, &type)) == NULL) { |
| 686 |
smux_send_close(fd, SMUXC_PACKETFORMAT); |
| 687 |
close(fd); |
| 688 |
DEBUGMSGTL(("smux", "[smux_accept] peer on %d sent bad open", fd)); |
| 689 |
return -1; |
| 690 |
} else if (type != (u_char) SMUX_OPEN) { |
| 691 |
smux_send_close(fd, SMUXC_PROTOCOLERROR); |
| 692 |
close(fd); |
| 693 |
DEBUGMSGTL(("smux", |
| 694 |
"[smux_accept] peer on %d did not send open: (%d)\n", |
| 695 |
fd, type)); |
| 696 |
return -1; |
| 697 |
} |
| 698 |
ptr = smux_open_process(fd, ptr, &len, &fail); |
| 699 |
if (fail) { |
| 700 |
smux_send_close(fd, SMUXC_AUTHENTICATIONFAILURE); |
| 701 |
close(fd); |
| 702 |
DEBUGMSGTL(("smux", |
| 703 |
"[smux_accept] peer on %d failed authentication\n", |
| 704 |
fd)); |
| 705 |
return -1; |
| 706 |
} |
| 707 |
|
| 708 |
/* |
| 709 |
* he's OK |
| 710 |
*/ |
| 711 |
#ifdef SO_RCVTIMEO |
| 712 |
if (setsockopt |
| 713 |
(fd, SOL_SOCKET, SO_RCVTIMEO, (void *) &tv, sizeof(tv)) < 0) { |
| 714 |
DEBUGMSGTL(("smux", |
| 715 |
"[smux_accept] setsockopt(SO_RCVTIMEO) failed fd %d\n", |
| 716 |
fd)); |
| 717 |
snmp_log_perror("smux_accept: setsockopt SO_RCVTIMEO"); |
| 718 |
} |
| 719 |
#endif |
| 720 |
npeers++; |
| 721 |
DEBUGMSGTL(("smux", "[smux_accept] fd %d\n", fd)); |
| 722 |
|
| 723 |
/* |
| 724 |
* Process other PDUs already read, e.g. a registerRequest. |
| 725 |
*/ |
| 726 |
len = length - (ptr - data); |
| 727 |
if (smux_pdu_process(fd, ptr, len) < 0) { |
| 728 |
/* |
| 729 |
* Easy come, easy go. Clean-up is already done. |
| 730 |
*/ |
| 731 |
return -1; |
| 732 |
} |
| 733 |
} |
| 734 |
return fd; |
| 735 |
} |
| 736 |
|
| 737 |
int |
| 738 |
smux_process(int fd) |
| 739 |
{ |
| 740 |
int length, tmp_length; |
| 741 |
u_char data[SMUXMAXPKTSIZE]; |
| 742 |
u_char type, *ptr; |
| 743 |
size_t packet_len; |
| 744 |
|
| 745 |
do |
| 746 |
{ |
| 747 |
length = recvfrom(fd, (char *) data, SMUXMAXPKTSIZE, MSG_PEEK, NULL, |
| 748 |
NULL); |
| 749 |
} |
| 750 |
while((length == -1) && ((errno == EINTR) || (errno == EAGAIN))); |
| 751 |
|
| 752 |
if (length <= 0) |
| 753 |
{ |
| 754 |
if (length < 0) |
| 755 |
snmp_log_perror("[smux_process] peek failed"); |
| 756 |
smux_peer_cleanup(fd); |
| 757 |
return -1; |
| 758 |
} |
| 759 |
|
| 760 |
/* |
| 761 |
* determine if we received more than one packet |
| 762 |
*/ |
| 763 |
packet_len = length; |
| 764 |
ptr = asn_parse_header(data, &packet_len, &type); |
| 765 |
if (ptr == NULL) |
| 766 |
return -1; |
| 767 |
packet_len += (ptr - data); |
| 768 |
if (length > packet_len) { |
| 769 |
/* |
| 770 |
* set length to receive only the first packet |
| 771 |
*/ |
| 772 |
length = packet_len; |
| 773 |
} |
| 774 |
|
| 775 |
tmp_length = length; |
| 776 |
do |
| 777 |
{ |
| 778 |
length = tmp_length; |
| 779 |
length = recvfrom(fd, (char *) data, length, 0, NULL, NULL); |
| 780 |
} |
| 781 |
while((length == -1) && ((errno == EINTR) || (errno == EAGAIN))); |
| 782 |
|
| 783 |
if (length <= 0) { |
| 784 |
/* |
| 785 |
* the peer went away, close this descriptor |
| 786 |
* * and delete it from the list |
| 787 |
*/ |
| 788 |
DEBUGMSGTL(("smux", |
| 789 |
"[smux_process] peer on fd %d died or timed out\n", |
| 790 |
fd)); |
| 791 |
smux_peer_cleanup(fd); |
| 792 |
return -1; |
| 793 |
} |
| 794 |
|
| 795 |
return smux_pdu_process(fd, data, length); |
| 796 |
} |
| 797 |
|
| 798 |
static int |
| 799 |
smux_pdu_process(int fd, u_char * data, size_t length) |
| 800 |
{ |
| 801 |
int error; |
| 802 |
size_t len; |
| 803 |
u_char *ptr, type; |
| 804 |
|
| 805 |
DEBUGMSGTL(("smux", "[smux_pdu_process] Processing %" NETSNMP_PRIz |
| 806 |
"d bytes\n", length)); |
| 807 |
|
| 808 |
error = 0; |
| 809 |
ptr = data; |
| 810 |
while (error == 0 && ptr != NULL && ptr < data + length) { |
| 811 |
len = length - (ptr - data); |
| 812 |
ptr = asn_parse_header(ptr, &len, &type); |
| 813 |
if (ptr == NULL) { |
| 814 |
DEBUGMSGTL(("smux", "[smux_pdu_process] cannot parse header\n")); |
| 815 |
break; |
| 816 |
} |
| 817 |
DEBUGMSGTL(("smux", "[smux_pdu_process] type is %d\n", |
| 818 |
(int) type)); |
| 819 |
switch (type) { |
| 820 |
case SMUX_OPEN: |
| 821 |
smux_send_close(fd, SMUXC_PROTOCOLERROR); |
| 822 |
DEBUGMSGTL(("smux", |
| 823 |
"[smux_pdu_process] peer on fd %d sent duplicate open?\n", |
| 824 |
fd)); |
| 825 |
smux_peer_cleanup(fd); |
| 826 |
error = -1; |
| 827 |
break; |
| 828 |
case SMUX_CLOSE: |
| 829 |
ptr = smux_close_process(fd, ptr, &len); |
| 830 |
smux_peer_cleanup(fd); |
| 831 |
error = -1; |
| 832 |
break; |
| 833 |
case SMUX_RREQ: |
| 834 |
ptr = smux_rreq_process(fd, ptr, &len); |
| 835 |
break; |
| 836 |
case SMUX_RRSP: |
| 837 |
error = -1; |
| 838 |
smux_send_close(fd, SMUXC_PROTOCOLERROR); |
| 839 |
smux_peer_cleanup(fd); |
| 840 |
DEBUGMSGTL(("smux", |
| 841 |
"[smux_pdu_process] peer on fd %d sent RRSP!\n", |
| 842 |
fd)); |
| 843 |
break; |
| 844 |
case SMUX_SOUT: |
| 845 |
error = -1; |
| 846 |
smux_send_close(fd, SMUXC_PROTOCOLERROR); |
| 847 |
smux_peer_cleanup(fd); |
| 848 |
DEBUGMSGTL(("smux", "This shouldn't have happened!\n")); |
| 849 |
break; |
| 850 |
case SMUX_TRAP: |
| 851 |
DEBUGMSGTL(("smux", "Got trap from peer on fd %d\n", fd)); |
| 852 |
if (ptr) |
| 853 |
{ |
| 854 |
DEBUGMSGTL(("smux", "[smux_pdu_process] call smux_trap_process.\n")); |
| 855 |
ptr = smux_trap_process(ptr, &len); |
| 856 |
} |
| 857 |
else |
| 858 |
{ |
| 859 |
DEBUGMSGTL(("smux", "[smux_pdu_process] smux_trap_process not called: ptr=NULL.\n")); |
| 860 |
DEBUGMSGTL(("smux", "[smux_pdu_process] Error: \n%s\n", snmp_api_errstring(0))); |
| 861 |
} |
| 862 |
/* |
| 863 |
* watch out for close on top of this...should return correct end |
| 864 |
*/ |
| 865 |
break; |
| 866 |
default: |
| 867 |
smux_send_close(fd, SMUXC_PACKETFORMAT); |
| 868 |
smux_peer_cleanup(fd); |
| 869 |
DEBUGMSGTL(("smux", "[smux_pdu_process] Wrong type %d\n", |
| 870 |
(int) type)); |
| 871 |
error = -1; |
| 872 |
break; |
| 873 |
} |
| 874 |
} |
| 875 |
return error; |
| 876 |
} |
| 877 |
|
| 878 |
static u_char * |
| 879 |
smux_open_process(int fd, u_char * ptr, size_t * len, int *fail) |
| 880 |
{ |
| 881 |
u_char type; |
| 882 |
long version; |
| 883 |
oid oid_name[MAX_OID_LEN]; |
| 884 |
char passwd[SMUXMAXSTRLEN]; |
| 885 |
char descr[SMUXMAXSTRLEN]; |
| 886 |
char oid_print[SMUXMAXSTRLEN]; |
| 887 |
int i; |
| 888 |
size_t oid_name_len, string_len; |
| 889 |
|
| 890 |
if (!(ptr = asn_parse_int(ptr, len, &type, &version, sizeof(version)))) { |
| 891 |
DEBUGMSGTL(("smux", "[smux_open_process] version parse failed\n")); |
| 892 |
*fail = TRUE; |
| 893 |
return ((ptr += *len)); |
| 894 |
} |
| 895 |
DEBUGMSGTL(("smux", |
| 896 |
"[smux_open_process] version %ld, len %" NETSNMP_PRIz |
| 897 |
"u, type %d\n", version, *len, (int) type)); |
| 898 |
|
| 899 |
oid_name_len = MAX_OID_LEN; |
| 900 |
if ((ptr = asn_parse_objid(ptr, len, &type, oid_name, |
| 901 |
&oid_name_len)) == NULL) { |
| 902 |
DEBUGMSGTL(("smux", "[smux_open_process] oid parse failed\n")); |
| 903 |
*fail = TRUE; |
| 904 |
return ((ptr += *len)); |
| 905 |
} |
| 906 |
snprint_objid(oid_print, sizeof(oid_print), oid_name, oid_name_len); |
| 907 |
|
| 908 |
if (snmp_get_do_debugging()) { |
| 909 |
DEBUGMSGTL(("smux", "[smux_open_process] smux peer: %s\n", |
| 910 |
oid_print)); |
| 911 |
DEBUGMSGTL(("smux", "[smux_open_process] len %" NETSNMP_PRIz |
| 912 |
"u, type %d\n", *len, (int) type)); |
| 913 |
} |
| 914 |
|
| 915 |
string_len = SMUXMAXSTRLEN; |
| 916 |
if ((ptr = asn_parse_string(ptr, len, &type, (u_char *) descr, |
| 917 |
&string_len)) == NULL) { |
| 918 |
DEBUGMSGTL(("smux", "[smux_open_process] descr parse failed\n")); |
| 919 |
*fail = TRUE; |
| 920 |
return ((ptr += *len)); |
| 921 |
} |
| 922 |
|
| 923 |
if (snmp_get_do_debugging()) { |
| 924 |
DEBUGMSGTL(("smux", "[smux_open_process] smux peer descr: ")); |
| 925 |
for (i = 0; i < (int) string_len; i++) |
| 926 |
DEBUGMSG(("smux", "%c", descr[i])); |
| 927 |
DEBUGMSG(("smux", "\n")); |
| 928 |
DEBUGMSGTL(("smux", "[smux_open_process] len %" NETSNMP_PRIz |
| 929 |
"u, type %d\n", *len, (int) type)); |
| 930 |
} |
| 931 |
descr[string_len] = 0; |
| 932 |
|
| 933 |
string_len = SMUXMAXSTRLEN; |
| 934 |
if ((ptr = asn_parse_string(ptr, len, &type, (u_char *) passwd, |
| 935 |
&string_len)) == NULL) { |
| 936 |
DEBUGMSGTL(("smux", "[smux_open_process] passwd parse failed\n")); |
| 937 |
*fail = TRUE; |
| 938 |
return ((ptr += *len)); |
| 939 |
} |
| 940 |
|
| 941 |
if (snmp_get_do_debugging()) { |
| 942 |
DEBUGMSGTL(("smux", "[smux_open_process] smux peer passwd: ")); |
| 943 |
for (i = 0; i < (int) string_len; i++) |
| 944 |
DEBUGMSG(("smux", "%c", passwd[i])); |
| 945 |
DEBUGMSG(("smux", "\n")); |
| 946 |
DEBUGMSGTL(("smux", "[smux_open_process] len %" NETSNMP_PRIz |
| 947 |
"u, type %d\n", *len, (int) type)); |
| 948 |
} |
| 949 |
passwd[string_len] = '\0'; |
| 950 |
if (!smux_auth_peer(oid_name, oid_name_len, passwd, fd)) { |
| 951 |
snmp_log(LOG_WARNING, |
| 952 |
"refused smux peer: oid %s, descr %s\n", |
| 953 |
oid_print, descr); |
| 954 |
*fail = TRUE; |
| 955 |
return ptr; |
| 956 |
} |
| 957 |
DEBUGMSGTL(("smux", |
| 958 |
"accepted smux peer: oid %s, descr %s\n", |
| 959 |
oid_print, descr)); |
| 960 |
*fail = FALSE; |
| 961 |
return ptr; |
| 962 |
} |
| 963 |
|
| 964 |
static void |
| 965 |
smux_send_close(int fd, int reason) |
| 966 |
{ |
| 967 |
u_char outpacket[3], *ptr; |
| 968 |
|
| 969 |
ptr = outpacket; |
| 970 |
|
| 971 |
*(ptr++) = (u_char) SMUX_CLOSE; |
| 972 |
*(ptr++) = (u_char) 1; |
| 973 |
*ptr = (u_char) (reason & 0xFF); |
| 974 |
|
| 975 |
if (snmp_get_do_debugging()) |
| 976 |
DEBUGMSGTL(("smux", |
| 977 |
"[smux_close] sending close to fd %d, reason %d\n", fd, |
| 978 |
reason)); |
| 979 |
|
| 980 |
/* |
| 981 |
* send a response back |
| 982 |
*/ |
| 983 |
if (sendto(fd, (char *) outpacket, 3, 0, NULL, 0) < 0) { |
| 984 |
snmp_log_perror("[smux_snmp_close] send failed"); |
| 985 |
} |
| 986 |
} |
| 987 |
|
| 988 |
|
| 989 |
static int |
| 990 |
smux_auth_peer(oid * name, size_t namelen, char *passwd, int fd) |
| 991 |
{ |
| 992 |
int i; |
| 993 |
char oid_print[SMUXMAXSTRLEN]; |
| 994 |
|
| 995 |
if (snmp_get_do_debugging()) { |
| 996 |
snprint_objid(oid_print, sizeof(oid_print), name, namelen); |
| 997 |
DEBUGMSGTL(("smux:auth", "[smux_auth_peer] Authorizing: %s, %s\n", |
| 998 |
oid_print, passwd)); |
| 999 |
} |
| 1000 |
|
| 1001 |
for (i = 0; i < nauths; i++) { |
| 1002 |
if (snmp_get_do_debugging()) { |
| 1003 |
snprint_objid(oid_print, sizeof(oid_print), |
| 1004 |
Auths[i]->sa_oid, Auths[i]->sa_oid_len); |
| 1005 |
DEBUGMSGTL(("smux:auth", "[smux_auth_peer] Checking OID: %s (%d)\n", |
| 1006 |
oid_print, i)); |
| 1007 |
} |
| 1008 |
if (snmp_oid_compare(Auths[i]->sa_oid, Auths[i]->sa_oid_len, |
| 1009 |
name, namelen) == 0) { |
| 1010 |
if (snmp_get_do_debugging()) { |
| 1011 |
DEBUGMSGTL(("smux:auth", "[smux_auth_peer] Checking P/W: %s (%d)\n", |
| 1012 |
Auths[i]->sa_passwd, Auths[i]->sa_active_fd)); |
| 1013 |
} |
| 1014 |
if (!(strcmp(Auths[i]->sa_passwd, passwd)) && |
| 1015 |
(Auths[i]->sa_active_fd == -1)) { |
| 1016 |
/* |
| 1017 |
* matched, mark the auth |
| 1018 |
*/ |
| 1019 |
Auths[i]->sa_active_fd = fd; |
| 1020 |
return 1; |
| 1021 |
} |
| 1022 |
} |
| 1023 |
} |
| 1024 |
/* |
| 1025 |
* did not match oid and passwd |
| 1026 |
*/ |
| 1027 |
return 0; |
| 1028 |
} |
| 1029 |
|
| 1030 |
|
| 1031 |
/* |
| 1032 |
* XXX - Bells and Whistles: |
| 1033 |
* Need to catch signal when snmpd goes down and send close pdu to gated |
| 1034 |
*/ |
| 1035 |
static u_char * |
| 1036 |
smux_close_process(int fd, u_char * ptr, size_t * len) |
| 1037 |
{ |
| 1038 |
long down = 0; |
| 1039 |
int length = *len; |
| 1040 |
|
| 1041 |
/* |
| 1042 |
* This is the integer part of the close pdu |
| 1043 |
*/ |
| 1044 |
while (length--) { |
| 1045 |
down = (down << 8) | (long) *ptr; |
| 1046 |
ptr++; |
| 1047 |
} |
| 1048 |
|
| 1049 |
DEBUGMSGTL(("smux", |
| 1050 |
"[smux_close_process] close from peer on fd %d reason %ld\n", |
| 1051 |
fd, down)); |
| 1052 |
smux_peer_cleanup(fd); |
| 1053 |
|
| 1054 |
return NULL; |
| 1055 |
} |
| 1056 |
|
| 1057 |
static u_char * |
| 1058 |
smux_rreq_process(int sd, u_char * ptr, size_t * len) |
| 1059 |
{ |
| 1060 |
long priority, rpriority; |
| 1061 |
long operation; |
| 1062 |
oid oid_name[MAX_OID_LEN]; |
| 1063 |
size_t oid_name_len; |
| 1064 |
int i, result; |
| 1065 |
u_char type; |
| 1066 |
smux_reg *rptr, *nrptr; |
| 1067 |
netsnmp_handler_registration *reg; |
| 1068 |
|
| 1069 |
oid_name_len = MAX_OID_LEN; |
| 1070 |
ptr = asn_parse_objid(ptr, len, &type, oid_name, &oid_name_len); |
| 1071 |
|
| 1072 |
DEBUGMSGTL(("smux", "[smux_rreq_process] smux subtree: ")); |
| 1073 |
DEBUGMSGOID(("smux", oid_name, oid_name_len)); |
| 1074 |
DEBUGMSG(("smux", "\n")); |
| 1075 |
|
| 1076 |
if ((ptr = asn_parse_int(ptr, len, &type, &priority, |
| 1077 |
sizeof(priority))) == NULL) { |
| 1078 |
DEBUGMSGTL(("smux", |
| 1079 |
"[smux_rreq_process] priority parse failed\n")); |
| 1080 |
smux_send_rrsp(sd, -1); |
| 1081 |
return NULL; |
| 1082 |
} |
| 1083 |
DEBUGMSGTL(("smux", "[smux_rreq_process] priority %ld\n", priority)); |
| 1084 |
|
| 1085 |
if ((ptr = asn_parse_int(ptr, len, &type, &operation, |
| 1086 |
sizeof(operation))) == NULL) { |
| 1087 |
DEBUGMSGTL(("smux", |
| 1088 |
"[smux_rreq_process] operation parse failed\n")); |
| 1089 |
smux_send_rrsp(sd, -1); |
| 1090 |
return NULL; |
| 1091 |
} |
| 1092 |
DEBUGMSGTL(("smux", "[smux_rreq_process] operation %ld\n", operation)); |
| 1093 |
|
| 1094 |
if (operation == SMUX_REGOP_DELETE) { |
| 1095 |
/* |
| 1096 |
* search the active list for this registration |
| 1097 |
*/ |
| 1098 |
rptr = |
| 1099 |
smux_find_match(ActiveRegs, sd, oid_name, oid_name_len, |
| 1100 |
priority); |
| 1101 |
if (rptr) { |
| 1102 |
rpriority = rptr->sr_priority; |
| 1103 |
/* |
| 1104 |
* unregister the mib |
| 1105 |
*/ |
| 1106 |
unregister_mib(rptr->sr_name, rptr->sr_name_len); |
| 1107 |
/* |
| 1108 |
* find a replacement |
| 1109 |
*/ |
| 1110 |
nrptr = |
| 1111 |
smux_find_replacement(rptr->sr_name, rptr->sr_name_len); |
| 1112 |
if (nrptr) { |
| 1113 |
/* |
| 1114 |
* found one |
| 1115 |
*/ |
| 1116 |
smux_replace_active(rptr, nrptr); |
| 1117 |
} else { |
| 1118 |
/* |
| 1119 |
* no replacement found |
| 1120 |
*/ |
| 1121 |
smux_list_detach(&ActiveRegs, rptr); |
| 1122 |
free(rptr); |
| 1123 |
} |
| 1124 |
smux_send_rrsp(sd, rpriority); |
| 1125 |
return ptr; |
| 1126 |
} |
| 1127 |
/* |
| 1128 |
* search the passive list for this registration |
| 1129 |
*/ |
| 1130 |
rptr = |
| 1131 |
smux_find_match(PassiveRegs, sd, oid_name, oid_name_len, |
| 1132 |
priority); |
| 1133 |
if (rptr) { |
| 1134 |
rpriority = rptr->sr_priority; |
| 1135 |
smux_list_detach(&PassiveRegs, rptr); |
| 1136 |
free(rptr); |
| 1137 |
smux_send_rrsp(sd, rpriority); |
| 1138 |
return ptr; |
| 1139 |
} |
| 1140 |
/* |
| 1141 |
* This peer cannot unregister the tree, it does not |
| 1142 |
* * belong to him. Send him an error. |
| 1143 |
*/ |
| 1144 |
smux_send_rrsp(sd, -1); |
| 1145 |
return ptr; |
| 1146 |
|
| 1147 |
} else if ((operation == SMUX_REGOP_REGISTER_RO) || |
| 1148 |
(operation == SMUX_REGOP_REGISTER_RW)) { |
| 1149 |
if (priority < -1) { |
| 1150 |
DEBUGMSGTL(("smux", |
| 1151 |
"[smux_rreq_process] peer fd %d invalid priority %ld", |
| 1152 |
sd, priority)); |
| 1153 |
smux_send_rrsp(sd, -1); |
| 1154 |
return NULL; |
| 1155 |
} |
| 1156 |
if ((nrptr = malloc(sizeof(smux_reg))) == NULL) { |
| 1157 |
snmp_log_perror("[smux_rreq_process] malloc"); |
| 1158 |
smux_send_rrsp(sd, -1); |
| 1159 |
return NULL; |
| 1160 |
} |
| 1161 |
nrptr->sr_priority = priority; |
| 1162 |
nrptr->sr_name_len = oid_name_len; |
| 1163 |
nrptr->sr_fd = sd; |
| 1164 |
for (i = 0; i < (int) oid_name_len; i++) |
| 1165 |
nrptr->sr_name[i] = oid_name[i]; |
| 1166 |
|
| 1167 |
/* |
| 1168 |
* See if this tree matches or scopes any of the |
| 1169 |
* * active trees. |
| 1170 |
*/ |
| 1171 |
for (rptr = ActiveRegs; rptr; rptr = rptr->sr_next) { |
| 1172 |
result = |
| 1173 |
snmp_oid_compare(oid_name, oid_name_len, rptr->sr_name, |
| 1174 |
rptr->sr_name_len); |
| 1175 |
if (result == 0) { |
| 1176 |
if (oid_name_len == rptr->sr_name_len) { |
| 1177 |
if (nrptr->sr_priority == -1) { |
| 1178 |
nrptr->sr_priority = rptr->sr_priority; |
| 1179 |
do { |
| 1180 |
nrptr->sr_priority++; |
| 1181 |
} while (smux_list_add(&PassiveRegs, nrptr)); |
| 1182 |
goto done; |
| 1183 |
} else if (nrptr->sr_priority < rptr->sr_priority) { |
| 1184 |
/* |
| 1185 |
* Better priority. There are no better |
| 1186 |
* * priorities for this tree in the passive list, |
| 1187 |
* * so replace the current active tree. |
| 1188 |
*/ |
| 1189 |
smux_replace_active(rptr, nrptr); |
| 1190 |
goto done; |
| 1191 |
} else { |
| 1192 |
/* |
| 1193 |
* Equal or worse priority |
| 1194 |
*/ |
| 1195 |
do { |
| 1196 |
nrptr->sr_priority++; |
| 1197 |
} while (smux_list_add(&PassiveRegs, nrptr) == -1); |
| 1198 |
goto done; |
| 1199 |
} |
| 1200 |
} else if (oid_name_len < rptr->sr_name_len) { |
| 1201 |
/* |
| 1202 |
* This tree scopes a current active |
| 1203 |
* * tree. Replace the current active tree. |
| 1204 |
*/ |
| 1205 |
smux_replace_active(rptr, nrptr); |
| 1206 |
goto done; |
| 1207 |
} else { /* oid_name_len > rptr->sr_name_len */ |
| 1208 |
/* |
| 1209 |
* This tree is scoped by a current |
| 1210 |
* * active tree. |
| 1211 |
*/ |
| 1212 |
do { |
| 1213 |
nrptr->sr_priority++; |
| 1214 |
} while (smux_list_add(&PassiveRegs, nrptr) == -1); |
| 1215 |
goto done; |
| 1216 |
} |
| 1217 |
} |
| 1218 |
} |
| 1219 |
/* |
| 1220 |
* We didn't find it in the active list. Add it at |
| 1221 |
* * the requested priority. |
| 1222 |
*/ |
| 1223 |
if (nrptr->sr_priority == -1) |
| 1224 |
nrptr->sr_priority = 0; |
| 1225 |
|
| 1226 |
reg = netsnmp_create_handler_registration("smux", |
| 1227 |
smux_handler, |
| 1228 |
nrptr->sr_name, |
| 1229 |
nrptr->sr_name_len, |
| 1230 |
HANDLER_CAN_RWRITE); |
| 1231 |
if (reg == NULL) { |
| 1232 |
snmp_log(LOG_ERR, "SMUX: cannot create new smux peer " |
| 1233 |
"registration\n"); |
| 1234 |
smux_send_rrsp(sd, -1); |
| 1235 |
free(nrptr); |
| 1236 |
return NULL; |
| 1237 |
} |
| 1238 |
if (netsnmp_register_handler(reg) != MIB_REGISTERED_OK) { |
| 1239 |
snmp_log(LOG_ERR, "SMUX: cannot register new smux peer\n"); |
| 1240 |
smux_send_rrsp(sd, -1); |
| 1241 |
free(nrptr); |
| 1242 |
return NULL; |
| 1243 |
} |
| 1244 |
nrptr->reginfo = reg; |
| 1245 |
smux_list_add(&ActiveRegs, nrptr); |
| 1246 |
|
| 1247 |
done: |
| 1248 |
smux_send_rrsp(sd, nrptr->sr_priority); |
| 1249 |
return ptr; |
| 1250 |
} else { |
| 1251 |
DEBUGMSGTL(("smux", "[smux_rreq_process] unknown operation\n")); |
| 1252 |
smux_send_rrsp(sd, -1); |
| 1253 |
return NULL; |
| 1254 |
} |
| 1255 |
} |
| 1256 |
|
| 1257 |
/* |
| 1258 |
* Find the registration with a matching descriptor, OID and priority. If |
| 1259 |
* the priority is -1 then find a registration with a matching descriptor, |
| 1260 |
* a matching OID, and the highest priority. |
| 1261 |
*/ |
| 1262 |
static smux_reg * |
| 1263 |
smux_find_match(smux_reg * regs, int sd, oid * oid_name, |
| 1264 |
size_t oid_name_len, long priority) |
| 1265 |
{ |
| 1266 |
smux_reg *rptr, *bestrptr; |
| 1267 |
|
| 1268 |
bestrptr = NULL; |
| 1269 |
for (rptr = regs; rptr; rptr = rptr->sr_next) { |
| 1270 |
if (rptr->sr_fd != sd) |
| 1271 |
continue; |
| 1272 |
if (snmp_oid_compare |
| 1273 |
(rptr->sr_name, rptr->sr_name_len, oid_name, oid_name_len)) |
| 1274 |
continue; |
| 1275 |
if (rptr->sr_priority == priority) |
| 1276 |
return rptr; |
| 1277 |
if (priority != -1) |
| 1278 |
continue; |
| 1279 |
if (bestrptr) { |
| 1280 |
if (bestrptr->sr_priority > rptr->sr_priority) |
| 1281 |
bestrptr = rptr; |
| 1282 |
} else { |
| 1283 |
bestrptr = rptr; |
| 1284 |
} |
| 1285 |
} |
| 1286 |
return bestrptr; |
| 1287 |
} |
| 1288 |
|
| 1289 |
static void |
| 1290 |
smux_replace_active(smux_reg * actptr, smux_reg * pasptr) |
| 1291 |
{ |
| 1292 |
netsnmp_handler_registration *reg; |
| 1293 |
|
| 1294 |
smux_list_detach(&ActiveRegs, actptr); |
| 1295 |
if (actptr->reginfo) { |
| 1296 |
netsnmp_unregister_handler(actptr->reginfo); |
| 1297 |
actptr->reginfo = NULL; |
| 1298 |
} |
| 1299 |
|
| 1300 |
smux_list_detach(&PassiveRegs, pasptr); |
| 1301 |
|
| 1302 |
(void) smux_list_add(&ActiveRegs, pasptr); |
| 1303 |
free(actptr); |
| 1304 |
|
| 1305 |
reg = netsnmp_create_handler_registration("smux", |
| 1306 |
smux_handler, |
| 1307 |
pasptr->sr_name, |
| 1308 |
pasptr->sr_name_len, |
| 1309 |
HANDLER_CAN_RWRITE); |
| 1310 |
if (reg == NULL) { |
| 1311 |
snmp_log(LOG_ERR, "SMUX: cannot create new smux peer registration\n"); |
| 1312 |
pasptr->reginfo = NULL; |
| 1313 |
return; |
| 1314 |
} |
| 1315 |
if (netsnmp_register_handler(reg) != MIB_REGISTERED_OK) { |
| 1316 |
snmp_log(LOG_ERR, "SMUX: cannot register new smux peer\n"); |
| 1317 |
pasptr->reginfo = NULL; |
| 1318 |
return; |
| 1319 |
} |
| 1320 |
pasptr->reginfo = reg; |
| 1321 |
} |
| 1322 |
|
| 1323 |
static void |
| 1324 |
smux_list_detach(smux_reg ** head, smux_reg * m_remove) |
| 1325 |
{ |
| 1326 |
smux_reg *rptr, *rptr2; |
| 1327 |
|
| 1328 |
if (*head == NULL) { |
| 1329 |
DEBUGMSGTL(("smux", "[smux_list_detach] Ouch!")); |
| 1330 |
return; |
| 1331 |
} |
| 1332 |
if (*head == m_remove) { |
| 1333 |
*head = (*head)->sr_next; |
| 1334 |
return; |
| 1335 |
} |
| 1336 |
for (rptr = *head, rptr2 = rptr->sr_next; rptr2; |
| 1337 |
rptr2 = rptr2->sr_next, rptr = rptr->sr_next) { |
| 1338 |
if (rptr2 == m_remove) { |
| 1339 |
rptr->sr_next = rptr2->sr_next; |
| 1340 |
return; |
| 1341 |
} |
| 1342 |
} |
| 1343 |
} |
| 1344 |
|
| 1345 |
/* |
| 1346 |
* Attempt to add a registration (in order) to a list. If the |
| 1347 |
* add fails (because of an existing registration with equal |
| 1348 |
* priority) return -1. |
| 1349 |
*/ |
| 1350 |
static int |
| 1351 |
smux_list_add(smux_reg ** head, smux_reg * add) |
| 1352 |
{ |
| 1353 |
smux_reg *rptr, *prev; |
| 1354 |
int result; |
| 1355 |
|
| 1356 |
if (*head == NULL) { |
| 1357 |
*head = add; |
| 1358 |
(*head)->sr_next = NULL; |
| 1359 |
return 0; |
| 1360 |
} |
| 1361 |
prev = NULL; |
| 1362 |
for (rptr = *head; rptr; rptr = rptr->sr_next) { |
| 1363 |
result = snmp_oid_compare(add->sr_name, add->sr_name_len, |
| 1364 |
rptr->sr_name, rptr->sr_name_len); |
| 1365 |
if (result == 0) { |
| 1366 |
/* |
| 1367 |
* Same tree... |
| 1368 |
*/ |
| 1369 |
if (add->sr_priority == rptr->sr_priority) { |
| 1370 |
/* |
| 1371 |
* ... same pri : nope |
| 1372 |
*/ |
| 1373 |
return -1; |
| 1374 |
} else if (add->sr_priority < rptr->sr_priority) { |
| 1375 |
/* |
| 1376 |
* ... lower pri : insert and return |
| 1377 |
*/ |
| 1378 |
add->sr_next = rptr; |
| 1379 |
if ( prev ) { prev->sr_next = add; } |
| 1380 |
else { *head = add; } |
| 1381 |
return 0; |
| 1382 |
#ifdef XXX |
| 1383 |
} else { |
| 1384 |
/* |
| 1385 |
* ... higher pri : put after |
| 1386 |
*/ |
| 1387 |
add->sr_next = rptr->sr_next; |
| 1388 |
rptr->sr_next = add; |
| 1389 |
#endif |
| 1390 |
} |
| 1391 |
} else if (result < 0) { |
| 1392 |
/* |
| 1393 |
* Earlier tree : insert and return |
| 1394 |
*/ |
| 1395 |
add->sr_next = rptr; |
| 1396 |
if ( prev ) { prev->sr_next = add; } |
| 1397 |
else { *head = add; } |
| 1398 |
return 0; |
| 1399 |
#ifdef XXX |
| 1400 |
} else { |
| 1401 |
/* |
| 1402 |
* Later tree : put after |
| 1403 |
*/ |
| 1404 |
add->sr_next = rptr->sr_next; |
| 1405 |
rptr->sr_next = add; |
| 1406 |
return 0; |
| 1407 |
#endif |
| 1408 |
} |
| 1409 |
prev = rptr; |
| 1410 |
} |
| 1411 |
/* |
| 1412 |
* Otherwise, this entry must come last |
| 1413 |
*/ |
| 1414 |
if ( prev ) { prev->sr_next = add; } |
| 1415 |
else { *head = add; } |
| 1416 |
add->sr_next = NULL; |
| 1417 |
return 0; |
| 1418 |
} |
| 1419 |
|
| 1420 |
/* |
| 1421 |
* Find a replacement for this registration. In order |
| 1422 |
* of preference: |
| 1423 |
* |
| 1424 |
* - Least difference in subtree length |
| 1425 |
* - Best (lowest) priority |
| 1426 |
* |
| 1427 |
* For example, if we need to replace .1.3.6.1.69, |
| 1428 |
* we would pick .1.3.6.1.69.1 instead of .1.3.6.69.1.1 |
| 1429 |
* |
| 1430 |
*/ |
| 1431 |
static smux_reg * |
| 1432 |
smux_find_replacement(oid * name, size_t name_len) |
| 1433 |
{ |
| 1434 |
smux_reg *rptr, *bestptr; |
| 1435 |
int bestlen, difflen; |
| 1436 |
|
| 1437 |
bestlen = SMUX_MAX_PRIORITY; |
| 1438 |
bestptr = NULL; |
| 1439 |
|
| 1440 |
for (rptr = PassiveRegs; rptr; rptr = rptr->sr_next) { |
| 1441 |
if (!snmp_oidtree_compare(rptr->sr_name, rptr->sr_name_len, |
| 1442 |
name, name_len)) { |
| 1443 |
if ((difflen = rptr->sr_name_len - name_len) |
| 1444 |
< bestlen || !bestptr) { |
| 1445 |
bestlen = difflen; |
| 1446 |
bestptr = rptr; |
| 1447 |
} else if ((difflen == bestlen) && |
| 1448 |
(rptr->sr_priority < bestptr->sr_priority)) |
| 1449 |
bestptr = rptr; |
| 1450 |
} |
| 1451 |
} |
| 1452 |
return bestptr; |
| 1453 |
} |
| 1454 |
|
| 1455 |
u_char * |
| 1456 |
smux_snmp_process(int exact, |
| 1457 |
oid * objid, |
| 1458 |
size_t * len, |
| 1459 |
size_t * return_len, u_char * return_type, int sd) |
| 1460 |
{ |
| 1461 |
u_char packet[SMUXMAXPKTSIZE], *ptr, result[SMUXMAXPKTSIZE]; |
| 1462 |
ssize_t length = SMUXMAXPKTSIZE; |
| 1463 |
int tmp_length; |
| 1464 |
u_char type; |
| 1465 |
size_t packet_len; |
| 1466 |
|
| 1467 |
/* |
| 1468 |
* Send the query to the peer |
| 1469 |
*/ |
| 1470 |
if (exact) |
| 1471 |
type = SMUX_GET; |
| 1472 |
else |
| 1473 |
type = SMUX_GETNEXT; |
| 1474 |
|
| 1475 |
if (smux_build(type, smux_reqid, objid, len, 0, NULL, |
| 1476 |
*len, packet, (size_t *) &length) < 0) { |
| 1477 |
snmp_log(LOG_ERR, "[smux_snmp_process]: smux_build failed\n"); |
| 1478 |
return NULL; |
| 1479 |
} |
| 1480 |
DEBUGMSGTL(("smux", "[smux_snmp_process] oid from build: ")); |
| 1481 |
DEBUGMSGOID(("smux", objid, *len)); |
| 1482 |
DEBUGMSG(("smux", "\n")); |
| 1483 |
|
| 1484 |
if (sendto(sd, (char *) packet, length, 0, NULL, 0) < 0) { |
| 1485 |
snmp_log_perror("[smux_snmp_process] send failed"); |
| 1486 |
} |
| 1487 |
|
| 1488 |
DEBUGMSGTL(("smux", |
| 1489 |
"[smux_snmp_process] Sent %d request to peer; %" NETSNMP_PRIz "d bytes\n", |
| 1490 |
(int) type, length)); |
| 1491 |
|
| 1492 |
while (1) { |
| 1493 |
/* |
| 1494 |
* peek at what's received |
| 1495 |
*/ |
| 1496 |
length = recvfrom(sd, (char *) result, SMUXMAXPKTSIZE, MSG_PEEK, NULL, |
| 1497 |
NULL); |
| 1498 |
if (length <= 0) { |
| 1499 |
if ((length == -1) && ((errno == EINTR) || (errno == EAGAIN))) |
| 1500 |
{ |
| 1501 |
continue; |
| 1502 |
} |
| 1503 |
else |
| 1504 |
{ |
| 1505 |
snmp_log_perror("[smux_snmp_process] peek failed"); |
| 1506 |
smux_peer_cleanup(sd); |
| 1507 |
smux_snmp_select_list_del(sd); |
| 1508 |
return NULL; |
| 1509 |
} |
| 1510 |
} |
| 1511 |
|
| 1512 |
DEBUGMSGTL(("smux", "[smux_snmp_process] Peeked at %" NETSNMP_PRIz "d bytes\n", |
| 1513 |
length)); |
| 1514 |
DEBUGDUMPSETUP("smux_snmp_process", result, length); |
| 1515 |
|
| 1516 |
/* |
| 1517 |
* determine if we received more than one packet |
| 1518 |
*/ |
| 1519 |
packet_len = length; |
| 1520 |
ptr = asn_parse_header(result, &packet_len, &type); |
| 1521 |
if (ptr == NULL) |
| 1522 |
return NULL; |
| 1523 |
packet_len += (ptr - result); |
| 1524 |
if (length > packet_len) { |
| 1525 |
/* |
| 1526 |
* set length to receive only the first packet |
| 1527 |
*/ |
| 1528 |
length = packet_len; |
| 1529 |
} |
| 1530 |
|
| 1531 |
/* |
| 1532 |
* receive the first packet |
| 1533 |
*/ |
| 1534 |
tmp_length = length; |
| 1535 |
do |
| 1536 |
{ |
| 1537 |
length = tmp_length; |
| 1538 |
length = recvfrom(sd, (char *) result, length, 0, NULL, NULL); |
| 1539 |
} |
| 1540 |
while((length == -1) && ((errno == EINTR) || (errno == EAGAIN))); |
| 1541 |
|
| 1542 |
if (length <= 0) { |
| 1543 |
snmp_log_perror("[smux_snmp_process] recv failed"); |
| 1544 |
smux_peer_cleanup(sd); |
| 1545 |
smux_snmp_select_list_del(sd); |
| 1546 |
return NULL; |
| 1547 |
} |
| 1548 |
|
| 1549 |
DEBUGMSGTL(("smux", "[smux_snmp_process] Received %" NETSNMP_PRIz "d bytes\n", |
| 1550 |
length)); |
| 1551 |
|
| 1552 |
if (result[0] == SMUX_TRAP) { |
| 1553 |
DEBUGMSGTL(("smux", "[smux_snmp_process] Received trap\n")); |
| 1554 |
DEBUGMSGTL(("smux", "Got trap from peer on fd %d\n", sd)); |
| 1555 |
ptr = asn_parse_header(result, (size_t *) &length, &type); |
| 1556 |
if (ptr == NULL) |
| 1557 |
return NULL; |
| 1558 |
smux_trap_process(ptr, (size_t *) &length); |
| 1559 |
|
| 1560 |
/* |
| 1561 |
* go and peek at received data again |
| 1562 |
*/ |
| 1563 |
/* |
| 1564 |
* we could receive the reply or another trap |
| 1565 |
*/ |
| 1566 |
} else { |
| 1567 |
/* |
| 1568 |
* Interpret reply |
| 1569 |
*/ |
| 1570 |
ptr = smux_parse(result, objid, len, return_len, return_type); |
| 1571 |
/* |
| 1572 |
* ptr will point to query result or NULL if error |
| 1573 |
*/ |
| 1574 |
break; |
| 1575 |
} |
| 1576 |
} /* while (1) */ |
| 1577 |
|
| 1578 |
return ptr; |
| 1579 |
} |
| 1580 |
|
| 1581 |
static u_char * |
| 1582 |
smux_parse(u_char * rsp, |
| 1583 |
oid * objid, |
| 1584 |
size_t * oidlen, size_t * return_len, u_char * return_type) |
| 1585 |
{ |
| 1586 |
size_t length = SMUXMAXPKTSIZE; |
| 1587 |
u_char *ptr, type; |
| 1588 |
long reqid, errstat, errindex; |
| 1589 |
|
| 1590 |
ptr = rsp; |
| 1591 |
|
| 1592 |
/* |
| 1593 |
* Return pointer to the snmp/smux return value. |
| 1594 |
* return_len should contain the number of bytes in the value |
| 1595 |
* returned above. |
| 1596 |
* objid is the next object, with len for GETNEXT. |
| 1597 |
* objid and len are not changed for GET |
| 1598 |
*/ |
| 1599 |
ptr = asn_parse_header(ptr, &length, &type); |
| 1600 |
if (ptr == NULL || type != SNMP_MSG_RESPONSE) |
| 1601 |
return NULL; |
| 1602 |
|
| 1603 |
if ((ptr = asn_parse_int(ptr, &length, &type, &reqid, |
| 1604 |
sizeof(reqid))) == NULL) { |
| 1605 |
DEBUGMSGTL(("smux", "[smux_parse] parse of reqid failed\n")); |
| 1606 |
return NULL; |
| 1607 |
} |
| 1608 |
if ((ptr = asn_parse_int(ptr, &length, &type, &errstat, |
| 1609 |
sizeof(errstat))) == NULL) { |
| 1610 |
DEBUGMSGTL(("smux", |
| 1611 |
"[smux_parse] parse of error status failed\n")); |
| 1612 |
return NULL; |
| 1613 |
} |
| 1614 |
if ((ptr = asn_parse_int(ptr, &length, &type, &errindex, |
| 1615 |
sizeof(errindex))) == NULL) { |
| 1616 |
DEBUGMSGTL(("smux", "[smux_parse] parse of error index failed\n")); |
| 1617 |
return NULL; |
| 1618 |
} |
| 1619 |
|
| 1620 |
/* |
| 1621 |
* XXX How to send something intelligent back in case of an error |
| 1622 |
*/ |
| 1623 |
DEBUGMSGTL(("smux", |
| 1624 |
"[smux_parse] Message type %d, reqid %ld, errstat %ld, \n\terrindex %ld\n", |
| 1625 |
(int) type, reqid, errstat, errindex)); |
| 1626 |
if (ptr == NULL || errstat != SNMP_ERR_NOERROR) |
| 1627 |
return NULL; |
| 1628 |
|
| 1629 |
/* |
| 1630 |
* stuff to return |
| 1631 |
*/ |
| 1632 |
return (smux_parse_var |
| 1633 |
(ptr, &length, objid, oidlen, return_len, return_type)); |
| 1634 |
} |
| 1635 |
|
| 1636 |
|
| 1637 |
static u_char * |
| 1638 |
smux_parse_var(u_char * varbind, |
| 1639 |
size_t * varbindlength, |
| 1640 |
oid * objid, |
| 1641 |
size_t * oidlen, size_t * varlength, u_char * vartype) |
| 1642 |
{ |
| 1643 |
oid var_name[MAX_OID_LEN]; |
| 1644 |
size_t var_name_len; |
| 1645 |
size_t var_val_len; |
| 1646 |
u_char *var_val; |
| 1647 |
size_t str_len, objid_len; |
| 1648 |
size_t len; |
| 1649 |
u_char *ptr; |
| 1650 |
u_char type; |
| 1651 |
|
| 1652 |
ptr = varbind; |
| 1653 |
len = *varbindlength; |
| 1654 |
|
| 1655 |
DEBUGMSGTL(("smux", "[smux_parse_var] before any processing: ")); |
| 1656 |
DEBUGMSGOID(("smux", objid, *oidlen)); |
| 1657 |
DEBUGMSG(("smux", "\n")); |
| 1658 |
|
| 1659 |
ptr = asn_parse_header(ptr, &len, &type); |
| 1660 |
if (ptr == NULL || type != (ASN_SEQUENCE | ASN_CONSTRUCTOR)) { |
| 1661 |
snmp_log(LOG_NOTICE, "[smux_parse_var] Panic: type %d\n", |
| 1662 |
(int) type); |
| 1663 |
return NULL; |
| 1664 |
} |
| 1665 |
|
| 1666 |
/* |
| 1667 |
* get hold of the objid and the asn1 coded value |
| 1668 |
*/ |
| 1669 |
var_name_len = MAX_OID_LEN; |
| 1670 |
ptr = snmp_parse_var_op(ptr, var_name, &var_name_len, vartype, |
| 1671 |
&var_val_len, &var_val, &len); |
| 1672 |
|
| 1673 |
*oidlen = var_name_len; |
| 1674 |
memcpy(objid, var_name, var_name_len * sizeof(oid)); |
| 1675 |
|
| 1676 |
DEBUGMSGTL(("smux", "[smux_parse_var] returning oid : ")); |
| 1677 |
DEBUGMSGOID(("smux", objid, *oidlen)); |
| 1678 |
DEBUGMSG(("smux", "\n")); |
| 1679 |
/* |
| 1680 |
* XXX |
| 1681 |
*/ |
| 1682 |
len = SMUXMAXPKTSIZE; |
| 1683 |
DEBUGMSGTL(("smux", |
| 1684 |
"[smux_parse_var] Asn coded len of var %" NETSNMP_PRIz |
| 1685 |
"u, type %d\n", var_val_len, (int) *vartype)); |
| 1686 |
|
| 1687 |
switch ((short) *vartype) { |
| 1688 |
case ASN_INTEGER: |
| 1689 |
*varlength = sizeof(long); |
| 1690 |
asn_parse_int(var_val, &len, vartype, |
| 1691 |
(long *) &smux_long, *varlength); |
| 1692 |
return (u_char *) & smux_long; |
| 1693 |
break; |
| 1694 |
case ASN_COUNTER: |
| 1695 |
case ASN_GAUGE: |
| 1696 |
case ASN_TIMETICKS: |
| 1697 |
case ASN_UINTEGER: |
| 1698 |
*varlength = sizeof(u_long); |
| 1699 |
asn_parse_unsigned_int(var_val, &len, vartype, |
| 1700 |
(u_long *) & smux_ulong, *varlength); |
| 1701 |
return (u_char *) & smux_ulong; |
| 1702 |
break; |
| 1703 |
case ASN_COUNTER64: |
| 1704 |
*varlength = sizeof(smux_counter64); |
| 1705 |
asn_parse_unsigned_int64(var_val, &len, vartype, |
| 1706 |
(struct counter64 *) &smux_counter64, |
| 1707 |
*varlength); |
| 1708 |
return (u_char *) & smux_counter64; |
| 1709 |
break; |
| 1710 |
case ASN_IPADDRESS: |
| 1711 |
*varlength = 4; |
| 1712 |
/* |
| 1713 |
* consume the tag and length, but just copy here |
| 1714 |
* because we know it is an ip address |
| 1715 |
*/ |
| 1716 |
if ((var_val = asn_parse_header(var_val, &len, &type)) == NULL) |
| 1717 |
return NULL; |
| 1718 |
memcpy((u_char *) & (smux_sa.sin_addr.s_addr), var_val, |
| 1719 |
*varlength); |
| 1720 |
return (u_char *) & (smux_sa.sin_addr.s_addr); |
| 1721 |
break; |
| 1722 |
case ASN_OCTET_STR: |
| 1723 |
/* |
| 1724 |
* XXX |
| 1725 |
*/ |
| 1726 |
if (len == 0) |
| 1727 |
return NULL; |
| 1728 |
str_len = SMUXMAXSTRLEN; |
| 1729 |
asn_parse_string(var_val, &len, vartype, smux_str, &str_len); |
| 1730 |
*varlength = str_len; |
| 1731 |
return smux_str; |
| 1732 |
break; |
| 1733 |
case ASN_OPAQUE: |
| 1734 |
case ASN_NSAP: |
| 1735 |
case ASN_OBJECT_ID: |
| 1736 |
objid_len = MAX_OID_LEN; |
| 1737 |
asn_parse_objid(var_val, &len, vartype, smux_objid, &objid_len); |
| 1738 |
*varlength = objid_len * sizeof(oid); |
| 1739 |
return (u_char *) smux_objid; |
| 1740 |
break; |
| 1741 |
case SNMP_NOSUCHOBJECT: |
| 1742 |
case SNMP_NOSUCHINSTANCE: |
| 1743 |
case SNMP_ENDOFMIBVIEW: |
| 1744 |
case ASN_NULL: |
| 1745 |
return NULL; |
| 1746 |
break; |
| 1747 |
case ASN_BIT_STR: |
| 1748 |
/* |
| 1749 |
* XXX |
| 1750 |
*/ |
| 1751 |
if (len == 0) |
| 1752 |
return NULL; |
| 1753 |
str_len = SMUXMAXSTRLEN; |
| 1754 |
asn_parse_bitstring(var_val, &len, vartype, smux_str, &str_len); |
| 1755 |
*varlength = str_len; |
| 1756 |
return (u_char *) smux_str; |
| 1757 |
break; |
| 1758 |
default: |
| 1759 |
snmp_log(LOG_ERR, "bad type returned (%x)\n", *vartype); |
| 1760 |
return NULL; |
| 1761 |
break; |
| 1762 |
} |
| 1763 |
} |
| 1764 |
|
| 1765 |
/* |
| 1766 |
* XXX This is a bad hack - do not want to muck with ucd code |
| 1767 |
*/ |
| 1768 |
static int |
| 1769 |
smux_build(u_char type, |
| 1770 |
long reqid, |
| 1771 |
oid * objid, |
| 1772 |
size_t * oidlen, |
| 1773 |
u_char val_type, |
| 1774 |
u_char * val, size_t val_len, u_char * packet, size_t * length) |
| 1775 |
{ |
| 1776 |
u_char *ptr, *save1, *save2; |
| 1777 |
size_t len; |
| 1778 |
long errstat = 0; |
| 1779 |
long errindex = 0; |
| 1780 |
|
| 1781 |
/* |
| 1782 |
* leave space for Seq and length |
| 1783 |
*/ |
| 1784 |
save1 = packet; |
| 1785 |
ptr = packet + 4; |
| 1786 |
len = *length - 4; |
| 1787 |
|
| 1788 |
/* |
| 1789 |
* build reqid |
| 1790 |
*/ |
| 1791 |
ptr = asn_build_int(ptr, &len, |
| 1792 |
(u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE | |
| 1793 |
ASN_INTEGER), &reqid, |
| 1794 |
sizeof(reqid)); |
| 1795 |
if (ptr == NULL) { |
| 1796 |
return -1; |
| 1797 |
} |
| 1798 |
|
| 1799 |
/* |
| 1800 |
* build err stat |
| 1801 |
*/ |
| 1802 |
ptr = asn_build_int(ptr, &len, |
| 1803 |
(u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE | |
| 1804 |
ASN_INTEGER), &errstat, sizeof(errstat)); |
| 1805 |
if (ptr == NULL) { |
| 1806 |
return -1; |
| 1807 |
} |
| 1808 |
|
| 1809 |
/* |
| 1810 |
* build err index |
| 1811 |
*/ |
| 1812 |
ptr = asn_build_int(ptr, &len, |
| 1813 |
(u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE | |
| 1814 |
ASN_INTEGER), &errindex, |
| 1815 |
sizeof(errindex)); |
| 1816 |
if (ptr == NULL) { |
| 1817 |
return -1; |
| 1818 |
} |
| 1819 |
|
| 1820 |
save2 = ptr; |
| 1821 |
ptr += 4; |
| 1822 |
len -= 4; |
| 1823 |
|
| 1824 |
if (type != SMUX_SET) { |
| 1825 |
val_type = ASN_NULL; |
| 1826 |
val_len = 0; |
| 1827 |
} |
| 1828 |
|
| 1829 |
/* |
| 1830 |
* build var list : snmp_build_var_op not liked by gated XXX |
| 1831 |
*/ |
| 1832 |
ptr = snmp_build_var_op(ptr, objid, oidlen, val_type, val_len, |
| 1833 |
val, &len); |
| 1834 |
if (ptr == NULL) { |
| 1835 |
return -1; |
| 1836 |
} |
| 1837 |
|
| 1838 |
len = ptr - save1; |
| 1839 |
asn_build_sequence(save1, &len, type, (ptr - save1 - 4)); |
| 1840 |
|
| 1841 |
len = ptr - save2; |
| 1842 |
asn_build_sequence(save2, &len, |
| 1843 |
(ASN_SEQUENCE | ASN_CONSTRUCTOR), |
| 1844 |
(ptr - save2 - 4)); |
| 1845 |
|
| 1846 |
*length = ptr - packet; |
| 1847 |
|
| 1848 |
return 0; |
| 1849 |
} |
| 1850 |
|
| 1851 |
static void |
| 1852 |
smux_peer_cleanup(int sd) |
| 1853 |
{ |
| 1854 |
smux_reg *nrptr, *rptr, *rptr2; |
| 1855 |
int i; |
| 1856 |
netsnmp_handler_registration *reg; |
| 1857 |
|
| 1858 |
/* |
| 1859 |
* close the descriptor |
| 1860 |
*/ |
| 1861 |
close(sd); |
| 1862 |
|
| 1863 |
/* |
| 1864 |
* delete all of the passive registrations that this peer owns |
| 1865 |
*/ |
| 1866 |
for (rptr = PassiveRegs; rptr; rptr = nrptr) { |
| 1867 |
nrptr = rptr->sr_next; |
| 1868 |
if (rptr->sr_fd == sd) { |
| 1869 |
smux_list_detach(&PassiveRegs, rptr); |
| 1870 |
free(rptr); |
| 1871 |
} |
| 1872 |
rptr = nrptr; |
| 1873 |
} |
| 1874 |
/* |
| 1875 |
* find replacements for all of the active registrations found |
| 1876 |
*/ |
| 1877 |
for (rptr = ActiveRegs; rptr; rptr = rptr2) { |
| 1878 |
rptr2 = rptr->sr_next; |
| 1879 |
if (rptr->sr_fd == sd) { |
| 1880 |
smux_list_detach(&ActiveRegs, rptr); |
| 1881 |
if (rptr->reginfo) { |
| 1882 |
netsnmp_unregister_handler(rptr->reginfo); |
| 1883 |
rptr->reginfo = NULL; |
| 1884 |
} |
| 1885 |
if ((nrptr = smux_find_replacement(rptr->sr_name, |
| 1886 |
rptr->sr_name_len)) != |
| 1887 |
NULL) { |
| 1888 |
smux_list_detach(&PassiveRegs, nrptr); |
| 1889 |
reg = netsnmp_create_handler_registration("smux", |
| 1890 |
smux_handler, |
| 1891 |
nrptr->sr_name, |
| 1892 |
nrptr->sr_name_len, |
| 1893 |
HANDLER_CAN_RWRITE); |
| 1894 |
if (reg == NULL) { |
| 1895 |
snmp_log(LOG_ERR, "SMUX: cannot create new smux peer " |
| 1896 |
"registration\n"); |
| 1897 |
continue; |
| 1898 |
} |
| 1899 |
if (netsnmp_register_handler(reg) != MIB_REGISTERED_OK) { |
| 1900 |
snmp_log(LOG_ERR, "SMUX: cannot register new smux peer\n"); |
| 1901 |
continue; |
| 1902 |
} |
| 1903 |
nrptr->reginfo = reg; |
| 1904 |
smux_list_add(&ActiveRegs, nrptr); |
| 1905 |
} |
| 1906 |
free(rptr); |
| 1907 |
} |
| 1908 |
} |
| 1909 |
|
| 1910 |
/* |
| 1911 |
* decrement the peer count |
| 1912 |
*/ |
| 1913 |
npeers--; |
| 1914 |
|
| 1915 |
/* |
| 1916 |
* make his auth available again |
| 1917 |
*/ |
| 1918 |
for (i = 0; i < nauths; i++) { |
| 1919 |
if (Auths[i]->sa_active_fd == sd) { |
| 1920 |
char oid_name[128]; |
| 1921 |
Auths[i]->sa_active_fd = -1; |
| 1922 |
snprint_objid(oid_name, sizeof(oid_name), Auths[i]->sa_oid, |
| 1923 |
Auths[i]->sa_oid_len); |
| 1924 |
DEBUGMSGTL(("smux", "peer disconnected: %s\n", oid_name)); |
| 1925 |
} |
| 1926 |
} |
| 1927 |
} |
| 1928 |
|
| 1929 |
int |
| 1930 |
smux_send_rrsp(int sd, int pri) |
| 1931 |
{ |
| 1932 |
u_char outdata[2 + sizeof(int)]; |
| 1933 |
u_char *ptr = outdata; |
| 1934 |
int intsize = sizeof(int); |
| 1935 |
u_int mask = ((u_int) 0xFF) << (8 * (sizeof(int) - 1)); |
| 1936 |
/* |
| 1937 |
* e.g. mask is 0xFF000000 on a 32-bit machine |
| 1938 |
*/ |
| 1939 |
int sent; |
| 1940 |
|
| 1941 |
/* |
| 1942 |
* This is kind of like calling asn_build_int(), but the |
| 1943 |
* encoding will always be the size of an integer on this |
| 1944 |
* machine, never shorter. |
| 1945 |
*/ |
| 1946 |
*ptr++ = (u_char) SMUX_RRSP; |
| 1947 |
*ptr++ = (u_char) intsize; |
| 1948 |
|
| 1949 |
/* |
| 1950 |
* Copy each byte, most significant first. |
| 1951 |
*/ |
| 1952 |
while (intsize--) { |
| 1953 |
*ptr++ = (u_char) ((pri & mask) >> (8 * (sizeof(int) - 1))); |
| 1954 |
pri <<= 8; |
| 1955 |
} |
| 1956 |
|
| 1957 |
sent = sendto(sd, (char *) outdata, sizeof outdata, 0, NULL, 0); |
| 1958 |
if (sent < 0) { |
| 1959 |
DEBUGMSGTL(("smux", "[smux_send_rrsp] send failed\n")); |
| 1960 |
} |
| 1961 |
return (sent); |
| 1962 |
} |
| 1963 |
|
| 1964 |
static u_char * |
| 1965 |
smux_trap_process(u_char * rsp, size_t * len) |
| 1966 |
{ |
| 1967 |
oid sa_enterpriseoid[MAX_OID_LEN], var_name[MAX_OID_LEN]; |
| 1968 |
size_t datalen, var_name_len, var_val_len, maxlen; |
| 1969 |
size_t sa_enterpriseoid_len; |
| 1970 |
u_char vartype, *ptr, *var_val; |
| 1971 |
|
| 1972 |
long trap, specific; |
| 1973 |
u_long timestamp; |
| 1974 |
|
| 1975 |
netsnmp_variable_list *snmptrap_head, *snmptrap_ptr, *snmptrap_tmp; |
| 1976 |
snmptrap_head = NULL; |
| 1977 |
snmptrap_ptr = NULL; |
| 1978 |
|
| 1979 |
ptr = rsp; |
| 1980 |
|
| 1981 |
/* |
| 1982 |
* parse the sub-agent enterprise oid |
| 1983 |
*/ |
| 1984 |
sa_enterpriseoid_len = MAX_OID_LEN; |
| 1985 |
if ((ptr = asn_parse_objid(ptr, len, |
| 1986 |
&vartype, (oid *) & sa_enterpriseoid, |
| 1987 |
&sa_enterpriseoid_len)) == NULL) { |
| 1988 |
DEBUGMSGTL(("smux", |
| 1989 |
"[smux_trap_process] asn_parse_objid failed\n")); |
| 1990 |
return NULL; |
| 1991 |
} |
| 1992 |
|
| 1993 |
/* |
| 1994 |
* parse the agent-addr ipAddress |
| 1995 |
*/ |
| 1996 |
datalen = SMUXMAXSTRLEN; |
| 1997 |
if (((ptr = asn_parse_string(ptr, len, |
| 1998 |
&vartype, smux_str, |
| 1999 |
&datalen)) == NULL) || |
| 2000 |
(vartype != (u_char) ASN_IPADDRESS)) { |
| 2001 |
DEBUGMSGTL(("smux", |
| 2002 |
"[smux_trap_process] asn_parse_string failed\n")); |
| 2003 |
return NULL; |
| 2004 |
} |
| 2005 |
|
| 2006 |
/* |
| 2007 |
* parse the generic trap int |
| 2008 |
*/ |
| 2009 |
datalen = sizeof(long); |
| 2010 |
if ((ptr = asn_parse_int(ptr, len, &vartype, &trap, datalen)) == NULL) { |
| 2011 |
DEBUGMSGTL(("smux", |
| 2012 |
"[smux_trap_process] asn_parse_int generic failed\n")); |
| 2013 |
return NULL; |
| 2014 |
} |
| 2015 |
|
| 2016 |
/* |
| 2017 |
* parse the specific trap int |
| 2018 |
*/ |
| 2019 |
datalen = sizeof(long); |
| 2020 |
if ((ptr = asn_parse_int(ptr, len, |
| 2021 |
&vartype, &specific, datalen)) == NULL) { |
| 2022 |
DEBUGMSGTL(("smux", |
| 2023 |
"[smux_trap_process] asn_parse_int specific failed\n")); |
| 2024 |
return NULL; |
| 2025 |
} |
| 2026 |
|
| 2027 |
/* |
| 2028 |
* parse the timeticks timestamp |
| 2029 |
*/ |
| 2030 |
datalen = sizeof(u_long); |
| 2031 |
if (((ptr = asn_parse_unsigned_int(ptr, len, |
| 2032 |
&vartype, (u_long *) & timestamp, |
| 2033 |
datalen)) == NULL) || |
| 2034 |
(vartype != (u_char) ASN_TIMETICKS)) { |
| 2035 |
DEBUGMSGTL(("smux", |
| 2036 |
"[smux_trap_process] asn_parse_unsigned_int (timestamp) failed\n")); |
| 2037 |
return NULL; |
| 2038 |
} |
| 2039 |
|
| 2040 |
/* |
| 2041 |
* parse out the overall sequence |
| 2042 |
*/ |
| 2043 |
ptr = asn_parse_header(ptr, len, &vartype); |
| 2044 |
if (ptr == NULL || vartype != (ASN_SEQUENCE | ASN_CONSTRUCTOR)) { |
| 2045 |
return NULL; |
| 2046 |
} |
| 2047 |
|
| 2048 |
/* |
| 2049 |
* parse the variable bindings |
| 2050 |
*/ |
| 2051 |
while (ptr && *len) { |
| 2052 |
|
| 2053 |
/* |
| 2054 |
* get the objid and the asn1 coded value |
| 2055 |
*/ |
| 2056 |
var_name_len = MAX_OID_LEN; |
| 2057 |
ptr = snmp_parse_var_op(ptr, var_name, &var_name_len, &vartype, |
| 2058 |
&var_val_len, (u_char **) & var_val, len); |
| 2059 |
|
| 2060 |
if (ptr == NULL) |
| 2061 |
goto err; |
| 2062 |
|
| 2063 |
maxlen = SMUXMAXPKTSIZE; |
| 2064 |
switch ((short) vartype) { |
| 2065 |
case ASN_INTEGER: |
| 2066 |
var_val_len = sizeof(long); |
| 2067 |
asn_parse_int(var_val, &maxlen, &vartype, |
| 2068 |
(long *) &smux_long, var_val_len); |
| 2069 |
var_val = (u_char *) & smux_long; |
| 2070 |
break; |
| 2071 |
case ASN_COUNTER: |
| 2072 |
case ASN_GAUGE: |
| 2073 |
case ASN_TIMETICKS: |
| 2074 |
case ASN_UINTEGER: |
| 2075 |
var_val_len = sizeof(u_long); |
| 2076 |
asn_parse_unsigned_int(var_val, &maxlen, &vartype, |
| 2077 |
(u_long *) & smux_ulong, var_val_len); |
| 2078 |
var_val = (u_char *) & smux_ulong; |
| 2079 |
break; |
| 2080 |
case ASN_COUNTER64: |
| 2081 |
var_val_len = sizeof(smux_counter64); |
| 2082 |
asn_parse_unsigned_int64(var_val, &maxlen, &vartype, |
| 2083 |
(struct counter64 *) &smux_counter64, |
| 2084 |
var_val_len); |
| 2085 |
var_val = (u_char *) & smux_counter64; |
| 2086 |
break; |
| 2087 |
case ASN_IPADDRESS: |
| 2088 |
var_val_len = 4; |
| 2089 |
/* |
| 2090 |
* consume the tag and length, but just copy here |
| 2091 |
* because we know it is an ip address |
| 2092 |
*/ |
| 2093 |
if ((var_val = |
| 2094 |
asn_parse_header(var_val, &maxlen, &vartype)) == NULL) |
| 2095 |
goto err; |
| 2096 |
memcpy((u_char *) & (smux_sa.sin_addr.s_addr), var_val, |
| 2097 |
var_val_len); |
| 2098 |
var_val = (u_char *) & (smux_sa.sin_addr.s_addr); |
| 2099 |
break; |
| 2100 |
case ASN_OPAQUE: |
| 2101 |
case ASN_OCTET_STR: |
| 2102 |
/* |
| 2103 |
* XXX |
| 2104 |
*/ |
| 2105 |
var_val_len = SMUXMAXSTRLEN; |
| 2106 |
asn_parse_string(var_val, &maxlen, &vartype, |
| 2107 |
smux_str, &var_val_len); |
| 2108 |
var_val = smux_str; |
| 2109 |
break; |
| 2110 |
case ASN_OBJECT_ID: |
| 2111 |
var_val_len = MAX_OID_LEN; |
| 2112 |
asn_parse_objid(var_val, &maxlen, &vartype, |
| 2113 |
smux_objid, &var_val_len); |
| 2114 |
var_val_len *= sizeof(oid); |
| 2115 |
var_val = (u_char *) smux_objid; |
| 2116 |
break; |
| 2117 |
case SNMP_NOSUCHOBJECT: |
| 2118 |
case SNMP_NOSUCHINSTANCE: |
| 2119 |
case SNMP_ENDOFMIBVIEW: |
| 2120 |
case ASN_NULL: |
| 2121 |
var_val = NULL; |
| 2122 |
break; |
| 2123 |
case ASN_BIT_STR: |
| 2124 |
/* |
| 2125 |
* XXX |
| 2126 |
*/ |
| 2127 |
var_val_len = SMUXMAXSTRLEN; |
| 2128 |
asn_parse_bitstring(var_val, &maxlen, &vartype, |
| 2129 |
smux_str, &var_val_len); |
| 2130 |
var_val = (u_char *) smux_str; |
| 2131 |
break; |
| 2132 |
case ASN_NSAP: |
| 2133 |
default: |
| 2134 |
snmp_log(LOG_ERR, "bad type returned (%x)\n", vartype); |
| 2135 |
var_val = NULL; |
| 2136 |
break; |
| 2137 |
} |
| 2138 |
|
| 2139 |
snmptrap_tmp = calloc(1, sizeof(netsnmp_variable_list)); |
| 2140 |
if (snmptrap_tmp == NULL) |
| 2141 |
goto err; |
| 2142 |
if (snmptrap_head == NULL) { |
| 2143 |
snmptrap_head = snmptrap_tmp; |
| 2144 |
snmptrap_ptr = snmptrap_head; |
| 2145 |
} else { |
| 2146 |
snmptrap_ptr->next_variable = snmptrap_tmp; |
| 2147 |
snmptrap_ptr = snmptrap_ptr->next_variable; |
| 2148 |
} |
| 2149 |
|
| 2150 |
snmptrap_ptr->type = vartype; |
| 2151 |
snmptrap_ptr->next_variable = NULL; |
| 2152 |
snmp_set_var_objid(snmptrap_ptr, var_name, var_name_len); |
| 2153 |
snmp_set_var_value(snmptrap_ptr, (char *) var_val, var_val_len); |
| 2154 |
|
| 2155 |
} |
| 2156 |
|
| 2157 |
/* |
| 2158 |
* send the traps |
| 2159 |
*/ |
| 2160 |
send_enterprise_trap_vars(trap, specific, (oid *) & sa_enterpriseoid, |
| 2161 |
sa_enterpriseoid_len, snmptrap_head); |
| 2162 |
|
| 2163 |
/* |
| 2164 |
* free trap variables |
| 2165 |
*/ |
| 2166 |
snmp_free_varbind(snmptrap_head); |
| 2167 |
|
| 2168 |
return ptr; |
| 2169 |
|
| 2170 |
err: |
| 2171 |
snmp_free_varbind(snmptrap_head); |
| 2172 |
return NULL; |
| 2173 |
} |
| 2174 |
|
| 2175 |
#define NUM_SOCKETS 32 |
| 2176 |
static int sdlist[NUM_SOCKETS], sdlen = 0; |
| 2177 |
|
| 2178 |
int smux_snmp_select_list_add(int sd) |
| 2179 |
{ |
| 2180 |
if (sdlen < NUM_SOCKETS) |
| 2181 |
{ |
| 2182 |
sdlist[sdlen++] = sd; |
| 2183 |
return(1); |
| 2184 |
} |
| 2185 |
return(0); |
| 2186 |
} |
| 2187 |
|
| 2188 |
int smux_snmp_select_list_del(int sd) |
| 2189 |
{ |
| 2190 |
int i, found=0; |
| 2191 |
|
| 2192 |
for (i = 0; i < (sdlen); i++) { |
| 2193 |
if (sdlist[i] == sd) |
| 2194 |
{ |
| 2195 |
sdlist[i] = 0; |
| 2196 |
found = 1; |
| 2197 |
} |
| 2198 |
if ((found) &&(i < (sdlen - 1))) |
| 2199 |
sdlist[i] = sdlist[i + 1]; |
| 2200 |
} |
| 2201 |
if (found) |
| 2202 |
{ |
| 2203 |
sdlen--; |
| 2204 |
return(1); |
| 2205 |
} |
| 2206 |
return(0); |
| 2207 |
} |
| 2208 |
|
| 2209 |
int smux_snmp_select_list_get_length(void) |
| 2210 |
{ |
| 2211 |
return(sdlen); |
| 2212 |
} |
| 2213 |
|
| 2214 |
int smux_snmp_select_list_get_SD_from_List(int pos) |
| 2215 |
{ |
| 2216 |
if (pos < NUM_SOCKETS) |
| 2217 |
{ |
| 2218 |
return(sdlist[pos]); |
| 2219 |
} |
| 2220 |
return(0); |
| 2221 |
} |