+int startNet2Net(char *name) {
+ connection *conn = NULL;
+ connection *conn_iter;
+
+ conn_iter = getConnections();
+
+ while (conn_iter) {
+ if ((strcmp(conn_iter->type, "net") == 0) && (strcmp(conn_iter->name, name) == 0)) {
+ conn = conn_iter;
+ break;
+ }
+ conn_iter = conn_iter->next;
+ }
+
+ if (conn == NULL) {
+ fprintf(stderr, "Connection not found.\n");
+ return 1;
+ }
+
+ if (strcmp(conn->status, "on") != 0) {
+ fprintf(stderr, "Connection '%s' is not enabled.\n", conn->name);
+ return 1;
+ }
+
+ fprintf(stderr, "Starting connection %s...\n", conn->name);
+
+ char configfile[STRING_SIZE];
+ snprintf(configfile, STRING_SIZE - 1, CONFIG_ROOT "/ovpn/n2nconf/%s/%s.conf",
+ conn->name, conn->name);
+
+ FILE *fp = fopen(configfile, "r");
+ if (fp == NULL) {
+ fprintf(stderr, "Could not find configuration file for connection '%s' at '%s'.\n",
+ conn->name, configfile);
+ return 2;
+ }
+ fclose(fp);
+
+ // Make sure all firewall rules are up to date.
+ setFirewallRules();
+
+ // Get the external IP address.
+ char address[STRING_SIZE] = "";
+ int r = readExternalAddress(address);
+ if (r) {
+ fprintf(stderr, "Could not read the external address\n");
+ exit(1);
+ }
+
+ char command[STRING_SIZE];
+ snprintf(command, STRING_SIZE-1, "/sbin/modprobe tun");
+ executeCommand(command);
+ snprintf(command, STRING_SIZE-1, "/usr/sbin/openvpn --local %s --config %s", address, configfile);
+ executeCommand(command);
+
+ return 0;
+}
+
+int killNet2Net(char *name) {
+ connection *conn = NULL;
+ connection *conn_iter;
+
+ conn_iter = getConnections();
+
+ while (conn_iter) {
+ if (strcmp(conn_iter->name, name) == 0) {
+ conn = conn_iter;
+ break;
+ }
+ conn_iter = conn_iter->next;
+ }
+
+ if (conn == NULL) {
+ fprintf(stderr, "Connection not found.\n");
+ return 1;
+ }
+
+ char pidfile[STRING_SIZE];
+ snprintf(pidfile, STRING_SIZE - 1, "/var/run/%sn2n.pid", conn->name);
+
+ int pid = readPidFile(pidfile);
+ if (!pid > 0) {
+ fprintf(stderr, "Could not read pid file of connection %s.", conn->name);
+ return 1;
+ }
+
+ fprintf(stderr, "Killing connection %s (PID %d)...\n", conn->name, pid);
+ kill(pid, SIGTERM);
+
+ char command[STRING_SIZE];
+ snprintf(command, STRING_SIZE - 1, "/bin/rm -f %s", pidfile);
+ executeCommand(command);
+
+ return 0;
+}
+
+void startAllNet2Net() {
+ int exitcode = 0, _exitcode = 0;
+
+ connection *conn = getConnections();
+
+ while(conn) {
+ /* Skip all connections that are not of type "net" or disabled. */
+ if ((strcmp(conn->type, "net") != 0) || (strcmp(conn->status, "on") != 0)) {
+ conn = conn->next;
+ continue;
+ }
+
+ _exitcode = startNet2Net(conn->name);
+ conn = conn->next;
+
+ if (_exitcode > exitcode) {
+ exitcode = _exitcode;
+ }
+ }
+
+ exit(exitcode);
+}
+
+void killAllNet2Net() {
+ int exitcode = 0, _exitcode = 0;
+
+ connection *conn = getConnections();
+
+ while(conn) {
+ /* Skip all connections that are not of type "net". */
+ if (strcmp(conn->type, "net") != 0) {
+ conn = conn->next;
+ continue;
+ }
+
+ _exitcode = killNet2Net(conn->name);
+ conn = conn->next;
+
+ if (_exitcode > exitcode) {
+ exitcode = _exitcode;
+ }
+ }
+
+ exit(exitcode);
+}
+