File: 04-upstream-multiple-wan-interfaces.diff

package info (click to toggle)
linux-igd 1.0%2Bcvs20070630-5
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 888 kB
  • ctags: 618
  • sloc: ansic: 2,066; xml: 593; sh: 136; makefile: 50
file content (411 lines) | stat: -rw-r--r-- 17,694 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
Description: Allow multiple WAN interfaces (untested)
Origin: upstream

diff -ruN linuxigd-cvs/arcoscom_extra.c linuxigd-cvs.3/arcoscom_extra.c
--- linuxigd-cvs/arcoscom_extra.c	1970-01-01 03:00:00.000000000 +0300
+++ linuxigd-cvs.3/arcoscom_extra.c	2008-01-03 19:11:02.000000000 +0300
@@ -0,0 +1,133 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <net/if.h>
+
+#include "globals.h"
+#include "util.h"
+
+void initGlobals( globals_p ioGlobales ) {
+   memset( ( void * ) ioGlobales, sizeof( struct GLOBALS ), 0 );
+}
+
+int parseCommandLine(int argc, char ** argv, globals_p ioGlobales ) {
+   int ret = 0;
+   register int argc_count;
+   int parameters_without_switches = 0;
+   int lan_switch_found = 0;
+   int wan_switch_found = 0;
+   
+   for( argc_count = 1; argc_count < argc && !ret; ++argc_count ) {
+      if( !strcmp( argv[ argc_count ], "-f" ) ) { // foreground parameter
+         lan_switch_found = 0;
+         wan_switch_found = 0;
+         parameters_without_switches = 0;
+		   ioGlobales->foreground = 1;
+      }
+      else if( !strcmp( argv[ argc_count ], "-L" ) ) { // LAN interfaces parameter
+         lan_switch_found = 1;
+         wan_switch_found = 0;
+         parameters_without_switches = 0;
+      }
+      else if( !strcmp( argv[ argc_count ], "-W" ) ) { // WAN interfaces parameter
+         lan_switch_found = 0;
+         wan_switch_found = 1;
+         parameters_without_switches = 0;
+      }
+      else {
+         if( wan_switch_found ) { // It's a WAN interface name
+            if( 0 == ioGlobales->extCount ) {
+               strncpy( ioGlobales->extInterfaceName, argv[ argc_count ], IFNAMSIZ );
+            }
+            if( ioGlobales->extCount < MAX_WAN_IFACES ) {
+               strncpy( ioGlobales->extInterfaces[ ioGlobales->extCount ], argv[ argc_count ], IFNAMSIZ );
+               ioGlobales->extCount++;
+            }
+         }
+         else if( lan_switch_found ) { // It's a LAN interface name
+            if( 0 == ioGlobales->intCount ) {
+               strncpy( ioGlobales->intInterfaceName, argv[ argc_count ], IFNAMSIZ );
+               // Get the internal ip address to start the daemon on
+               if( 0 == GetIpAddressStr(ioGlobales->intIpAddress, ioGlobales->intInterfaceName) ) {
+                  fprintf(stderr, "Invalid internal interface name '%s'\n", ioGlobales->intInterfaceName);
+                  ret = EXIT_FAILURE;
+               }
+            }
+            if( ioGlobales->intCount < MAX_WAN_IFACES ) {
+               strncpy( ioGlobales->intInterfaces[ ioGlobales->intCount ], argv[ argc_count ], IFNAMSIZ );
+               // Get the internal ip address to start the daemon on
+               if( 0 == GetIpAddressStr( 
+                           ioGlobales->intIpAddresses[ ioGlobales->intCount ],
+                           ioGlobales->intInterfaces[ ioGlobales->intCount ]
+                           )
+                 ) {
+                  fprintf(stderr, "Invalid internal interface name '%s'\n", ioGlobales->intInterfaceName);
+                  ret = EXIT_FAILURE;
+               }
+               ioGlobales->intCount++;
+            }
+         }
+         else {
+            switch( parameters_without_switches ) {
+               case 0: // Obsolete WAN interface
+                  if( 0 == ioGlobales->extCount ) {
+                     strncpy( ioGlobales->extInterfaceName, argv[ argc_count ], IFNAMSIZ );
+                  }
+                  if( ioGlobales->extCount < MAX_WAN_IFACES ) {
+                     strncpy( ioGlobales->extInterfaces[ ioGlobales->extCount ], argv[ argc_count ], IFNAMSIZ );
+                     ioGlobales->extCount++;
+                  }
+                  break;
+               case 1: // Obsolete LAN interface
+                  if( 0 == ioGlobales->intCount ) {
+                     strncpy( ioGlobales->intInterfaceName, argv[ argc_count ], IFNAMSIZ );
+	                  // Get the internal ip address to start the daemon on
+	                  if( 0 == GetIpAddressStr(ioGlobales->intIpAddress, ioGlobales->intInterfaceName) ) {
+		                  fprintf(stderr, "Invalid internal interface name '%s'\n", ioGlobales->intInterfaceName);
+		                  ret = EXIT_FAILURE;
+	                  }
+                  }
+                  if( ioGlobales->intCount < MAX_LAN_IFACES ) {
+                     strncpy( ioGlobales->intInterfaces[ ioGlobales->intCount ], argv[ argc_count ], IFNAMSIZ );
+	                  // Get the internal ip address to start the daemon on
+	                  if( 0 == GetIpAddressStr( 
+	                              ioGlobales->intIpAddresses[ ioGlobales->intCount ],
+	                              ioGlobales->intInterfaces[ ioGlobales->intCount ]
+	                              )
+	                    ) {
+		                  fprintf(stderr, "Invalid internal interface name '%s'\n", ioGlobales->intInterfaceName);
+		                  ret = EXIT_FAILURE;
+	                  }
+                     ioGlobales->intCount++;
+                  }
+                  break;
+               default:
+                  ret = 1;
+                  break;
+            }
+         }
+         parameters_without_switches++;
+      }
+   }   
+  
+   if( !ioGlobales->intCount || !ioGlobales->extCount ) {
+      fprintf(stderr, "Parameters error found.\n", ioGlobales->intInterfaceName);
+      ret = 1;
+   }
+   
+   return ret;
+}
+
+void printUsage( void ) {
+   printf(
+      "SINGLE WAN INTERFACE USAGE:\n"
+      "   upnpd [-f] <external ifname> <internal ifname>\n"
+      "MULTIPLE WAN INTERFACE USAGE (subject to change):\n"
+      "   upnpd [-f] -W <external ifname list> -L <internal ifname list>\n"
+      "\n"
+      "  -f\tdon't daemonize\n"
+      "Examples:\n"
+      "   upnpd ppp0 eth0\n"
+      "   upnpd -W ppp0 ppp1 -L eth0\n"
+      );
+}
--- linuxigd-cvs/arcoscom_extra.h	1970-01-01 03:00:00.000000000 +0300
+++ linuxigd-cvs.3/arcoscom_extra.h	2008-01-03 19:11:02.000000000 +0300
@@ -0,0 +1,10 @@
+#ifndef _ARCOSCOM_EXTRA_H_
+#define _ARCOSCOM_EXTRA_H_
+
+#include "globals.h"
+
+void initGlobals( globals_p ioGlobales );
+int parseCommandLine(int argc, char ** argv, globals_p ioGlobales );
+void printUsage( void );
+
+#endif // _ARCOSCOM_EXTRA_H_
--- linuxigd-cvs/CHANGES	2008-01-03 18:40:37.000000000 +0300
+++ linuxigd-cvs.3/CHANGES	2008-01-03 19:11:02.000000000 +0300
@@ -1,3 +1,10 @@
+2007-10-23 Samuel Daz Garca <samueldg@arcoscom.com>
+  * Multiple WAN interfaces: First attempt.
+
+  * Prepared code for multiple LAN interfaces.
+
+  * Changed command line arguments (with backward compatibility).
+
 2007-06-30 Magnus Hyllander <mhyllander@users.sourceforge.net>
   * Added the listenport option, which lets you select which UPnP port
     to listen to. The port number is passed to UpnpInit when
