home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
MISC
/
mtp.shar
/
mtpd.c
< prev
next >
Wrap
C/C++ Source or Header
|
2009-11-06
|
3KB
|
95 lines
/* mtpd.c, Version 1.1, Created 2/9/90 */
/* Dr Alan M. McIvor, BP Sunbury Research Centre */
/* Daemon for handling mtp connections */
#include <stdio.h>
#include <types.h>
#include <socket.h>
#include <in.h>
#include <netdb.h>
#include <strings.h>
#include <errno.h>
#include "mtp.h"
main(argc, argv)
int argc;
char **argv;
{
int s; /* socket */
struct protoent *tcp; /* tcp protocol structure */
struct sockaddr_in sname; /* socket name */
struct sockaddr_in from; /* name of skt connecting to this one */
int fromlen;
int ns; /* socket connecting to this one */
void fork_handler();
if ((tcp = getprotobyname("tcp")) == (struct protoent *)0)
exit(_errmsg(errno, "tcp protocol unknown\n"));
if ((s = socket(AF_INET, SOCK_STREAM, tcp->p_proto)) == -1)
exit(_errmsg(errno, "Could not get socket\n"));
memset((char *)&sname, 0, sizeof(sname)); /* zero sname */
sname.sin_family = AF_INET;
sname.sin_addr.s_addr = INADDR_ANY; /* wild INET card address */
sname.sin_port = MTP_PORT;
if (bind(s, (struct sockaddr *)&sname, sizeof(sname)) == -1)
exit(_errmsg(errno, "Could not bind name\n"));
if (listen(s, 5) == -1)
exit(_errmsg(errno, "Could not listen\n"));
while (FOREVER)
{
/* printf("**mtpd - accepting ...\n");*/
fromlen = sizeof(from);
if ((ns = accept(s, (struct sockaddr *)&from, &fromlen)) == -1)
exit(_errmsg(errno, "Accept error %d\n", errno));
/* printf("**mtpd - accepted.\n");*/
/*
printf("**mtpd - About to fork %s with s=%d, ns=%d\n",
CONNECTION_HANDLER, s, ns);
*/
/* fork child process to talk to socket */
fork_handler(s, ns);
/* printf("**mtpd - connection handler forked.\n");*/
if (close(ns) == -1)
exit(_errmsg(errno, "Close error\n"));
}
/* shouldn't ever get here */
if (shutdown(s, 2) == -1)
exit(_errmsg(errno, "Could not shutdown\n"));
if (close(s) == -1)
exit(_errmsg(errno, "Close error\n"));
exit(0);
}
void fork_handler(s, ns)
int s, ns; /* socket descriptors */
{
extern int os9forkc();
extern char **environ;
char *argblk[4]; /* arguments to pass to mtpdc */
argblk[0] = (char *)malloc(32);
strcpy(argblk[0], CONNECTION_HANDLER);
argblk[1] = (char *)malloc(32);
sprintf(argblk[1], "%d", s);
argblk[2] = (char *)malloc(32);
sprintf(argblk[2], "%d", ns);
argblk[3] = (char *)NULL;
if (os9exec(os9forkc, argblk[0], argblk, environ, 0, 0, 5) <= 0)
exit(_errmsg(errno, "Can't fork %s\n", CONNECTION_HANDLER));
return;
}