/* RIP packet tracing
 * Copyright 1991 Phil Karn, KA9Q
 *
 *  Changes Copyright (c) 1993 Jeff White - N0POY, All Rights Reserved.
 *  Permission granted for non-commercial copying and use, provided
 *  this notice is retained.
 *
 * Rehack for RIP-2 (RFC1388) by N0POY 4/1993
 *
 * Beta release 11/10/93 V0.91
 *
 * 2/19/94 release V1.0
 *
 */
#include "global.h"
#ifdef RIP
#include "mbuf.h"
#include "netuser.h"
#include "timer.h"
#include "rip.h"
#include "trace.h"
  
// from trace.c
void fmtline __ARGS((FILE *fp,int16 addr,char *buf,int16 len));
  
void
rip_dump(fp,bpp)
FILE *fp;
struct mbuf **bpp;
{
    struct rip_route entry;
    struct rip_authenticate *ripauth;
    int i;
    int cmd,version;
    int16 len;
    int16 domain;
    char ipaddmask[25];
  
    fprintf(fp,"RIP: ");
    cmd = PULLCHAR(bpp);
    version = PULLCHAR(bpp);
    switch(cmd){
        case RIPCMD_REQUEST:
            fprintf(fp,"REQUEST");
            break;
        case RIPCMD_RESPONSE:
            fprintf(fp,"RESPONSE");
            break;
        default:
            fprintf(fp," cmd %u",cmd);
            break;
    }
  
    domain = pull16(bpp);
  
    len = len_p(*bpp);
    fprintf(fp," vers %u entries %u domain %u:\n",version,len / RIP_ENTRY, domain);
  
    i = 0;
    while(len >= RIP_ENTRY){
        /* Pull an entry off the packet */
        pullentry(&entry,bpp);
        len -= RIP_ENTRY;
  
        if (entry.rip_family == RIP_AF_AUTH) {
            ripauth = (struct rip_authenticate *)&entry;
            fprintf(fp,"RIP: AUTHENTICATION Type %d  Password: %.16s\n",
            ripauth->rip_auth_type,ripauth->rip_auth_str);
            continue;
        }
  
        if(entry.rip_family != RIP_AF_INET) {
            /* Skip non-IP addresses */
            continue;
        }
  
        if (version >= RIP_VERSION_2) {
            sprintf(ipaddmask, "%s/%-4d", inet_ntoa(entry.rip_dest),
            mask2width(entry.rip_dest_mask));
        } else {
            sprintf(ipaddmask, "%s/??", inet_ntoa(entry.rip_dest));
        }
        fprintf(fp,"%-20s%-3u",ipaddmask, entry.rip_metric);
  
        if((++i % 3) == 0){
            putc('\n',fp);
        }
    }
    if((i % 3) != 0)
        putc('\n',fp);
}
  
#endif /* RIP */
  
