From d9f661ee5d9db58ccbd3b3dae5f7570efca46a0d Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Fri, 5 Jul 2019 10:08:09 +0200 Subject: [PATCH] Use uint16_t for port numbers. This cleans up the mix of int and unsigned short for these variables. --- src/parse.c | 50 ++++++++++++++++++++++++++------------------------ src/parse.peg | 12 +++++++----- src/rinetd.c | 24 +++++++++++------------- src/rinetd.h | 8 +++++--- src/types.h | 8 +++++--- 5 files changed, 54 insertions(+), 48 deletions(-) diff --git a/src/parse.c b/src/parse.c index 1562a9c..496c73a 100644 --- a/src/parse.c +++ b/src/parse.c @@ -21,8 +21,9 @@ int currentLine; \ int isAuthAllow; \ char *tmpPort; \ - int tmpPortNum, tmpProto; \ - int bindPortNum, bindProto, connectPortNum, connectProto; \ + uint16_t tmpPortNum, tmpProto; \ + uint16_t bindPortNum, connectPortNum; \ + int bindProto, connectProto; \ int serverTimeout; \ char *bindAddress, *connectAddress, *sourceAddress; #define YY_INPUT(yyctx, buf, result, max_size) \ @@ -353,7 +354,7 @@ YY_ACTION(void) yy_1_sol(yycontext *yy, char *yytext, int yyleng) #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_sol\n")); { -#line 156 +#line 158 ++yy->currentLine; ; } #undef yythunkpos @@ -367,7 +368,7 @@ YY_ACTION(void) yy_1_invalid_syntax(yycontext *yy, char *yytext, int yyleng) #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_invalid_syntax\n")); { -#line 135 +#line 137 fprintf(stderr, "rinetd: invalid syntax at line %d: %s\n", yy->currentLine, yytext); @@ -385,7 +386,7 @@ YY_ACTION(void) yy_1_logcommon(yycontext *yy, char *yytext, int yyleng) #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_logcommon\n")); { -#line 130 +#line 132 logFormatCommon = 1; ; @@ -401,7 +402,7 @@ YY_ACTION(void) yy_1_pidlogfile(yycontext *yy, char *yytext, int yyleng) #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_pidlogfile\n")); { -#line 122 +#line 124 pidLogFileName = strdup(yytext); if (!pidLogFileName) { @@ -420,7 +421,7 @@ YY_ACTION(void) yy_1_logfile(yycontext *yy, char *yytext, int yyleng) #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_logfile\n")); { -#line 114 +#line 116 logFileName = strdup(yytext); if (!logFileName) { @@ -439,7 +440,7 @@ YY_ACTION(void) yy_1_auth_key(yycontext *yy, char *yytext, int yyleng) #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_auth_key\n")); { -#line 111 +#line 113 yy->isAuthAllow = (yytext[0] == 'a'); ; } #undef yythunkpos @@ -453,7 +454,7 @@ YY_ACTION(void) yy_1_auth_rule(yycontext *yy, char *yytext, int yyleng) #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_auth_rule\n")); { -#line 89 +#line 91 allRules = (Rule *) realloc(allRules, sizeof(Rule) * (allRulesCount + 1)); @@ -487,7 +488,7 @@ YY_ACTION(void) yy_3_proto(yycontext *yy, char *yytext, int yyleng) #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_3_proto\n")); { -#line 86 +#line 88 yy->tmpProto = protoTcp; ; } #undef yythunkpos @@ -501,7 +502,7 @@ YY_ACTION(void) yy_2_proto(yycontext *yy, char *yytext, int yyleng) #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_2_proto\n")); { -#line 85 +#line 87 yy->tmpProto = protoUdp; ; } #undef yythunkpos @@ -515,7 +516,7 @@ YY_ACTION(void) yy_1_proto(yycontext *yy, char *yytext, int yyleng) #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_proto\n")); { -#line 84 +#line 86 yy->tmpProto = protoTcp; ; } #undef yythunkpos @@ -529,7 +530,7 @@ YY_ACTION(void) yy_1_port(yycontext *yy, char *yytext, int yyleng) #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_port\n")); { -#line 83 +#line 85 yy->tmpPort = strdup(yytext); ; } #undef yythunkpos @@ -543,15 +544,16 @@ YY_ACTION(void) yy_1_full_port(yycontext *yy, char *yytext, int yyleng) #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_full_port\n")); { -#line 73 +#line 74 char const *proto = yy->tmpProto == protoTcp ? "tcp" : "udp"; struct servent *service = getservbyname(yy->tmpPort, proto); - yy->tmpPortNum = service ? ntohs(service->s_port) : atoi(yy->tmpPort); - if (yy->tmpPortNum == 0 || yy->tmpPortNum >= 65536) { + int port = service ? ntohs(service->s_port) : atoi(yy->tmpPort); + if (port <= 0 || port >= 65536) { syslog(LOG_ERR, "port %s/%s missing or out of range\n", yy->tmpPort, proto); PARSE_ERROR; } + yy->tmpPortNum = (uint16_t)port; ; } #undef yythunkpos @@ -565,7 +567,7 @@ YY_ACTION(void) yy_1_option_source(yycontext *yy, char *yytext, int yyleng) #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_option_source\n")); { -#line 70 +#line 71 yy->sourceAddress = strdup(yytext); ; } #undef yythunkpos @@ -579,7 +581,7 @@ YY_ACTION(void) yy_1_option_timeout(yycontext *yy, char *yytext, int yyleng) #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_option_timeout\n")); { -#line 69 +#line 70 yy->serverTimeout = atoi(yytext); ; } #undef yythunkpos @@ -593,7 +595,7 @@ YY_ACTION(void) yy_1_connect_port(yycontext *yy, char *yytext, int yyleng) #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_connect_port\n")); { -#line 64 +#line 65 yy->connectPortNum = yy->tmpPortNum; yy->connectProto = yy->tmpProto; ; } #undef yythunkpos @@ -607,7 +609,7 @@ YY_ACTION(void) yy_1_bind_port(yycontext *yy, char *yytext, int yyleng) #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_bind_port\n")); { -#line 63 +#line 64 yy->bindPortNum = yy->tmpPortNum; yy->bindProto = yy->tmpProto; ; } #undef yythunkpos @@ -621,7 +623,7 @@ YY_ACTION(void) yy_1_connect_address(yycontext *yy, char *yytext, int yyleng) #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_connect_address\n")); { -#line 62 +#line 63 yy->connectAddress = strdup(yytext); ; } #undef yythunkpos @@ -635,7 +637,7 @@ YY_ACTION(void) yy_1_bind_address(yycontext *yy, char *yytext, int yyleng) #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_bind_address\n")); { -#line 61 +#line 62 yy->bindAddress = strdup(yytext); ; } #undef yythunkpos @@ -649,7 +651,7 @@ YY_ACTION(void) yy_1_server_rule(yycontext *yy, char *yytext, int yyleng) #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_server_rule\n")); { -#line 52 +#line 53 addServer(yy->bindAddress, yy->bindPortNum, yy->bindProto, yy->connectAddress, yy->connectPortNum, yy->connectProto, @@ -1346,7 +1348,7 @@ YY_PARSE(yycontext *) YYRELEASE(yycontext *yyctx) } #endif -#line 160 "parse.peg" +#line 162 "parse.peg" void parseConfiguration(char const *file) diff --git a/src/parse.peg b/src/parse.peg index cae69b2..f462f49 100644 --- a/src/parse.peg +++ b/src/parse.peg @@ -1,6 +1,6 @@ # Copyright © 1997—1999 Thomas Boutell # and Boutell.Com, Inc. -# © 2003—2017 Sam Hocevar +# © 2003—2019 Sam Hocevar # # This software is released for free use under the terms of # the GNU Public License, version 2 or higher. NO WARRANTY @@ -22,8 +22,9 @@ int currentLine; \ int isAuthAllow; \ char *tmpPort; \ - int tmpPortNum, tmpProto; \ - int bindPortNum, bindProto, connectPortNum, connectProto; \ + uint16_t tmpPortNum, tmpProto; \ + uint16_t bindPortNum, connectPortNum; \ + int bindProto, connectProto; \ int serverTimeout; \ char *bindAddress, *connectAddress, *sourceAddress; #define YY_INPUT(yyctx, buf, result, max_size) \ @@ -73,11 +74,12 @@ full-port = port proto { char const *proto = yy->tmpProto == protoTcp ? "tcp" : "udp"; struct servent *service = getservbyname(yy->tmpPort, proto); - yy->tmpPortNum = service ? ntohs(service->s_port) : atoi(yy->tmpPort); - if (yy->tmpPortNum == 0 || yy->tmpPortNum >= 65536) { + int port = service ? ntohs(service->s_port) : atoi(yy->tmpPort); + if (port <= 0 || port >= 65536) { syslog(LOG_ERR, "port %s/%s missing or out of range\n", yy->tmpPort, proto); PARSE_ERROR; } + yy->tmpPortNum = (uint16_t)port; } port = < (number | service) > { yy->tmpPort = strdup(yytext); } diff --git a/src/rinetd.c b/src/rinetd.c index 3b0b66e..c1fb7a2 100644 --- a/src/rinetd.c +++ b/src/rinetd.c @@ -1,6 +1,6 @@ /* Copyright © 1997—1999 Thomas Boutell and Boutell.Com, Inc. - © 2003—2017 Sam Hocevar + © 2003—2019 Sam Hocevar This software is released for free use under the terms of the GNU Public License, version 2 or higher. NO WARRANTY @@ -248,8 +248,8 @@ static void readConfiguration(char const *file) { } } -void addServer(char *bindAddress, int bindPort, int bindProto, - char *connectAddress, int connectPort, int connectProto, +void addServer(char *bindAddress, uint16_t bindPort, int bindProto, + char *connectAddress, uint16_t connectPort, int connectProto, int serverTimeout, char *sourceAddress) { /* Turn all of this stuff into reasonable addresses */ @@ -287,7 +287,7 @@ void addServer(char *bindAddress, int bindPort, int bindProto, /* Warn -- don't exit. */ syslog(LOG_ERR, "couldn't bind to " "address %s port %d (%m)\n", - bindAddress, bindPort); + bindAddress, (int)bindPort); closesocket(fd); exit(1); } @@ -297,7 +297,7 @@ void addServer(char *bindAddress, int bindPort, int bindProto, /* Warn -- don't exit. */ syslog(LOG_ERR, "couldn't listen to " "address %s port %d (%m)\n", - bindAddress, bindPort); + bindAddress, (int)bindPort); closesocket(fd); } @@ -988,10 +988,8 @@ static void logEvent(ConnectionInfo const *cnx, ServerInfo const *srv, int resul bytesInput = cnx->remote.recvBytes; } - char const *fromHost = "?"; - int fromPort = 0; - char const *toHost = "?"; - int toPort = 0; + char const *fromHost = "?", *toHost = "?"; + uint16_t fromPort = 0, toPort = 0; if (srv != NULL) { fromHost = srv->fromHost; fromPort = srv->fromPort; @@ -1026,8 +1024,8 @@ static void logEvent(ConnectionInfo const *cnx, ServerInfo const *srv, int resul sign, timz / 60, timz % 60, - fromHost, fromPort, - toHost, toPort, + fromHost, (int)fromPort, + toHost, (int)toPort, logMessages[result], bytesOutput, bytesInput); @@ -1038,8 +1036,8 @@ static void logEvent(ConnectionInfo const *cnx, ServerInfo const *srv, int resul "\t%d\t%s\n", tstr, addressText, - fromHost, fromPort, - toHost, toPort, + fromHost, (int)fromPort, + toHost, (int)toPort, bytesInput, bytesOutput, logMessages[result]); diff --git a/src/rinetd.h b/src/rinetd.h index f5226a2..9b0ef4c 100644 --- a/src/rinetd.h +++ b/src/rinetd.h @@ -1,6 +1,6 @@ /* Copyright © 1997—1999 Thomas Boutell and Boutell.Com, Inc. - © 2003—2017 Sam Hocevar + © 2003—2019 Sam Hocevar This software is released for free use under the terms of the GNU Public License, version 2 or higher. NO WARRANTY @@ -19,6 +19,8 @@ # include #endif /* _WIN32 */ +#include + /* Constants */ static int const RINETD_BUFFER_SIZE = 16384; @@ -44,7 +46,7 @@ extern FILE *logFile; /* Functions */ -void addServer(char *bindAddress, int bindPort, int bindProto, - char *connectAddress, int connectPort, int connectProto, +void addServer(char *bindAddress, uint16_t bindPort, int bindProto, + char *connectAddress, uint16_t connectPort, int connectProto, int serverTimeout, char *sourceAddress); diff --git a/src/types.h b/src/types.h index 9a26c8c..36223d3 100644 --- a/src/types.h +++ b/src/types.h @@ -1,6 +1,6 @@ /* Copyright © 1997—1999 Thomas Boutell and Boutell.Com, Inc. - © 2003—2017 Sam Hocevar + © 2003—2019 Sam Hocevar This software is released for free use under the terms of the GNU Public License, version 2 or higher. NO WARRANTY @@ -9,6 +9,7 @@ #pragma once #include +#include enum ruleType { allowRule, @@ -33,12 +34,13 @@ struct _server_info { /* In network order, for network purposes */ struct in_addr localAddr; - unsigned short localPort; + uint16_t localPort; struct in_addr sourceAddr; /* In ASCII and local byte order, for logging purposes */ char *fromHost, *toHost; - int fromPort, fromProto, toPort, toProto; + int16_t fromPort, toPort; + int fromProto, toProto; /* Offset and count into list of allow and deny rules. Any rules prior to globalAllowRules and globalDenyRules are global rules. */