--- linuxigd-cvs/etc/upnpd.rc	2006-08-16 03:34:56.000000000 +0400
+++ linuxigd-cvs.3/etc/upnpd.rc	2008-01-03 19:11:02.000000000 +0300
@@ -34,7 +34,7 @@
 	if [ ! -f /var/lock/subsys/upnpd ]; then
 	    echo -n $"Starting $prog: "
 	    [ "$ALLOW_MULTICAST" != "no" ] && route add -net 239.0.0.0 netmask 255.0.0.0 $INTIFACE
-	    daemon "$UPNPD" $EXTIFACE $INTIFACE
+	    daemon "$UPNPD" -W $EXTIFACE -L $INTIFACE
 	    RETVAL=$?
 	    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/upnpd
 	    echo
--- linuxigd-cvs/globals.h	2008-01-03 18:40:40.000000000 +0300
+++ linuxigd-cvs.3/globals.h	2008-01-03 19:11:02.000000000 +0300
@@ -6,17 +6,23 @@
 #define CHAIN_NAME_LEN 32
 #define BITRATE_LEN 32
 #define PATH_LEN 64
-#define RESULT_LEN 512
+#define RESULT_LEN 1024
 #define NUM_LEN 32
+#define MAX_IFACES 8
+#define MAX_LAN_IFACES MAX_IFACES
+#define MAX_WAN_IFACES MAX_IFACES
 
 #ifndef min
 #define min(a,b) ((a) < (b) ? (a) : (b))
 #endif
 
