/* AX25 mailbox interface
 * Copyright 1991 Phil Karn, KA9Q
 *
 *	May '91	Bill Simpson
 *		move to separate file for compilation & linking
 */
#include "global.h"
#include "proc.h"
#include "iface.h"
#include "pktdrvr.h"
#include "ax25.h"
#include "usock.h"
#include "socket.h"
#include "session.h"
#include "mailbox.h"
#include "ax25mail.h"

extern int Mbjumpstart;

/* Axi_sock is kept in Socket.c, so that this module won't be called */

int
ax25start(argc,argv,p)
int argc;
char *argv[];
void *p;
{
    int s,whatcall;
	struct usock *up;

	if (Axi_sock != -1)
		return 0;

	psignal(Curproc,0);	/* Don't keep the parser waiting */
	chname(Curproc,"AX25 listener");
	Axi_sock = socket(AF_AX25,SOCK_STREAM,0);
	/* bind() is done automatically */
	if(listen(Axi_sock,1) == -1){
		close_s(Axi_sock);
		return -1;
	}
	for(;;){
		if((s = accept(Axi_sock,NULLCHAR,NULLINT)) == -1)
			break;	/* Service is shutting down */

#ifdef notdef
        /* THIS is now done in LAPB.C - WG7J */
        if(availmem() < Memthresh + 5120L) {
            shutdown(s,1);
        } else {
#endif

			/* Spawn a server */
            up = itop(s);
            whatcall = up->cb.ax25->jumpstarted;
            if (whatcall & IP_LINK)		/* don't start MBOX for IP VC connect */
            	continue;
            /* If jumpstart is off, then
			 * eat the line that triggered the connection
			 * and start the mailbox
			 */
			if(!Mbjumpstart) {
				sockmode(s,SOCK_ASCII);    /* To make recvline work */
				recvline(s,NULLCHAR,80);
			} else {
				/* Check to see if jumpstart was actually used for
				 * this connection.
				 * If not, then again eat the line triggering this all
				 */
                if(!(whatcall&JUMPSTARTED)) {
					sockmode(s,SOCK_ASCII);    /* To make recvline work */
					recvline(s,NULLCHAR,80);
				}
			}
            if(newproc("mbox",2048,mbx_incom,s,(void *)whatcall,NULL,0) == NULLPROC)
		close_s(s);
#ifdef notdef
        }
#endif
    }
	return (deleteserver (&Axi_sock));
}
int
ax250(argc,argv,p)
int argc;
char *argv[];
void *p;
{
	return (deleteserver (&Axi_sock));
}

