diff options
author | Holger Hans Peter Freyther <zecke@selfish.org> | 2011-01-21 18:00:36 +0100 |
---|---|---|
committer | Holger Hans Peter Freyther <zecke@selfish.org> | 2011-01-21 19:40:49 +0100 |
commit | eab20964b2f059898fc804584e55a157c021afc0 (patch) | |
tree | e6dfcce7b7d40d761a2bfb13200794ba42006a89 /src/snmp_mtp.c | |
parent | 4da421da6fed6135f7c01eeb6769fb2a82d476cb (diff) |
udp: Make the SNMP code asynchronouson-waves/multiple-links
Do not block the application when doing a SNMP request. Work
with the results coming back from the callback. Right now a
link can only be taken down and up.
Diffstat (limited to 'src/snmp_mtp.c')
-rw-r--r-- | src/snmp_mtp.c | 67 |
1 files changed, 56 insertions, 11 deletions
diff --git a/src/snmp_mtp.c b/src/snmp_mtp.c index ec4d892..2cdf323 100644 --- a/src/snmp_mtp.c +++ b/src/snmp_mtp.c @@ -41,39 +41,83 @@ static void add_pdu_var(netsnmp_pdu *pdu, const char *mib_name, int id, const ch } } -static void send_pdu(netsnmp_session *ss, netsnmp_pdu *pdu) +static int link_up_cb(int op, netsnmp_session *ss, + int reqid, netsnmp_pdu *pdu, void *data) +{ + struct snmp_mtp_session *mtp = ss->myvoid; + int link_index = (int) data; + + if (mtp->last_up_req != reqid) + return 0; + + mtp->last_up_req = 0; + snmp_mtp_callback(mtp, SNMP_LINK_UP, + op == STAT_TIMEOUT ? SNMP_STATUS_TIMEOUT : SNMP_STATUS_OK, + link_index); + return 0; +} + +static int link_down_cb(int op, netsnmp_session *ss, + int reqid, netsnmp_pdu *pdu, void *data) +{ + struct snmp_mtp_session *mtp = ss->myvoid; + int link_index = (int) data; + + if (mtp->last_do_req != reqid) + return 0; + + mtp->last_do_req = 0; + snmp_mtp_callback(mtp, SNMP_LINK_DOWN, + op == STAT_TIMEOUT ? SNMP_STATUS_TIMEOUT : SNMP_STATUS_OK, + link_index); + return 0; +} + +static int send_pdu(netsnmp_session *ss, netsnmp_pdu *pdu, int kind, int link_id) { int status; - netsnmp_pdu *response; + netsnmp_callback cb; + + cb = kind == SNMP_LINK_UP ? link_up_cb : link_down_cb; - status = snmp_synch_response(ss, pdu, &response); - if (status == STAT_ERROR) { - snmp_sess_perror("set failed", ss); - } else if (status == STAT_TIMEOUT) { - fprintf(stderr, "Timeout for SNMP.\n"); + status = snmp_async_send(ss, pdu, cb, (void *) link_id); + if (status == 0) { + snmp_log(LOG_ERR, "Failed to send async request.\n"); + snmp_free_pdu(pdu); } - if (response) - snmp_free_pdu(response); + return status; } static void snmp_mtp_start_c7_datalink(struct snmp_mtp_session *session, int link_id) { + int status; netsnmp_pdu *pdu; pdu = snmp_pdu_create(SNMP_MSG_SET); add_pdu_var(pdu, "PTI-NexusWareC7-MIB::nwc7DatalinkCommand", link_id, "nwc7DatalinkCmdPowerOn"); add_pdu_var(pdu, "PTI-NexusWareC7-MIB::nwc7Mtp2Active", link_id, "true"); - send_pdu(session->ss, pdu); + status = send_pdu(session->ss, pdu, SNMP_LINK_UP, link_id); + + if (status == 0) + snmp_mtp_callback(session, SNMP_LINK_UP, SNMP_STATUS_TIMEOUT, link_id); + else + session->last_up_req = status; + } static void snmp_mtp_stop_c7_datalink(struct snmp_mtp_session *session, int link_id) { + int status; netsnmp_pdu *pdu; pdu = snmp_pdu_create(SNMP_MSG_SET); add_pdu_var(pdu, "PTI-NexusWareC7-MIB::nwc7Mtp2Active", link_id, "false"); - send_pdu(session->ss, pdu); + status = send_pdu(session->ss, pdu, SNMP_LINK_DOWN, link_id); + if (status == 0) + snmp_mtp_callback(session, SNMP_LINK_DOWN, SNMP_STATUS_TIMEOUT, link_id); + else + session->last_do_req = status; } struct snmp_mtp_session *snmp_mtp_session_create(char *host) @@ -88,6 +132,7 @@ struct snmp_mtp_session *snmp_mtp_session_create(char *host) session->session.version = SNMP_VERSION_1; session->session.community = (unsigned char *) "private"; session->session.community_len = strlen((const char *) session->session.community); + session->session.myvoid = session; session->ss = snmp_open(&session->session); if (!session->ss) { |