[Help-gnutls] Re: Build gnutls on windows

Martin Lambers marlam at marlam.de
Wed Sep 21 20:57:38 CEST 2005


On Mon, 19. Sep 2005, 23:45:41 +0200, Simon Josefsson wrote:
> Can you test whether:
> 
> http://josefsson.org/daily/gnutls/gnutls-20050919.tar.gz
> 
> build correctly for you?

It does not, but the changes required to make it build correctly are
small:

The first patch removes 'char *program_name = "gnutls";' from
lib/gnutls_global.c. This apparently reverts a change from 2005-08-30.
I did not get the missing symbols error afterwards. When does it cause
problems for you?

The second patch changes the example code in doc/examples:
1. Replace bzero with memset.
2. Don't use mmap (this is the same change that was done in src/cli.c).
3. Include a different header on WIN32.
4. Use inet_ntoa instead of inet_ntop on WIN32.
But maybe it would be better to disable compilation of the examples on
Win32 instead of cluttering example code with #ifdefs.
The changes 1 and 2 may be useful nevertheless; I can send a patch with
only these changes if you wish.

With these patches, the following works on a Debian machine with the
mingw32 packages:
$ ./configure --host=i586-mingw32msvc --prefix=/tmp/t
$ make
$ make install

A quick test with mpop compiled against the resulting library did not
show problems.

Martin
-------------- next part --------------
diff -uNr gnutls-1.2.8/lib/gnutls_global.c gnutls-20050919+win32_patches/lib/gnutls_global.c
--- gnutls-1.2.8/lib/gnutls_global.c	2005-08-31 02:23:05.000000000 +0200
+++ gnutls-20050919+win32_patches/lib/gnutls_global.c	2005-09-21 18:37:23.991989000 +0200
@@ -39,9 +39,6 @@
 ASN1_TYPE _gnutls_pkix1_asn;
 ASN1_TYPE _gnutls_gnutls_asn;
 
-/* To shut up missing symbols from error module. */
-char *program_name = "gnutls";
-
 /**
   * gnutls_global_set_log_function - This function sets the logging function
   * @log_func: it's a log function
-------------- next part --------------
diff -uNr gnutls-1.2.8/doc/examples/ex-cert-select.c gnutls-20050919+win32_patches/doc/examples/ex-cert-select.c
--- gnutls-1.2.8/doc/examples/ex-cert-select.c	2005-08-11 02:23:02.000000000 +0200
+++ gnutls-20050919+win32_patches/doc/examples/ex-cert-select.c	2005-09-21 18:48:54.391989000 +0200
@@ -3,11 +3,14 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
+#ifdef _WIN32
+# include <winsock2.h>
+#else
+# include <sys/socket.h>
+# include <netinet/in.h>
+# include <arpa/inet.h>
+#endif
 #include <unistd.h>
-#include <sys/mman.h>
 #include <sys/stat.h>
 #include <gnutls/gnutls.h>
 #include <gnutls/x509.h>
@@ -37,38 +40,33 @@
 gnutls_x509_privkey_t key;
 
 /* Helper functions to load a certificate and key
- * files into memory. They use mmap for simplicity.
+ * files into memory.
  */
-static gnutls_datum_t
-mmap_file (const char *file)
+static gnutls_datum load_file(const char *file)
 {
-  int fd;
-  gnutls_datum_t mmaped_file = { NULL, 0 };
-  struct stat stat_st;
+  FILE *f;
+  gnutls_datum loaded_file = { NULL, 0 };
+  long filelen;
   void *ptr;
 
-  fd = open (file, 0);
-  if (fd == -1)
-    return mmaped_file;
-
-  fstat (fd, &stat_st);
-
-  ptr = mmap (NULL, stat_st.st_size, PROT_READ, MAP_SHARED, fd, 0);
-  close (fd);
-
-  if (ptr == MAP_FAILED)
-    return mmaped_file;
-
-  mmaped_file.data = ptr;
-  mmaped_file.size = stat_st.st_size;
+  if (!(f = fopen(file, "r"))
+      || fseek(f, 0, SEEK_END) != 0
+      || (filelen = ftell(f)) < 0
+      || fseek(f, 0, SEEK_SET) != 0
+      || !(ptr = malloc((size_t)filelen))
+      || fread(ptr, 1, (size_t)filelen, f) < (size_t)filelen)
+    {
+      return loaded_file;
+    }
 
-  return mmaped_file;
+  loaded_file.data = ptr;
+  loaded_file.size = (unsigned int)filelen;
+  return loaded_file;
 }
 
