📄 ax_sub.c
字号:
FD_ZERO(&ready); FD_SET(axSocket, &ready); if ((retval = select(axSocket+1, &ready, 0, 0, &tvp)) < 0) { snmpdLog (SNMP_ERROR, "Select failed...\n"); close(axSocket); return (ERROR); } if ((retval) && FD_ISSET(axSocket, &ready)) { do { if ((got = recv(axSocket, (char *) rcvBuf, sizeof(rcvBuf), 0)) == -1) { if (errno != EWOULDBLOCK) { close(axSocket); envoy_ax_chunk_clean(&ax_myfoo.chunk); snmpdLog (SNMP_ERROR, "Error in receiving OPEN response\n"); return (ERROR); } break; } envoy_ax_chunk_handler(rcvBuf, got, envoy_ax_sa_handler, &ax_myfoo.chunk, &ax_sasb, axSubAdmin, axSubSend, axSubFree, axSubAdd, &ax_myfoo); } while (1); } else break; } while(1); /* Register with the Master agent */ ax_myfoo.op = ENVOY_AX_REGISTER; EBufferInitialize(&ebuff); /* Here is where we loop through the list of registration requests */ for ( i=0 ; agentXRegList[i] != (SNMP_MC_AX_REGLIST_T *) NULL ; i++) { /* We call the ith instance of the function to create a * registration packet. We provide a packet_id, session_id, * timeout and priority. Other parameters are known or assumed * by the function */ if (agentXRegList[i](&ebuff, (ax_byte_order | ax_ndc), ax_packet_id++, ax_session_id, ax_timeout, ax_priority) != 0) { snmpdLog (SNMP_ERROR, "Couldn't create registration packet\n"); EBufferClean(&ebuff); envoy_ax_chunk_clean(&ax_myfoo.chunk); close(axSocket); return(ERROR); } MEMCPY(rcvBuf, EBufferStart(&ebuff), EBufferUsed(&ebuff)); used = EBufferUsed(&ebuff); if (send(axSocket, (char *) rcvBuf, used, 0) == -1) { snmpdLog (SNMP_ERROR, "Sending registration packet failed!\n"); EBufferClean(&ebuff); envoy_ax_chunk_clean(&ax_myfoo.chunk); close(axSocket); return(ERROR); } EBufferClean(&ebuff); /* Again, we need to check our responses... */ do { tvp.tv_sec = 1; tvp.tv_usec = 0; FD_ZERO(&ready); FD_SET(axSocket, &ready); if ((retval = select(axSocket+1, &ready, 0, 0, &tvp)) < 0) { snmpdLog (SNMP_ERROR, "Select failed...\n"); close(axSocket); } if ((retval) && FD_ISSET(axSocket, &ready)) { do { if ((got = recv(axSocket, (char *) rcvBuf, sizeof(rcvBuf), 0)) == -1) { if (errno != EWOULDBLOCK) { close(axSocket); envoy_ax_chunk_clean(&ax_myfoo.chunk); snmpdLog (SNMP_ERROR, "Error in receiving REGISTER response\n"); return (ERROR); } break; } envoy_ax_chunk_handler(rcvBuf, got, envoy_ax_sa_handler, &ax_myfoo.chunk, &ax_sasb, axSubAdmin, axSubSend, axSubFree, axSubAdd, &ax_myfoo); } while (1); } else break; } while(1); } /* * At this point, we have reason to believe that we've opened our * session and properly registered our subtrees. We can now go on * to the main select() loop. */ return (OK); } /********************************************************************************* snmpAxSubagentTask - wait for connections and messages from the subagents** This routine calls axSubagentInit to open a session with the master agent* and register the subagents mib. The task then sits in a select() loop on * the AgentX socket and waits for the master to send messages.** Messages received will be passed through to envoy_ax_chunk_handler().** RETURNS: N/A**/LOCAL void snmpAxSubagentTask (void) { EBUFFER_T ebuff; int socklive = 1; if (axSubagentInit () == ERROR) { snmpdLog (SNMP_ERROR, "Error in initializing AgentX subagent task\n"); return; } EBufferInitialize(&ebuff); EBufferClean(&ebuff); while (socklive) { int got; fd_set ready; /* Set up the select call and then wait for a packet */ FD_ZERO(&ready); FD_SET(axSocket, &ready); if (select(axSocket+1, &ready, 0, 0, 0) < 0) { snmpdLog (SNMP_ERROR, "Select failed ...\n"); socklive = 0; continue; } /* If we receive anything, it's got to be for AgentX */ if (FD_ISSET(axSocket, &ready)) { do { /* We've received data on our connection... */ if ((got = recv(axSocket, (char *)rcvBuf, sizeof(rcvBuf), 0)) == -1) { if (errno != EWOULDBLOCK) { close(axSocket); envoy_ax_chunk_clean(&ax_myfoo.chunk); socklive = 0; } break; } if (envoy_ax_chunk_handler(rcvBuf, got, envoy_ax_sa_handler, &ax_myfoo.chunk, &ax_sasb, axSubAdmin, axSubSend, axSubFree, axSubAdd, &ax_myfoo)) { close(axSocket); envoy_ax_chunk_clean(&ax_myfoo.chunk); socklive = 0; break; } } while (1); } } } LOCAL void axSubAdmin(ptr_t cookie, ptr_t pkt) { ENVOY_AX_PKT_T *ax_pkt = (ENVOY_AX_PKT_T *)pkt; char *op_string; if (ax_pkt->type == ENVOY_AX_RESPONSE) { op_string = ax_strings[ax_myfoo.op]; if (ax_pkt->error_stat) { snmpdLog (SNMP_ERROR, "axSubAdmin: error received from master\n"); } else { if (ax_myfoo.op == ENVOY_AX_OPEN) { ax_session_id = ax_pkt->session_id; ax_myfoo.open_done = 1; } } ax_myfoo.op = 0; } return; } LOCAL void axSubAdd(ptr_t cookie) { ax_myfoo.ref_count++; } LOCAL void axSubFree(ptr_t cookie, int foobar) { ax_myfoo.ref_count--; } LOCAL int axSubSend(ptr_t cookie, ptr_t ax_pkt, ptr_t vblist, ALENGTH_T need) { bits8_t *pkt; EBUFFER_T ebuff; EBufferInitialize(&ebuff); if ((pkt = SNMP_memory_alloc(need)) == (bits8_t *) 0) return 1; EBufferSetup(BFL_IS_STATIC, &ebuff, pkt, need); if (envoy_ax_pkt_encode(ax_pkt, vblist, &ebuff, need)) { SNMP_memory_free(pkt); return(1); } if (send(axSocket, (char *) pkt, need, 0) == -1) snmpdLog (SNMP_ERROR, "send error!\n"); SNMP_memory_free(pkt); return(0); } /********************************************************************************* subagentAxCleanup - free resources allocated for SNMP AgentX subagent** This routine is called from the cleanup routine in snmpIoLib if the agent* fails to allocate resources. This routine closes all sockets and all other* resources that have been allocated for the master agent.** RETURNS: N/A**/void subagentAxCleanup (void) { /* we need to be careful here, since we do have a select loop * running on this set of sockets. For now, we'll do nothing. */ return; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -