home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 3
/
PDCD_3.iso
/
internet
/
tcpipsrc
/
h
/
if
/
MiscServ
/
c
/
SMISC
< prev
Wrap
Text File
|
1994-06-24
|
3KB
|
126 lines
/* Miscellaneous servers */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "global.h"
#include "mbuf.h"
#include "netuser.h"
#include "timer.h"
#include "tcp.h"
#include "misc.h"
#include "arc.h"
static void disc_recv(struct tcb *, int16);
static void echo_recv(struct tcb *, int16);
static void echo_trans(struct tcb *, int16);
static void misc_state(struct tcb *, char, char);
static struct tcb *disc_tcb,*echo_tcb;
/* Start up discard server */
int dis1(int argc, char **argv)
{
struct socket lsocket;
lsocket.address = ip_addr;
if(argc < 2)
lsocket.port = DISCARD_PORT;
else
lsocket.port = atoi(argv[1]);
disc_tcb = open_tcp(&lsocket,NULLSOCK,TCP_SERVER,0,(void(*)())disc_recv,NULLVFP,(void(*)())misc_state,0,(char *)NULL);
return(0);
}
/* Start echo server */
int echo1(int argc, char **argv)
{
struct socket lsocket;
lsocket.address = ip_addr;
if(argc < 2)
lsocket.port = ECHO_PORT;
else
lsocket.port = atoi(argv[1]);
echo_tcb = open_tcp(&lsocket,NULLSOCK,TCP_SERVER,0,(void(*)())echo_recv,(void(*)())echo_trans,(void(*)())misc_state,0,(char *)NULL);
return(0);
}
/* Shut down miscellaneous servers */
int dis0(void)
{
if(disc_tcb != NULLTCB)
close_tcp(disc_tcb);
return(0);
}
int echo0(void)
{
if(echo_tcb != NULLTCB)
close_tcp(echo_tcb);
return(0);
}
/* Discard server receiver upcall */
static void disc_recv(struct tcb *tcb, int16 cnt)
{
struct mbuf *bp;
if(recv_tcp(tcb,&bp,cnt) > 0)
free_p(bp); /* Discard */
}
/* Echo server receive
* Copies only as much will fit on the transmit queue
*/
static void echo_recv(struct tcb *tcb, int16 cnt)
{
struct mbuf *bp;
int acnt;
if(cnt == 0){
close_tcp(tcb);
return;
}
acnt = min(cnt,tcb->snd.wnd);
if(acnt > 0){
/* Get only as much will fit in the send window */
recv_tcp(tcb,&bp,tcb->snd.wnd);
send_tcp(tcb,bp);
}
}
/* Echo server transmit
* Copies anything that might have been left in the receiver queue
*/
static void echo_trans(struct tcb *tcb, int16 cnt)
{
struct mbuf *bp;
if(tcb->rcvcnt > 0){
/* Get only as much will fit in the send window */
recv_tcp(tcb,&bp,cnt);
send_tcp(tcb,bp);
}
}
/* Log connection state changes; also respond to remote closes */
static void misc_state(register struct tcb *tcb, char old, char new)
{
old = old;
switch(new){
case ESTABLISHED:
log_event(tcb,"open %d",tcb->conn.local.port);
break;
case CLOSE_WAIT:
close_tcp(tcb);
break;
case CLOSED:
log_event(tcb,"close %d",tcb->conn.local.port);
del_tcp(tcb);
/* Clean up if server is being shut down */
if(tcb == disc_tcb)
disc_tcb = NULLTCB;
else if(tcb == echo_tcb)
echo_tcb = NULLTCB;
break;
}
}