Lines 1874-1911
snmp_client_set_port(struct snmp_client *cl, const char *p)
Link Here
|
1874 |
return (0); |
1874 |
return (0); |
1875 |
} |
1875 |
} |
1876 |
|
1876 |
|
|
|
1877 |
static const char *const trans_list[] = { |
1878 |
[SNMP_TRANS_UDP] = "udp::", |
1879 |
[SNMP_TRANS_LOC_DGRAM] = "dgram::", |
1880 |
[SNMP_TRANS_LOC_STREAM] = "stream::", |
1881 |
[SNMP_TRANS_UDP6] = "udp6::", |
1882 |
}; |
1883 |
|
1877 |
/** |
1884 |
/** |
1878 |
* Try to get a transport identifier which is a leading alphanumeric string |
1885 |
* Try to get a transport identifier which is a leading alphanumeric string |
1879 |
* (starting with '_' or a letter and including also '_') terminated by |
1886 |
* terminated by a double colon. The string may not be empty. The transport |
1880 |
* a double colon. The string may not be empty. The transport identifier |
1887 |
* identifier is optional. |
1881 |
* is optional. |
|
|
1882 |
* |
1888 |
* |
1883 |
* \param sc client struct to set errors |
1889 |
* \param sc client struct to set errors |
1884 |
* \param strp possible start of transport; updated to point to |
1890 |
* \param strp possible start of transport; updated to point to |
1885 |
* the next character to parse |
1891 |
* the next character to parse |
1886 |
* |
1892 |
* |
1887 |
* \return end of transport; equals *strp if there is none; NULL if there |
1893 |
* \return transport identifier |
1888 |
* was an error |
|
|
1889 |
*/ |
1894 |
*/ |
1890 |
static inline const char * |
1895 |
static inline int |
1891 |
get_transp(struct snmp_client *sc, const char **strp) |
1896 |
get_transp(struct snmp_client *sc, const char **strp) |
1892 |
{ |
1897 |
{ |
1893 |
const char *p = *strp; |
1898 |
const char *p; |
|
|
1899 |
size_t i; |
1894 |
|
1900 |
|
1895 |
if (isascii(*p) && (isalpha(*p) || *p == '_')) { |
1901 |
for (i = 0; i < nitems(trans_list); i++) { |
1896 |
p++; |
1902 |
if (trans_list[i] == NULL && *trans_list[i] != '\0') |
1897 |
while (isascii(*p) && (isalnum(*p) || *p == '_')) |
1903 |
continue; |
1898 |
p++; |
1904 |
p = strstr(*strp, trans_list[i]); |
1899 |
if (p[0] == ':' && p[1] == ':') { |
1905 |
if (p == *strp) { |
1900 |
*strp = p + 2; |
1906 |
*strp += strlen(trans_list[i]); |
1901 |
return (p); |
1907 |
return ((int)i); |
1902 |
} |
1908 |
} |
1903 |
} |
1909 |
} |
|
|
1910 |
|
1911 |
p = *strp; |
1904 |
if (p[0] == ':' && p[1] == ':') { |
1912 |
if (p[0] == ':' && p[1] == ':') { |
1905 |
seterr(sc, "empty transport specifier"); |
1913 |
seterr(sc, "empty transport specifier"); |
1906 |
return (NULL); |
1914 |
return (-1); |
1907 |
} |
1915 |
} |
1908 |
return (*strp); |
1916 |
/* by default assume UDP */ |
|
|
1917 |
return (SNMP_TRANS_UDP); |
1909 |
} |
1918 |
} |
1910 |
|
1919 |
|
1911 |
/** |
1920 |
/** |
Lines 2143-2166
save_str(struct snmp_client *sc, const char *const s[2])
Link Here
|
2143 |
int |
2152 |
int |
2144 |
snmp_parse_server(struct snmp_client *sc, const char *str) |
2153 |
snmp_parse_server(struct snmp_client *sc, const char *str) |
2145 |
{ |
2154 |
{ |
2146 |
#if DEBUG_PARSE |
|
|
2147 |
const char *const orig = str; |
2155 |
const char *const orig = str; |
2148 |
#endif |
|
|
2149 |
|
2150 |
const char *const trans_list[] = { |
2151 |
[SNMP_TRANS_UDP] = "udp", |
2152 |
[SNMP_TRANS_LOC_DGRAM] = "dgram", |
2153 |
[SNMP_TRANS_LOC_STREAM] = "stream", |
2154 |
[SNMP_TRANS_UDP6] = "udp6", |
2155 |
}; |
2156 |
|
2157 |
/* parse input */ |
2156 |
/* parse input */ |
2158 |
const char *const transp[2] = { |
2157 |
int i, trans = get_transp(sc, &str); |
2159 |
str, |
2158 |
if (trans < 0) |
2160 |
get_transp(sc, &str), |
|
|
2161 |
}; |
2162 |
if (transp[1] == NULL) |
2163 |
return (-1); |
2159 |
return (-1); |
|
|
2160 |
/* choose automatically */ |
2161 |
i = orig == str ? -1: trans; |
2164 |
|
2162 |
|
2165 |
const char *const comm[2] = { |
2163 |
const char *const comm[2] = { |
2166 |
str, |
2164 |
str, |
Lines 2206-2212
snmp_parse_server(struct snmp_client *sc, const char *str)
Link Here
|
2206 |
} |
2204 |
} |
2207 |
|
2205 |
|
2208 |
#if DEBUG_PARSE |
2206 |
#if DEBUG_PARSE |
2209 |
printf("transp: %zu %zu\n", transp[0] - orig, transp[1] - orig); |
2207 |
printf("transp: %u\n", trans); |
2210 |
printf("comm: %zu %zu\n", comm[0] - orig, comm[1] - orig); |
2208 |
printf("comm: %zu %zu\n", comm[0] - orig, comm[1] - orig); |
2211 |
printf("ipv6: %zu %zu\n", ipv6[0] - orig, ipv6[1] - orig); |
2209 |
printf("ipv6: %zu %zu\n", ipv6[0] - orig, ipv6[1] - orig); |
2212 |
printf("ipv4: %zu %zu\n", ipv4[0] - orig, ipv4[1] - orig); |
2210 |
printf("ipv4: %zu %zu\n", ipv4[0] - orig, ipv4[1] - orig); |
Lines 2215-2283
snmp_parse_server(struct snmp_client *sc, const char *str)
Link Here
|
2215 |
#endif |
2213 |
#endif |
2216 |
|
2214 |
|
2217 |
/* analyse and allocate */ |
2215 |
/* analyse and allocate */ |
2218 |
int i = -1; |
|
|
2219 |
if (transp[0] != transp[1]) { |
2220 |
for (i = 0; i < (int)nitems(trans_list); i++) { |
2221 |
if (trans_list[i] != NULL && |
2222 |
strlen(trans_list[i]) == (size_t)(transp[1] - |
2223 |
transp[0]) && !strncmp(trans_list[i], transp[0], |
2224 |
transp[1] - transp[0])) |
2225 |
break; |
2226 |
} |
2227 |
|
2228 |
if (i == (int)nitems(trans_list)) { |
2229 |
seterr(sc, "unknown transport specifier '%.*s'", |
2230 |
transp[1] - transp[0], transp[0]); |
2231 |
return (-1); |
2232 |
} |
2233 |
} |
2234 |
|
2235 |
char *chost; |
2216 |
char *chost; |
2236 |
|
2217 |
|
2237 |
if (ipv6[0] != ipv6[1]) { |
2218 |
if (ipv6[0] != ipv6[1]) { |
2238 |
if ((chost = save_str(sc, ipv6)) == NULL) |
2219 |
if ((chost = save_str(sc, ipv6)) == NULL) |
2239 |
return (-1); |
2220 |
return (-1); |
2240 |
if (i == -1) |
2221 |
if (i == -1 || trans == SNMP_TRANS_UDP) |
2241 |
i = SNMP_TRANS_UDP6; |
2222 |
trans = SNMP_TRANS_UDP6; |
2242 |
} else if (ipv4[0] != ipv4[1]) { |
2223 |
} else if (ipv4[0] != ipv4[1]) { |
2243 |
if ((chost = save_str(sc, ipv4)) == NULL) |
2224 |
if ((chost = save_str(sc, ipv4)) == NULL) |
2244 |
return (-1); |
2225 |
return (-1); |
2245 |
if (i == -1) |
2226 |
if (i == -1) |
2246 |
i = SNMP_TRANS_UDP; |
2227 |
trans = SNMP_TRANS_UDP; |
2247 |
} else { |
2228 |
} else { |
2248 |
if ((chost = save_str(sc, host)) == NULL) |
2229 |
if ((chost = save_str(sc, host)) == NULL) |
2249 |
return (-1); |
2230 |
return (-1); |
2250 |
|
2231 |
|
2251 |
if (i == -1) { |
2232 |
if (i == -1) { |
2252 |
/* Default transport is UDP unless the host contains |
2233 |
/* |
2253 |
* a slash in which case we default to DGRAM. */ |
2234 |
* Default transport is UDP unless the host contains |
2254 |
i = SNMP_TRANS_UDP; |
2235 |
* a slash in which case we default to DGRAM. |
|
|
2236 |
*/ |
2237 |
trans = SNMP_TRANS_UDP; |
2255 |
for (const char *p = host[0]; p < host[1]; p++) |
2238 |
for (const char *p = host[0]; p < host[1]; p++) |
2256 |
if (*p == '/') { |
2239 |
if (*p == '/') { |
2257 |
i = SNMP_TRANS_LOC_DGRAM; |
2240 |
trans = SNMP_TRANS_LOC_DGRAM; |
2258 |
break; |
2241 |
break; |
2259 |
} |
2242 |
} |
2260 |
} |
2243 |
} |
2261 |
} |
2244 |
} |
2262 |
|
2245 |
|
2263 |
char *cport = save_str(sc, port); |
2246 |
char *cport; |
|
|
2247 |
|
2248 |
if (port[0] == port[1] && ( |
2249 |
trans == SNMP_TRANS_UDP || trans == SNMP_TRANS_UDP6)) { |
2250 |
/* If port was not specified, use "snmp" name by default */ |
2251 |
cport = strdup("snmp"); |
2252 |
} else |
2253 |
cport = save_str(sc, port); |
2254 |
|
2264 |
if (cport == NULL) { |
2255 |
if (cport == NULL) { |
2265 |
free(chost); |
2256 |
free(chost); |
2266 |
return (-1); |
2257 |
return (-1); |
2267 |
} |
2258 |
} |
2268 |
|
2259 |
|
2269 |
/* commit */ |
2260 |
/* commit */ |
2270 |
sc->trans = i; |
2261 |
sc->trans = trans; |
2271 |
|
2262 |
/* |
2272 |
strncpy(sc->read_community, comm[0], comm[1] - comm[0]); |
2263 |
* If community string was specified and it is empty, overwrite it. |
2273 |
sc->read_community[comm[1] - comm[0]] = '\0'; |
2264 |
* If it was not specified, use default. |
2274 |
strncpy(sc->write_community, comm[0], comm[1] - comm[0]); |
2265 |
*/ |
2275 |
sc->write_community[comm[1] - comm[0]] = '\0'; |
2266 |
if (comm[0] != comm[1] || strrchr(comm[0], '@') != NULL) { |
|
|
2267 |
strncpy(sc->read_community, comm[0], comm[1] - comm[0]); |
2268 |
sc->read_community[comm[1] - comm[0]] = '\0'; |
2269 |
strncpy(sc->write_community, comm[0], comm[1] - comm[0]); |
2270 |
sc->write_community[comm[1] - comm[0]] = '\0'; |
2271 |
} |
2276 |
|
2272 |
|
2277 |
free(sc->chost); |
2273 |
free(sc->chost); |
2278 |
sc->chost = chost; |
2274 |
sc->chost = chost; |
2279 |
free(sc->cport); |
2275 |
free(sc->cport); |
2280 |
sc->cport = cport; |
2276 |
sc->cport = cport; |
2281 |
|
2277 |
|
|
|
2278 |
#if DEBUG_PARSE |
2279 |
printf("Committed values:\n"); |
2280 |
printf("trans: %u\n", sc->trans); |
2281 |
printf("comm: '%s'/'%s'\n", sc->read_community, sc->write_community); |
2282 |
printf("host: '%s'\n", sc->chost); |
2283 |
printf("port: '%s'\n", sc->cport); |
2284 |
#endif |
2282 |
return (0); |
2285 |
return (0); |
2283 |
} |
2286 |
} |