Plan 9 from Bell Labs’s /usr/web/sources/contrib/gabidiaz/root/sys/src/cmd/snmpfs/ndbutil.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <u.h>
#include <libc.h>
#include <bio.h>
#include <ndb.h>
#include "ndbutil.h"

enum {
	Get,
	GetNext,
	Walk,
	Bulk,
	Set,
	Rocomm,
	Rwcomm,
	Version,
	Retries,
	Nrep,
	Maxrep,
	Oid,
	Setval,
	Timeout
};

char *configtab[] = {
	[Get] "get",
	[GetNext] "getnext",
	[Walk] "walk",
	[Bulk] "bulk",
	[Set] "set",
	[Rocomm] "rocomm",
	[Rwcomm] "rwcomm",
	[Version] "version",
	[Retries] "retries",
	[Nrep] "nrep",
	[Maxrep] "maxrep",
	[Oid] "oid",
	[Setval] "setval",
	[Timeout] "timeout"
};



/* we expect op is defined just after each oid */
/* if not defined, we defaults to Get */
int
ndbgetop(char *sys, char *oid) {
	Ndbtuple *t, *nt;
	int op;
	
	t = ndbsearch(NDB, &s, "sys", sys);
	if(t){
		for(nt = t; nt; nt = nt->entry) {
			if ( strlen(nt->val) == strlen(oid) && strncmp(nt->val,oid,strlen(oid)) == 0 ) {
				if ( nt = nt->entry ) {
					if ( strncmp(nt->attr,"op",2) == 0 ) {
						for(op=Get;op<Rocomm;op++) {
							if ( strncmp(nt->val,configtab[op],strlen(configtab[op])) == 0 ) {
								ndbfree(t);
								return op;
							}
						}
					} else {
						ndbfree(t);
						return GetNext;
					}
				}
			}
		}
		/* if there is no oid in ndb, try GetNext by default */
		/* this is done to be able to create arbitrary files without ndb */
		ndbfree(t);
		return GetNext;
	}
	
	/* sys not found -  return valid pdu to be able to use  */
	/* hosts that are not in ndb - for testing purposes */
	return GetNext;
}

/*just takes the first match */
char*
ndbget(char *host, char *attr) {

	Ndbtuple *t;
	char *buff = nil;
	
	t = ndbipinfo(NDB, "sys", host, &attr, 1);
	if ( t != nil )
		buff = strdup(t->val);
		 
	ndbfree(t);
	return buff;

}

int
isoid(char *s)
{
	char *f[1];
	int n;
	char *aux;

	aux = strdup(s);
	n = getfields(aux, f, 1, 0, ".");

	if ( n < 0 || atoi(f[0]) < 1 ) {
		free(aux);
		return 0;
	}
	free(aux);
	return 1;

}
/* translate a name to an oid */
/* if it doesn't found the entry in NDB file */
/* returns a copy of the string *s */
char*
resolve(char *str)
{
	Ndbs s;
	char *buff;

	buff = ndbgetvalue(NDB, &s, "name", str, "oid", nil);
	if ( buff != nil )
		return buff;
	else {
		 if (isoid(str))
			return strdup(str);
		else
			return nil;
	}
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.