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
/
snmpbasic.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-05-11
|
8KB
|
373 lines
/*
This package includes the basic routine to do snmp retrieve
*/
#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 <ctype.h>
#include <signal.h>
#include <setjmp.h>
#include "asn1.h"
#include "snmp.h"
#include "snmparse.h"
#include "snmpencode.h"
#define MaxTimeout_sec 5
jmp_buf timeoutbuf;
void timer()
{
longjmp(timeoutbuf,1);
}
/*
change a string to a oid struct
*/
struct oid textoid(char *str)
{
int len,sub_len;
char sub_id[10];
unsigned short sub_num;
struct oid idd;
int i;
len=0;
sub_len=0;
bzero(sub_id, sizeof(sub_id));
for (i=0; str[i] != '\0';i++) {
if (str[i] != '.') {
sub_id[sub_len]=str[i];
sub_len++;
}
else {
sub_id[sub_len]='\0';
sub_num=(unsigned short)atoi(sub_id);
/*assign to idd struct*/
idd.id[len]=sub_num;
len++;
/*initialize*/
bzero(sub_id, sizeof(sub_id));
sub_len=0;
}
}
idd.len=len;
/*
printf("\n");
for (i=0;i<len;i++)
printf("BEGIN:%d ",idd.id[i]);
*/
return(idd);
}
/*
send snmp package
receive snmp package
Both in straight buff
*/
void snmp_send_recv(char *hostname,int len,unsigned char *buff,unsigned char *buff_in)
{
struct sockaddr_in sa; /*local socket*/
struct sockaddr_in from; /*remote socket */
struct hostent *hostptr; /*host address struct*/
int s; /*socket number */
int fromlen; /*remote socket length*/
unsigned long server_ipaddr_host; /*real ip addr in host*/
unsigned long server_ipaddr_net; /*real ip addr in net*/
int cc;
/*
initialize socket
*/
bzero((char *)&sa,sizeof(sa));
sa.sin_family=AF_INET;
if (hostptr=gethostbyname(hostname)) {
bcopy(hostptr->h_addr,&sa.sin_addr,hostptr->h_length);
}
else {
if(server_ipaddr_host=inet_addr(hostname)) {
server_ipaddr_net=htonl(server_ipaddr_host);/*change to network format*/
bcopy(&server_ipaddr_net,&sa.sin_addr,sizeof(server_ipaddr_net));
}
else {
fprintf(stderr,"can not find such host name");
/* exit(0); may be C++ don't allow to use it*/
};
}
if ( (s=socket(AF_INET,SOCK_DGRAM,0) )<0) {
fprintf(stderr,"can not create socket");
exit(0);
};
/*
put service number
*/
sa.sin_port=htons(snmp_port);
signal(SIGALRM,timer);
(void)setjmp(timeoutbuf);
/*
send the first request packet
*/
if ((cc=sendto(s,buff,len,0,(struct sockaddr *)&sa,sizeof(sa))) <0) {
fprintf(stderr, "\nsend error");
exit(0);
}
/*
receive packet
*/
alarm(5);
if ((cc=recvfrom(s,buff_in,600,0,(struct sockaddr *)&from,&fromlen)) <0){
fprintf(stderr, "\nread error");
exit(0);
}
alarm(0);
close(s);
}
/*
read mib.txt file
put all "name, obj, type " in an array of structure snbentry
return: the number of mib items in the MIB.TXT file
*/
int init_mib()
{
int i,j;
int finish;
char obj_str[MAXSNMPNAME];
char type_str[MAXSNMPNAME];
char name[MAXSNMPNAME];
struct oid id;
FILE *mib_file; /*MIB.TXT file */
if ((mib_file=fopen("MIB.TXT","r")) == NULL) {
printf("\n MIB.TXT can not be opened");
exit(0);
};
i=0;
while (1) {
bzero(name,sizeof(name));
fscanf(mib_file,"%s",name);
if (!strcmp(name,"END_OF_MIB.TXT")) {
break;
}
strcpy(mib[i].name,name);
fscanf(mib_file,"%s",obj_str);
fscanf(mib_file,"%s",type_str);
/* printf("\n%s,%s, %s",mib[i].name,obj_str,type_str);
*/
id=textoid(obj_str);
mib[i].sb_oid=id;
/*
printf("\n");
for (j=0;j<mib[i].sb_oid.len;j++)
printf("BEGIN:%d ",mib[i].sb_oid.id[j]);
*/
if (!strcmp("counter",type_str))
mib[i].sb_val.sv_type=(int)ASN1_COUNTER;
if (!strcmp("integer",type_str))
mib[i].sb_val.sv_type=ASN1_INT;
if (!strcmp("gauge",type_str))
mib[i].sb_val.sv_type=ASN1_GAUGE;
if (!strcmp("ticks",type_str))
mib[i].sb_val.sv_type=ASN1_TIMETICKS;
if (!strcmp("number",type_str))
mib[i].sb_val.sv_type=ASN1_INT;
/* if (!strcmp("counter",type_str))
mib[i].sb_val.sv_type=ASN1_;
*/
i++;
}
close(mib_file);
return(i);
}
/*
give a snbentry struct of a name
*/
struct snbentry get_struct(char *name)
{
struct snbentry sn;
struct oid idd;
char prefix[100];
int pre_index;
int i,j;
unsigned short sub_num;
char sub_id[10];
int sub_len;
/*printf("\n in get_struct");*/
/*
sn.sb_oid.len=9; sn.sb_oid.id[0]=1; sn.sb_oid.id[1]=3;
sn.sb_oid.id[2]=6; sn.sb_oid.id[3]=1; sn.sb_oid.id[4]=2;
sn.sb_oid.id[5]=1; sn.sb_oid.id[6]=5; sn.sb_oid.id[7]=j;j++;
sn.sb_oid.id[8]=0;
*/
if (isdigit(name[0])) {
idd=textoid(name);
}
else {
pre_index=0;
for (j=0;name[j] != '\0'; j++) {
if (name[j] != '.') {
prefix[pre_index]=name[j];
pre_index++;
}
else {
prefix[pre_index]='\0';
break;
}
}
/* printf("\n prefix is:%s",prefix);
*/
for (i=0;i<NUM_MIBITEM;i++) {
/*
printf("\n in the loop,name is:%s",mib[i].name);
*/
if (!strcmp(mib[i].name,prefix))
idd=mib[i].sb_oid;
}
/* printf("\n");
for (i=0;i<idd.len;i++)
printf("B:%d ",idd.id[i]);
*/
bzero(sub_id,sizeof(sub_id));
sub_len=0;
for (j++;name[j] != '\0'; j++)
if (name[j] != '.') {
sub_id[sub_len]=name[j];
sub_len++;
}
else {
sub_id[sub_len]='\0';
sub_num=(unsigned short)atoi(sub_id);
/*assign to idd struct*/
idd.id[idd.len]=sub_num;
idd.len++;
/*initialize*/
bzero(sub_id, sizeof(sub_id));
sub_len=0;
}
};
/*
printf("\n");
for (i=0;i<idd.len;i++)
printf("B:%d ",idd.id[i]);
*/
sn.sb_oid=idd;
return(sn);
}
/*
procedure: give the struct snbentry according to the "name"
"name": is an instance, it can be single, it can be a variable inside a
table
"
So, we can know its value
*/
struct snbentry get_value(char *name,char *hostname)
{
struct req_desc *rqdq,*rqdq_in ;
struct snbentry bind1,bind1_in;
unsigned char buff[SNMPMAXSZ],buff_in[SNMPMAXSZ],reqidsave[10],reqidsavelen;
int snmpdev,len,frlen;
int i;
/* printf("\n in get_value function");
*/
bzero(buff,SNMPMAXSZ);
bzero(buff_in,SNMPMAXSZ);
/*make packet
*/
bind1=get_struct(name);
rqdq=new struct req_desc;
rqdq->bind=bind1;
rqdq->reqidlen=0;
rqdq->err_stat=0;
rqdq->err_idx=0;
len=mkpacket(rqdq,buff);
/*send packet and receive packet
*/
/*
for (i=0; i<=len; i++)
printf("%x ",buff[i]);
*/
snmp_send_recv(hostname,len,buff,buff_in);
/*
printf("\n");
for (i=0; i<=len+20; i++)
printf("%x ",buff_in[i]);
*/
/*
decode snmp packet
*/
rqdq_in=new struct req_desc;
de_packet(rqdq_in,buff_in,len);
alreadval(rqdq_in); /*change from ASN.1 to internal*/
bind1_in=rqdq_in->bind;
strcpy(bind1_in.name,name);
return(bind1_in);
}