Use uint16_t for port numbers.

This cleans up the mix of int and unsigned short for these variables.
This commit is contained in:
Sam Hocevar 2019-07-05 10:08:09 +02:00
parent a37e13c910
commit d9f661ee5d
5 changed files with 54 additions and 48 deletions

View File

@ -21,8 +21,9 @@
int currentLine; \ int currentLine; \
int isAuthAllow; \ int isAuthAllow; \
char *tmpPort; \ char *tmpPort; \
int tmpPortNum, tmpProto; \ uint16_t tmpPortNum, tmpProto; \
int bindPortNum, bindProto, connectPortNum, connectProto; \ uint16_t bindPortNum, connectPortNum; \
int bindProto, connectProto; \
int serverTimeout; \ int serverTimeout; \
char *bindAddress, *connectAddress, *sourceAddress; char *bindAddress, *connectAddress, *sourceAddress;
#define YY_INPUT(yyctx, buf, result, max_size) \ #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 #define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_sol\n")); yyprintf((stderr, "do yy_1_sol\n"));
{ {
#line 156 #line 158
++yy->currentLine; ; ++yy->currentLine; ;
} }
#undef yythunkpos #undef yythunkpos
@ -367,7 +368,7 @@ YY_ACTION(void) yy_1_invalid_syntax(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos #define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_invalid_syntax\n")); yyprintf((stderr, "do yy_1_invalid_syntax\n"));
{ {
#line 135 #line 137
fprintf(stderr, "rinetd: invalid syntax at line %d: %s\n", fprintf(stderr, "rinetd: invalid syntax at line %d: %s\n",
yy->currentLine, yytext); yy->currentLine, yytext);
@ -385,7 +386,7 @@ YY_ACTION(void) yy_1_logcommon(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos #define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_logcommon\n")); yyprintf((stderr, "do yy_1_logcommon\n"));
{ {
#line 130 #line 132
logFormatCommon = 1; logFormatCommon = 1;
; ;
@ -401,7 +402,7 @@ YY_ACTION(void) yy_1_pidlogfile(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos #define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_pidlogfile\n")); yyprintf((stderr, "do yy_1_pidlogfile\n"));
{ {
#line 122 #line 124
pidLogFileName = strdup(yytext); pidLogFileName = strdup(yytext);
if (!pidLogFileName) { if (!pidLogFileName) {
@ -420,7 +421,7 @@ YY_ACTION(void) yy_1_logfile(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos #define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_logfile\n")); yyprintf((stderr, "do yy_1_logfile\n"));
{ {
#line 114 #line 116
logFileName = strdup(yytext); logFileName = strdup(yytext);
if (!logFileName) { if (!logFileName) {
@ -439,7 +440,7 @@ YY_ACTION(void) yy_1_auth_key(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos #define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_auth_key\n")); yyprintf((stderr, "do yy_1_auth_key\n"));
{ {
#line 111 #line 113
yy->isAuthAllow = (yytext[0] == 'a'); ; yy->isAuthAllow = (yytext[0] == 'a'); ;
} }
#undef yythunkpos #undef yythunkpos
@ -453,7 +454,7 @@ YY_ACTION(void) yy_1_auth_rule(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos #define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_auth_rule\n")); yyprintf((stderr, "do yy_1_auth_rule\n"));
{ {
#line 89 #line 91
allRules = (Rule *) allRules = (Rule *)
realloc(allRules, sizeof(Rule) * (allRulesCount + 1)); 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 #define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_3_proto\n")); yyprintf((stderr, "do yy_3_proto\n"));
{ {
#line 86 #line 88
yy->tmpProto = protoTcp; ; yy->tmpProto = protoTcp; ;
} }
#undef yythunkpos #undef yythunkpos
@ -501,7 +502,7 @@ YY_ACTION(void) yy_2_proto(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos #define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_2_proto\n")); yyprintf((stderr, "do yy_2_proto\n"));
{ {
#line 85 #line 87
yy->tmpProto = protoUdp; ; yy->tmpProto = protoUdp; ;
} }
#undef yythunkpos #undef yythunkpos
@ -515,7 +516,7 @@ YY_ACTION(void) yy_1_proto(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos #define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_proto\n")); yyprintf((stderr, "do yy_1_proto\n"));
{ {
#line 84 #line 86
yy->tmpProto = protoTcp; ; yy->tmpProto = protoTcp; ;
} }
#undef yythunkpos #undef yythunkpos
@ -529,7 +530,7 @@ YY_ACTION(void) yy_1_port(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos #define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_port\n")); yyprintf((stderr, "do yy_1_port\n"));
{ {
#line 83 #line 85
yy->tmpPort = strdup(yytext); ; yy->tmpPort = strdup(yytext); ;
} }
#undef yythunkpos #undef yythunkpos
@ -543,15 +544,16 @@ YY_ACTION(void) yy_1_full_port(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos #define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_full_port\n")); yyprintf((stderr, "do yy_1_full_port\n"));
{ {
#line 73 #line 74
char const *proto = yy->tmpProto == protoTcp ? "tcp" : "udp"; char const *proto = yy->tmpProto == protoTcp ? "tcp" : "udp";
struct servent *service = getservbyname(yy->tmpPort, proto); struct servent *service = getservbyname(yy->tmpPort, proto);
yy->tmpPortNum = service ? ntohs(service->s_port) : atoi(yy->tmpPort); int port = service ? ntohs(service->s_port) : atoi(yy->tmpPort);
if (yy->tmpPortNum == 0 || yy->tmpPortNum >= 65536) { if (port <= 0 || port >= 65536) {
syslog(LOG_ERR, "port %s/%s missing or out of range\n", yy->tmpPort, proto); syslog(LOG_ERR, "port %s/%s missing or out of range\n", yy->tmpPort, proto);
PARSE_ERROR; PARSE_ERROR;
} }
yy->tmpPortNum = (uint16_t)port;
; ;
} }
#undef yythunkpos #undef yythunkpos
@ -565,7 +567,7 @@ YY_ACTION(void) yy_1_option_source(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos #define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_option_source\n")); yyprintf((stderr, "do yy_1_option_source\n"));
{ {
#line 70 #line 71
yy->sourceAddress = strdup(yytext); ; yy->sourceAddress = strdup(yytext); ;
} }
#undef yythunkpos #undef yythunkpos
@ -579,7 +581,7 @@ YY_ACTION(void) yy_1_option_timeout(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos #define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_option_timeout\n")); yyprintf((stderr, "do yy_1_option_timeout\n"));
{ {
#line 69 #line 70
yy->serverTimeout = atoi(yytext); ; yy->serverTimeout = atoi(yytext); ;
} }
#undef yythunkpos #undef yythunkpos
@ -593,7 +595,7 @@ YY_ACTION(void) yy_1_connect_port(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos #define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_connect_port\n")); yyprintf((stderr, "do yy_1_connect_port\n"));
{ {
#line 64 #line 65
yy->connectPortNum = yy->tmpPortNum; yy->connectProto = yy->tmpProto; ; yy->connectPortNum = yy->tmpPortNum; yy->connectProto = yy->tmpProto; ;
} }
#undef yythunkpos #undef yythunkpos
@ -607,7 +609,7 @@ YY_ACTION(void) yy_1_bind_port(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos #define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_bind_port\n")); yyprintf((stderr, "do yy_1_bind_port\n"));
{ {
#line 63 #line 64
yy->bindPortNum = yy->tmpPortNum; yy->bindProto = yy->tmpProto; ; yy->bindPortNum = yy->tmpPortNum; yy->bindProto = yy->tmpProto; ;
} }
#undef yythunkpos #undef yythunkpos
@ -621,7 +623,7 @@ YY_ACTION(void) yy_1_connect_address(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos #define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_connect_address\n")); yyprintf((stderr, "do yy_1_connect_address\n"));
{ {
#line 62 #line 63
yy->connectAddress = strdup(yytext); ; yy->connectAddress = strdup(yytext); ;
} }
#undef yythunkpos #undef yythunkpos
@ -635,7 +637,7 @@ YY_ACTION(void) yy_1_bind_address(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos #define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_bind_address\n")); yyprintf((stderr, "do yy_1_bind_address\n"));
{ {
#line 61 #line 62
yy->bindAddress = strdup(yytext); ; yy->bindAddress = strdup(yytext); ;
} }
#undef yythunkpos #undef yythunkpos
@ -649,7 +651,7 @@ YY_ACTION(void) yy_1_server_rule(yycontext *yy, char *yytext, int yyleng)
#define yythunkpos yy->__thunkpos #define yythunkpos yy->__thunkpos
yyprintf((stderr, "do yy_1_server_rule\n")); yyprintf((stderr, "do yy_1_server_rule\n"));
{ {
#line 52 #line 53
addServer(yy->bindAddress, yy->bindPortNum, yy->bindProto, addServer(yy->bindAddress, yy->bindPortNum, yy->bindProto,
yy->connectAddress, yy->connectPortNum, yy->connectProto, yy->connectAddress, yy->connectPortNum, yy->connectProto,
@ -1346,7 +1348,7 @@ YY_PARSE(yycontext *) YYRELEASE(yycontext *yyctx)
} }
#endif #endif
#line 160 "parse.peg" #line 162 "parse.peg"
void parseConfiguration(char const *file) void parseConfiguration(char const *file)

View File

@ -1,6 +1,6 @@
# Copyright © 1997—1999 Thomas Boutell <boutell@boutell.com> # Copyright © 1997—1999 Thomas Boutell <boutell@boutell.com>
# and Boutell.Com, Inc. # and Boutell.Com, Inc.
# © 2003—2017 Sam Hocevar <sam@hocevar.net> # © 2003—2019 Sam Hocevar <sam@hocevar.net>
# #
# This software is released for free use under the terms of # This software is released for free use under the terms of
# the GNU Public License, version 2 or higher. NO WARRANTY # the GNU Public License, version 2 or higher. NO WARRANTY
@ -22,8 +22,9 @@
int currentLine; \ int currentLine; \
int isAuthAllow; \ int isAuthAllow; \
char *tmpPort; \ char *tmpPort; \
int tmpPortNum, tmpProto; \ uint16_t tmpPortNum, tmpProto; \
int bindPortNum, bindProto, connectPortNum, connectProto; \ uint16_t bindPortNum, connectPortNum; \
int bindProto, connectProto; \
int serverTimeout; \ int serverTimeout; \
char *bindAddress, *connectAddress, *sourceAddress; char *bindAddress, *connectAddress, *sourceAddress;
#define YY_INPUT(yyctx, buf, result, max_size) \ #define YY_INPUT(yyctx, buf, result, max_size) \
@ -73,11 +74,12 @@ full-port = port proto
{ {
char const *proto = yy->tmpProto == protoTcp ? "tcp" : "udp"; char const *proto = yy->tmpProto == protoTcp ? "tcp" : "udp";
struct servent *service = getservbyname(yy->tmpPort, proto); struct servent *service = getservbyname(yy->tmpPort, proto);
yy->tmpPortNum = service ? ntohs(service->s_port) : atoi(yy->tmpPort); int port = service ? ntohs(service->s_port) : atoi(yy->tmpPort);
if (yy->tmpPortNum == 0 || yy->tmpPortNum >= 65536) { if (port <= 0 || port >= 65536) {
syslog(LOG_ERR, "port %s/%s missing or out of range\n", yy->tmpPort, proto); syslog(LOG_ERR, "port %s/%s missing or out of range\n", yy->tmpPort, proto);
PARSE_ERROR; PARSE_ERROR;
} }
yy->tmpPortNum = (uint16_t)port;
} }
port = < (number | service) > { yy->tmpPort = strdup(yytext); } port = < (number | service) > { yy->tmpPort = strdup(yytext); }

View File

@ -1,6 +1,6 @@
/* Copyright © 1997—1999 Thomas Boutell <boutell@boutell.com> /* Copyright © 1997—1999 Thomas Boutell <boutell@boutell.com>
and Boutell.Com, Inc. and Boutell.Com, Inc.
© 20032017 Sam Hocevar <sam@hocevar.net> © 20032019 Sam Hocevar <sam@hocevar.net>
This software is released for free use under the terms of This software is released for free use under the terms of
the GNU Public License, version 2 or higher. NO WARRANTY 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, void addServer(char *bindAddress, uint16_t bindPort, int bindProto,
char *connectAddress, int connectPort, int connectProto, char *connectAddress, uint16_t connectPort, int connectProto,
int serverTimeout, char *sourceAddress) int serverTimeout, char *sourceAddress)
{ {
/* Turn all of this stuff into reasonable addresses */ /* Turn all of this stuff into reasonable addresses */
@ -287,7 +287,7 @@ void addServer(char *bindAddress, int bindPort, int bindProto,
/* Warn -- don't exit. */ /* Warn -- don't exit. */
syslog(LOG_ERR, "couldn't bind to " syslog(LOG_ERR, "couldn't bind to "
"address %s port %d (%m)\n", "address %s port %d (%m)\n",
bindAddress, bindPort); bindAddress, (int)bindPort);
closesocket(fd); closesocket(fd);
exit(1); exit(1);
} }
@ -297,7 +297,7 @@ void addServer(char *bindAddress, int bindPort, int bindProto,
/* Warn -- don't exit. */ /* Warn -- don't exit. */
syslog(LOG_ERR, "couldn't listen to " syslog(LOG_ERR, "couldn't listen to "
"address %s port %d (%m)\n", "address %s port %d (%m)\n",
bindAddress, bindPort); bindAddress, (int)bindPort);
closesocket(fd); closesocket(fd);
} }
@ -988,10 +988,8 @@ static void logEvent(ConnectionInfo const *cnx, ServerInfo const *srv, int resul
bytesInput = cnx->remote.recvBytes; bytesInput = cnx->remote.recvBytes;
} }
char const *fromHost = "?"; char const *fromHost = "?", *toHost = "?";
int fromPort = 0; uint16_t fromPort = 0, toPort = 0;
char const *toHost = "?";
int toPort = 0;
if (srv != NULL) { if (srv != NULL) {
fromHost = srv->fromHost; fromHost = srv->fromHost;
fromPort = srv->fromPort; fromPort = srv->fromPort;
@ -1026,8 +1024,8 @@ static void logEvent(ConnectionInfo const *cnx, ServerInfo const *srv, int resul
sign, sign,
timz / 60, timz / 60,
timz % 60, timz % 60,
fromHost, fromPort, fromHost, (int)fromPort,
toHost, toPort, toHost, (int)toPort,
logMessages[result], logMessages[result],
bytesOutput, bytesOutput,
bytesInput); bytesInput);
@ -1038,8 +1036,8 @@ static void logEvent(ConnectionInfo const *cnx, ServerInfo const *srv, int resul
"\t%d\t%s\n", "\t%d\t%s\n",
tstr, tstr,
addressText, addressText,
fromHost, fromPort, fromHost, (int)fromPort,
toHost, toPort, toHost, (int)toPort,
bytesInput, bytesInput,
bytesOutput, bytesOutput,
logMessages[result]); logMessages[result]);

View File

@ -1,6 +1,6 @@
/* Copyright © 1997—1999 Thomas Boutell <boutell@boutell.com> /* Copyright © 1997—1999 Thomas Boutell <boutell@boutell.com>
and Boutell.Com, Inc. and Boutell.Com, Inc.
© 20032017 Sam Hocevar <sam@hocevar.net> © 20032019 Sam Hocevar <sam@hocevar.net>
This software is released for free use under the terms of This software is released for free use under the terms of
the GNU Public License, version 2 or higher. NO WARRANTY the GNU Public License, version 2 or higher. NO WARRANTY
@ -19,6 +19,8 @@
# include <syslog.h> # include <syslog.h>
#endif /* _WIN32 */ #endif /* _WIN32 */
#include <stdint.h>
/* Constants */ /* Constants */
static int const RINETD_BUFFER_SIZE = 16384; static int const RINETD_BUFFER_SIZE = 16384;
@ -44,7 +46,7 @@ extern FILE *logFile;
/* Functions */ /* Functions */
void addServer(char *bindAddress, int bindPort, int bindProto, void addServer(char *bindAddress, uint16_t bindPort, int bindProto,
char *connectAddress, int connectPort, int connectProto, char *connectAddress, uint16_t connectPort, int connectProto,
int serverTimeout, char *sourceAddress); int serverTimeout, char *sourceAddress);

View File

@ -1,6 +1,6 @@
/* Copyright © 1997—1999 Thomas Boutell <boutell@boutell.com> /* Copyright © 1997—1999 Thomas Boutell <boutell@boutell.com>
and Boutell.Com, Inc. and Boutell.Com, Inc.
© 20032017 Sam Hocevar <sam@hocevar.net> © 20032019 Sam Hocevar <sam@hocevar.net>
This software is released for free use under the terms of This software is released for free use under the terms of
the GNU Public License, version 2 or higher. NO WARRANTY the GNU Public License, version 2 or higher. NO WARRANTY
@ -9,6 +9,7 @@
#pragma once #pragma once
#include <time.h> #include <time.h>
#include <stdint.h>
enum ruleType { enum ruleType {
allowRule, allowRule,
@ -33,12 +34,13 @@ struct _server_info {
/* In network order, for network purposes */ /* In network order, for network purposes */
struct in_addr localAddr; struct in_addr localAddr;
unsigned short localPort; uint16_t localPort;
struct in_addr sourceAddr; struct in_addr sourceAddr;
/* In ASCII and local byte order, for logging purposes */ /* In ASCII and local byte order, for logging purposes */
char *fromHost, *toHost; 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 /* Offset and count into list of allow and deny rules. Any rules
prior to globalAllowRules and globalDenyRules are global rules. */ prior to globalAllowRules and globalDenyRules are global rules. */