Hi,
After this patch was applied I have not been able to enable tethering. Through debugging I
found that function __connman_nat_enable returns the error: "Cannot enable NAT -9/Bad
file descriptor", this error is gotten when parameter ip_forward is trying to be
modified by editing the file with write() fucntion.
Trying to find the reason of the error I added the O_RDWR mode in open() to check if it
was a permissions problem but it did not work and it continue giving the same error. Next,
I tried to come back to fopen() instead of use open() and it worked.
What do you think is happening? What is wrong when file
"/proc/sys/net/ipv4/ip_forward" is handled with the system calls open/read/write
instead of standard functions fopen/fprintf/fscanf?
Best regards,
Jose Blanquicet
-----Original Message-----
From: connman [mailto:connman-bounces@lists.01.org] On Behalf Of Patrik Flykt
Sent: martedì 12 aprile 2016 15:02
To: connman(a)lists.01.org
Subject: [PATCH v2] nat: Remember previous IPv4 forwarding value
When NAT is enabled, store the previous IPv4 forwarding setting so that it can be restored
to its former value when disabling NAT.
---
v2: Fix err = -errno and fix a few conditionals to be more readable
src/nat.c | 40 ++++++++++++++++++++++++++++++----------
1 file changed, 30 insertions(+), 10 deletions(-)
diff --git a/src/nat.c b/src/nat.c
index 063f085..b739e11 100644
--- a/src/nat.c
+++ b/src/nat.c
@@ -25,7 +25,10 @@
#endif
#include <errno.h>
-#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
#include "connman.h"
@@ -42,20 +45,37 @@ struct connman_nat {
static int enable_ip_forward(bool enable) {
- FILE *f;
+ static char value = 0;
+ int f, err = 0;
- f = fopen("/proc/sys/net/ipv4/ip_forward", "r+");
- if (!f)
+ if ((f = open("/proc/sys/net/ipv4/ip_forward", O_CLOEXEC)) < 0)
return -errno;
- if (enable)
- fprintf(f, "1");
- else
- fprintf(f, "0");
+ if (!value) {
+ if (read(f, &value, sizeof(value)) < 0)
+ value = 0;
+ }
- fclose(f);
+ if (enable) {
+ char allow = '1';
- return 0;
+ if (write (f, &allow, sizeof(allow)) < 0)
+ err = -errno;
+ } else {
+ char deny = '0';
+
+ if (value)
+ deny = value;
+
+ if (write(f, &deny, sizeof(deny)) < 0)
+ err = -errno;
+
+ value = 0;
+ }
+
+ close(f);
+
+ return err;
}
static int enable_nat(struct connman_nat *nat)
--
2.8.0.rc3
_______________________________________________
connman mailing list
connman(a)lists.01.org
https://lists.01.org/mailman/listinfo/connman