Back to index

glibc  2.9
files-network.c
Go to the documentation of this file.
00001 /* Networks file parser in nss_files module.
00002    Copyright (C) 1996, 1997, 1998, 2000, 2001 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <netinet/in.h>
00021 #include <arpa/inet.h>
00022 #include <netdb.h>
00023 
00024 #define ENTNAME             netent
00025 #define DATABASE     "networks"
00026 #define NEED_H_ERRNO
00027 
00028 struct netent_data {};
00029 
00030 #define TRAILING_LIST_MEMBER              n_aliases
00031 #define TRAILING_LIST_SEPARATOR_P  isspace
00032 #include "files-parse.c"
00033 LINE_PARSER
00034 ("#",
00035  {
00036    char *addr;
00037    char *cp;
00038    int n = 1;
00039 
00040    STRING_FIELD (result->n_name, isspace, 1);
00041 
00042    STRING_FIELD (addr, isspace, 1);
00043    /* 'inet_network' does not add zeroes at the end if the network number
00044       does not four byte values.  We add them outselves if necessary.  */
00045    cp = strchr (addr, '.');
00046    if (cp != NULL)
00047      {
00048        ++n;
00049        cp = strchr (cp + 1, '.');
00050        if (cp != NULL)
00051         {
00052           ++n;
00053           cp = strchr (cp + 1, '.');
00054           if (cp != NULL)
00055             ++n;
00056         }
00057      }
00058    if (n < 4)
00059      {
00060        char *newp = (char *) alloca (strlen (addr) + (4 - n) * 2 + 1);
00061        cp = stpcpy (newp, addr);
00062        do
00063         {
00064           *cp++ = '.';
00065           *cp++ = '0';
00066         }
00067        while (++n < 4);
00068        *cp = '\0';
00069        addr = newp;
00070      }
00071    result->n_net = inet_network (addr);
00072    result->n_addrtype = AF_INET;
00073 
00074  })
00075 
00076 #include "files-XXX.c"
00077 
00078 DB_LOOKUP (netbyname, ,,
00079           LOOKUP_NAME_CASE (n_name, n_aliases),
00080           const char *name)
00081 
00082 DB_LOOKUP (netbyaddr, ,,
00083           {
00084             if (result->n_addrtype == type && result->n_net == net)
00085               /* Bingo!  */
00086               break;
00087           }, uint32_t net, int type)