File: qemuipsanitize.c

package info (click to toggle)
cowdancer 0.73
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 752 kB
  • ctags: 269
  • sloc: ansic: 3,822; sh: 481; makefile: 191; cpp: 6
file content (74 lines) | stat: -rw-r--r-- 1,401 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*** IP address sanitization for qemu.
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <regex.h>
#include "qemuipsanitize.h"

const char* sanitize_ipaddress(const char*addr)
{
  /* return 10.0.2.2 (qemu host OS address) if localhost */
  const char* local_host="10.0.2.2";
  struct hostent* h;
  h=gethostbyname(addr);
  if(h)
    {
      if (h->h_addr[0]==127)
	{
	  return local_host;
	}
    }
  return addr;
}

static char* r_offstr(regmatch_t m, const char*s)
{
  char*r;
  if(m.rm_so!=-1)
    {
      r=strdup(s+m.rm_so);
      r[m.rm_eo-m.rm_so]=0;
      return r;
    }
  else return strdup("");
}

char* sanitize_mirror(const char*addr)
{
  /* parse IP address string */
  regex_t r;
  int e;
  regmatch_t m[5];
  char*buf=NULL;
  char* a;
  char* b;
  char* c;
  char* d;
  
  if((e=regcomp(&r, "^([^:]*://)([^:/]*)(:[0-9]+)?(.*)$", REG_EXTENDED)))
    {
      /* error */
      fprintf(stderr, "failed compiling regexp: %i\n", e);
      return strdup(addr);
    }
  if((e=regexec(&r, addr, 5, m, 0)))
    {
      fprintf(stderr, "failed regexp match: %i\n", e);
      return strdup(addr);
    }
  asprintf(&buf,"%s%s%s%s",
	   a=r_offstr(m[1],addr),
	   sanitize_ipaddress(b=r_offstr(m[2],addr)),
	   c=r_offstr(m[3],addr),
	   d=r_offstr(m[4],addr));
  free(a);
  free(b);
  free(c);
  free(d);
  regfree(&r);
  return buf;
}