VPN reconnection fails after manually disabling VPN

When disabling VPN manually, there was no trigger for ipsec-tools to
send “delete message” to VPN server. Therefore, connection information
is left in VPN server and next connection fails.

Fix this issue as below:
- Add “delete message” sending via flush in ipsec-tools when racoon
  daemon stops
- Keep daemon alive when VPN.java exit() to let it finish sending to
  VPN server
- Since daemon will stop itself when monitor socket is closed, replace
  stop(daemon) with close(socket); thus remove redundant socket closing
  in VPN exit()

(cherry picked from commit cb1e9555c0938513dc78b35f3f6e661848b0de9a)

Bug: 28279646
Bug: 33467086
Change-Id: I64591874a1de5d0dd4b5834211d07effb962cd6b
diff --git a/main.c b/main.c
index a504846..d7f10e0 100644
--- a/main.c
+++ b/main.c
@@ -126,6 +126,7 @@
 #endif
 
 extern void setup(int argc, char **argv);
+extern void shutdown_session();
 
 static int monitors;
 static void (*callbacks[10])(int fd);
@@ -202,6 +203,8 @@
             for (i = 0; i < monitors; ++i) {
                 if (pollfds[i].revents & POLLHUP) {
                     do_plog(LLV_INFO, "Connection is closed\n", pollfds[i].fd);
+                    shutdown_session();
+
                     /* Wait for few seconds to consume late messages. */
                     sleep(5);
                     exit(1);
diff --git a/setup.c b/setup.c
index 69b5b70..fce9bf4 100644
--- a/setup.c
+++ b/setup.c
@@ -53,6 +53,7 @@
 #include "privsep.h"
 #include "throttle.h"
 #include "misc.h"
+#include "handler.h"
 
 static struct localconf localconf;
 static struct sainfo sainfo;
@@ -671,3 +672,11 @@
 {
     return 0;
 }
+
+void shutdown_session()
+{
+    flushph2();
+    flushph1();
+    isakmp_close();
+    pfkey_close(localconf.sock_pfkey);
+}