diff options
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 50 |
1 files changed, 30 insertions, 20 deletions
@@ -83,52 +83,62 @@ int string_to_id(char_t *w, char_t *a) | |||
83 | } | 83 | } |
84 | 84 | ||
85 | /* Parse the -L parameter */ | 85 | /* Parse the -L parameter */ |
86 | /* 0 = success */ | 86 | /* true = success */ |
87 | int parse_local_port_forward(char *string, int *local_port, char **hostname, int *remote_port) | 87 | bool parse_local_port_forward(char *string, int *local_port, char **hostname, int *remote_port) |
88 | { | 88 | { |
89 | char *lport; | 89 | char *lport; |
90 | char *host; | ||
91 | char *rport; | ||
92 | 90 | ||
93 | /* Alternative delimiter '@', as ':' is forbidden in some environments */ | 91 | /* Alternative delimiter '@', as ':' is forbidden in some environments */ |
94 | |||
95 | lport = strtok(string, ":@"); | 92 | lport = strtok(string, ":@"); |
96 | host = strtok(NULL, ":@"); | ||
97 | rport = strtok(NULL, ":@"); | ||
98 | 93 | ||
99 | if(!lport || !host || !rport) | 94 | if(!(*local_port = atoi(lport))) |
100 | { | 95 | { |
101 | return -1; | 96 | return false; |
102 | } | 97 | } |
103 | 98 | ||
104 | *local_port = atoi(lport); | 99 | if(parse_pipe_port_forward(lport + strlen(lport), hostname, remote_port)) |
105 | *hostname = host; | 100 | { |
106 | *remote_port = atoi(rport); | 101 | return *remote_port; |
107 | 102 | } | |
108 | return 0; | 103 | return false; |
109 | } | 104 | } |
110 | 105 | ||
111 | /* Parse the -W parameter */ | 106 | /* Parse the -W parameter */ |
112 | /* 0 = success */ | 107 | /* true = success */ |
113 | int parse_pipe_port_forward(char *string, char **hostname, int *remote_port) | 108 | bool parse_pipe_port_forward(char *string, char **hostname, int *remote_port) |
114 | { | 109 | { |
115 | char *host; | 110 | char *host; |
116 | char *rport; | 111 | char *rport; |
117 | 112 | ||
118 | /* Alternative delimiter '@', as ':' is forbidden in some environments */ | 113 | /* Alternative delimiter '@', as ':' is forbidden in some environments */ |
119 | |||
120 | host = strtok(string, ":@"); | 114 | host = strtok(string, ":@"); |
121 | rport = strtok(NULL, ":@"); | 115 | rport = strtok(NULL, ""); |
122 | 116 | ||
123 | if(!host || !rport) | 117 | if(!host || !rport) |
124 | { | 118 | { |
125 | return -1; | 119 | return false; |
126 | } | 120 | } |
127 | 121 | ||
128 | *hostname = host; | 122 | *hostname = host; |
129 | *remote_port = atoi(rport); | 123 | *remote_port = atoi(rport); |
130 | 124 | ||
131 | return 0; | 125 | if(*remote_port > 0 && *remote_port < 65535) |
126 | { | ||
127 | /* This is tolerant of nonsense tokens after the port. */ | ||
128 | return true; | ||
129 | } | ||
130 | else | ||
131 | { | ||
132 | /* Port 0 is not allowed in the input. Only a literal '*' can produce a | ||
133 | * port 0 in the output, which will be treated as a wildcard if this is | ||
134 | * a rule. */ | ||
135 | if (rport[0] != '*') | ||
136 | { | ||
137 | return false; | ||
138 | } | ||
139 | /* Return an error if an extra token follows, but tolerate whitespace. */ | ||
140 | return !strtok(rport+1, "\n\t "); | ||
141 | } | ||
132 | } | 142 | } |
133 | 143 | ||
134 | void* file_raw(char *path, uint32_t *size) | 144 | void* file_raw(char *path, uint32_t *size) |