/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk
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
extern "C" {
#include <sys/types.h>		// getaddrinfo, gai_strerror, socket, inet_pton
				// connect
#include <sys/socket.h>		// getaddrinfo, gai_strerror, socket, inet_pton
				// connect
#include <unistd.h>		// close
#include <netdb.h>		// getaddrinfo, gai_strerror
#include <arpa/inet.h>		// inet_pton
#include <sys/select.h> 	// select
#include <gnutls/gnutls.h>
}

#include <cstdio>		// fprintf
#include <cerrno>		// perror
#include <cstring> 		// memset

#define SOCKET_ERR(err,s) if(err<0) {perror(s);return(1);}
#define PORT 49001
#define CERTFILE "client-cert.pem"
#define KEYFILE "client-key.pem"
#define CAFILE "ca.pem"

gnutls_certificate_credentials_t x509_cred;

gnutls_session_t
initgnutls(){
  gnutls_session_t session;

  gnutls_global_init ();

  /* X509 stuff */
  gnutls_certificate_allocate_credentials (&x509_cred);
  gnutls_certificate_set_x509_trust_file (x509_cred, CAFILE, GNUTLS_X509_FMT_PEM);
  gnutls_certificate_set_x509_key_file (x509_cred, CERTFILE, KEYFILE,
					GNUTLS_X509_FMT_PEM);

  //Gnutls stuff
  gnutls_init (&session, GNUTLS_CLIENT);
  gnutls_set_default_priority (session);
  gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, x509_cred);
  return session;
}


int main (){
  int sd, ret;
  char buffer[512];
  struct sockaddr_in6 to;
  struct sockaddr_in6 from;
  gnutls_session_t session;
  fd_set rfds_orig;
  struct timeval timeout;

  session = initgnutls ();

  sd = socket(PF_INET6, SOCK_DGRAM, 0);
  SOCKET_ERR(sd,"socket");
 
  {
    int flag = 1;
    ret = setsockopt(sd, SOL_SOCKET, SO_BROADCAST, & flag, sizeof(flag));
    SOCKET_ERR(ret,"setsockopt broadcast");
  }

  setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, "eth0", 5);
  SOCKET_ERR(ret,"setsockopt bindtodevice");

  memset (&to, '\0', sizeof (to));
  to.sin6_family = AF_INET6;
  ret = inet_pton(AF_INET6, "ff02::1" , &to.sin6_addr);
  SOCKET_ERR(ret,"setsockopt bindtodevice");
  to.sin6_port = htons (PORT);	// Server Port number

  FD_ZERO(&rfds_orig);
  FD_SET(sd, &rfds_orig);

  timeout.tv_sec = 10;
  timeout.tv_usec = 0;


  for(;;){
    sendto(sd, "Marco", 5, 0, reinterpret_cast<const sockaddr*>(&to), sizeof(to));

    fd_set rfds = rfds_orig;

    ret = select(sd+1, &rfds, 0, 0, & timeout);
    SOCKET_ERR(sd,"select");

    if (ret){
      socklen_t from_len = sizeof(from);
      ret = recvfrom(sd,buffer,512,0, reinterpret_cast<sockaddr *>(& from),
		     & from_len);
      SOCKET_ERR(ret,"recv");

      if (strncmp(buffer,"Polo", 4) == 0){
	break;
      }
    }
  }

  write(1,buffer,ret);
  write(1,"\n",1);

  //shutdown procedure
  close(sd);

  sleep(1);

  sd = socket(PF_INET6, SOCK_STREAM, 0);
  SOCKET_ERR(sd,"socket");

  setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, "eth0", 5);
  SOCKET_ERR(ret,"setsockopt bindtodevice");

  memset(&to,0,sizeof(to));
  to.sin6_family = from.sin6_family;
  to.sin6_port   = from.sin6_port;
  to.sin6_addr   = from.sin6_addr;
  to.sin6_scope_id   = from.sin6_scope_id;

  ret = connect(sd,reinterpret_cast<struct sockaddr *>(&to),sizeof(to));
  SOCKET_ERR(ret,"connect");

  gnutls_transport_set_ptr (session, reinterpret_cast<gnutls_transport_ptr_t> (sd));

  ret = gnutls_handshake (session);

  if (ret < 0)
    {
      fprintf (stderr, "*** Handshake failed\n");
      gnutls_perror (ret);
      return 1;
    }
  printf ("- Handshake was completed\n");

  //message to be seent
  gnutls_record_send (session, "The secret message is \"squeamish ossifrage\"\n", 44);

  //shutdown procedure
  gnutls_bye (session, GNUTLS_SHUT_RDWR);
  close(sd);
  gnutls_deinit (session);
  gnutls_certificate_free_credentials (x509_cred);
  gnutls_global_deinit ();

  close(sd);

  return 0;
}