+typedef char T_INTERFACENAME[ IFNAMSIZ ]; // Interface name type.
+
 struct GLOBALS {
-  char extInterfaceName[IFNAMSIZ]; // The name of the external interface, picked up from the
+  T_INTERFACENAME extInterfaceName; // The name of the external interface, picked up from the
                                    // command line
-  char intInterfaceName[IFNAMSIZ]; // The name of the internal interface, picked from command line
+  T_INTERFACENAME intInterfaceName; // The name of the internal interface, picked from command line
+   char intIpAddress[16];     // Server internal ip address
 
   // All vars below are read from /etc/upnpd.conf in main.c
   int debug;  // 1 - print debug messages to syslog
@@ -36,6 +42,15 @@
   char descDocName[PATH_LEN];
   char xmlPath[PATH_LEN];
   int listenport;	//The port to listen on
+
+// Now we can use lists for external and/or internal interfaces with diferent params  
+  T_INTERFACENAME extInterfaces[ MAX_WAN_IFACES ]; // Pointer to external interfaces names array.
+  int extCount; // Number of external interfaces. Must be <= MAX_WAN_IFACES
+  T_INTERFACENAME intInterfaces[ MAX_LAN_IFACES ]; // Pointer to internal interfaces names array.
+  char intIpAddresses[ MAX_LAN_IFACES ][ 16 ];     // Server internal ip address
+  int intCount; // Number of internal interfaces. Must be <= MAX_LAN_IFACES
+
+  int foreground;
 };
 
 typedef struct GLOBALS* globals_p;
--- linuxigd-cvs/main.c	2008-01-03 18:40:41.000000000 +0300
+++ linuxigd-cvs.3/main.c	2008-01-03 19:11:02.000000000 +0300
@@ -15,6 +15,7 @@
 #include "gatedevice.h"
 #include "util.h"
 #include "pmlist.h"
+#include "arcoscom_extra.h"
 
 // Global variables
 globals g_vars;
