home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 5 Edit
/
05-Edit.zip
/
emos2tcp.zip
/
TCP.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-12-01
|
4KB
|
191 lines
/*
* TCP/IP stream emulation for GNU Emacs.
* Copyright (C) 1988, 1989, 1992, 1993 Free Software Foundation, Inc.
* Author: Masanobu Umeda
* Maintainer: umerin@mse.kyutech.ac.jp
This file is part of GNU Emacs.
GNU Emacs is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Yasunari, Itoh at PFU limited contributed for Fujitsu UTS and SX/A.
*
* Thu Apr 6 13:47:37 JST 1989
* USG fixes by Sakaeda <saka@mickey.trad.pf.fujitsu.junet>
*
* For Fujitsu UTS compile with:
* cc -O -o tcp tcp.c -DFUJITSU_UTS -lu -lsocket
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <ctype.h>
#include <memory.h>
#include <types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netlib.h>
#define INCL_BASE
#include <os2.h>
#define HF_STDIN 0
#define HF_STDOUT 1
#if defined(DEBUG)
FILE *con;
#endif
/*
nntp_to_emacs - reads from nntp server and writes to stdout.
*/
void nntp_to_emacs(void *param)
{
int nntpsock = *(int *)param;
ULONG bytesread, byteswritten;
static char buffer[2048];
APIRET rc;
while((bytesread = recv(nntpsock, buffer, sizeof(buffer), 0)) > 0)
{
#if defined(DEBUG)
buffer[bytesread] = 0;
fprintf(con, "%3d from server: %s", bytesread, buffer);
#endif
rc = fwrite(buffer, bytesread, 1, stdout);
fflush(stdout);
}
}
/*
emacs_to_nntp - take emacs' data and send it to nntp
*/
void emacs_to_nntp(void *param)
{
int nntpsock = *(int *)param;
ULONG bytesread, byteswritten;
static char buffer[1024];
APIRET rc;
while(rc = DosRead(HF_STDIN, buffer, sizeof(buffer), &bytesread),
rc == NO_ERROR)
{
#if defined(DEBUG)
buffer[bytesread] = 0;
fprintf(con, " %3d to server: %s", bytesread, buffer);
#endif
send(nntpsock, buffer, bytesread, 0);
}
}
int main (int argc, char *argv[])
{
struct hostent *host;
static struct sockaddr_in sockin, sockme;
struct servent *serv;
char *hostname = NULL;
char *service = "nntp";
u_short port;
int server; /* NNTP Server */
int false = 0; /* FALSE flag for setsockopt () */
#define BUFFSIZE 16384
char inbuff[BUFFSIZE], outbuff[BUFFSIZE];
if (argc < 2)
{
fprintf (stderr, "Usage: %s HOST [SERVICE]\n", argv[0]);
exit (EXIT_FAILURE);
}
#if defined(DEBUG)
con = fopen("CON", "w");
#endif
if (argc >= 2)
hostname = argv[1];
if (argc >= 3)
service = argv[2];
if ((host = gethostbyname (hostname)) == NULL)
{
perror ("gethostbyname");
exit (EXIT_FAILURE);
}
if (isdigit (service[0]))
port = htons(atoi (service));
else
{
serv = getservbyname (service, "tcp");
if (serv == NULL)
{
perror ("getservbyname");
exit (EXIT_FAILURE);
}
port = serv->s_port;
}
memset (&sockin, 0, sizeof (sockin));
sockin.sin_family = host->h_addrtype;
memcpy ((caddr_t)&sockin.sin_addr, host->h_addr, host->h_length);
sockin.sin_port = port;
if ((server = socket (AF_INET, SOCK_STREAM, 0)) < 0)
{
perror ("socket");
exit (EXIT_FAILURE);
}
if (setsockopt (server, SOL_SOCKET, SO_REUSEADDR,
(unsigned char *)&false, sizeof (false)))
{
perror ("setsockopt");
exit (EXIT_FAILURE);
}
memset (&sockme, 0, sizeof (sockme));
sockme.sin_family = sockin.sin_family;
sockme.sin_addr.s_addr = INADDR_ANY;
if (bind (server, &sockme, sizeof (sockme)) < 0)
{
perror ("bind");
exit (EXIT_FAILURE);
}
if (connect (server, &sockin, sizeof (sockin)) < 0)
{
perror ("connect");
soclose (server);
exit (EXIT_FAILURE);
}
_beginthread(emacs_to_nntp, 0, 16384, &server); /* Start one function as a
thread */
nntp_to_emacs(&server); /* Run the other */
soclose (server);
return EXIT_SUCCESS;
}