Back to index

glibc  2.9
nis_getservlist.c
Go to the documentation of this file.
00001 /* Copyright (c) 1997, 1998, 1999, 2000, 2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
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 <string.h>
00021 #include <rpcsvc/nis.h>
00022 
00023 #include "nis_xdr.h"
00024 #include "nis_intern.h"
00025 
00026 nis_server **
00027 nis_getservlist (const_nis_name dir)
00028 {
00029   nis_result *res;
00030   nis_server **serv;
00031 
00032   res = nis_lookup (dir, FOLLOW_LINKS);
00033 
00034   if (res != NULL && NIS_RES_STATUS (res) == NIS_SUCCESS)
00035     {
00036       unsigned long i;
00037       nis_server *server;
00038 
00039       serv =
00040        malloc (sizeof (nis_server *) *
00041               (NIS_RES_OBJECT (res)->DI_data.do_servers.do_servers_len + 1));
00042       if (__builtin_expect (serv == NULL, 0))
00043        {
00044          nis_freeresult (res);
00045          return NULL;
00046        }
00047 
00048       for (i = 0; i < NIS_RES_OBJECT (res)->DI_data.do_servers.do_servers_len;
00049           ++i)
00050        {
00051          server =
00052            &NIS_RES_OBJECT (res)->DI_data.do_servers.do_servers_val[i];
00053          serv[i] = calloc (1, sizeof (nis_server));
00054          if (__builtin_expect (serv[i] == NULL, 0))
00055            {
00056            free_all:
00057              while (i-- > 0)
00058               {
00059                 free (serv[i]->pkey.n_bytes);
00060                 if (serv[i]->ep.ep_val != NULL)
00061                   {
00062                     unsigned long int j;
00063                     for (j = 0; j < serv[i]->ep.ep_len; ++j)
00064                      {
00065                        free (serv[i]->ep.ep_val[j].proto);
00066                        free (serv[i]->ep.ep_val[j].family);
00067                        free (serv[i]->ep.ep_val[j].uaddr);
00068                      }
00069                     free (serv[i]->ep.ep_val);
00070                   }
00071                 free (serv[i]->name);
00072                 free (serv[i]);
00073               }
00074 
00075              free (serv);
00076 
00077              nis_freeresult (res);
00078 
00079              return NULL;
00080            }
00081 
00082          if (server->name != NULL)
00083            {
00084              serv[i]->name = strdup (server->name);
00085              if (__builtin_expect (serv[i]->name == NULL, 0))
00086               {
00087                 ++i;
00088                 goto free_all;
00089               }
00090            }
00091 
00092           serv[i]->ep.ep_len = server->ep.ep_len;
00093           if (serv[i]->ep.ep_len > 0)
00094             {
00095               unsigned long int j;
00096 
00097               serv[i]->ep.ep_val =
00098               malloc (server->ep.ep_len * sizeof (endpoint));
00099              if (__builtin_expect (serv[i]->ep.ep_val == NULL, 0))
00100               {
00101                 ++i;
00102                 goto free_all;
00103               }
00104 
00105               for (j = 0; j < serv[i]->ep.ep_len; ++j)
00106                 {
00107                   if (server->ep.ep_val[j].uaddr)
00108                     serv[i]->ep.ep_val[j].uaddr =
00109                     strdup (server->ep.ep_val[j].uaddr);
00110                   else
00111                     serv[i]->ep.ep_val[j].uaddr = NULL;
00112                   if (server->ep.ep_val[j].family)
00113                   serv[i]->ep.ep_val[j].family =
00114                     strdup (server->ep.ep_val[j].family);
00115                   else
00116                     serv[i]->ep.ep_val[j].family = NULL;
00117                   if (server->ep.ep_val[j].proto)
00118                   serv[i]->ep.ep_val[j].proto =
00119                     strdup (server->ep.ep_val[j].proto);
00120                   else
00121                   serv[i]->ep.ep_val[j].proto = NULL;
00122                 }
00123             }
00124 
00125           serv[i]->key_type = server->key_type;
00126           serv[i]->pkey.n_len = server->pkey.n_len;
00127           if (server->pkey.n_len > 0)
00128             {
00129               serv[i]->pkey.n_bytes = malloc (server->pkey.n_len);
00130               if (__builtin_expect (serv[i]->pkey.n_bytes == NULL, 0))
00131               {
00132                 ++i;
00133                 goto free_all;
00134               }
00135               memcpy (serv[i]->pkey.n_bytes, server->pkey.n_bytes,
00136                       server->pkey.n_len);
00137             }
00138         }
00139       serv[i] = NULL;
00140     }
00141   else
00142     {
00143       serv = malloc (sizeof (nis_server *));
00144       if (__builtin_expect (serv != NULL, 0))
00145        serv[0] = NULL;
00146     }
00147 
00148   nis_freeresult (res);
00149 
00150   return serv;
00151 }
00152 
00153 void
00154 nis_freeservlist (nis_server **serv)
00155 {
00156   int i;
00157 
00158   if (serv == NULL)
00159     return;
00160 
00161   i = 0;
00162   while (serv[i] != NULL)
00163     {
00164       xdr_free ((xdrproc_t)_xdr_nis_server, (char *)serv[i]);
00165       free (serv[i]);
00166       ++i;
00167     }
00168   free (serv);
00169 }