fix missing bracket in solaris codepath (#589)
[rofl0r-proxychains-ng.git] / src / common.c
blob1da1c454295c7d95c5bc3b0afac088b2e248e3a3
1 #include "common.h"
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <stdio.h>
6 const char *proxy_type_strmap[] = {
7 "http",
8 "socks4",
9 "socks5",
12 const char *chain_type_strmap[] = {
13 "dynamic_chain",
14 "strict_chain",
15 "random_chain",
16 "round_robin_chain",
19 const char *proxy_state_strmap[] = {
20 "play",
21 "down",
22 "blocked",
23 "busy",
26 /* isnumericipv4() taken from libulz */
27 int pc_isnumericipv4(const char* ipstring) {
28 size_t x = 0, n = 0, d = 0;
29 int wasdot = 0;
30 while(1) {
31 switch(ipstring[x]) {
32 case 0: goto done;
33 case '.':
34 if(!n || wasdot) return 0;
35 d++;
36 wasdot = 1;
37 break;
38 case '0': case '1': case '2': case '3': case '4':
39 case '5': case '6': case '7': case '8': case '9':
40 n++;
41 wasdot = 0;
42 break;
43 default:
44 return 0;
46 x++;
48 done:
49 if(d == 3 && n >= 4 && n <= 12) return 1;
50 return 0;
53 // stolen from libulz (C) rofl0r
54 void pc_stringfromipv4(unsigned char *ip_buf_4_bytes, char *outbuf_16_bytes) {
55 unsigned char *p;
56 char *o = outbuf_16_bytes;
57 unsigned char n;
58 for(p = ip_buf_4_bytes; p < ip_buf_4_bytes + 4; p++) {
59 n = *p;
60 if(*p >= 100) {
61 if(*p >= 200)
62 *(o++) = '2';
63 else
64 *(o++) = '1';
65 n %= 100;
67 if(*p >= 10) {
68 *(o++) = (n / 10) + '0';
69 n %= 10;
71 *(o++) = n + '0';
72 *(o++) = '.';
74 o[-1] = 0;
77 static int check_path(char *path) {
78 if(!path)
79 return 0;
80 return access(path, R_OK) != -1;
83 char *get_config_path(char* default_path, char* pbuf, size_t bufsize) {
84 char buf[512];
85 // top priority: user defined path
86 char *path = default_path;
87 if(check_path(path))
88 goto have;
90 // priority 1: env var PROXYCHAINS_CONF_FILE
91 path = getenv(PROXYCHAINS_CONF_FILE_ENV_VAR);
92 if(check_path(path))
93 goto have;
95 // priority 2; proxychains conf in actual dir
96 path = getcwd(buf, sizeof(buf));
97 snprintf(pbuf, bufsize, "%s/%s", path, PROXYCHAINS_CONF_FILE);
98 path = pbuf;
99 if(check_path(path))
100 goto have;
102 // priority 3; $HOME/.proxychains/proxychains.conf
103 path = getenv("HOME");
104 snprintf(pbuf, bufsize, "%s/.proxychains/%s", path, PROXYCHAINS_CONF_FILE);
105 path = pbuf;
106 if(check_path(path))
107 goto have;
109 // priority 3b: ~/config/settings/proxychains.conf (for haiku)
110 path = getenv("HOME");
111 snprintf(pbuf, bufsize, "%s/config/settings/%s", path, PROXYCHAINS_CONF_FILE);
112 path = pbuf;
113 if(check_path(path))
114 goto have;
116 // priority 4: $SYSCONFDIR/proxychains.conf
117 path = SYSCONFDIR "/" PROXYCHAINS_CONF_FILE;
118 if(check_path(path))
119 goto have;
121 // priority 5: /etc/proxychains.conf
122 path = "/etc/" PROXYCHAINS_CONF_FILE;
123 if(check_path(path))
124 goto have;
126 perror("couldnt find configuration file");
127 exit(1);
129 return NULL;
130 have:
131 return path;