MCPcopy Create free account
hub / github.com/F-Stack/f-stack / StrToPortRange

Function StrToPortRange

tools/ipfw/nat.c:226–268  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

224}
225
226static int
227StrToPortRange (const char* str, const char* proto, port_range *portRange)
228{
229 char* sep;
230 struct servent* sp;
231 char* end;
232 u_short loPort;
233 u_short hiPort;
234
235 /* First see if this is a service, return corresponding port if so. */
236 sp = getservbyname (str,proto);
237 if (sp) {
238 SETLOPORT(*portRange, ntohs(sp->s_port));
239 SETNUMPORTS(*portRange, 1);
240 return 0;
241 }
242
243 /* Not a service, see if it's a single port or port range. */
244 sep = strchr (str, '-');
245 if (sep == NULL) {
246 SETLOPORT(*portRange, strtol(str, &end, 10));
247 if (end != str) {
248 /* Single port. */
249 SETNUMPORTS(*portRange, 1);
250 return 0;
251 }
252
253 /* Error in port range field. */
254 errx (EX_DATAERR, "%s/%s: unknown service", str, proto);
255 }
256
257 /* Port range, get the values and sanity check. */
258 sscanf (str, "%hu-%hu", &loPort, &hiPort);
259 SETLOPORT(*portRange, loPort);
260 SETNUMPORTS(*portRange, 0); /* Error by default */
261 if (loPort <= hiPort)
262 SETNUMPORTS(*portRange, hiPort - loPort + 1);
263
264 if (GETNUMPORTS(*portRange) == 0)
265 errx (EX_DATAERR, "invalid port range %s", str);
266
267 return 0;
268}
269
270static int
271StrToProto (const char* str)

Callers 2

StrToAddrAndPortRangeFunction · 0.85
setup_redir_portFunction · 0.85

Calls 3

strchrFunction · 0.85
strtolFunction · 0.85
sscanfFunction · 0.85

Tested by

no test coverage detected