home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / fax-3.2.1 / lib / libutil / tcp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-31  |  3.3 KB  |  137 lines

  1. /*
  2.   This file is part of the NetFax system.
  3.  
  4.   (c) Copyright 1989 by David M. Siegel and Sundar Narasimhan.
  5.       All rights reserved.
  6.  
  7.     This program is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation.
  10.  
  11.     This program is distributed in the hope that it will be useful, 
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of 
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20.   
  21. #include <stdio.h>
  22. #include <sys/types.h>
  23. #include <sys/socket.h>
  24. #include <netinet/in.h>
  25. #include <netdb.h>
  26.  
  27. #include "tcp.h"
  28.  
  29. /*
  30.   Make a socket that will listen for connection on the given
  31.   services.  Return the fd of that listener socket.
  32. */
  33. int
  34. tcp_make_listener(service)
  35. char *service;
  36. {
  37.     struct sockaddr_in sin;
  38.     int on = 1;
  39.     int port;
  40.     int s;
  41.  
  42.     /* get the port */
  43.     if ((port = service_get_portnum(service, "tcp")) < 0)
  44.       return (-1);
  45.  
  46.     /* accept connections from any address on the given port */
  47.     sin.sin_addr.s_addr = htonl(INADDR_ANY);
  48.     sin.sin_port = port;
  49.     
  50.     /* create the socket for listening */
  51.     if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  52.       return (-1);
  53.  
  54.     if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) < 0) {
  55.         close(s);
  56.         return (-1);
  57.     }
  58.     
  59.     /* bind address to the socket */
  60.     if (bind(s, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
  61.     close(s);
  62.     return (-1);
  63.     }
  64.  
  65.     /* listen for backlog of max 5 connection */
  66.     listen(s, 5);
  67.  
  68.     return (s);
  69. }
  70.  
  71. /*
  72.   When given a listener socket, accept a new client connection on
  73.   that socket.  Return the fd to the new client.  Enable sending
  74.   keepalive packets to insure that client is alive.
  75. */
  76. int
  77. tcp_accept_connection(s)
  78. int s;
  79. {
  80.     struct sockaddr_in from;
  81.     int len = sizeof (from);
  82.     int new;
  83.     int on = 1;
  84.  
  85.     /* accept a connection from the listener, and get new fd */
  86.     if ((new = accept(s, (struct sockaddr *)&from, &len)) < 0)
  87.       return (-1);
  88.  
  89.     /* enable keepalive messages */
  90.     if (setsockopt(new, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0) {
  91.     close(new);
  92.     return (-1);
  93.     }
  94.  
  95.     return (new);
  96. }
  97.  
  98. /*
  99.   Establish a stream connection to the given host with the given
  100.   services.  Return an fd to the connection.
  101. */
  102. int
  103. tcp_make_connection(host, service)
  104. char *host;
  105. char *service;
  106. {
  107.     int port;
  108.     struct hostent *hp;
  109.     struct sockaddr_in sin;
  110.     int s;
  111.  
  112.     /* get the port */
  113.     if ((port = service_get_portnum(service, "tcp")) < 0)
  114.       return (-1);
  115.  
  116.     /* find the host */
  117.     if ((hp = gethostbyname(host)) == NULL)
  118.       return (-1);
  119.  
  120.     /* set address and port for the connection */
  121.     bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
  122.     sin.sin_port = port;
  123.     sin.sin_family = hp->h_addrtype;
  124.     
  125.     /* create the socket for the connection */
  126.     if ((s = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0)
  127.       return (-1);
  128.     
  129.     /* connect to the host */
  130.     if (connect(s, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
  131.     close(s);
  132.     return (-1);
  133.     }
  134.  
  135.     return (s);
  136. }
  137.