• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

IpMapConf.cc

Go to the documentation of this file.
00001 /** @file
00002 
00003   Loading @c IpMap from a configuration file.
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
00022  */
00023 
00024 // Copied from IPRange.cc for backwards compatibility.
00025 
00026 # include <ts/IpMap.h>
00027 # include <ts/IpMapConf.h>
00028 # include <ts/ink_memory.h>
00029 
00030 static size_t const ERR_STRING_LEN = 256;
00031 static size_t const MAX_LINE_SIZE = 2048;
00032 
00033 // Returns 0 if successful, 1 if failed
00034 // line  Input text (source line).
00035 // n     Amount of data in @a line.
00036 // i     [in,out] Offset in line.
00037 // addr  [out] Destination for address.
00038 // err   Buffer for error string (must be ERR_STRING_LEN big).
00039 int
00040 read_addr(char *line, int n, int *i, sockaddr* addr, char* err)
00041 {
00042   int k;
00043   char dst[INET6_ADDRSTRLEN];
00044   char* src = line + *i;
00045   bool bracketed_p = false;
00046 
00047   // Allow enclosing brackets to be more consistent but
00048   // don't bother passing it to @c ntop.
00049   if ((*i < n) && ('[' == *src)) {
00050     ++*i, ++src, bracketed_p = true;
00051   }
00052 
00053   for (k = 0; k < INET6_ADDRSTRLEN && *i < n && (isxdigit(*src) || '.' == *src || ':' == *src) ; ++k, ++*i, ++src) {
00054     dst[k] = *src;
00055   }
00056 
00057   if (bracketed_p && (! (*i < n) || (']' != *src))) {
00058     snprintf(err, ERR_STRING_LEN, "Unclosed brackets");
00059     return EINVAL;
00060   }
00061 
00062   if (k == sizeof(dst)) {
00063     snprintf(err, ERR_STRING_LEN, "IP address too long");
00064     return EINVAL;
00065   }
00066 
00067   dst[k] = '\0';
00068   if (0 != ats_ip_pton(dst, addr)) {
00069     snprintf(err, ERR_STRING_LEN, "IP address '%s' improperly formatted", dst);
00070     return EINVAL;
00071   }
00072   return 0;
00073 }
00074 
00075 char *
00076 Load_IpMap_From_File(IpMap* map, int fd, const char *key_str)
00077 {
00078   char* zret = 0;
00079   FILE* f = fdopen(dup(fd), "r"); // dup so we don't close the original fd.
00080 
00081   if (f != NULL) {
00082     zret = Load_IpMap_From_File(map, f, key_str);
00083     fclose(f);
00084   } else {
00085     zret = (char *)ats_malloc(ERR_STRING_LEN);
00086     snprintf(zret, ERR_STRING_LEN, "Unable to reopen file descriptor as stream %d:%s", errno, strerror(errno));
00087   }
00088   return zret;
00089 }
00090 
00091 // Skip space in line, returning true if more data is available
00092 // (not end of line).
00093 //
00094 // line    Source line.
00095 // n       Line length.
00096 // offset  Current offset
00097 static inline bool skip_space(char* line, int n, int& offset )
00098 {
00099   while (offset < n && isspace(line[offset]))
00100     ++offset;
00101   return offset < n;
00102 }
00103 
00104 // Returns 0 if successful, error string otherwise
00105 char *
00106 Load_IpMap_From_File(IpMap* map, FILE* f, const char *key_str)
00107 {
00108   int i, n, line_no;
00109   int key_len = strlen(key_str);
00110   IpEndpoint laddr, raddr;
00111   char line[MAX_LINE_SIZE];
00112   char err_buff[ERR_STRING_LEN];
00113 
00114   // First hardcode 127.0.0.1 into the table
00115   map->mark(INADDR_LOOPBACK);
00116 
00117   line_no = 0;
00118   while (fgets(line, MAX_LINE_SIZE, f)) {
00119     ++line_no;
00120     n = strlen(line);
00121     // Find first white space which terminates the line key.
00122     for ( i = 0 ; i < n && ! isspace(line[i]); ++i)
00123       ;
00124     if (i != key_len || 0 != strncmp(line, key_str, key_len))
00125       continue;
00126     // Now look for IP address
00127     while (true) {
00128       if (!skip_space(line, n, i))
00129         break;
00130 
00131       if (0 != read_addr(line, n,  &i, &laddr.sa, err_buff)) {
00132         char *error_str = (char *)ats_malloc(ERR_STRING_LEN);
00133         snprintf(error_str, ERR_STRING_LEN, "Invalid input configuration (%s) at line %d offset %d - '%s'", err_buff,
00134                  line_no, i, line);
00135         return error_str;
00136       }
00137 
00138       if (!skip_space(line, n, i) || line[i] == ',') {
00139         // You have read an IP address. Enter it in the table
00140         map->mark(&laddr);
00141         if (i == n)
00142           break;
00143         else
00144           ++i;
00145       } else if (line[i] == '-') {
00146         // What you have just read is the start of the range,
00147         // Now, read the end of the IP range
00148         ++i;
00149         if (!skip_space(line, n, i)) {
00150           char *error_str = (char *)ats_malloc(ERR_STRING_LEN);
00151           snprintf(error_str, ERR_STRING_LEN, "Invalid input (unterminated range) at line %d offset %d - '%s'", line_no,
00152                    i, line);
00153           return error_str;
00154         } else if (0 != read_addr(line, n, &i, &raddr.sa, err_buff)) {
00155           char *error_str = (char *)ats_malloc(ERR_STRING_LEN);
00156           snprintf(error_str, ERR_STRING_LEN, "Invalid input (%s) at line %d offset %d - '%s'", err_buff, line_no, i,
00157                    line);
00158           return error_str;
00159         }
00160         map->mark(&laddr.sa, &raddr.sa);
00161         if (!skip_space(line, n, i))
00162           break;
00163         if (line[i] != ',') {
00164           char *error_str = (char *)ats_malloc(ERR_STRING_LEN);
00165           snprintf(error_str, ERR_STRING_LEN, "Invalid input (expecting comma) at line %d offset %d - '%s'", line_no,
00166                    i, line);
00167           return error_str;
00168         }
00169         ++i;
00170       } else {
00171         char *error_str = (char *)ats_malloc(ERR_STRING_LEN);
00172         snprintf(error_str, ERR_STRING_LEN, "Invalid input (expecting dash or comma) at line %d offset %d", line_no, i);
00173         return error_str;
00174       }
00175     }
00176   }
00177   return 0;
00178 }

Generated by  doxygen 1.7.1