re-format

This commit is contained in:
Doug MacEachern 2005-03-11 21:09:58 +00:00
parent 634ac82d1c
commit 94f842e473
1 changed files with 197 additions and 199 deletions

View File

@ -47,7 +47,6 @@ static char copyright[] =
#include <stropts.h> #include <stropts.h>
#include <unistd.h> #include <unistd.h>
/* /*
* Local static values * Local static values
*/ */
@ -60,7 +59,6 @@ static int Sd = -1; /* stream device descriptor; not open
* if -1 */ * if -1 */
static char ErrMsg[GET_MIB2_ERRMSGL]; /* error message buffer */ static char ErrMsg[GET_MIB2_ERRMSGL]; /* error message buffer */
/* /*
* close_mib2() - close MIB2 access * close_mib2() - close MIB2 access
* *
@ -72,32 +70,29 @@ static char ErrMsg[GET_MIB2_ERRMSGL]; /* error message buffer */
*/ */
int int
close_mib2( close_mib2(char **errmsg)
char **errmsg /* error message buffer return
* address */
)
{ {
*errmsg = ErrMsg; *errmsg = ErrMsg;
if (Sd < 0) { if (Sd < 0) {
(void) strcpy(ErrMsg, "close_mib2: socket not open"); (void) strcpy(ErrMsg, "close_mib2: socket not open");
return(GET_MIB2_ERR_NOTOPEN); return(GET_MIB2_ERR_NOTOPEN);
} }
if (close(Sd)) { if (close(Sd)) {
(void) sprintf(ErrMsg, "close_mib2: %s", strerror(errno)); (void) sprintf(ErrMsg, "close_mib2: %s", strerror(errno));
return(GET_MIB2_ERR_CLOSE); return(GET_MIB2_ERR_CLOSE);
} }
Sd = -1; Sd = -1;
if (Dbl && Db) { if (Dbl && Db) {
Dbl = 0; Dbl = 0;
free((void *)Db); free((void *)Db);
Db = NULL; Db = NULL;
} }
if (Smbl && Smb) { if (Smbl && Smb) {
Smbl = 0; Smbl = 0;
free((void *)Smb); free((void *)Smb);
Smb = NULL; Smb = NULL;
} }
return(GET_MIB2_OK); return(GET_MIB2_OK);
} }
@ -115,160 +110,164 @@ close_mib2(
*/ */
int int
get_mib2( get_mib2(struct opthdr **opt,
struct opthdr **opt, /* opthdr struct pointer return char **data,
* address */ int *datalen,
char **data, /* data buffer return address */ char **errmsg)
int *datalen, /* data length return address */
char **errmsg /* error message buffer return
* address */
)
{ {
static struct T_optmgmt_ack *a; /* message ACK pointer */ static struct T_optmgmt_ack *a; /* message ACK pointer */
static struct strbuf c; /* streams control buffer */ static struct strbuf c; /* streams control buffer */
struct strbuf d; /* streams data buffer */ struct strbuf d; /* streams data buffer */
static struct T_error_ack *e; /* message error pointer */ static struct T_error_ack *e; /* message error pointer */
int err; /* error code */ int err; /* error code */
int f; /* flags */ int f; /* flags */
static struct opthdr *o; /* message option pointer */ static struct opthdr *o; /* message option pointer */
static struct T_optmgmt_req *r; /* message request pointer */ static struct T_optmgmt_req *r; /* message request pointer */
int rc; /* reply code */ int rc; /* reply code */
*errmsg = ErrMsg; *errmsg = ErrMsg;
/* /*
* If MIB2 access isn't open, open it and issue the preliminary stream * If MIB2 access isn't open, open it and issue the preliminary stream
* messages. * messages.
*/ */
if (Sd < 0) { if (Sd < 0) {
/* /*
* Open access. Return on error. * Open access. Return on error.
*/ */
if ((err = open_mib2(errmsg))) if ((err = open_mib2(errmsg))) {
return(err); return(err);
}
/* /*
* Set up message request and option. * Set up message request and option.
*/ */
r = (struct T_optmgmt_req *)Smb; r = (struct T_optmgmt_req *)Smb;
o = (struct opthdr *)&Smb[sizeof(struct T_optmgmt_req)]; o = (struct opthdr *)&Smb[sizeof(struct T_optmgmt_req)];
r->PRIM_type = T_OPTMGMT_REQ; r->PRIM_type = T_OPTMGMT_REQ;
r->OPT_offset = sizeof(struct T_optmgmt_req); r->OPT_offset = sizeof(struct T_optmgmt_req);
r->OPT_length = sizeof(struct opthdr); r->OPT_length = sizeof(struct opthdr);
#if defined(MI_T_CURRENT) #if defined(MI_T_CURRENT)
r->MGMT_flags = MI_T_CURRENT; r->MGMT_flags = MI_T_CURRENT;
#else /* !defined(MI_T_CURRENT) */ #else /* !defined(MI_T_CURRENT) */
# if defined(T_CURRENT) # if defined(T_CURRENT)
r->MGMT_flags = T_CURRENT; r->MGMT_flags = T_CURRENT;
# else /* !defined(T_CURRENT) */ # else /* !defined(T_CURRENT) */
#error "Neither MI_T_CURRENT nor T_CURRENT are defined." #error "Neither MI_T_CURRENT nor T_CURRENT are defined."
# endif /* defined(T_CURRENT) */ # endif /* defined(T_CURRENT) */
#endif /* defined(MI_T_CURRENT) */ #endif /* defined(MI_T_CURRENT) */
o->level = MIB2_IP; o->level = MIB2_IP;
o->name = o->len = 0; o->name = o->len = 0;
c.buf = Smb; c.buf = Smb;
c.len = r->OPT_offset + r->OPT_length; c.len = r->OPT_offset + r->OPT_length;
/* /*
* Put the message. * Put the message.
*/ */
if (putmsg(Sd, &c, (struct strbuf *)NULL, 0) == -1) { if (putmsg(Sd, &c, (struct strbuf *)NULL, 0) == -1) {
(void) sprintf(ErrMsg, (void) sprintf(ErrMsg,
"get_mib2: putmsg request: %s", strerror(errno)); "get_mib2: putmsg request: %s", strerror(errno));
return(GET_MIB2_ERR_PUTMSG); return(GET_MIB2_ERR_PUTMSG);
} }
/* /*
* Set up to process replies. * Set up to process replies.
*/ */
a = (struct T_optmgmt_ack *)Smb; a = (struct T_optmgmt_ack *)Smb;
c.maxlen = Smbl; c.maxlen = Smbl;
e = (struct T_error_ack *)Smb; e = (struct T_error_ack *)Smb;
o = (struct opthdr *)&Smb[sizeof(struct T_optmgmt_ack)]; o = (struct opthdr *)&Smb[sizeof(struct T_optmgmt_ack)];
} }
/* /*
* Get the next (first) reply message. * Get the next (first) reply message.
*/ */
f = 0; f = 0;
if ((rc = getmsg(Sd, &c, NULL, &f)) < 0) { if ((rc = getmsg(Sd, &c, NULL, &f)) < 0) {
(void) sprintf(ErrMsg, "get_mib2: getmsg(reply): %s", (void) sprintf(ErrMsg, "get_mib2: getmsg(reply): %s",
strerror(errno)); strerror(errno));
return(GET_MIB2_ERR_GETMSGR); return(GET_MIB2_ERR_GETMSGR);
} }
/* /*
* Check for end of data. * Check for end of data.
*/ */
if (rc == 0 if (rc == 0
&& c.len >= sizeof(struct T_optmgmt_ack) && c.len >= sizeof(struct T_optmgmt_ack)
&& a->PRIM_type == T_OPTMGMT_ACK && a->PRIM_type == T_OPTMGMT_ACK
&& a->MGMT_flags == T_SUCCESS && a->MGMT_flags == T_SUCCESS
&& o->len == 0) { && o->len == 0)
err = close_mib2(errmsg); {
if (err) err = close_mib2(errmsg);
return(err); if (err) {
return(GET_MIB2_EOD); return(err);
} }
/* return(GET_MIB2_EOD);
* Check for error. }
*/ /*
if (c.len >= sizeof(struct T_error_ack) * Check for error.
&& e->PRIM_type == T_ERROR_ACK) { */
(void) sprintf(ErrMsg, if (c.len >= sizeof(struct T_error_ack)
"get_mib2: T_ERROR_ACK: len=%d, TLI=%#x, UNIX=%#x", && e->PRIM_type == T_ERROR_ACK)
c.len, (int)e->TLI_error, (int)e->UNIX_error); {
return(GET_MIB2_ERR_ACK); (void) sprintf(ErrMsg,
} "get_mib2: T_ERROR_ACK: len=%d, TLI=%#x, UNIX=%#x",
/* c.len, (int)e->TLI_error, (int)e->UNIX_error);
* Check for no data. return(GET_MIB2_ERR_ACK);
*/ }
if (rc != MOREDATA /*
* Check for no data.
*/
if (rc != MOREDATA
|| c.len < sizeof(struct T_optmgmt_ack) || c.len < sizeof(struct T_optmgmt_ack)
|| a->PRIM_type != T_OPTMGMT_ACK || a->PRIM_type != T_OPTMGMT_ACK
|| a->MGMT_flags != T_SUCCESS) { || a->MGMT_flags != T_SUCCESS)
(void) sprintf(ErrMsg, {
"get_mib2: T_OPTMGMT_ACK: rc=%d len=%d type=%#x flags=%#x", (void) sprintf(ErrMsg,
rc, c.len, (int)a->PRIM_type, (int)a->MGMT_flags); "get_mib2: T_OPTMGMT_ACK: "
return(GET_MIB2_ERR_NODATA); "rc=%d len=%d type=%#x flags=%#x",
} rc, c.len, (int)a->PRIM_type, (int)a->MGMT_flags);
/* return(GET_MIB2_ERR_NODATA);
* Allocate (or enlarge) the data buffer. }
*/ /*
if (o->len >= Dbl) { * Allocate (or enlarge) the data buffer.
Dbl = o->len; */
if (Db == NULL) if (o->len >= Dbl) {
Db = (char *)malloc(Dbl); Dbl = o->len;
else if (Db == NULL) {
Db = (char *)realloc(Db, Dbl); Db = (char *)malloc(Dbl);
if (Db == NULL) { }
(void) sprintf(ErrMsg, else {
"get_mib2: no space for %d byte data buffer", Dbl); Db = (char *)realloc(Db, Dbl);
return(GET_MIB2_ERR_NOSPC); }
} if (Db == NULL) {
} (void) sprintf(ErrMsg,
/* "get_mib2: no space for %d byte data buffer", Dbl);
* Get the data part of the message -- the MIB2 part. return(GET_MIB2_ERR_NOSPC);
*/ }
d.maxlen = o->len; }
d.buf = Db; /*
d.len = 0; * Get the data part of the message -- the MIB2 part.
f = 0; */
if ((rc = getmsg(Sd, NULL, &d, &f)) < 0) { d.maxlen = o->len;
(void) sprintf(ErrMsg, "get_mib2: getmsg(data): %s", d.buf = Db;
strerror(errno)); d.len = 0;
return(GET_MIB2_ERR_GETMSGD); f = 0;
} if ((rc = getmsg(Sd, NULL, &d, &f)) < 0) {
if (rc) { (void) sprintf(ErrMsg, "get_mib2: getmsg(data): %s",
(void) sprintf(ErrMsg, strerror(errno));
"get_mib2: getmsg(data): rc=%d maxlen=%d len=%d: %s", return(GET_MIB2_ERR_GETMSGD);
rc, d.maxlen, d.len, strerror(errno)); }
return(GET_MIB2_ERR_GETMSGD); if (rc) {
} (void) sprintf(ErrMsg,
/* "get_mib2: getmsg(data): rc=%d maxlen=%d len=%d: %s",
* Compose a successful return. rc, d.maxlen, d.len, strerror(errno));
*/ return(GET_MIB2_ERR_GETMSGD);
*opt = o; }
*data = Db; /*
*datalen = d.len; * Compose a successful return.
return(GET_MIB2_OK); */
*opt = o;
*data = Db;
*datalen = d.len;
return(GET_MIB2_OK);
} }
@ -283,52 +282,51 @@ get_mib2(
*/ */
int int
open_mib2( open_mib2(char **errmsg)
char **errmsg /* error message buffer return
* address */
)
{ {
*errmsg = ErrMsg; *errmsg = ErrMsg;
/* /*
* It's an error if the stream device is already open. * It's an error if the stream device is already open.
*/ */
if (Sd >= 0) { if (Sd >= 0) {
(void) strcpy(ErrMsg, "open_mib2: MIB2 access already open"); (void) strcpy(ErrMsg, "open_mib2: MIB2 access already open");
return(GET_MIB2_ERR_OPEN); return(GET_MIB2_ERR_OPEN);
} }
/* /*
* Open the ARP stream device, push TCP and UDP on it. * Open the ARP stream device, push TCP and UDP on it.
*/ */
if ((Sd = open(GET_MIB2_ARPDEV, O_RDWR, 0600)) < 0) { if ((Sd = open(GET_MIB2_ARPDEV, O_RDWR, 0600)) < 0) {
(void) sprintf(ErrMsg, "open_mib2: %s: %s", GET_MIB2_ARPDEV, (void) sprintf(ErrMsg, "open_mib2: %s: %s", GET_MIB2_ARPDEV,
strerror(errno)); strerror(errno));
return(GET_MIB2_ERR_ARPOPEN); return(GET_MIB2_ERR_ARPOPEN);
} }
if (ioctl(Sd, I_PUSH, GET_MIB2_TCPSTREAM) == -1) { if (ioctl(Sd, I_PUSH, GET_MIB2_TCPSTREAM) == -1) {
(void) sprintf(ErrMsg, "open_mib2: push %s: %s", (void) sprintf(ErrMsg, "open_mib2: push %s: %s",
GET_MIB2_TCPSTREAM, strerror(errno)); GET_MIB2_TCPSTREAM, strerror(errno));
return(GET_MIB2_ERR_TCPPUSH); return(GET_MIB2_ERR_TCPPUSH);
} }
if (ioctl(Sd, I_PUSH, GET_MIB2_UDPSTREAM) == -1) { if (ioctl(Sd, I_PUSH, GET_MIB2_UDPSTREAM) == -1) {
(void) sprintf(ErrMsg, "open_mib2: push %s: %s", (void) sprintf(ErrMsg, "open_mib2: push %s: %s",
GET_MIB2_UDPSTREAM, strerror(errno)); GET_MIB2_UDPSTREAM, strerror(errno));
return(GET_MIB2_ERR_UDPPUSH); return(GET_MIB2_ERR_UDPPUSH);
} }
/* /*
* Allocate a stream message buffer. * Allocate a stream message buffer.
*/ */
Smbl = sizeof(struct opthdr) + sizeof(struct T_optmgmt_req); Smbl = sizeof(struct opthdr) + sizeof(struct T_optmgmt_req);
if (Smbl < (sizeof (struct opthdr) + sizeof(struct T_optmgmt_ack))) if (Smbl < (sizeof (struct opthdr) + sizeof(struct T_optmgmt_ack))) {
Smbl = sizeof (struct opthdr) + sizeof(struct T_optmgmt_ack); Smbl = sizeof (struct opthdr) + sizeof(struct T_optmgmt_ack);
if (Smbl < sizeof(struct T_error_ack)) }
Smbl = sizeof(struct T_error_ack); if (Smbl < sizeof(struct T_error_ack)) {
if ((Smb = (char *)malloc(Smbl)) == NULL) { Smbl = sizeof(struct T_error_ack);
(void) strcpy(ErrMsg, }
"open_mib2: no space for stream message buffer"); if ((Smb = (char *)malloc(Smbl)) == NULL) {
return(GET_MIB2_ERR_NOSPC); (void) strcpy(ErrMsg,
} "open_mib2: no space for stream message buffer");
/* return(GET_MIB2_ERR_NOSPC);
* All is OK. Return that indication. }
*/ /*
return(GET_MIB2_OK); * All is OK. Return that indication.
*/
return(GET_MIB2_OK);
} }