@@ -22,36 +23,19 @@
 int main (int argc, char** argv)
 {
 	char descDocUrl[7+15+1+5+1+sizeof(g_vars.descDocName)+1]; // http://ipaddr:port/docName<null>
-	char intIpAddress[16];     // Server internal ip address
 	sigset_t sigsToCatch;
 	int ret, signum, arg = 1, foreground = 0;
+	
+	initGlobals( &g_vars );
 
-	if (argc < 3 || argc > 4) {
-	  printf("Usage: upnpd [-f] <external ifname> <internal ifname>\n");
-	  printf("  -f\tdon't daemonize\n");
-	  printf("Example: upnpd ppp0 eth0\n");
-	  exit(0);
-	}
+   if( parseCommandLine( argc, argv, &g_vars ) ) {
+      printUsage();
+      exit( 0 );
+   }
 
 	parseConfigFile(&g_vars);
 
-	// check for '-f' option
-	if (strcmp(argv[arg], "-f") == 0) {
-		foreground = 1;
-		arg++;
-	}
-
-	// Save interface names for later use
-	strncpy(g_vars.extInterfaceName, argv[arg++], IFNAMSIZ);
-	strncpy(g_vars.intInterfaceName, argv[arg++], IFNAMSIZ);
-
-	// Get the internal ip address to start the daemon on
-	if (GetIpAddressStr(intIpAddress, g_vars.intInterfaceName) == 0) {
-		fprintf(stderr, "Invalid internal interface name '%s'\n", g_vars.intInterfaceName);
-		exit(EXIT_FAILURE);
-	}
-
-	if (!foreground) {
+	if (!g_vars.foreground) {
 		struct rlimit resourceLimit = { 0, 0 };
 		pid_t pid, sid;
 		unsigned int i;
@@ -114,9 +98,9 @@
 
 	// Initialize UPnP SDK on the internal Interface
 	trace(3, "Initializing UPnP SDK ... ");
-	if ( (ret = UpnpInit(intIpAddress,g_vars.listenport) ) != UPNP_E_SUCCESS)
+	if ( (ret = UpnpInit(g_vars.intIpAddress,g_vars.listenport) ) != UPNP_E_SUCCESS)
 	{
-		syslog (LOG_ERR, "Error Initializing UPnP SDK on IP %s port %d",intIpAddress,g_vars.listenport);
+		syslog (LOG_ERR, "Error Initializing UPnP SDK on IP %s port %d",g_vars.intIpAddress,g_vars.listenport);
 		syslog (LOG_ERR, "  UpnpInit returned %d", ret);
 		UpnpFinish();
 		exit(1);
--- linuxigd-cvs/Makefile	2008-01-03 18:40:38.000000000 +0300
+++ linuxigd-cvs.3/Makefile	2008-01-03 19:11:02.000000000 +0300
@@ -6,7 +6,7 @@
 CC=gcc
 INCLUDES=
 LIBS= -lpthread -lupnp -lixml -lthreadutil
-FILES= main.o gatedevice.o pmlist.o util.o config.o
+FILES= arcoscom_extra.o main.o gatedevice.o pmlist.o util.o config.o
 
 CFLAGS += -Wall -g -O2
 
--- linuxigd-cvs/pmlist.c	2008-01-03 18:40:41.000000000 +0300
+++ linuxigd-cvs.3/pmlist.c	2008-01-03 19:12:12.000000000 +0300
@@ -233,9 +233,12 @@
 {
     if (enabled)
     {
+      register int external_iface_count;
+
       char dest[DEST_LEN];
       snprintf(dest, DEST_LEN, "%s:%s", internalClient, internalPort);
 
+      for( external_iface_count = 0; external_iface_count < g_vars.extCount; ++external_iface_count ) {
 #if HAVE_LIBIPTC
 	char *buffer = malloc(strlen(internalClient) + strlen(internalPort) + 2);
 	if (buffer == NULL) {
@@ -251,17 +254,17 @@
 	  iptc_add_rule("filter", g_vars.forwardChainName, protocol, NULL, NULL, NULL, internalClient, NULL, internalPort, "ACCEPT", NULL, g_vars.forwardRulesAppend ? TRUE : FALSE);
 	}
 	trace(3, "iptc_add_rule %s %s %s %s %s %s %s %s",
-	      "nat", g_vars.preroutingChainName, protocol, g_vars.extInterfaceName, externalPort, "DNAT", dest, "APPEND");
-	iptc_add_rule("nat", g_vars.preroutingChainName, protocol, g_vars.extInterfaceName, NULL, NULL, NULL, NULL, externalPort, "DNAT", dest, TRUE);
+	      "nat", g_vars.preroutingChainName, protocol, g_vars.extInterfaces[ external_iface_count ], externalPort, "DNAT", dest, "APPEND");
+	iptc_add_rule("nat", g_vars.preroutingChainName, protocol, g_vars.extInterfaces[ external_iface_count ], NULL, NULL, NULL, NULL, externalPort, "DNAT", dest, TRUE);
 #else
 	int status;
 	
 	if (g_vars.createForwardRules)
 	{
-	  char *args[] = {g_vars.iptables, g_vars.forwardRulesAppend ? "-A" : "-I", g_vars.forwardChainName, "-p", protocol, "-d", internalClient, "--dport", internalPort, "-j", "ACCEPT", NULL};
+	  char *args[] = {g_vars.iptables, g_vars.forwardRulesAppend ? "-A" : "-I", g_vars.forwardChainName, "-i", g_vars.extInterfaces[ external_iface_count ], "-p", protocol, "-d", internalClient, "--dport", internalPort, "-j", "ACCEPT", NULL};
 	  
-	  trace(3, "%s %s %s -p %s -d %s --dport %s -j ACCEPT", 
-		g_vars.iptables,g_vars.forwardRulesAppend ? "-A" : "-I",g_vars.forwardChainName, protocol, internalClient, internalPort);
+	  trace(3, "%s %s %s -i %s -p %s -d %s --dport %s -j ACCEPT", 
+		g_vars.iptables,g_vars.forwardRulesAppend ? "-A" : "-I",g_vars.forwardChainName, g_vars.extInterfaces[ external_iface_count ], protocol, internalClient, internalPort);
 	  if (!fork()) {
 	    int rc = execv(g_vars.iptables, args);
 	    exit(rc);
@@ -271,10 +274,10 @@
 	}
 
 	{
-	  char *args[] = {g_vars.iptables, "-t", "nat", "-A", g_vars.preroutingChainName, "-i", g_vars.extInterfaceName, "-p", protocol, "--dport", externalPort, "-j", "DNAT", "--to", dest, NULL};
+	  char *args[] = {g_vars.iptables, "-t", "nat", "-A", g_vars.preroutingChainName, "-i", g_vars.extInterfaces[ external_iface_count ], "-p", protocol, "--dport", externalPort, "-j", "DNAT", "--to", dest, NULL};
 
 	  trace(3, "%s -t nat -A %s -i %s -p %s --dport %s -j DNAT --to %s", 
-		g_vars.iptables, g_vars.preroutingChainName, g_vars.extInterfaceName, protocol, externalPort, dest);
+		g_vars.iptables, g_vars.preroutingChainName, g_vars.extInterfaces[ external_iface_count ], protocol, externalPort, dest);
 	  if (!fork()) {
 	    int rc = execv(g_vars.iptables, args);
 	    exit(rc);
@@ -283,6 +286,7 @@
 	  }
 	}
 #endif
+      } // for
     }
     return 1;
 }
@@ -291,13 +295,15 @@
 {
     if (enabled)
     {
+      register int external_iface_count;
       char dest[DEST_LEN];
       snprintf(dest, DEST_LEN, "%s:%s", internalClient, internalPort);
 
+      for( external_iface_count = 0; external_iface_count < g_vars.extCount; ++external_iface_count ) {
 #if HAVE_LIBIPTC
 	trace(3, "iptc_delete_rule %s %s %s %s %s %s %s",
-	      "nat", g_vars.preroutingChainName, protocol, g_vars.extInterfaceName, externalPort, "DNAT", dest);
-	iptc_delete_rule("nat", g_vars.preroutingChainName, protocol, g_vars.extInterfaceName, NULL, NULL, NULL, NULL, externalPort, "DNAT", dest);
+	      "nat", g_vars.preroutingChainName, protocol, g_vars.extInterfaces[ external_iface_count ], externalPort, "DNAT", dest);
+	iptc_delete_rule("nat", g_vars.preroutingChainName, protocol, g_vars.extInterfaces[ external_iface_count ], NULL, NULL, NULL, NULL, externalPort, "DNAT", dest);
 	if (g_vars.createForwardRules)
 	{
 	  trace(3, "iptc_delete_rule %s %s %s %s %s %s",
@@ -308,10 +314,10 @@
 	int status;
 	
 	{
-	  char *args[] = {g_vars.iptables, "-t", "nat", "-D", g_vars.preroutingChainName, "-i", g_vars.extInterfaceName, "-p", protocol, "--dport", externalPort, "-j", "DNAT", "--to", dest, NULL};
+	  char *args[] = {g_vars.iptables, "-t", "nat", "-D", g_vars.preroutingChainName, "-i", g_vars.extInterfaces[ external_iface_count ], "-p", protocol, "--dport", externalPort, "-j", "DNAT", "--to", dest, NULL};
 
 	  trace(3, "%s -t nat -D %s -i %s -p %s --dport %s -j DNAT --to %s",
-		g_vars.iptables, g_vars.preroutingChainName, g_vars.extInterfaceName, protocol, externalPort, dest);
+		g_vars.iptables, g_vars.preroutingChainName, g_vars.extInterfaces[ external_iface_count ], protocol, externalPort, dest);
 	  
 	  if (!fork()) {
 	    int rc = execv(g_vars.iptables, args);
@@ -323,10 +329,10 @@
 
 	if (g_vars.createForwardRules)
 	{
-	  char *args[] = {g_vars.iptables, "-D", g_vars.forwardChainName, "-p", protocol, "-d", internalClient, "--dport", internalPort, "-j", "ACCEPT", NULL};
+	  char *args[] = {g_vars.iptables, "-D", g_vars.forwardChainName, "-i", g_vars.extInterfaces[ external_iface_count ], "-p", protocol, "-d", internalClient, "--dport", internalPort, "-j", "ACCEPT", NULL};
 	  
-	  trace(3, "%s -D %s -p %s -d %s --dport %s -j ACCEPT",
-		g_vars.iptables, g_vars.forwardChainName, protocol, internalClient, internalPort);
+	  trace(3, "%s -D %s -i %s -p %s -d %s --dport %s -j ACCEPT",
+		g_vars.iptables, g_vars.forwardChainName, g_vars.extInterfaces[ external_iface_count ], protocol, internalClient, internalPort);
 	  if (!fork()) {
 	    int rc = execv(g_vars.iptables, args);
 	    exit(rc);
@@ -335,6 +341,7 @@
 	  }
 	}
 #endif
+      } // for
     }
     return 1;
 }