home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.umcs.maine.edu
/
2015-02-07.ftp.umcs.maine.edu.tar
/
ftp.umcs.maine.edu
/
pub
/
thesis
/
zhongy
/
snmp
/
snmp.shar
/
snmpencode.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-05-11
|
4KB
|
177 lines
/*
This package include routines to encode a C structure to a
snmp package
programmer: Zhong Yunxiang
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include "asn1.h"
#include "snmp.h"
/*
make packet in buff acording the information
in the struct req_desc
We firstly allocate in reverse order, then reverse it
*/
int mkpacket(struct req_desc *redq, unsigned char *buff)
{
int i,j;
int len,obj_len;
unsigned char buf[SNMPMAXSZ];
struct snbentry sn;
unsigned char len_obj_in,len_obj_out;
unsigned char len_seq1_in,len_seq1_out;
unsigned char len_seq2_in,len_seq2_out;
unsigned char len_err_out;
unsigned char len_reqid_in,len_reqid_out;
unsigned char len_operation_in,len_operation_out;
unsigned char len_comm_out,len_version_out,len_total;
/* printf("\n in the mkpacket function");
*/
len=0;
/* last null */
buf[len]=0x00; len++;
buf[len]=ASN1_NULL; len++;
/*obj id*/
sn=redq->bind;
obj_len=sn.sb_oid.len;
for (i=obj_len-1; i>1; i--) {
buf[len]=(unsigned char)sn.sb_oid.id[i];
len++;
}
buf[len]=0x2B; len++;
len_obj_in=sn.sb_oid.len-1; /*total inter length of obj in ASN.1*/
/*len of obj*/
/* buf[len]=0x08; len++; */
buf[len]=len_obj_in; len++;
/*printf("\n len_obj_in is %x = 0x08, %x",buf[len-1],len_obj_in);
*/
buf[len]=ASN1_OBJID; len++;
len_obj_out=len_obj_in+2;
/*len of seq*/
len_seq1_in=len_obj_out+2; /*total length of seq1 inter in ASN.1*/
/*plus null*/
/*buf[len]=0x0C; len++;*/
buf[len]=len_seq1_in; len++;
/*printf("\n len_seq1_in is %x =0x0C, %x",buf[len-1],len_seq1_in);
*/
buf[len]=ASN1_SEQ; len++;
len_seq1_out=len_seq1_in+2;
/*len of seq*/
len_seq2_in=len_seq1_out; /*total length of seq2 inter in ASN.1*/
/*buf[len]=0x0E; len++;*/
buf[len]=len_seq2_in;len++;
/*printf("\n len_seq2_in is %x =0x0e, %x",buf[len-1],len_seq2_in);
*/
buf[len]=ASN1_SEQ; len++;
len_seq2_out=len_seq2_in+2; /*total length of seq2 out in ASN.1*/
/*error index */
buf[len]=0x00; len++;
buf[len]=0x01; len++;
buf[len]=ASN1_INT; len++;
/*error status*/
buf[len]=0x00; len++;
buf[len]=0x01; len++;
buf[len]=ASN1_INT; len++;
len_err_out=6;
/*requid id*/
buf[len]=0x02; len++;
buf[len]=0x56; len++;
buf[len]=0xAE; len++;
buf[len]=0x05; len++;
/*reid len*/
buf[len]=0x04; len++;
len_reqid_in=4; /*length of requid */
buf[len]=ASN1_INT; len++;
len_reqid_out=len_reqid_in+2; /*total out length of error field and redid*/
/*len of operation*/
/*buf[len]=0x1C; len++; */
len_operation_in=len_seq2_out+len_reqid_out + len_err_out;
/*total inter length of operation*/
buf[len]=len_operation_in; len++;
/*printf("\n len_operation_in is %x =0x1C, %x",buf[len-1],len_operation_in);
*/
buf[len]=PDU_GET; len++;
len_operation_out=len_operation_in+2; /*total out length of operation*/
/*community*/
buf[len]=0x63; len++;
buf[len]=0x69; len++;
buf[len]=0x6C; len++;
buf[len]=0x62; len++;
buf[len]=0x75; len++;
buf[len]=0x70; len++;
/*len of community*/
buf[len]=0x06; len++;
buf[len]=ASN1_OCTSTR; len++;
len_comm_out=8; /*total out length of community*/
/*version*/
buf[len]=0x00; len++;
/*len*/
buf[len]=0x01; len++;
buf[len]=ASN1_INT; len++;
len_version_out=3; /*total out length of version*/
/*total len*/
/*buf[len]=0x29; len++; */
len_total=len_operation_out + len_version_out + len_comm_out;
/*total out length of whole packet*/
buf[len]=len_total; len++;
/*printf("\n len_total is %x =0x29, %x",buf[len-1],len_total);
*/
buf[len]=ASN1_SEQ;
/*
reverse
*/
j=len;
for (i=0;i<=len;i++) {
buff[i]=buf[j];
j=j-1;
}
/*return len+1 */
return(len+1);
}