* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
-
+ *
* You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * with this program; see the file COPYING; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <zebra.h>
+#include "printfrr.h"
#include "stream.h"
#include "vty.h"
#include "hash.h"
#include "if.h"
#include "command.h"
-#include "isisd/dict.h"
#include "isisd/isis_constants.h"
#include "isisd/isis_common.h"
#include "isisd/isis_flags.h"
#include "isisd/isisd.h"
#include "isisd/isis_misc.h"
-#include "isisd/isis_tlv.h"
#include "isisd/isis_lsp.h"
#include "isisd/isis_constants.h"
#include "isisd/isis_adjacency.h"
/* staticly assigned vars for printing purposes */
struct in_addr new_prefix;
-/* len of xxxx.xxxx.xxxx + place for #0 termination */
-char sysid[15];
-/* len of xxxx.xxxx.xxxx + place for #0 termination */
-char snpa[15];
/* len of xx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xx */
-char isonet[51];
/* + place for #0 termination */
-/* len of xxxx.xxxx.xxxx.xx.xx + place for #0 termination */
-char lspid[21];
+char isonet[51];
/* len of xxYxxMxWxdxxhxxmxxs + place for #0 termination */
char datestring[20];
char nlpidstring[30];
/*
* This converts the isonet to its printable format
*/
-const char *isonet_print(const u_char *from, int len)
+const char *isonet_print(const uint8_t *from, int len)
{
int i = 0;
char *pos = isonet;
* Returns 0 on error, length of buff on ok
* extract dot from the dotted str, and insert all the number in a buff
*/
-int dotformat2buff(u_char *buff, const char *dotted)
+int dotformat2buff(uint8_t *buff, const char *dotted)
{
int dotlen, len = 0;
const char *pos = dotted;
- u_char number[3];
+ uint8_t number[3];
int nextdotpos = 2;
number[2] = '\0';
break;
}
- if ((isxdigit((int)*pos)) && (isxdigit((int)*(pos + 1)))) {
+ if ((isxdigit((unsigned char)*pos)) &&
+ (isxdigit((unsigned char)*(pos + 1)))) {
memcpy(number, pos, 2);
pos += 2;
} else {
/*
* conversion of XXXX.XXXX.XXXX to memory
*/
-int sysid2buff(u_char *buff, const char *dotted)
+int sysid2buff(uint8_t *buff, const char *dotted)
{
int len = 0;
const char *pos = dotted;
- u_char number[3];
+ uint8_t number[3];
number[2] = '\0';
// surely not a sysid_string if not 14 length
pos++;
continue;
}
- if ((isxdigit((int)*pos)) && (isxdigit((int)*(pos + 1)))) {
+ if ((isxdigit((unsigned char)*pos)) &&
+ (isxdigit((unsigned char)*(pos + 1)))) {
memcpy(number, pos, 2);
pos += 2;
} else {
return len;
}
+const char *nlpid2str(uint8_t nlpid)
+{
+ static char buf[4];
+ switch (nlpid) {
+ case NLPID_IP:
+ return "IPv4";
+ case NLPID_IPV6:
+ return "IPv6";
+ case NLPID_SNAP:
+ return "SNAP";
+ case NLPID_CLNP:
+ return "CLNP";
+ case NLPID_ESIS:
+ return "ES-IS";
+ default:
+ snprintf(buf, sizeof(buf), "%" PRIu8, nlpid);
+ return buf;
+ }
+}
+
/*
* converts the nlpids struct (filled by TLV #129)
* into a string
int i;
for (i = 0; i < nlpids->count; i++) {
- switch (nlpids->nlpids[i]) {
- case NLPID_IP:
- pos += sprintf(pos, "IPv4");
- break;
- case NLPID_IPV6:
- pos += sprintf(pos, "IPv6");
- break;
- case NLPID_SNAP:
- pos += sprintf(pos, "SNAP");
- break;
- case NLPID_CLNP:
- pos += sprintf(pos, "CLNP");
- break;
- case NLPID_ESIS:
- pos += sprintf(pos, "ES-IS");
- break;
- default:
- pos += sprintf(pos, "unknown");
- break;
- }
+ pos += sprintf(pos, "%s", nlpid2str(nlpids->nlpids[i]));
if (nlpids->count - i > 1)
pos += sprintf(pos, ", ");
}
return nlpidstring;
}
-/*
- * supports the given af ?
- */
-int speaks(struct nlpids *nlpids, int family)
-{
- int i, speaks = 0;
-
- if (nlpids == (struct nlpids *)NULL)
- return speaks;
- for (i = 0; i < nlpids->count; i++) {
- if (family == AF_INET && nlpids->nlpids[i] == NLPID_IP)
- speaks = 1;
- if (family == AF_INET6 && nlpids->nlpids[i] == NLPID_IPV6)
- speaks = 1;
- }
-
- return speaks;
-}
-
/*
* Returns 0 on error, IS-IS Circuit Type on ok
*/
/*
* Print functions - we print to static vars
*/
-const char *snpa_print(const u_char *from)
+const char *snpa_print(const uint8_t *from)
{
- int i = 0;
- u_char *pos = (u_char *)snpa;
-
- if (!from)
- return "unknown";
-
- while (i < ETH_ALEN - 1) {
- if (i & 1) {
- sprintf((char *)pos, "%02x.", *(from + i));
- pos += 3;
- } else {
- sprintf((char *)pos, "%02x", *(from + i));
- pos += 2;
- }
- i++;
- }
+ return isis_format_id(from, ISIS_SYS_ID_LEN);
+}
- sprintf((char *)pos, "%02x", *(from + (ISIS_SYS_ID_LEN - 1)));
- pos += 2;
- *(pos) = '\0';
+const char *sysid_print(const uint8_t *from)
+{
+ return isis_format_id(from, ISIS_SYS_ID_LEN);
+}
- return snpa;
+const char *rawlspid_print(const uint8_t *from)
+{
+ return isis_format_id(from, 8);
}
-const char *sysid_print(const u_char *from)
+#define FORMAT_ID_SIZE sizeof("0000.0000.0000.00-00")
+const char *isis_format_id(const uint8_t *id, size_t len)
{
- int i = 0;
- char *pos = sysid;
+#define FORMAT_BUF_COUNT 4
+ static char buf_ring[FORMAT_BUF_COUNT][FORMAT_ID_SIZE];
+ static size_t cur_buf = 0;
- if (!from)
- return "unknown";
+ char *rv;
- while (i < ISIS_SYS_ID_LEN - 1) {
- if (i & 1) {
- sprintf(pos, "%02x.", *(from + i));
- pos += 3;
- } else {
- sprintf(pos, "%02x", *(from + i));
- pos += 2;
- }
- i++;
- }
+ cur_buf++;
+ if (cur_buf >= FORMAT_BUF_COUNT)
+ cur_buf = 0;
- sprintf(pos, "%02x", *(from + (ISIS_SYS_ID_LEN - 1)));
- pos += 2;
- *(pos) = '\0';
+ rv = buf_ring[cur_buf];
- return sysid;
-}
+ if (!id) {
+ snprintf(rv, FORMAT_ID_SIZE, "unknown");
+ return rv;
+ }
-const char *rawlspid_print(const u_char *from)
-{
- char *pos = lspid;
- if (!from)
- return "unknown";
- memcpy(pos, sysid_print(from), 15);
- pos += 14;
- sprintf(pos, ".%02x", LSP_PSEUDO_ID(from));
- pos += 3;
- sprintf(pos, "-%02x", LSP_FRAGMENT(from));
- pos += 3;
+ if (len < 6) {
+ snprintf(rv, FORMAT_ID_SIZE, "Short ID");
+ return rv;
+ }
- *(pos) = '\0';
+ snprintf(rv, FORMAT_ID_SIZE, "%02x%02x.%02x%02x.%02x%02x", id[0], id[1],
+ id[2], id[3], id[4], id[5]);
+
+ if (len > 6)
+ snprintf(rv + 14, FORMAT_ID_SIZE - 14, ".%02x", id[6]);
+ if (len > 7)
+ snprintf(rv + 17, FORMAT_ID_SIZE - 17, "-%02x", id[7]);
- return lspid;
+ return rv;
}
-const char *time2string(u_int32_t time)
+const char *time2string(uint32_t time)
{
char *pos = datestring;
- u_int32_t rest;
+ uint32_t rest;
if (time == 0)
return "-";
return timer;
}
-struct in_addr newprefix2inaddr(u_char *prefix_start, u_char prefix_masklen)
+struct in_addr newprefix2inaddr(uint8_t *prefix_start, uint8_t prefix_masklen)
{
memset(&new_prefix, 0, sizeof(new_prefix));
memcpy(&new_prefix, prefix_start,
return new_prefix;
}
-/*
- * Returns host.name if any, otherwise
- * it returns the system hostname.
- */
-const char *unix_hostname(void)
-{
- static struct utsname names;
- const char *hostname;
-
- hostname = host.name;
- if (!hostname) {
- uname(&names);
- hostname = names.nodename;
- }
-
- return hostname;
-}
-
/*
* Returns the dynamic hostname associated with the passed system ID.
* If no dynamic hostname found then returns formatted system ID.
*/
-const char *print_sys_hostname(const u_char *sysid)
+const char *print_sys_hostname(const uint8_t *sysid)
{
struct isis_dynhn *dyn;
/* For our system ID return our host name */
if (memcmp(sysid, isis->sysid, ISIS_SYS_ID_LEN) == 0)
- return unix_hostname();
+ return cmd_hostname_get();
dyn = dynhn_find_by_id(sysid);
if (dyn)
- return (const char *)dyn->name.name;
+ return dyn->hostname;
return sysid_print(sysid);
}
zlog_debug("[%8.8s] %-50.50s %s", addrstr, hexstr, charstr);
return;
}
+
+void log_multiline(int priority, const char *prefix, const char *format, ...)
+{
+ char shortbuf[256];
+ va_list ap;
+ char *p;
+
+ va_start(ap, format);
+ p = vasnprintfrr(MTYPE_TMP, shortbuf, sizeof(shortbuf), format, ap);
+ va_end(ap);
+
+ if (!p)
+ return;
+
+ char *saveptr = NULL;
+ for (char *line = strtok_r(p, "\n", &saveptr); line;
+ line = strtok_r(NULL, "\n", &saveptr)) {
+ zlog(priority, "%s%s", prefix, line);
+ }
+
+ if (p != shortbuf)
+ XFREE(MTYPE_TMP, p);
+}
+
+void vty_multiline(struct vty *vty, const char *prefix, const char *format, ...)
+{
+ char shortbuf[256];
+ va_list ap;
+ char *p;
+
+ va_start(ap, format);
+ p = vasnprintfrr(MTYPE_TMP, shortbuf, sizeof(shortbuf), format, ap);
+ va_end(ap);
+
+ if (!p)
+ return;
+
+ char *saveptr = NULL;
+ for (char *line = strtok_r(p, "\n", &saveptr); line;
+ line = strtok_r(NULL, "\n", &saveptr)) {
+ vty_out(vty, "%s%s\n", prefix, line);
+ }
+
+ if (p != shortbuf)
+ XFREE(MTYPE_TMP, p);
+}
+
+void vty_out_timestr(struct vty *vty, time_t uptime)
+{
+ struct tm *tm;
+ time_t difftime = time(NULL);
+ difftime -= uptime;
+ tm = gmtime(&difftime);
+
+ if (difftime < ONE_DAY_SECOND)
+ vty_out(vty, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
+ tm->tm_sec);
+ else if (difftime < ONE_WEEK_SECOND)
+ vty_out(vty, "%dd%02dh%02dm", tm->tm_yday, tm->tm_hour,
+ tm->tm_min);
+ else
+ vty_out(vty, "%02dw%dd%02dh", tm->tm_yday / 7,
+ tm->tm_yday - ((tm->tm_yday / 7) * 7), tm->tm_hour);
+ vty_out(vty, " ago");
+}