From 94f842e473f7070359e570a575d70af65c68062a Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Fri, 11 Mar 2005 21:09:58 +0000 Subject: [PATCH] re-format --- src/os/solaris/get_mib2.c | 396 +++++++++++++++++++------------------- 1 file changed, 197 insertions(+), 199 deletions(-) diff --git a/src/os/solaris/get_mib2.c b/src/os/solaris/get_mib2.c index 9740eec9..5ea1c847 100644 --- a/src/os/solaris/get_mib2.c +++ b/src/os/solaris/get_mib2.c @@ -47,7 +47,6 @@ static char copyright[] = #include #include - /* * Local static values */ @@ -60,7 +59,6 @@ static int Sd = -1; /* stream device descriptor; not open * if -1 */ static char ErrMsg[GET_MIB2_ERRMSGL]; /* error message buffer */ - /* * close_mib2() - close MIB2 access * @@ -72,32 +70,29 @@ static char ErrMsg[GET_MIB2_ERRMSGL]; /* error message buffer */ */ int -close_mib2( - char **errmsg /* error message buffer return - * address */ - ) +close_mib2(char **errmsg) { - *errmsg = ErrMsg; - if (Sd < 0) { - (void) strcpy(ErrMsg, "close_mib2: socket not open"); - return(GET_MIB2_ERR_NOTOPEN); - } - if (close(Sd)) { - (void) sprintf(ErrMsg, "close_mib2: %s", strerror(errno)); - return(GET_MIB2_ERR_CLOSE); - } - Sd = -1; - if (Dbl && Db) { - Dbl = 0; - free((void *)Db); - Db = NULL; - } - if (Smbl && Smb) { - Smbl = 0; - free((void *)Smb); - Smb = NULL; - } - return(GET_MIB2_OK); + *errmsg = ErrMsg; + if (Sd < 0) { + (void) strcpy(ErrMsg, "close_mib2: socket not open"); + return(GET_MIB2_ERR_NOTOPEN); + } + if (close(Sd)) { + (void) sprintf(ErrMsg, "close_mib2: %s", strerror(errno)); + return(GET_MIB2_ERR_CLOSE); + } + Sd = -1; + if (Dbl && Db) { + Dbl = 0; + free((void *)Db); + Db = NULL; + } + if (Smbl && Smb) { + Smbl = 0; + free((void *)Smb); + Smb = NULL; + } + return(GET_MIB2_OK); } @@ -115,160 +110,164 @@ close_mib2( */ int -get_mib2( - struct opthdr **opt, /* opthdr struct pointer return - * address */ - char **data, /* data buffer return address */ - int *datalen, /* data length return address */ - char **errmsg /* error message buffer return - * address */ - ) +get_mib2(struct opthdr **opt, + char **data, + int *datalen, + char **errmsg) { - static struct T_optmgmt_ack *a; /* message ACK pointer */ - static struct strbuf c; /* streams control buffer */ - struct strbuf d; /* streams data buffer */ - static struct T_error_ack *e; /* message error pointer */ - int err; /* error code */ - int f; /* flags */ - static struct opthdr *o; /* message option pointer */ - static struct T_optmgmt_req *r; /* message request pointer */ - int rc; /* reply code */ + static struct T_optmgmt_ack *a; /* message ACK pointer */ + static struct strbuf c; /* streams control buffer */ + struct strbuf d; /* streams data buffer */ + static struct T_error_ack *e; /* message error pointer */ + int err; /* error code */ + int f; /* flags */ + static struct opthdr *o; /* message option pointer */ + static struct T_optmgmt_req *r; /* message request pointer */ + int rc; /* reply code */ - *errmsg = ErrMsg; -/* - * If MIB2 access isn't open, open it and issue the preliminary stream - * messages. - */ - if (Sd < 0) { + *errmsg = ErrMsg; + /* + * If MIB2 access isn't open, open it and issue the preliminary stream + * messages. + */ + if (Sd < 0) { /* * Open access. Return on error. */ - if ((err = open_mib2(errmsg))) - return(err); + if ((err = open_mib2(errmsg))) { + return(err); + } /* * Set up message request and option. */ - r = (struct T_optmgmt_req *)Smb; - o = (struct opthdr *)&Smb[sizeof(struct T_optmgmt_req)]; - r->PRIM_type = T_OPTMGMT_REQ; - r->OPT_offset = sizeof(struct T_optmgmt_req); - r->OPT_length = sizeof(struct opthdr); + r = (struct T_optmgmt_req *)Smb; + o = (struct opthdr *)&Smb[sizeof(struct T_optmgmt_req)]; + r->PRIM_type = T_OPTMGMT_REQ; + r->OPT_offset = sizeof(struct T_optmgmt_req); + r->OPT_length = sizeof(struct opthdr); #if defined(MI_T_CURRENT) - r->MGMT_flags = MI_T_CURRENT; + r->MGMT_flags = MI_T_CURRENT; #else /* !defined(MI_T_CURRENT) */ # if defined(T_CURRENT) - r->MGMT_flags = T_CURRENT; + r->MGMT_flags = T_CURRENT; # else /* !defined(T_CURRENT) */ #error "Neither MI_T_CURRENT nor T_CURRENT are defined." # endif /* defined(T_CURRENT) */ #endif /* defined(MI_T_CURRENT) */ - o->level = MIB2_IP; - o->name = o->len = 0; - c.buf = Smb; - c.len = r->OPT_offset + r->OPT_length; + o->level = MIB2_IP; + o->name = o->len = 0; + c.buf = Smb; + c.len = r->OPT_offset + r->OPT_length; /* * Put the message. */ - if (putmsg(Sd, &c, (struct strbuf *)NULL, 0) == -1) { - (void) sprintf(ErrMsg, - "get_mib2: putmsg request: %s", strerror(errno)); - return(GET_MIB2_ERR_PUTMSG); - } + if (putmsg(Sd, &c, (struct strbuf *)NULL, 0) == -1) { + (void) sprintf(ErrMsg, + "get_mib2: putmsg request: %s", strerror(errno)); + return(GET_MIB2_ERR_PUTMSG); + } /* * Set up to process replies. */ - a = (struct T_optmgmt_ack *)Smb; - c.maxlen = Smbl; - e = (struct T_error_ack *)Smb; - o = (struct opthdr *)&Smb[sizeof(struct T_optmgmt_ack)]; - } -/* - * Get the next (first) reply message. - */ - f = 0; - if ((rc = getmsg(Sd, &c, NULL, &f)) < 0) { - (void) sprintf(ErrMsg, "get_mib2: getmsg(reply): %s", - strerror(errno)); - return(GET_MIB2_ERR_GETMSGR); - } -/* - * Check for end of data. - */ - if (rc == 0 + a = (struct T_optmgmt_ack *)Smb; + c.maxlen = Smbl; + e = (struct T_error_ack *)Smb; + o = (struct opthdr *)&Smb[sizeof(struct T_optmgmt_ack)]; + } + /* + * Get the next (first) reply message. + */ + f = 0; + if ((rc = getmsg(Sd, &c, NULL, &f)) < 0) { + (void) sprintf(ErrMsg, "get_mib2: getmsg(reply): %s", + strerror(errno)); + return(GET_MIB2_ERR_GETMSGR); + } + /* + * Check for end of data. + */ + if (rc == 0 && c.len >= sizeof(struct T_optmgmt_ack) && a->PRIM_type == T_OPTMGMT_ACK && a->MGMT_flags == T_SUCCESS - && o->len == 0) { - err = close_mib2(errmsg); - if (err) - return(err); - return(GET_MIB2_EOD); - } -/* - * Check for error. - */ - if (c.len >= sizeof(struct T_error_ack) - && e->PRIM_type == T_ERROR_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); - return(GET_MIB2_ERR_ACK); - } -/* - * Check for no data. - */ - if (rc != MOREDATA + && o->len == 0) + { + err = close_mib2(errmsg); + if (err) { + return(err); + } + return(GET_MIB2_EOD); + } + /* + * Check for error. + */ + if (c.len >= sizeof(struct T_error_ack) + && e->PRIM_type == T_ERROR_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); + return(GET_MIB2_ERR_ACK); + } + /* + * Check for no data. + */ + if (rc != MOREDATA || c.len < sizeof(struct T_optmgmt_ack) || a->PRIM_type != T_OPTMGMT_ACK - || a->MGMT_flags != T_SUCCESS) { - (void) sprintf(ErrMsg, - "get_mib2: T_OPTMGMT_ACK: 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) { - Dbl = o->len; - if (Db == NULL) - Db = (char *)malloc(Dbl); - else - Db = (char *)realloc(Db, Dbl); - if (Db == NULL) { - (void) sprintf(ErrMsg, - "get_mib2: no space for %d byte data buffer", Dbl); - return(GET_MIB2_ERR_NOSPC); - } - } -/* - * Get the data part of the message -- the MIB2 part. - */ - d.maxlen = o->len; - d.buf = Db; - d.len = 0; - f = 0; - if ((rc = getmsg(Sd, NULL, &d, &f)) < 0) { - (void) sprintf(ErrMsg, "get_mib2: getmsg(data): %s", - strerror(errno)); - return(GET_MIB2_ERR_GETMSGD); - } - if (rc) { - (void) sprintf(ErrMsg, - "get_mib2: getmsg(data): rc=%d maxlen=%d len=%d: %s", - rc, d.maxlen, d.len, strerror(errno)); - return(GET_MIB2_ERR_GETMSGD); - } -/* - * Compose a successful return. - */ - *opt = o; - *data = Db; - *datalen = d.len; - return(GET_MIB2_OK); + || a->MGMT_flags != T_SUCCESS) + { + (void) sprintf(ErrMsg, + "get_mib2: T_OPTMGMT_ACK: " + "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) { + Dbl = o->len; + if (Db == NULL) { + Db = (char *)malloc(Dbl); + } + else { + Db = (char *)realloc(Db, Dbl); + } + if (Db == NULL) { + (void) sprintf(ErrMsg, + "get_mib2: no space for %d byte data buffer", Dbl); + return(GET_MIB2_ERR_NOSPC); + } + } + /* + * Get the data part of the message -- the MIB2 part. + */ + d.maxlen = o->len; + d.buf = Db; + d.len = 0; + f = 0; + if ((rc = getmsg(Sd, NULL, &d, &f)) < 0) { + (void) sprintf(ErrMsg, "get_mib2: getmsg(data): %s", + strerror(errno)); + return(GET_MIB2_ERR_GETMSGD); + } + if (rc) { + (void) sprintf(ErrMsg, + "get_mib2: getmsg(data): rc=%d maxlen=%d len=%d: %s", + rc, d.maxlen, d.len, strerror(errno)); + return(GET_MIB2_ERR_GETMSGD); + } + /* + * Compose a successful return. + */ + *opt = o; + *data = Db; + *datalen = d.len; + return(GET_MIB2_OK); } @@ -283,52 +282,51 @@ get_mib2( */ int -open_mib2( - char **errmsg /* error message buffer return - * address */ - ) +open_mib2(char **errmsg) { - *errmsg = ErrMsg; -/* - * It's an error if the stream device is already open. - */ - if (Sd >= 0) { - (void) strcpy(ErrMsg, "open_mib2: MIB2 access already open"); - return(GET_MIB2_ERR_OPEN); - } -/* - * Open the ARP stream device, push TCP and UDP on it. - */ - if ((Sd = open(GET_MIB2_ARPDEV, O_RDWR, 0600)) < 0) { - (void) sprintf(ErrMsg, "open_mib2: %s: %s", GET_MIB2_ARPDEV, - strerror(errno)); - return(GET_MIB2_ERR_ARPOPEN); - } - if (ioctl(Sd, I_PUSH, GET_MIB2_TCPSTREAM) == -1) { - (void) sprintf(ErrMsg, "open_mib2: push %s: %s", - GET_MIB2_TCPSTREAM, strerror(errno)); - return(GET_MIB2_ERR_TCPPUSH); - } - if (ioctl(Sd, I_PUSH, GET_MIB2_UDPSTREAM) == -1) { - (void) sprintf(ErrMsg, "open_mib2: push %s: %s", - GET_MIB2_UDPSTREAM, strerror(errno)); - return(GET_MIB2_ERR_UDPPUSH); - } -/* - * Allocate a stream message buffer. - */ - Smbl = sizeof(struct opthdr) + sizeof(struct T_optmgmt_req); - if (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 ((Smb = (char *)malloc(Smbl)) == NULL) { - (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); + *errmsg = ErrMsg; + /* + * It's an error if the stream device is already open. + */ + if (Sd >= 0) { + (void) strcpy(ErrMsg, "open_mib2: MIB2 access already open"); + return(GET_MIB2_ERR_OPEN); + } + /* + * Open the ARP stream device, push TCP and UDP on it. + */ + if ((Sd = open(GET_MIB2_ARPDEV, O_RDWR, 0600)) < 0) { + (void) sprintf(ErrMsg, "open_mib2: %s: %s", GET_MIB2_ARPDEV, + strerror(errno)); + return(GET_MIB2_ERR_ARPOPEN); + } + if (ioctl(Sd, I_PUSH, GET_MIB2_TCPSTREAM) == -1) { + (void) sprintf(ErrMsg, "open_mib2: push %s: %s", + GET_MIB2_TCPSTREAM, strerror(errno)); + return(GET_MIB2_ERR_TCPPUSH); + } + if (ioctl(Sd, I_PUSH, GET_MIB2_UDPSTREAM) == -1) { + (void) sprintf(ErrMsg, "open_mib2: push %s: %s", + GET_MIB2_UDPSTREAM, strerror(errno)); + return(GET_MIB2_ERR_UDPPUSH); + } + /* + * Allocate a stream message buffer. + */ + Smbl = sizeof(struct opthdr) + sizeof(struct T_optmgmt_req); + if (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 ((Smb = (char *)malloc(Smbl)) == NULL) { + (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); }