"Fossies" - the Fresh Open Source Software Archive

Member "portfwd-0.29/src/solve.cc" (6 May 2002, 3421 Bytes) of package /linux/privat/old/portfwd-0.29.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2   solve.cc
    3 
    4   $Id: solve.cc,v 1.3 2002/05/06 03:02:40 evertonm Exp $
    5  */
    6 
    7 #include <stdlib.h>
    8 #include <ctype.h>
    9 #include <netdb.h>
   10 #include <sys/types.h>
   11 #include <netinet/in.h>
   12 #include <syslog.h>
   13 #include <string.h>
   14 
   15 #include "util.h"
   16 #include "solve.h"
   17 
   18 
   19 const int FIRST_PORT = 0;
   20 const int LAST_PORT  = 65535;
   21 
   22 const char *protoname_tab[] = 
   23 {
   24   "tcp",
   25   "udp",
   26   0
   27 };
   28 
   29 int protonumber_tab[] =
   30 {
   31   -1,
   32   -1
   33 };
   34 
   35 const char *get_protoname(proto_t proto)
   36 {
   37   return protoname_tab[proto];
   38 }
   39 
   40 void solve_protonumbers()
   41 {
   42   struct protoent *pe;
   43 
   44   for (int i = 0; protoname_tab[i]; ++i) {
   45     const char *protoname = get_protoname((proto_t) i);
   46     pe = getprotobyname(protoname);
   47     if (!pe) {
   48       syslog(LOG_ERR, "Failure solving protocol name: %s\n", protoname);
   49       exit(1);
   50     }
   51     protonumber_tab[i] = pe->p_proto;
   52   }
   53 }
   54 
   55 int get_protonumber(proto_t proto)
   56 {
   57   return protonumber_tab[proto];
   58 }
   59 
   60 /*
   61  * Return -1 on error.
   62  */
   63 int solve_portnumber(const char *portname, const char *protoname) {
   64 
   65   if (isdigit(*portname))
   66     return atoi(portname);
   67 
   68   struct servent *se;
   69   se = getservbyname(portname, protoname);
   70   if (!se)
   71     return -1;
   72 
   73   return ntohs(se->s_port);
   74 }
   75 
   76 int solve_port(const char *portname, const char *protoname) {
   77 
   78   int portnumber = solve_portnumber(portname, protoname);
   79   if (portnumber == -1) {
   80     syslog(LOG_ERR, "Failure solving port name: %s/%s\n", portname, protoname);
   81     exit(1);
   82   }
   83 
   84   return portnumber;
   85 }
   86 
   87 int solve_hostname_addr(char *buf, size_t *buf_len, const char *hostname)
   88 {
   89   /*
   90    * Find address
   91    */
   92   struct hostent *he;
   93   he = gethostbyname(hostname);
   94   if (!he)
   95     return 1; /* Can't solve hostname */
   96 
   97   int blen = *buf_len; /* save buffer length */
   98 
   99   int addr_len = he->h_length; 
  100 
  101   *buf_len = addr_len; /* return address length */
  102 
  103   if (addr_len > blen)
  104     return 2; /* Insufficient buffer size */
  105 
  106   /*
  107    * Return address
  108    */
  109   memcpy(buf, *he->h_addr_list, addr_len);
  110 
  111   return 0;
  112 }
  113 
  114 struct ip_addr solve_hostname(const char *hostname) {
  115   const int BUF_SZ = 32;
  116   char buf[BUF_SZ];
  117   size_t buf_len = BUF_SZ;
  118   
  119   int result = solve_hostname_addr(buf, &buf_len, hostname);
  120   if (result) {
  121     switch (result) {
  122     case 1:
  123       syslog(LOG_ERR, "solve_hostname(%s): Can't solve hostnames\n", hostname);
  124       break;
  125     case 2:
  126       syslog(LOG_ERR, "solve_hostname(%s): Insufficient buffer space for hostname address (buf_len=%d, addr_len=%d)\n", hostname, BUF_SZ, buf_len);
  127       break;
  128     default:
  129       syslog(LOG_ERR, "solve_hostname(%s): Unexpected error: %d", hostname, result);
  130     }
  131     exit(1);
  132   }
  133   
  134   struct ip_addr ip;
  135   ip.len = buf_len;
  136   ip.addr = (char *) malloc(ip.len);
  137   if (!ip.addr) {
  138     syslog(LOG_ERR, "Can't allocate memory for address: %d bytes\n", ip.len);
  139     exit(1);
  140   }
  141 
  142   memcpy(ip.addr, buf, ip.len);
  143 
  144   return ip;
  145 }
  146 
  147 uid_t solve_user(const char *username)
  148 {
  149   struct passwd *pwd;
  150 
  151   if (isdigit(*username))
  152     pwd = getpwuid(atoi(username));
  153   else
  154     pwd = getpwnam(username);
  155   
  156   if (!pwd) {
  157     syslog(LOG_ERR, "Can't solve user: %s: %m", username);
  158     exit(1);
  159   }
  160 
  161   return pwd->pw_uid;
  162 }
  163 
  164 gid_t solve_group(const char *groupname)
  165 {
  166   struct group *grp;
  167 
  168   if (isdigit(*groupname))
  169     grp = getgrgid(atoi(groupname));
  170   else
  171     grp = getgrnam(groupname);
  172   
  173   if (!grp) {
  174     syslog(LOG_ERR, "Can't solve group: %s: %m", groupname);
  175     exit(1);
  176   }
  177 
  178   return grp->gr_gid;
  179 }
  180 
  181 /* Eof: solve.cc */