00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <sys/confnet.h>
00036
00037 #include <stdlib.h>
00038 #include <string.h>
00039
00040 #include <pro/snmp_auth.h>
00041 #include <pro/snmp_session.h>
00042
00047
00048 static uint32_t snmp_reqid;
00049 static uint8_t temp_buffer[SNMP_MAX_LEN];
00050
00061 SNMP_SESSION *SnmpSessionOpen(uint32_t ip, uint16_t port, uint8_t * id, size_t idlen)
00062 {
00063 SNMP_SESSION *session = calloc(1, sizeof(SNMP_SESSION));
00064
00065 if (session) {
00066
00067 session->sess_sock = NutUdpCreateSocket(0);
00068 if (session->sess_sock == NULL) {
00069
00070 free(session);
00071 session = NULL;
00072 } else {
00073
00074 session->sess_rem_addr = ip;
00075 session->sess_rem_port = port;
00076
00077 memcpy(session->sess_id, id, idlen);
00078 session->sess_id_len = idlen;
00079 }
00080 }
00081 return session;
00082 }
00083
00090 void SnmpSessionClose(SNMP_SESSION * session)
00091 {
00092 if (session->sess_sock) {
00093 NutUdpDestroySocket(session->sess_sock);
00094 }
00095 free(session);
00096 }
00097
00112 static int SnmpMsgBuild(SNMP_SESSION * session, SNMP_PDU * pdu, uint8_t * packet, size_t * psize)
00113 {
00114 uint8_t *cp;
00115 SNMP_VARLIST *vp;
00116 size_t length;
00117 int total;
00118 long lval;
00119
00120
00121
00122
00123 length = *psize;
00124 cp = packet;
00125 for (vp = pdu->pdu_variables; vp; vp = vp->var_next) {
00126 cp = SnmpVarBuild(cp, &length, vp->var_name, vp->var_nlen, vp->var_type, vp->var_vptr, vp->var_vlen);
00127 if (cp == NULL) {
00128 return -1;
00129 }
00130 }
00131 total = cp - packet;
00132
00133
00134
00135
00136 length = SNMP_MAX_LEN;
00137 cp = AsnHeaderBuild(temp_buffer, &length, ASN_SEQUENCE | ASN_CONSTRUCTOR, total);
00138 if (cp == NULL) {
00139 return -1;
00140 }
00141
00142
00143
00144
00145 memcpy(cp, packet, total);
00146 total += cp - temp_buffer;
00147
00148
00149
00150
00151
00152
00153 length = *psize;
00154 if (pdu->pdu_cmd == SNMP_MSG_TRAP) {
00155
00156 cp = AsnOidBuild(packet, &length, ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OBJECT_ID, pdu->pdu_enterprise, pdu->pdu_enterprise_length);
00157 if (cp == NULL) {
00158 return -1;
00159 }
00160
00161 cp = AsnOctetStringBuild(cp, &length, ASN_IPADDRESS, (u_char *) & confnet.cdn_ip_addr, sizeof(confnet.cdn_ip_addr));
00162 if (cp == NULL) {
00163 return -1;
00164 }
00165
00166 lval = (long)pdu->pdu_trap_type;
00167 cp = AsnIntegerBuild(cp, &length, ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER, &lval);
00168 if (cp == NULL) {
00169 return -1;
00170 }
00171
00172 lval = (long)pdu->pdu_specific_type;
00173 cp = AsnIntegerBuild(cp, &length, ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER, &lval);
00174 if (cp == NULL) {
00175 return -1;
00176 }
00177
00178 cp = AsnIntegerBuild(cp, &length, ASN_TIMETICKS, (long *) &pdu->pdu_time);
00179 if (cp == NULL) {
00180 return -1;
00181 }
00182 } else {
00183
00184 cp = AsnIntegerBuild(packet, &length, ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER, (long *) &pdu->pdu_reqid);
00185 if (cp == NULL) {
00186 return -1;
00187 }
00188
00189 cp = AsnIntegerBuild(cp, &length, ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER, (long *) &pdu->pdu_errstat);
00190 if (cp == NULL) {
00191 return -1;
00192 }
00193
00194 cp = AsnIntegerBuild(cp, &length, ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER, (long *) &pdu->pdu_errindex);
00195 if (cp == NULL) {
00196 return -1;
00197 }
00198 }
00199
00200
00201
00202
00203 if (length < total) {
00204 return -1;
00205 }
00206 memcpy(cp, temp_buffer, total);
00207 total += cp - packet;
00208
00209
00210
00211
00212 length = SNMP_MAX_LEN;
00213 cp = AsnHeaderBuild(temp_buffer, &length, (u_char) pdu->pdu_cmd, total);
00214 if (cp == NULL || length < total) {
00215 return -1;
00216 }
00217
00218
00219
00220
00221 memcpy(cp, packet, total);
00222 total += cp - temp_buffer;
00223
00224
00225
00226
00227 length = *psize;
00228 cp = SnmpAuthBuild(session, packet, &length, total);
00229 if (cp == NULL) {
00230 return -1;
00231 }
00232
00233
00234
00235
00236 if ((*psize - (cp - packet)) < total) {
00237 return -1;
00238 }
00239 memcpy(cp, temp_buffer, total);
00240 total += cp - packet;
00241 *psize = total;
00242
00243 return 0;
00244 }
00245
00256 int SnmpSessionSendPdu(SNMP_SESSION * session, SNMP_PDU * pdu)
00257 {
00258 int rc = -1;
00259 uint8_t *packet;
00260 size_t length;
00261
00262 if (pdu->pdu_cmd == SNMP_MSG_TRAP) {
00263
00264 pdu->pdu_reqid = 1;
00265 }
00266 else if (pdu->pdu_reqid == 0) {
00267 pdu->pdu_reqid = ++snmp_reqid;
00268 }
00269
00270 length = SNMP_MAX_LEN;
00271 packet = malloc(length);
00272 if (packet) {
00273 if (SnmpMsgBuild(session, pdu, packet, &length) == 0) {
00274 rc = NutUdpSendTo(session->sess_sock, session->sess_rem_addr, session->sess_rem_port, packet, length);
00275 }
00276 free(packet);
00277 }
00278 return rc;
00279 }
00280