Inclui pequeno cliente de teste.
authorThadeu Lima de Souza Cascardo <cascardo@cascardo.info>
Thu, 2 May 2013 21:29:41 +0000 (18:29 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@cascardo.info>
Thu, 2 May 2013 21:29:41 +0000 (18:29 -0300)
Este cliente realiza o handshake inicial e o handshake TLS, e envia
uma mensagem minima para teste.

rnetclient.c [new file with mode: 0644]

diff --git a/rnetclient.c b/rnetclient.c
new file mode 100644 (file)
index 0000000..f01e248
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ *  Copyright (C) 2011  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <gnutls/gnutls.h>
+
+static void session_new(gnutls_session_t *session)
+{
+       static void *cred;
+       gnutls_init(session, GNUTLS_CLIENT);
+       gnutls_set_default_priority(*session);
+}
+
+int main(int argc, char **argv)
+{
+       struct sockaddr_in saddr;
+       int c;
+       int r;
+       char buffer[256];
+       gnutls_session_t session;
+       gnutls_global_init();
+       session_new(&session);
+       c = socket(PF_INET, SOCK_STREAM, 0);
+       saddr.sin_family = AF_INET;
+       saddr.sin_port = htons(3456);
+       saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+       connect(c, (struct sockaddr *) &saddr, sizeof(saddr));
+       gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) c);
+       buffer[0] = 1;
+       write(c, buffer, 1);
+       r = read(c, buffer, 1);
+       if (r != 1 && buffer[0] != 'E')
+               exit(1);
+       write(c, "00000000000000", 14);
+       r = read(c, buffer, 14);
+       if (r != 14)
+               exit(1);
+       if ((r = gnutls_handshake(session)) < 0)
+               fprintf(stderr, "error in handshake: %s\n",
+                               gnutls_strerror(r));
+       else
+               fprintf(stderr, "handshake ok\n");
+       buffer[0] = 0x40;
+       gnutls_record_send(session, buffer, 1);
+       while ((r = gnutls_record_recv(session, buffer, sizeof(buffer))) > 0)
+               write(1, buffer, r);
+       close(c);
+       gnutls_global_deinit();
+       return 0;
+}