#include <stdio.h>
#include "global.h"
#include "mbuf.h"
#include "proc.h"
#include "usock.h"
#include "socket.h"
#include "ax25.h"
#include "netrom.h"
#include "tcp.h"
#include "udp.h"
#include "commands.h"
#include "config.h"

/* Socket status display command */
int
dosock(argc,argv,p)
int argc;
char *argv[];
void *p;
{
	register struct usock *up;
	int s;

	if(argc < 2){
		tprintf("S# Type    PCB      Owner\n");
		for(s=0;s<Nusock;s++){
			up = &Usock[s];
			if(up->type == NOTUSED)
				continue;
			if(tprintf("%2d %-8s%-8lx %lx (%s)\n",
			 s,Socktypes[up->type],ptol(up->cb.p),
			 ptol(up->owner),up->owner->name) == EOF)
				break;
		}
		return 0;
	}
	s = atoi(argv[1]);
	if(s < 0 || s >= Nusock){
		tprintf("Number out of range\n");
		return 1;
	}
	up = &Usock[s];
	tprintf("%s %lx\n",Socktypes[up->type],ptol(up->cb.p));
	if(up->cb.p == NULL)
		return 0;
	switch(up->type){
	case TYPE_LOCAL_DGRAM:
		tprintf("Qlen: %u packets\n",socklen(s,0));
		break;
	case TYPE_LOCAL_STREAM:
		tprintf("Qlen: %u bytes\n",socklen(s,0));
		break;
	case TYPE_TCP:
		st_tcp(up->cb.tcb);
		break;
	case TYPE_UDP:
		st_udp(up->cb.udp,0);
		break;
#ifdef	AX25
	case TYPE_AX25I:
		st_ax25(up->cb.ax25);
		break;
#endif
#ifdef	NETROM
	case TYPE_NETROML4:
		donrdump(up->cb.nr4);
		break;
#endif
	}
	return 0;	
}

