home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
unix
/
volume26
/
port-lpr
/
part01
/
dnet-lpd-gw.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-04-09
|
6KB
|
268 lines
/*
* line printer daemon gateway for DECnet
*
* Listens on a DECnet port and copies packets to/from the local
* Berkeley line printer daemon.
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <netdb.h>
#include <ctype.h>
#include <errno.h>
#define MAIN_PROGRAM
#include "common.h"
char *DECnet_remoteNode;
char *DECnet_remoteUser;
void
dump_buf (fp, prefix, buf, size)
unsigned char *prefix, *buf;
int size;
FILE *fp;
{
int i;
fprintf (fp, "%s ", prefix);
for (i = 0; i < size; ++i)
if (buf[i] >= ' ' && buf[i] <= '~')
putc (buf[i], fp);
else if (buf[i] == '\n')
fprintf (fp, "\\n");
else
fprintf (fp, "\\%03o", buf[i] & 0377);
putc ('\n', fp);
fflush (fp);
}
char *
strsave (s)
char *s;
{
char *malloc ();
char *p;
if (s == NULL)
return NULL;
if ((p = malloc (strlen (s) + 1)) == NULL)
return NULL;
strcpy (p, s);
return p;
}
#ifdef sun
/*
* accept code for SunLink DNI
*/
#include <sys/ioctl.h>
#include <netdni/dni.h>
int
accept_decnet_connection ()
{
OpenBlock open_block;
SessionData session_data;
int dnet_fd;
dnet_fd = 3;
if (ioctl (dnet_fd, SES_GET_AI, &open_block) < 0) {
fprintf (stderr, "dnet-lpd: can't get access control information\n");
fflush (stderr);
return EOF;
}
DECnet_remoteNode = strsave (open_block.op_node_name);
DECnet_remoteUser = strsave (open_block.op_userid);
if (ioctl (dnet_fd, SES_ACCEPT, &session_data) < 0) {
fprintf (stderr, "dnet-lpd: can't accept inbound connection\n");
fflush (stderr);
return EOF;
}
return dnet_fd;
}
#endif
#ifdef ultrix
/*
* accept code for DECnet-Ultrix (untested)
*/
#include <netdnet/dn.h>
int
accept_decnet_connection ()
{
struct accessdata_dn acc_data;
int dnet_fd = 0;
setsockopt (dnet_fd, DNPROTO_NSP, DSO_CONACCESS, &acc_data,
sizeof (acc_data));
DECnet_remoteNode = strsave (getenv ("REMNODE"));
DECnet_remoteUser = strsave (getenv ("REMUSER"));
setsockopt (dnet_fd, DNPROTO_NSP, DSO_CONACCEPT, 0, 0);
return dnet_fd;
}
#endif
int
x_read (fd, buf, size)
int fd; char *buf; unsigned size;
{
int nread = read (fd, (char *) buf, size);
char prefix[10];
sprintf (prefix, "<%d<", fd);
if (debug)
dump_buf (stderr, prefix, buf, nread);
return nread;
}
int
x_write (fd, buf, size)
int fd; char *buf; unsigned size;
{
char prefix[10];
sprintf (prefix, ">%d>", fd);
if (debug)
dump_buf (stderr, prefix, buf, size);
return write (fd, (char *) buf, size);
}
int
y_read (fd, buf, size)
int fd; char *buf; unsigned size;
{
int nread = read (fd, (char *) buf, size);
if (debug)
fprintf (stderr, "<%d< (%d bytes)\n", fd, nread);
return nread;
}
int
y_write (fd, buf, size)
int fd; char *buf; unsigned size;
{
if (debug)
fprintf (stderr, ">%d> (%d bytes)\n", fd, size);
return write (fd, (char *) buf, size);
}
do_gateway (dnet_fd, tcp_fd)
int dnet_fd, tcp_fd;
{
char buf[512];
int nread;
while ((nread = x_read (dnet_fd, buf, sizeof buf)) > 0) {
switch (*buf) {
case '\02': /* receive a printer job */
x_write (tcp_fd, buf, nread);
nread = x_read (tcp_fd, buf, sizeof buf);
if (nread > 0) {
x_write (dnet_fd, buf, nread);
receive_job (dnet_fd, tcp_fd);
}
else
exit (0);
break;
case '\01': /* print any waiting jobs */
case '\03': /* display queue state (short) */
case '\04': /* display queue state (long) */
case '\05': /* remove jobs */
default:
x_write (tcp_fd, buf, nread);
while ((nread = x_read (tcp_fd, buf, sizeof buf)) > 0)
x_write (dnet_fd, buf, nread);
exit (0);
}
}
}
receive_job (dnet_fd, tcp_fd)
int dnet_fd, tcp_fd;
{
char buf[512];
int nread;
int count;
while ((nread = x_read (dnet_fd, buf, sizeof buf)) > 0) {
switch (*buf) {
case '\01':
default:
x_write (tcp_fd, buf, nread);
nread = x_read (tcp_fd, buf, sizeof buf);
if (nread > 0)
x_write (dnet_fd, buf, nread);
else
exit (0);
case '\02': /* send control file */
case '\03': /* send data file */
count = atoi (buf + 1); /* length of control file */
/* copy command to lpd ; get response*/
x_write (tcp_fd, buf, nread);
nread = x_read (tcp_fd, buf, sizeof buf);
if (nread > 0)
x_write (dnet_fd, buf, nread);
else
exit (0);
copy_file (dnet_fd, tcp_fd, count);
}
}
}
copy_file (src, dest, size)
int src, dest, size;
{
int nread;
char buf[512];
while (size > 0) {
if ((nread = y_read (src, buf, sizeof buf)) > 0) {
y_write (dest, buf, nread);
size -= nread;
}
else
break;
}
if (size > 0) {
int x;
bzero (buf, sizeof buf);
while (size > 0) {
x = size > 512 ? 512 : size;
y_write (dest, buf, x);
size -= x;
}
}
nread = x_read (src, buf, sizeof buf); /* should be a NUL octet */
x_write (dest, buf, nread);
nread = x_read (dest, buf, sizeof buf); /* acknowlegement */
x_write (src, buf, nread);
}
int debug = 0;
main (argc, argv)
char **argv;
{
int dnet_fd, tcp_fd;
if (debug)
freopen ("/tmp/dnet-lpd-gw.log", "a", stderr);
if ((dnet_fd = accept_decnet_connection ()) < 0) {
fprintf (stderr, "DECnet accept failed, exiting\n");
exit (1);
}
if ((tcp_fd = open_lpd ("localhost")) < 0) {
fprintf (stderr, "TCP open failed, exiting\n");
exit (1);
}
do_gateway (dnet_fd, tcp_fd);
exit (0);
}