home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / emos2tcp.zip / TCP.C < prev    next >
C/C++ Source or Header  |  1993-12-01  |  4KB  |  191 lines

  1. /*
  2.  * TCP/IP stream emulation for GNU Emacs.
  3.  * Copyright (C) 1988, 1989, 1992, 1993 Free Software Foundation, Inc.
  4.  
  5.  * Author: Masanobu Umeda
  6.  * Maintainer: umerin@mse.kyutech.ac.jp
  7.  
  8. This file is part of GNU Emacs.
  9.  
  10. GNU Emacs is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2, or (at your option)
  13. any later version.
  14.  
  15. GNU Emacs is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. GNU General Public License for more details.
  19.  
  20. You should have received a copy of the GNU General Public License
  21. along with GNU Emacs; see the file COPYING.     If not, write to
  22. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24.  *
  25.  * Yasunari, Itoh at PFU limited contributed for Fujitsu UTS and SX/A.
  26.  *
  27.  * Thu Apr    6 13:47:37 JST 1989
  28.  * USG fixes by Sakaeda <saka@mickey.trad.pf.fujitsu.junet>
  29.  *
  30.  * For Fujitsu UTS compile with:
  31.  *        cc -O -o tcp tcp.c -DFUJITSU_UTS -lu -lsocket
  32.  */
  33.  
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <fcntl.h>
  37. #include <ctype.h>
  38. #include <memory.h>
  39.  
  40. #include <types.h>
  41. #include <netinet/in.h>
  42. #include <sys/socket.h>
  43. #include <netdb.h>
  44. #include <netlib.h>
  45.  
  46. #define INCL_BASE
  47. #include <os2.h>
  48.  
  49. #define HF_STDIN 0
  50. #define HF_STDOUT 1
  51.  
  52.  
  53. #if defined(DEBUG)
  54. FILE *con;
  55. #endif
  56.  
  57.  
  58. /*
  59.    nntp_to_emacs - reads from nntp server and writes to stdout.
  60. */
  61.  
  62. void nntp_to_emacs(void *param)
  63. {
  64.     int nntpsock = *(int *)param;
  65.     ULONG bytesread, byteswritten;
  66.     static char buffer[2048];
  67.     APIRET rc;
  68.  
  69.     while((bytesread = recv(nntpsock, buffer, sizeof(buffer), 0)) > 0)
  70.     {
  71. #if defined(DEBUG)
  72.         buffer[bytesread] = 0;
  73.         fprintf(con, "%3d from server: %s", bytesread, buffer);
  74. #endif
  75.  
  76.         rc = fwrite(buffer, bytesread, 1, stdout);
  77.         fflush(stdout);
  78.     }
  79. }
  80.  
  81. /*
  82.    emacs_to_nntp - take emacs' data and send it to nntp
  83. */
  84.  
  85. void emacs_to_nntp(void *param)
  86. {
  87.     int nntpsock = *(int *)param;
  88.     ULONG bytesread, byteswritten;
  89.     static char buffer[1024];
  90.     APIRET rc;
  91.  
  92.     while(rc = DosRead(HF_STDIN, buffer, sizeof(buffer), &bytesread),
  93.           rc == NO_ERROR)
  94.     {
  95. #if defined(DEBUG)
  96.         buffer[bytesread] = 0;
  97.         fprintf(con, "  %3d to server: %s", bytesread, buffer);
  98. #endif
  99.         send(nntpsock, buffer, bytesread, 0);
  100.     }
  101. }
  102.  
  103. int main (int argc, char *argv[])
  104. {
  105.     struct hostent *host;
  106.     static struct sockaddr_in sockin, sockme;
  107.     struct servent *serv;
  108.     char *hostname = NULL;
  109.     char *service = "nntp";
  110.     u_short port;
  111.     int server;                    /* NNTP Server */
  112.     int false = 0;                /* FALSE flag for setsockopt () */
  113. #define BUFFSIZE 16384
  114.     char inbuff[BUFFSIZE], outbuff[BUFFSIZE];
  115.  
  116.     if (argc < 2)
  117.     {
  118.         fprintf (stderr, "Usage: %s HOST [SERVICE]\n", argv[0]);
  119.         exit (EXIT_FAILURE);
  120.     }
  121.  
  122. #if defined(DEBUG)
  123.     con = fopen("CON", "w");
  124. #endif
  125.  
  126.     if (argc >= 2)
  127.         hostname = argv[1];
  128.  
  129.     if (argc >= 3)
  130.         service = argv[2];
  131.  
  132.     if ((host = gethostbyname (hostname)) == NULL)
  133.     {
  134.         perror ("gethostbyname");
  135.         exit (EXIT_FAILURE);
  136.     }
  137.     if (isdigit (service[0]))
  138.         port = htons(atoi (service));
  139.     else
  140.     {
  141.         serv = getservbyname (service, "tcp");
  142.         if (serv == NULL)
  143.         {
  144.             perror ("getservbyname");
  145.             exit (EXIT_FAILURE);
  146.         }
  147.         port = serv->s_port;
  148.     }
  149.  
  150.     memset (&sockin, 0, sizeof (sockin));
  151.     sockin.sin_family = host->h_addrtype;
  152.     memcpy ((caddr_t)&sockin.sin_addr, host->h_addr, host->h_length);
  153.     sockin.sin_port = port;
  154.  
  155.     if ((server = socket (AF_INET, SOCK_STREAM, 0)) < 0)
  156.     {
  157.         perror ("socket");
  158.         exit (EXIT_FAILURE);
  159.     }
  160.  
  161.     if (setsockopt (server, SOL_SOCKET, SO_REUSEADDR,
  162.                     (unsigned char *)&false, sizeof (false)))
  163.     {
  164.         perror ("setsockopt");
  165.         exit (EXIT_FAILURE);
  166.     }
  167.     memset (&sockme, 0, sizeof (sockme));
  168.     sockme.sin_family = sockin.sin_family;
  169.     sockme.sin_addr.s_addr = INADDR_ANY;
  170.     if (bind (server, &sockme, sizeof (sockme)) < 0)
  171.     {
  172.         perror ("bind");
  173.         exit (EXIT_FAILURE);
  174.     }
  175.     if (connect (server, &sockin, sizeof (sockin)) < 0)
  176.     {
  177.         perror ("connect");
  178.         soclose (server);
  179.         exit (EXIT_FAILURE);
  180.     }
  181.  
  182.     _beginthread(emacs_to_nntp, 0, 16384, &server);    /* Start one function as a
  183.                                                        thread */
  184.     nntp_to_emacs(&server);        /* Run the other */
  185.  
  186.     soclose (server);
  187.  
  188.     return EXIT_SUCCESS;
  189.  
  190. }
  191.