~ubuntu-branches/ubuntu/wily/trafficserver/wily

« back to all changes in this revision

Viewing changes to lib/ts/MatcherUtils.cc

  • Committer: Package Import Robot
  • Author(s): Adam Conrad
  • Date: 2012-12-17 22:28:16 UTC
  • mfrom: (5.1.8 raring-proposed)
  • Revision ID: package-import@ubuntu.com-20121217222816-7xwjsx5k76zkb63d
Tags: 3.2.0-1ubuntu1
* Revert FreeBSD strerror_r() fixes that give errors with glibc 2.16.
* Apply patch from Konstantinos Margaritis to define barriers on ARM.

Show diffs side-by-side

added added

removed removed

Lines of Context:
73
73
  // Allocate a buffer large enough to hold the entire file
74
74
  //   File size should be small and this makes it easy to
75
75
  //   do two passes on the file
76
 
  if ((file_buf = (char *) xmalloc((file_info.st_size + 1))) != NULL) {
77
 
    // Null terminate the buffer so that string operations will work
78
 
    file_buf[file_info.st_size] = '\0';
79
 
 
80
 
    read_size = (file_info.st_size > 0) ? read(fd, file_buf, file_info.st_size) : 0;
81
 
 
82
 
    // Check to make sure that we got the whole file
83
 
    if (read_size < 0) {
84
 
      Error("%s Read of %s file failed : %s", module_name, file_path, strerror(errno));
85
 
      xfree(file_buf);
86
 
      file_buf = NULL;
87
 
    } else if (read_size < file_info.st_size) {
88
 
      // We don't want to signal this error on WIN32 because the sizes
89
 
      // won't match if the file contains any CR/LF sequence.
90
 
      Error("%s Only able to read %d bytes out %d for %s file",
91
 
            module_name, read_size, (int) file_info.st_size, file_path);
92
 
      file_buf[read_size] = '\0';
93
 
    }
94
 
  } else {
95
 
    Error("%s Insufficient memory to read %s file", module_name, file_path);
 
76
  file_buf = (char *)ats_malloc(file_info.st_size + 1);
 
77
  // Null terminate the buffer so that string operations will work
 
78
  file_buf[file_info.st_size] = '\0';
 
79
 
 
80
  read_size = (file_info.st_size > 0) ? read(fd, file_buf, file_info.st_size) : 0;
 
81
 
 
82
  // Check to make sure that we got the whole file
 
83
  if (read_size < 0) {
 
84
    Error("%s Read of %s file failed : %s", module_name, file_path, strerror(errno));
 
85
    ats_free(file_buf);
 
86
    file_buf = NULL;
 
87
  } else if (read_size < file_info.st_size) {
 
88
    // We don't want to signal this error on WIN32 because the sizes
 
89
    // won't match if the file contains any CR/LF sequence.
 
90
    Error("%s Only able to read %d bytes out %d for %s file",
 
91
          module_name, read_size, (int) file_info.st_size, file_path);
 
92
    file_buf[read_size] = '\0';
96
93
  }
97
94
 
98
95
  if (file_buf && read_size_ptr) {
139
136
  return (write - buffer);
140
137
}
141
138
 
142
 
//   char* ExtractIpRange(char* match_str, ip_addr_t* addr1,
143
 
//                         ip_addr_t* addr2)
 
139
char const*
 
140
ExtractIpRange(char* match_str, in_addr_t* min, in_addr_t* max) {
 
141
  IpEndpoint ip_min, ip_max;
 
142
  char const* zret = ExtractIpRange(match_str, &ip_min.sa, &ip_max.sa);
 
143
  if (0 == zret) { // success
 
144
    if (ats_is_ip4(&ip_min) && ats_is_ip4(&ip_max)) {
 
145
      if (min) *min = ntohl(ats_ip4_addr_cast(&ip_min));
 
146
      if (max) *max = ntohl(ats_ip4_addr_cast(&ip_max));
 
147
    } else {
 
148
      zret = "The addresses were not IPv4 addresses.";
 
149
    }
 
150
  }
 
151
  return zret;
 
152
}
 
153
 
 
154
//   char* ExtractIpRange(char* match_str, sockaddr* addr1,
 
155
//                         sockaddr* addr2)
144
156
//
145
157
//   Attempts to extract either an Ip Address or an IP Range
146
158
//     from match_str.  The range should be two addresses
154
166
//     that describes the reason for the error.
155
167
//
156
168
const char *
157
 
ExtractIpRange(char *match_str, ip_addr_t *addr1, ip_addr_t *addr2)
 
169
ExtractIpRange(char *match_str, sockaddr* addr1, sockaddr* addr2)
158
170
{
159
171
  Tokenizer rangeTok("-/");
160
 
  bool mask = false;
 
172
  bool mask = strchr(match_str, '/') != NULL;
161
173
  int mask_bits;
162
174
  int mask_val;
163
175
  int numToks;
164
 
  ip_addr_t addr1_local;
165
 
  ip_addr_t addr2_local;
 
176
  IpEndpoint la1, la2;
166
177
 
167
 
  if (strchr(match_str, '/') != NULL) {
168
 
    mask = true;
169
 
  }
170
178
  // Extract the IP addresses from match data
171
179
  numToks = rangeTok.Initialize(match_str, SHARE_TOKS);
172
180
 
176
184
    return "malformed IP range";
177
185
  }
178
186
 
179
 
  addr1_local = htonl(inet_addr(rangeTok[0]));
 
187
  if (0 != ats_ip_pton(rangeTok[0], &la1.sa)) {
 
188
    return "malformed IP address";
 
189
  }
180
190
 
181
 
  if (addr1_local == (ip_addr_t) - 1 && strcmp(rangeTok[0], "255.255.255.255") != 0) {
182
 
    return "malformed ip address";
183
 
  }
184
191
  // Handle a IP range
185
192
  if (numToks == 2) {
186
193
 
187
 
    if (mask == true) {
 
194
    if (mask) {
 
195
      if (!ats_is_ip4(&la1)) {
 
196
        return "Masks supported only for IPv4";
 
197
      }
188
198
      // coverity[secure_coding]
189
199
      if (sscanf(rangeTok[1], "%d", &mask_bits) != 1) {
190
200
        return "bad mask specification";
197
207
      if (mask_bits == 32) {
198
208
        mask_val = 0;
199
209
      } else {
200
 
        mask_val = 0xffffffff >> mask_bits;
 
210
        mask_val = htonl(0xffffffff >> mask_bits);
201
211
      }
202
 
 
203
 
      addr2_local = addr1_local | mask_val;
204
 
      addr1_local = addr1_local & (mask_val ^ 0xffffffff);
 
212
      in_addr_t a = ats_ip4_addr_cast(&la1);
 
213
      ats_ip4_set(&la2, a | mask_val);
 
214
      ats_ip4_set(&la1, a & (mask_val ^ 0xffffffff));
205
215
 
206
216
    } else {
207
 
      addr2_local = htonl(inet_addr(rangeTok[1]));
208
 
      if (addr2_local == (ip_addr_t) - 1 && strcmp(rangeTok[1], "255.255.255.255") != 0) {
 
217
      if (0 != ats_ip_pton(rangeTok[1], &la2)) {
209
218
        return "malformed ip address at range end";
210
219
      }
211
220
    }
212
221
 
213
 
    if (addr1_local > addr2_local) {
 
222
    if (1 == ats_ip_addr_cmp(&la1.sa, &la2.sa)) {
214
223
      return "range start greater than range end";
215
224
    }
 
225
 
 
226
    ats_ip_copy(addr2, &la2);
216
227
  } else {
217
 
    addr2_local = addr1_local;
 
228
    ats_ip_copy(addr2, &la1);
218
229
  }
219
230
 
220
 
  *addr1 = addr1_local;
221
 
  *addr2 = addr2_local;
 
231
  ats_ip_copy(addr1, &la1);
222
232
  return NULL;
223
233
}
224
234
 
575
585
    return "Malformed entry";
576
586
  }
577
587
 
578
 
  if (p_line->type == MATCH_NONE) {
 
588
  if (!tags->empty() && p_line->type == MATCH_NONE) {
579
589
    if (tags->dest_error_msg == false) {
580
590
      return "No source specifier";
581
591
    } else {