]> mj.ucw.cz Git - moe.git/blobdiff - submit/connect.c
MO-P: Public parts of /mo include templates
[moe.git] / submit / connect.c
index 7f5709ea2616b7d5ee1992143418e8ee953ae85f..a15f17f7b2bb8e6b329b07c5f9e33367e5c282db 100644 (file)
@@ -4,7 +4,7 @@
  *  (c) 2007 Martin Mares <mj@ucw.cz>
  */
 
-#include "lib/lib.h"
+#include "ucw/lib.h"
 
 #include <stdio.h>
 #include <string.h>
@@ -26,7 +26,7 @@ tls_init(void)
 {
   int err;
 
-  log(L_INFO, "Initializing TLS");
+  msg(L_INFO, "Initializing TLS");
   gnutls_global_init();
   err = gnutls_certificate_allocate_credentials(&cert_cred);
   TLS_CHECK(gnutls_certificate_allocate_credentials);
@@ -74,7 +74,7 @@ tls_verify_cert(gnutls_session_t s)
   /* XXX: Neither we check host name */
 
   /* Check certificate purpose */
-  byte purp[256];
+  char purp[256];
   int purpi = 0;
   do
     {
@@ -100,7 +100,7 @@ tls_log_params(gnutls_session_t s)
   const char *comp = gnutls_compression_get_name(gnutls_compression_get(s));
   const char *cipher = gnutls_cipher_get_name(gnutls_cipher_get(s));
   const char *mac = gnutls_mac_get_name(gnutls_mac_get(s));
-  log(L_DEBUG, "TLS params: proto=%s kx=%s cert=%s comp=%s cipher=%s mac=%s",
+  msg(L_DEBUG, "TLS params: proto=%s kx=%s cert=%s comp=%s cipher=%s mac=%s",
     proto, kx, cert, comp, cipher, mac);
 }
 
@@ -112,7 +112,7 @@ int main(int argc UNUSED, char **argv UNUSED)
   if (sk < 0)
     die("socket: %m");
 
-  log(L_INFO, "Connecting to port %d", port);
+  msg(L_INFO, "Connecting to port %d", port);
   struct sockaddr_in sa;
   bzero(&sa, sizeof(sa));
   sa.sin_family = AF_INET;
@@ -121,37 +121,71 @@ int main(int argc UNUSED, char **argv UNUSED)
   if (connect(sk, (struct sockaddr *) &sa, sizeof(sa)) < 0)
     die("Cannot connect: %m");
 
+  msg(L_INFO, "Waiting for initial message");
+  char mesg[256];
+  int i = 0;
+  do
+    {
+      if (i >= (int)sizeof(mesg))
+       die("Response too long");
+      int c = read(sk, mesg+i, sizeof(mesg)-i);
+      if (c <= 0)
+       die("Connection broken");
+      i += c;
+    }
+  while (mesg[i-1] != '\n');
+  mesg[i-1] = 0;
+  if (mesg[0] != '+')
+    die("%s", mesg);
+  msg(L_INFO, "%s", mesg);
+
   gnutls_session_t s;
   gnutls_init(&s, GNUTLS_CLIENT);
   gnutls_set_default_priority(s);
   gnutls_credentials_set(s, GNUTLS_CRD_CERTIFICATE, cert_cred);
   gnutls_transport_set_ptr(s, (gnutls_transport_ptr_t) sk);
 
-  log(L_INFO, "Handshaking");
+  msg(L_INFO, "Handshaking");
   int err = gnutls_handshake(s); TLS_CHECK(gnutls_handshake);
   tls_log_params(s);
   const char *cert_err = tls_verify_cert(s);
   if (cert_err)
     die("Certificate verification failed: %s", cert_err);
 
-  log(L_INFO, "Session established");
+  msg(L_INFO, "Session established");
   for (;;)
     {
-      byte buf[1024];
-      if (!fgets(buf, sizeof(buf), stdin))
-       break;
-      int len = strlen(buf);
-      err = gnutls_record_send(s, buf, len); TLS_CHECK(gnutls_record_send);
-      err = gnutls_record_recv(s, buf, len); TLS_CHECK(gnutls_record_recv);
-      if (!err)
+      char buf[1024];
+      do
+       {
+         if (!fgets(buf, sizeof(buf), stdin))
+           goto done;
+         int len = strlen(buf);
+         err = gnutls_record_send(s, buf, len); TLS_CHECK(gnutls_record_send);
+       }
+      while (buf[0] != '\n');
+      int last = 0;
+      for (;;)
        {
-         log(L_INFO, "Connection closed");
-         break;
+         err = gnutls_record_recv(s, buf, sizeof(buf)); TLS_CHECK(gnutls_record_recv);
+         if (!err)
+           {
+             msg(L_INFO, "Connection closed");
+             break;
+           }
+         fwrite(buf, 1, err, stdout);
+         for (int i=0; i<err; i++)
+           {
+             if (buf[i] == '\n' && last == '\n')
+               goto next;
+             last = buf[i];
+           }
        }
-      fwrite(buf, 1, err, stdout);
+next:
       fflush(stdout);
     }
 
+done:
   gnutls_bye(s, GNUTLS_SHUT_RDWR);
   close(sk);
   gnutls_deinit(s);