From patchwork Fri Feb 6 16:56:11 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sven Eckelmann X-Patchwork-Id: 5367 Received: from mail.gmx.net (mail.gmx.net [213.165.64.20]) by open-mesh.net (8.14.3/8.13.4/Debian-3sarge3) with SMTP id n16GvVJC003029 for ; Fri, 6 Feb 2009 16:57:32 GMT Received: (qmail invoked by alias); 06 Feb 2009 16:56:13 -0000 Received: from unknown (EHLO localhost) [88.130.173.182] by mail.gmx.net (mp040) with SMTP; 06 Feb 2009 17:56:13 +0100 X-Authenticated: #15668376 X-Provags-ID: V01U2FsdGVkX1/Y0WJmMFXY2Z77frksey3wvE+UIRLIYulY3D3Wgd ghA4luRn19Mg9s From: Sven Eckelmann To: b.a.t.m.a.n@open-mesh.net Date: Fri, 6 Feb 2009 17:56:11 +0100 Message-Id: <1233939371-23629-1-git-send-email-sven.eckelmann@gmx.de> X-Mailer: git-send-email 1.6.0.6 X-Y-GMX-Trusted: 0 X-FuHaFi: 0.5 Subject: [B.A.T.M.A.N.] [PATCH] Introduce set/get functions for /proc files X-BeenThere: b.a.t.m.a.n@open-mesh.net X-Mailman-Version: 2.1.11 Precedence: list Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking List-Id: The list for a Better Approach To Mobile Ad-hoc Networking List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 06 Feb 2009 16:57:32 -0000 get_integer_file can be used to get an integer from the beginning of a file. It does the same as every other get_* function in linux/kernel.c has done before, but introduced extra error checking for fscanf to deal with the undefined behaviour of it in error cases in c89. It fixes also the problems that in future architectures with sizeof(int) > 4 an invalid write would happen when fscanf tries to write it's results into the target or that on big endian architectures with sizof(int) != 4 the result is complete different than the expected one when the content of the file is not 0. set_integer_file does the same for writing a specific integer to the file and thus resolve the problem of invalid read on architectures with sizof(int) > 4 and wrong file content on big endian systems with sizof(int) != 4. As side effect the probability to get attacked by a goto-raptor was reduced by the factor 1.08. Signed-off-by: Sven Eckelmann --- batman/linux/kernel.c | 89 ++++++++++++++++++++++--------------------------- 1 files changed, 40 insertions(+), 49 deletions(-) diff --git a/batman/linux/kernel.c b/batman/linux/kernel.c index 0595899..203fb33 100644 --- a/batman/linux/kernel.c +++ b/batman/linux/kernel.c @@ -27,6 +27,7 @@ #include #include #include +#include #include "../os.h" #include "../batman.h" @@ -36,9 +37,41 @@ +static int get_integer_file(const char* filename) +{ + FILE *f; + int32_t integer = 0; + int n; + + if((f = fopen(filename, "r")) == NULL) + return 0; + + n = fscanf(f, "%"SCNd32, &integer); + fclose(f); + + if (n == 0 || n == EOF) + integer = 0; + + return integer; +} + + + +static void set_integer_file(const char* filename, int32_t integer) +{ + FILE *f; + + if ((f = fopen(filename, "w")) == NULL) + return; + + fprintf(f, "%"PRId32, integer); + fclose(f); +} + + + void set_rp_filter(int32_t state, char* dev) { - FILE *f; char filename[100], *colon_ptr; /* if given interface is an alias use parent interface */ @@ -46,14 +79,8 @@ void set_rp_filter(int32_t state, char* dev) *colon_ptr = '\0'; sprintf( filename, "/proc/sys/net/ipv4/conf/%s/rp_filter", dev); + set_integer_file(filename, state); - if((f = fopen(filename, "w")) == NULL) - goto end; - - fprintf(f, "%d", state); - fclose(f); - -end: if ( colon_ptr != NULL ) *colon_ptr = ':'; } @@ -62,7 +89,6 @@ end: int32_t get_rp_filter(char *dev) { - FILE *f; int32_t state = 0; char filename[100], *colon_ptr; @@ -71,14 +97,8 @@ int32_t get_rp_filter(char *dev) *colon_ptr = '\0'; sprintf( filename, "/proc/sys/net/ipv4/conf/%s/rp_filter", dev); + state = get_integer_file(filename); - if((f = fopen(filename, "r")) == NULL) - goto end; - - fscanf(f, "%d", &state); - fclose(f); - -end: if ( colon_ptr != NULL ) *colon_ptr = ':'; @@ -89,7 +109,6 @@ end: void set_send_redirects( int32_t state, char* dev ) { - FILE *f; char filename[100], *colon_ptr; /* if given interface is an alias use parent interface */ @@ -97,14 +116,8 @@ void set_send_redirects( int32_t state, char* dev ) { *colon_ptr = '\0'; sprintf( filename, "/proc/sys/net/ipv4/conf/%s/send_redirects", dev); + set_integer_file(filename, state); - if((f = fopen(filename, "w")) == NULL) - goto end; - - fprintf(f, "%d", state); - fclose(f); - -end: if ( colon_ptr != NULL ) *colon_ptr = ':'; @@ -114,7 +127,6 @@ end: int32_t get_send_redirects( char *dev ) { - FILE *f; int32_t state = 0; char filename[100], *colon_ptr; @@ -123,14 +135,8 @@ int32_t get_send_redirects( char *dev ) { *colon_ptr = '\0'; sprintf( filename, "/proc/sys/net/ipv4/conf/%s/send_redirects", dev); + state = get_integer_file(filename); - if((f = fopen(filename, "r")) == NULL) - goto end; - - fscanf(f, "%d", &state); - fclose(f); - -end: if ( colon_ptr != NULL ) *colon_ptr = ':'; @@ -142,29 +148,14 @@ end: void set_forwarding(int32_t state) { - FILE *f; - - if((f = fopen("/proc/sys/net/ipv4/ip_forward", "w")) == NULL) - return; - - fprintf(f, "%d", state); - fclose(f); + set_integer_file("/proc/sys/net/ipv4/ip_forward", state); } int32_t get_forwarding(void) { - FILE *f; - int32_t state = 0; - - if((f = fopen("/proc/sys/net/ipv4/ip_forward", "r")) == NULL) - return 0; - - fscanf(f, "%d", &state); - fclose(f); - - return state; + return get_integer_file("/proc/sys/net/ipv4/ip_forward"); }