diff options
author | Brian Somers <brian@FreeBSD.org> | 1999-03-25 23:36:25 +0000 |
---|---|---|
committer | Brian Somers <brian@FreeBSD.org> | 1999-03-25 23:36:25 +0000 |
commit | fe3094cdd7132e84bd5e5b2ad5109daed7cc74ff (patch) | |
tree | 097e8fdade97a33209faa983c1060682c591144c /usr.sbin/ppp/nat_cmd.c | |
parent | 92c49d78da2be95b13dd887049d495629956f183 (diff) | |
download | src-fe3094cdd7132e84bd5e5b2ad5109daed7cc74ff.tar.gz src-fe3094cdd7132e84bd5e5b2ad5109daed7cc74ff.zip |
Allow port ranges in ``alias port''.
Notes
Notes:
svn path=/head/; revision=45042
Diffstat (limited to 'usr.sbin/ppp/nat_cmd.c')
-rw-r--r-- | usr.sbin/ppp/nat_cmd.c | 125 |
1 files changed, 83 insertions, 42 deletions
diff --git a/usr.sbin/ppp/nat_cmd.c b/usr.sbin/ppp/nat_cmd.c index 979b0b575a43..430ab472b67c 100644 --- a/usr.sbin/ppp/nat_cmd.c +++ b/usr.sbin/ppp/nat_cmd.c @@ -2,7 +2,7 @@ * The code in this file was written by Eivind Eklund <perhaps@yes.no>, * who places it in the public domain without restriction. * - * $Id: alias_cmd.c,v 1.20 1999/03/07 15:02:37 brian Exp $ + * $Id: alias_cmd.c,v 1.21 1999/03/07 18:13:44 brian Exp $ */ #include <sys/param.h> @@ -51,8 +51,9 @@ static int StrToAddr(const char *, struct in_addr *); -static int StrToPort(const char *, u_short *, const char *); -static int StrToAddrAndPort(const char *, struct in_addr *, u_short *, const char *); +static int StrToPortRange(const char *, u_short *, u_short *, const char *); +static int StrToAddrAndPort(const char *, struct in_addr *, u_short *, + u_short *, const char *); int @@ -61,11 +62,14 @@ alias_RedirectPort(struct cmdargs const *arg) if (!arg->bundle->AliasEnabled) { prompt_Printf(arg->prompt, "Alias not enabled\n"); return 1; - } else if (arg->argc == arg->argn+3) { + } else if (arg->argc == arg->argn + 3) { char proto_constant; const char *proto; - u_short local_port; - u_short alias_port; + u_short hlocalport; + u_short llocalport; + u_short haliasport; + u_short laliasport; + u_short port; int error; struct in_addr local_addr; struct in_addr null_addr; @@ -79,37 +83,51 @@ alias_RedirectPort(struct cmdargs const *arg) } else { prompt_Printf(arg->prompt, "port redirect: protocol must be" " tcp or udp\n"); - prompt_Printf(arg->prompt, "Usage: alias %s %s\n", arg->cmd->name, - arg->cmd->syntax); - return 1; + return -1; } - error = StrToAddrAndPort(arg->argv[arg->argn+1], &local_addr, &local_port, - proto); + error = StrToAddrAndPort(arg->argv[arg->argn+1], &local_addr, &llocalport, + &hlocalport, proto); if (error) { - prompt_Printf(arg->prompt, "port redirect: error reading" - " local addr:port\n"); - prompt_Printf(arg->prompt, "Usage: alias %s %s\n", arg->cmd->name, - arg->cmd->syntax); - return 1; + prompt_Printf(arg->prompt, "alias port: error reading localaddr:port\n"); + return -1; } - error = StrToPort(arg->argv[arg->argn+2], &alias_port, proto); + error = StrToPortRange(arg->argv[arg->argn+2], &laliasport, &haliasport, + proto); if (error) { - prompt_Printf(arg->prompt, "port redirect: error reading alias port\n"); - prompt_Printf(arg->prompt, "Usage: alias %s %s\n", arg->cmd->name, - arg->cmd->syntax); - return 1; + prompt_Printf(arg->prompt, "alias port: error reading alias port\n"); + return -1; } null_addr.s_addr = INADDR_ANY; - link = PacketAliasRedirectPort(local_addr, local_port, - null_addr, 0, - null_addr, alias_port, - proto_constant); + if (llocalport > hlocalport) { + port = llocalport; + llocalport = hlocalport; + hlocalport = port; + } + + if (laliasport > haliasport) { + port = laliasport; + laliasport = haliasport; + haliasport = port; + } - if (link == NULL) - prompt_Printf(arg->prompt, "port redirect: error returned by packed" - " aliasing engine (code=%d)\n", error); + if (haliasport - laliasport != hlocalport - llocalport) { + prompt_Printf(arg->prompt, "alias port: Port ranges must be equal\n"); + return -1; + } + + for (port = laliasport; port <= haliasport; port++) { + link = PacketAliasRedirectPort(local_addr, + htons(llocalport + (port - laliasport)), + null_addr, 0, null_addr, htons(port), + proto_constant); + + if (link == NULL) { + prompt_Printf(arg->prompt, "alias port: %d: error %d\n", port, error); + return 1; + } + } } else return -1; @@ -176,28 +194,51 @@ StrToAddr(const char *str, struct in_addr *addr) static int StrToPort(const char *str, u_short *port, const char *proto) { - int iport; struct servent *sp; char *end; - iport = strtol(str, &end, 10); - if (end != str) { - *port = htons(iport); - return 0; - } - sp = getservbyname(str, proto); - if (!sp) { - log_Printf(LogWARN, "StrToAddr: Unknown port or service %s/%s.\n", - str, proto); - return -1; + *port = strtol(str, &end, 10); + if (*end != '\0') { + sp = getservbyname(str, proto); + if (sp == NULL) { + log_Printf(LogWARN, "StrToAddr: Unknown port or service %s/%s.\n", + str, proto); + return -1; + } + *port = ntohs(sp->s_port); } - *port = sp->s_port; + return 0; } +static int +StrToPortRange(const char *str, u_short *low, u_short *high, const char *proto) +{ + char *minus; + int res; + + minus = strchr(str, '-'); + if (minus) + *minus = '\0'; /* Cheat the const-ness ! */ + + res = StrToPort(str, low, proto); + + if (minus) + *minus = '-'; /* Cheat the const-ness ! */ + + if (res == 0) { + if (minus) + res = StrToPort(minus + 1, high, proto); + else + *high = *low; + } + + return res; +} static int -StrToAddrAndPort(const char *str, struct in_addr *addr, u_short *port, const char *proto) +StrToAddrAndPort(const char *str, struct in_addr *addr, u_short *low, + u_short *high, const char *proto) { char *colon; int res; @@ -214,7 +255,7 @@ StrToAddrAndPort(const char *str, struct in_addr *addr, u_short *port, const cha if (res != 0) return -1; - return StrToPort(colon+1, port, proto); + return StrToPortRange(colon + 1, low, high, proto); } int |