nfs-ganesha 1.4

cidr_get.c

Go to the documentation of this file.
00001 /*
00002  * cidr_get - Get and return various semi-raw bits of info
00003  */
00004 #ifdef HAVE_CONFIG_H
00005 #include "config.h"
00006 #endif
00007 
00008 #include <errno.h>
00009 #include <stdlib.h>
00010 #include <string.h>
00011 
00012 #include "cidr.h"
00013 #include "abstract_mem.h"
00014 
00015 
00016 
00017 /* Get the prefix length */
00018 int
00019 cidr_get_pflen(const CIDR *block)
00020 {
00021         int i, j;
00022         int foundnmh;
00023         int pflen;
00024 
00025         if(block==NULL)
00026         {
00027                 errno = EFAULT;
00028                 return(-1);
00029         }
00030 
00031         /* Where do we start? */
00032         if(block->proto==CIDR_IPV4)
00033                 i=12;
00034         else if(block->proto==CIDR_IPV6)
00035                 i=0;
00036         else
00037         {
00038                 errno = ENOENT; /* Bad errno */
00039                 return(-1); /* Unknown */
00040         }
00041         
00042         /*
00043          * We're intentionally not supporting non-contiguous netmasks.  So,
00044          * if we find one, bomb out.
00045          */
00046         foundnmh=0;
00047         pflen=0;
00048         for(/* i */ ; i<=15 ; i++)
00049         {
00050                 for(j=7 ; j>=0 ; j--)
00051                 {
00052                         if((block->mask)[i] & (1<<j))
00053                         {
00054                                 /*
00055                                  * This is a network bit (1).  If we've already seen a
00056                                  * host bit (0), we need to bomb.
00057                                  */
00058                                 if(foundnmh==1)
00059                                 {
00060                                         errno = EINVAL;
00061                                         return(-1);
00062                                 }
00063 
00064                                 pflen++;
00065                         }
00066                         else
00067                                 foundnmh=1; /* A host bit */
00068                 }
00069         }
00070         
00071         /* If we get here, return the length */
00072         return(pflen);
00073 }
00074 
00075 
00076 /* Get the address bits */
00077 uint8_t *
00078 cidr_get_addr(const CIDR *addr)
00079 {
00080         uint8_t *toret;
00081 
00082         if(addr==NULL)
00083         {
00084                 errno = EFAULT;
00085                 return(NULL);
00086         }
00087 
00088         toret = gsh_calloc(16, sizeof(uint8_t));
00089         if(toret==NULL)
00090         {
00091                 errno = ENOMEM;
00092                 return(NULL);
00093         }
00094 
00095         /* Copy 'em in */
00096         memcpy(toret, addr->addr, sizeof(addr->addr));
00097 
00098         return(toret);
00099 }
00100 
00101 
00102 /* Get the netmask bits */
00103 uint8_t *
00104 cidr_get_mask(const CIDR *addr)
00105 {
00106         uint8_t *toret;
00107 
00108         if(addr==NULL)
00109         {
00110                 errno = EFAULT;
00111                 return(NULL);
00112         }
00113 
00114         toret = gsh_calloc(16, sizeof(uint8_t));
00115         if(toret==NULL)
00116         {
00117                 errno = ENOMEM;
00118                 return(NULL);
00119         }
00120 
00121         /* Copy 'em in */
00122         memcpy(toret, addr->mask, sizeof(addr->mask));
00123 
00124         return(toret);
00125 }
00126 
00127 
00128 /* Get the protocol */
00129 int
00130 cidr_get_proto(const CIDR *addr)
00131 {
00132 
00133         if(addr==NULL)
00134         {
00135                 errno = EFAULT;
00136                 return(-1);
00137         }
00138 
00139         return(addr->proto);
00140 }