-static void
-munmap_file (gnutls_datum_t data)
+static void unload_file(gnutls_datum data)
 {
-  munmap (data.data, data.size);
+  free(data.data);
 }
 
 /* Load the certificate and the private key.
@@ -79,7 +77,7 @@
   int ret;
   gnutls_datum_t data;
 
-  data = mmap_file (CERT_FILE);
+  data = load_file (CERT_FILE);
   if (data.data == NULL)
     {
       fprintf (stderr, "*** Error loading cert file.\n");
@@ -95,9 +93,9 @@
       exit (1);
     }
 
-  munmap_file (data);
+  unload_file (data);
 
-  data = mmap_file (KEY_FILE);
+  data = load_file (KEY_FILE);
   if (data.data == NULL)
     {
       fprintf (stderr, "*** Error loading key file.\n");
@@ -114,7 +112,7 @@
       exit (1);
     }
 
-  munmap_file (data);
+  unload_file (data);
 
 }
 
diff -uNr gnutls-1.2.8/doc/examples/ex-client1.c gnutls-20050919+win32_patches/doc/examples/ex-client1.c
--- gnutls-1.2.8/doc/examples/ex-client1.c	2005-08-11 02:23:02.000000000 +0200
+++ gnutls-20050919+win32_patches/doc/examples/ex-client1.c	2005-09-21 18:59:29.431989000 +0200
@@ -3,9 +3,13 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
+#ifdef _WIN32
+# include <winsock2.h>
+#else
+# include <sys/socket.h>
+# include <netinet/in.h>
+# include <arpa/inet.h>
+#endif
 #include <unistd.h>
 #include <gnutls/gnutls.h>
 
diff -uNr gnutls-1.2.8/doc/examples/ex-client2.c gnutls-20050919+win32_patches/doc/examples/ex-client2.c
--- gnutls-1.2.8/doc/examples/ex-client2.c	2005-08-13 02:23:03.000000000 +0200
+++ gnutls-20050919+win32_patches/doc/examples/ex-client2.c	2005-09-21 19:00:00.451989000 +0200
@@ -3,9 +3,13 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
+#ifdef _WIN32
+# include <winsock2.h>
+#else
+# include <sys/socket.h>
+# include <netinet/in.h>
+# include <arpa/inet.h>
+#endif
 #include <unistd.h>
 #include <gnutls/gnutls.h>
 
diff -uNr gnutls-1.2.8/doc/examples/ex-serv1.c gnutls-20050919+win32_patches/doc/examples/ex-serv1.c
--- gnutls-1.2.8/doc/examples/ex-serv1.c	2005-08-11 02:23:02.000000000 +0200
+++ gnutls-20050919+win32_patches/doc/examples/ex-serv1.c	2005-09-21 19:48:40.681989000 +0200
@@ -3,9 +3,13 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
+#ifdef _WIN32
+# include <winsock2.h>
+#else
+# include <sys/socket.h>
+# include <netinet/in.h>
+# include <arpa/inet.h>
+#endif
 #include <string.h>
 #include <unistd.h>
 #include <gnutls/gnutls.h>
@@ -126,8 +130,12 @@
       sd = accept (listen_sd, (SA *) & sa_cli, &client_len);
 
       printf ("- connection from %s, port %d\n",
-	      inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
-			 sizeof (topbuf)), ntohs (sa_cli.sin_port));
+#ifdef _WIN32
+	      inet_ntoa(sa_cli.sin_addr),
+#else
+	      inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf, sizeof (topbuf)),
+#endif 
+	      ntohs (sa_cli.sin_port));
 
       gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
       ret = gnutls_handshake (session);
@@ -147,7 +155,7 @@
       i = 0;
       for (;;)
 	{
-	  bzero (buffer, MAX_BUF + 1);
+	  memset (buffer, 0, MAX_BUF + 1);
 	  ret = gnutls_record_recv (session, buffer, MAX_BUF);
 
 	  if (ret == 0)
diff -uNr gnutls-1.2.8/doc/examples/ex-serv-anon.c gnutls-20050919+win32_patches/doc/examples/ex-serv-anon.c
--- gnutls-1.2.8/doc/examples/ex-serv-anon.c	2005-08-11 02:23:02.000000000 +0200
+++ gnutls-20050919+win32_patches/doc/examples/ex-serv-anon.c	2005-09-21 19:50:01.971989000 +0200
@@ -3,9 +3,13 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
+#ifdef _WIN32
+# include <winsock2.h>
+#else
+# include <sys/socket.h>
+# include <netinet/in.h>
+# include <arpa/inet.h>
+#endif
 #include <string.h>
 #include <unistd.h>
 #include <gnutls/gnutls.h>
@@ -111,8 +115,12 @@
       sd = accept (listen_sd, (SA *) & sa_cli, &client_len);
 
       printf ("- connection from %s, port %d\n",
-	      inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
-			 sizeof (topbuf)), ntohs (sa_cli.sin_port));
+#ifdef _WIN32
+	      inet_ntoa(sa_cli.sin_addr),
+#else
+	      inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf, sizeof (topbuf)),
+#endif 
+	      ntohs (sa_cli.sin_port));
 
       gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
       ret = gnutls_handshake (session);
@@ -132,7 +140,7 @@
       i = 0;
       for (;;)
 	{
-	  bzero (buffer, MAX_BUF + 1);
+	  memset (buffer, 0, MAX_BUF + 1);
 	  ret = gnutls_record_recv (session, buffer, MAX_BUF);
 
 	  if (ret == 0)
diff -uNr gnutls-1.2.8/doc/examples/ex-serv-export.c gnutls-20050919+win32_patches/doc/examples/ex-serv-export.c
--- gnutls-1.2.8/doc/examples/ex-serv-export.c	2005-08-11 02:23:02.000000000 +0200
+++ gnutls-20050919+win32_patches/doc/examples/ex-serv-export.c	2005-09-21 19:51:05.311989000 +0200
@@ -3,9 +3,13 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
+#ifdef _WIN32
+# include <winsock2.h>
+#else
+# include <sys/socket.h>
+# include <netinet/in.h>
+# include <arpa/inet.h>
+#endif
 #include <string.h>
 #include <unistd.h>
 #include <gnutls/gnutls.h>
@@ -171,8 +175,12 @@
       sd = accept (listen_sd, (SA *) & sa_cli, &client_len);
 
       printf ("- connection from %s, port %d\n",
-	      inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
-			 sizeof (topbuf)), ntohs (sa_cli.sin_port));
+#ifdef _WIN32
+	      inet_ntoa(sa_cli.sin_addr),
+#else
+	      inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf, sizeof (topbuf)),
+#endif 
+	      ntohs (sa_cli.sin_port));
 
       gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
       ret = gnutls_handshake (session);
@@ -191,7 +199,7 @@
       i = 0;
       for (;;)
 	{
-	  bzero (buffer, MAX_BUF + 1);
+	  memset (buffer, 0, MAX_BUF + 1);
 	  ret = gnutls_record_recv (session, buffer, MAX_BUF);
 
 	  if (ret == 0)
diff -uNr gnutls-1.2.8/doc/examples/ex-serv-pgp.c gnutls-20050919+win32_patches/doc/examples/ex-serv-pgp.c
--- gnutls-1.2.8/doc/examples/ex-serv-pgp.c	2005-08-11 02:23:02.000000000 +0200
+++ gnutls-20050919+win32_patches/doc/examples/ex-serv-pgp.c	2005-09-21 19:52:19.831989000 +0200
@@ -3,9 +3,13 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
+#ifdef _WIN32
+# include <winsock2.h>
+#else
+# include <sys/socket.h>
+# include <netinet/in.h>
+# include <arpa/inet.h>
+#endif
 #include <string.h>
 #include <unistd.h>
 #include <gnutls/gnutls.h>
@@ -130,8 +134,12 @@
       sd = accept (listen_sd, (SA *) & sa_cli, &client_len);
 
       printf ("- connection from %s, port %d\n",
-	      inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
-			 sizeof (topbuf)), ntohs (sa_cli.sin_port));
+#ifdef _WIN32
+	      inet_ntoa(sa_cli.sin_addr),
+#else
+	      inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf, sizeof (topbuf)),
+#endif 
+	      ntohs (sa_cli.sin_port));
 
       gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
       ret = gnutls_handshake (session);
@@ -151,7 +159,7 @@
       i = 0;
       for (;;)
 	{
-	  bzero (buffer, MAX_BUF + 1);
+	  memset (buffer, 0, MAX_BUF + 1);
 	  ret = gnutls_record_recv (session, buffer, MAX_BUF);
 
 	  if (ret == 0)
diff -uNr gnutls-1.2.8/doc/examples/ex-serv-srp.c gnutls-20050919+win32_patches/doc/examples/ex-serv-srp.c
--- gnutls-1.2.8/doc/examples/ex-serv-srp.c	2005-08-11 02:23:02.000000000 +0200
+++ gnutls-20050919+win32_patches/doc/examples/ex-serv-srp.c	2005-09-21 19:53:15.651989000 +0200
@@ -3,9 +3,13 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
+#ifdef _WIN32
+# include <winsock2.h>
+#else
+# include <sys/socket.h>
+# include <netinet/in.h>
+# include <arpa/inet.h>
+#endif
 #include <string.h>
 #include <unistd.h>
 #include <gnutls/gnutls.h>
@@ -115,8 +119,12 @@
       sd = accept (listen_sd, (SA *) & sa_cli, &client_len);
 
       printf ("- connection from %s, port %d\n",
-	      inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
-			 sizeof (topbuf)), ntohs (sa_cli.sin_port));
+#ifdef _WIN32
+	      inet_ntoa(sa_cli.sin_addr),
+#else
+	      inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf, sizeof (topbuf)),
+#endif 
+	      ntohs (sa_cli.sin_port));
 
       gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
       ret = gnutls_handshake (session);
@@ -135,7 +143,7 @@
       i = 0;
       for (;;)
 	{
-	  bzero (buffer, MAX_BUF + 1);
+	  memset (buffer, 0, MAX_BUF + 1);
 	  ret = gnutls_record_recv (session, buffer, MAX_BUF);
 
 	  if (ret == 0)
diff -uNr gnutls-1.2.8/doc/examples/tcp.c gnutls-20050919+win32_patches/doc/examples/tcp.c
--- gnutls-1.2.8/doc/examples/tcp.c	2005-08-12 12:13:40.000000000 +0200
+++ gnutls-20050919+win32_patches/doc/examples/tcp.c	2005-09-21 18:58:36.201989000 +0200
@@ -2,9 +2,14 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
+#ifdef _WIN32
+# include <winsock2.h>
+# define SHUT_RDWR SD_BOTH
+#else
+# include <sys/socket.h>
+# include <netinet/in.h>
+# include <arpa/inet.h>
+#endif
 #include <unistd.h>
 
 #define SA struct sockaddr
@@ -27,7 +32,11 @@
   memset (&sa, '\0', sizeof (sa));
   sa.sin_family = AF_INET;
   sa.sin_port = htons (atoi (PORT));
+#ifdef _WIN32
+  sa.sin_addr.s_addr = inet_addr(SERVER);
+#else
   inet_pton (AF_INET, SERVER, &sa.sin_addr);
+#endif
 
   err = connect (sd, (SA *) & sa, sizeof (sa));
   if (err < 0)


More information about the Gnutls-help mailing list