snmpclient.c (156067) | snmpclient.c (216294) |
---|---|
1/* 2 * Copyright (c) 2004-2005 3 * Hartmut Brandt. 4 * All rights reserved. 5 * Copyright (c) 2001-2003 6 * Fraunhofer Institute for Open Communication Systems (FhG Fokus). 7 * All rights reserved. 8 * --- 838 unchanged lines hidden (view full) --- 847 848 c->version = SNMP_V2c; 849 c->trans = SNMP_TRANS_UDP; 850 c->chost = NULL; 851 c->cport = NULL; 852 853 strcpy(c->read_community, "public"); 854 strcpy(c->write_community, "private"); | 1/* 2 * Copyright (c) 2004-2005 3 * Hartmut Brandt. 4 * All rights reserved. 5 * Copyright (c) 2001-2003 6 * Fraunhofer Institute for Open Communication Systems (FhG Fokus). 7 * All rights reserved. 8 * --- 838 unchanged lines hidden (view full) --- 847 848 c->version = SNMP_V2c; 849 c->trans = SNMP_TRANS_UDP; 850 c->chost = NULL; 851 c->cport = NULL; 852 853 strcpy(c->read_community, "public"); 854 strcpy(c->write_community, "private"); |
855 856 c->security_model = SNMP_SECMODEL_USM; 857 strcpy(c->cname, ""); |
|
855 856 c->timeout.tv_sec = 3; 857 c->timeout.tv_usec = 0; 858 c->retries = 3; 859 c->dump_pdus = 0; 860 c->txbuflen = c->rxbuflen = 10000; 861 862 c->fd = -1; 863 864 c->max_reqid = INT32_MAX; 865 c->min_reqid = 0; 866 c->next_reqid = 0; | 858 859 c->timeout.tv_sec = 3; 860 c->timeout.tv_usec = 0; 861 c->retries = 3; 862 c->dump_pdus = 0; 863 c->txbuflen = c->rxbuflen = 10000; 864 865 c->fd = -1; 866 867 c->max_reqid = INT32_MAX; 868 c->min_reqid = 0; 869 c->next_reqid = 0; |
870 871 c->engine.max_msg_size = 1500; /* XXX */ |
|
867} 868 869 870/* 871 * Open UDP client socket 872 */ 873static int 874open_client_udp(const char *host, const char *port) --- 252 unchanged lines hidden (view full) --- 1127} 1128 1129/* 1130 * initialize a snmp_pdu structure 1131 */ 1132void 1133snmp_pdu_create(struct snmp_pdu *pdu, u_int op) 1134{ | 872} 873 874 875/* 876 * Open UDP client socket 877 */ 878static int 879open_client_udp(const char *host, const char *port) --- 252 unchanged lines hidden (view full) --- 1132} 1133 1134/* 1135 * initialize a snmp_pdu structure 1136 */ 1137void 1138snmp_pdu_create(struct snmp_pdu *pdu, u_int op) 1139{ |
1135 memset(pdu,0,sizeof(struct snmp_pdu)); | 1140 memset(pdu, 0, sizeof(struct snmp_pdu)); 1141 |
1136 if (op == SNMP_PDU_SET) 1137 strlcpy(pdu->community, snmp_client.write_community, 1138 sizeof(pdu->community)); 1139 else 1140 strlcpy(pdu->community, snmp_client.read_community, 1141 sizeof(pdu->community)); 1142 1143 pdu->type = op; 1144 pdu->version = snmp_client.version; 1145 pdu->error_status = 0; 1146 pdu->error_index = 0; 1147 pdu->nbindings = 0; | 1142 if (op == SNMP_PDU_SET) 1143 strlcpy(pdu->community, snmp_client.write_community, 1144 sizeof(pdu->community)); 1145 else 1146 strlcpy(pdu->community, snmp_client.read_community, 1147 sizeof(pdu->community)); 1148 1149 pdu->type = op; 1150 pdu->version = snmp_client.version; 1151 pdu->error_status = 0; 1152 pdu->error_index = 0; 1153 pdu->nbindings = 0; |
1154 1155 if (snmp_client.version != SNMP_V3) 1156 return; 1157 1158 pdu->identifier = ++snmp_client.identifier; 1159 pdu->engine.max_msg_size = snmp_client.engine.max_msg_size; 1160 pdu->flags = 0; 1161 pdu->security_model = snmp_client.security_model; 1162 1163 if (snmp_client.security_model == SNMP_SECMODEL_USM) 1164 snmp_pdu_init_secparams(pdu, &snmp_client.engine, 1165 &snmp_client.user); 1166 else 1167 seterr(&snmp_client, "unknown security model"); 1168 1169 if (snmp_client.clen > 0) { 1170 memcpy(pdu->context_engine, snmp_client.cengine, 1171 snmp_client.clen); 1172 pdu->context_engine_len = snmp_client.clen; 1173 } else { 1174 memcpy(pdu->context_engine, snmp_client.engine.engine_id, 1175 snmp_client.engine.engine_len); 1176 pdu->context_engine_len = snmp_client.engine.engine_len; 1177 } 1178 1179 strlcpy(pdu->context_name, snmp_client.cname, 1180 sizeof(pdu->context_name)); |
|
1148} 1149 1150/* add pairs of (struct asn_oid, enum snmp_syntax) to an existing pdu */ 1151/* added 10/04/02 by kek: check for MAX_BINDINGS */ 1152int 1153snmp_add_binding(struct snmp_v1_pdu *pdu, ...) 1154{ 1155 va_list ap; --- 245 unchanged lines hidden (view full) --- 1401 seterr(&snmp_client, "recv: socket closed by peer"); 1402 errno = EPIPE; 1403 return (-1); 1404 } 1405 1406 abuf.asn_ptr = buf; 1407 abuf.asn_len = ret; 1408 | 1181} 1182 1183/* add pairs of (struct asn_oid, enum snmp_syntax) to an existing pdu */ 1184/* added 10/04/02 by kek: check for MAX_BINDINGS */ 1185int 1186snmp_add_binding(struct snmp_v1_pdu *pdu, ...) 1187{ 1188 va_list ap; --- 245 unchanged lines hidden (view full) --- 1434 seterr(&snmp_client, "recv: socket closed by peer"); 1435 errno = EPIPE; 1436 return (-1); 1437 } 1438 1439 abuf.asn_ptr = buf; 1440 abuf.asn_len = ret; 1441 |
1442 memset(pdu, 0, sizeof(*pdu)); 1443 if (snmp_client.security_model == SNMP_SECMODEL_USM) 1444 snmp_pdu_init_secparams(pdu, &snmp_client.engine, 1445 &snmp_client.user); 1446 |
|
1409 if (SNMP_CODE_OK != (ret = snmp_pdu_decode(&abuf, pdu, &ip))) { 1410 seterr(&snmp_client, "snmp_decode_pdu: failed %d", ret); 1411 free(buf); 1412 return (-1); 1413 } | 1447 if (SNMP_CODE_OK != (ret = snmp_pdu_decode(&abuf, pdu, &ip))) { 1448 seterr(&snmp_client, "snmp_decode_pdu: failed %d", ret); 1449 free(buf); 1450 return (-1); 1451 } |
1452 |
|
1414 free(buf); 1415 if (snmp_client.dump_pdus) 1416 snmp_pdu_dump(pdu); 1417 | 1453 free(buf); 1454 if (snmp_client.dump_pdus) 1455 snmp_pdu_dump(pdu); 1456 |
1457 snmp_client.engine.engine_time = pdu->engine.engine_time; 1458 snmp_client.engine.engine_boots = pdu->engine.engine_boots; 1459 |
|
1418 return (+1); 1419} 1420 1421static int 1422snmp_deliver_packet(struct snmp_pdu * resp) 1423{ 1424 struct sent_pdu *listentry; 1425 --- 254 unchanged lines hidden (view full) --- 1680 } 1681 } 1682 errno = ETIMEDOUT; 1683 seterr(&snmp_client, "retry count exceeded"); 1684 return (-1); 1685} 1686 1687int | 1460 return (+1); 1461} 1462 1463static int 1464snmp_deliver_packet(struct snmp_pdu * resp) 1465{ 1466 struct sent_pdu *listentry; 1467 --- 254 unchanged lines hidden (view full) --- 1722 } 1723 } 1724 errno = ETIMEDOUT; 1725 seterr(&snmp_client, "retry count exceeded"); 1726 return (-1); 1727} 1728 1729int |
1730snmp_discover_engine(char *passwd) 1731{ 1732 char cname[SNMP_ADM_STR32_SIZ]; 1733 enum snmp_authentication cap; 1734 enum snmp_privacy cpp; 1735 struct snmp_pdu req, resp; 1736 1737 if (snmp_client.version != SNMP_V3) 1738 seterr(&snmp_client, "wrong version"); 1739 1740 strlcpy(cname, snmp_client.user.sec_name, sizeof(cname)); 1741 cap = snmp_client.user.auth_proto; 1742 cpp = snmp_client.user.priv_proto; 1743 1744 snmp_client.engine.engine_len = 0; 1745 snmp_client.engine.engine_boots = 0; 1746 snmp_client.engine.engine_time = 0; 1747 snmp_client.user.auth_proto = SNMP_AUTH_NOAUTH; 1748 snmp_client.user.priv_proto = SNMP_PRIV_NOPRIV; 1749 memset(snmp_client.user.sec_name, 0, sizeof(snmp_client.user.sec_name)); 1750 1751 snmp_pdu_create(&req, SNMP_PDU_GET); 1752 1753 if (snmp_dialog(&req, &resp) == -1) 1754 return (-1); 1755 1756 if (resp.version != req.version) { 1757 seterr(&snmp_client, "wrong version"); 1758 return (-1); 1759 } 1760 1761 if (resp.error_status != SNMP_ERR_NOERROR) { 1762 seterr(&snmp_client, "Error %d in responce", resp.error_status); 1763 return (-1); 1764 } 1765 1766 snmp_client.engine.engine_len = resp.engine.engine_len; 1767 snmp_client.engine.max_msg_size = resp.engine.max_msg_size; 1768 memcpy(snmp_client.engine.engine_id, resp.engine.engine_id, 1769 resp.engine.engine_len); 1770 1771 strlcpy(snmp_client.user.sec_name, cname, 1772 sizeof(snmp_client.user.sec_name)); 1773 snmp_client.user.auth_proto = cap; 1774 snmp_client.user.priv_proto = cpp; 1775 1776 if (snmp_client.user.auth_proto == SNMP_AUTH_NOAUTH) 1777 return (0); 1778 1779 if (passwd == NULL || 1780 snmp_passwd_to_keys(&snmp_client.user, passwd) != SNMP_CODE_OK || 1781 snmp_get_local_keys(&snmp_client.user, snmp_client.engine.engine_id, 1782 snmp_client.engine.engine_len) != SNMP_CODE_OK) 1783 return (-1); 1784 1785 if (resp.engine.engine_boots != 0) 1786 snmp_client.engine.engine_boots = resp.engine.engine_boots; 1787 1788 if (resp.engine.engine_time != 0) { 1789 snmp_client.engine.engine_time = resp.engine.engine_time; 1790 return (0); 1791 } 1792 1793 snmp_pdu_create(&req, SNMP_PDU_GET); 1794 req.engine.engine_boots = 0; 1795 req.engine.engine_time = 0; 1796 1797 if (snmp_dialog(&req, &resp) == -1) 1798 return (-1); 1799 1800 if (resp.version != req.version) { 1801 seterr(&snmp_client, "wrong version"); 1802 return (-1); 1803 } 1804 1805 if (resp.error_status != SNMP_ERR_NOERROR) { 1806 seterr(&snmp_client, "Error %d in responce", resp.error_status); 1807 return (-1); 1808 } 1809 1810 snmp_client.engine.engine_boots = resp.engine.engine_boots; 1811 snmp_client.engine.engine_time = resp.engine.engine_time; 1812 1813 return (0); 1814} 1815 1816int |
|
1688snmp_client_set_host(struct snmp_client *cl, const char *h) 1689{ 1690 char *np; 1691 1692 if (h == NULL) { 1693 if (cl->chost != NULL) 1694 free(cl->chost); 1695 cl->chost = NULL; --- 129 unchanged lines hidden --- | 1817snmp_client_set_host(struct snmp_client *cl, const char *h) 1818{ 1819 char *np; 1820 1821 if (h == NULL) { 1822 if (cl->chost != NULL) 1823 free(cl->chost); 1824 cl->chost = NULL; --- 129 unchanged lines hidden --- |