home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / netpipes / part01 / portname.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-07  |  2.9 KB  |  135 lines

  1. /*
  2.  
  3.     portname.c, part of
  4.     faucet and hose: network pipe utilities
  5.     Copyright (C) 1992 Robert Forsman
  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; either version 2 of the License, or
  10.     (at your option) any later version.
  11.  
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21.     */
  22.  
  23. #include    <stdio.h>
  24. #include    <fcntl.h>
  25. #include    <errno.h>
  26. #include    <sys/socket.h>
  27. #include    <sys/un.h>
  28. #include    <netdb.h>
  29. #include    <netinet/in.h>
  30.  
  31. int name_to_inet_port(portname)
  32. char *portname;
  33. /* This procedure converts a character string to a port number.  It looks
  34.    up the service by name and if there is none, then it converts the string
  35.    to a number with sscanf */
  36. {
  37.   struct servent    *p;
  38.  
  39.   if (portname==NULL)
  40.     return 0;
  41.  
  42.   p = getservbyname(portname,"tcp");
  43.   if (p!=NULL)
  44.     {
  45.       return p->s_port;
  46.     }
  47.   else
  48.     {
  49.       int    port;
  50.       if (sscanf(portname,"%i",&port)!=1)
  51.     {
  52.       return 0;
  53.     }
  54.       else
  55.     return htons(port);
  56.     }
  57. }
  58.  
  59. int
  60. convert_hostname(name, addr)
  61.      char    *name;
  62.      struct in_addr    *addr;
  63. {
  64.   struct hostent    *hp;
  65.   int        len;
  66.  
  67.   hp = gethostbyname(name);
  68.   if (hp != NULL)
  69.     bcopy(hp->h_addr,addr,hp->h_length);
  70.   else
  71.     {
  72.       int    count;
  73.       unsigned int    a1,a2,a3,a4;
  74.       count = sscanf(name,"%i.%i.%i.%i%n", &a1, &a2, &a3, &a4, &len);
  75.       if (4!=count || 0!=name[len] )
  76.     return 0;
  77.       addr->S_un.S_un_b.s_b1 = a1;
  78.       addr->S_un.S_un_b.s_b2 = a2;
  79.       addr->S_un.S_un_b.s_b3 = a3;
  80.       addr->S_un.S_un_b.s_b4 = a4;
  81.     }
  82.   return 1;
  83. }
  84.  
  85.  
  86. int
  87. bindlocal(fd, name, domain)
  88.      int    fd, domain;
  89.      char    *name;
  90. {
  91.   struct sockaddr    laddr;
  92.   int    countdown;
  93.   int    rval;
  94.   
  95.   if (domain==AF_INET)
  96.     {
  97.       struct sockaddr_in    *srv = (struct sockaddr_in*)&laddr;
  98.       
  99.       srv->sin_family = AF_INET;
  100.       srv->sin_addr.s_addr = INADDR_ANY;
  101.       
  102.       srv->sin_port = name_to_inet_port(name);
  103.       
  104.       if (srv->sin_port==0)
  105.     {
  106.       fprintf(stderr, "port %s unknown\n", name);
  107.       return 0;
  108.     }
  109.     }
  110.   else
  111.     {
  112.       struct sockaddr_un    *srv = (struct sockaddr_un *)&laddr;
  113.       
  114.       srv->sun_family = AF_UNIX;
  115.       strcpy(srv->sun_path, name);
  116.     }
  117.   
  118.   countdown= (domain==AF_UNIX)?1:10;
  119.   do {
  120.     rval = bind(fd, &laddr, sizeof(laddr));
  121.     if (rval)
  122.       if (errno==EADDRINUSE && --countdown>0)
  123.     {
  124.       fprintf(stderr,"Address %s in use, sleeping 10.\n",
  125.           name);
  126.       sleep (10);
  127.       fprintf(stderr,"Trying again . . .\n");
  128.     }
  129.       else
  130.     return 0;
  131.   } while (rval);
  132.  
  133.   return 1;
  134. }
  135.