]> ruderich.org/simon Gitweb - tlsproxy/tlsproxy.git/blobdiff - src/tlsproxy.c
tlsproxy.h: Sort includes.
[tlsproxy/tlsproxy.git] / src / tlsproxy.c
index 1e8642fa4ca4e40d93879e058bffb974bc402a8e..0eefaa3e22ddae4bf9ff799b9ba857a16bd3b04e 100644 (file)
 #include "sem.h"
 #include "connection.h"
 
-/* socket(), bind(), accept(), listen() */
-#include <sys/types.h>
-#include <sys/socket.h>
-/* close() */
-#include <unistd.h>
-/* htons() */
 #include <arpa/inet.h>
-/* sigaction() */
-#include <signal.h>
-/* errno */
 #include <errno.h>
-/* pthread_*() */
 #include <pthread.h>
+#include <signal.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <unistd.h>
 
-/* For GnuTLS. */
+/* Necessary for GnuTLS when used with threads. */
 #include <gcrypt.h>
-
 GCRY_THREAD_OPTION_PTHREAD_IMPL;
 
 
@@ -83,7 +76,7 @@ static void print_usage(const char *argv);
 static void initialize_gnutls(void);
 static void deinitialize_gnutls(void);
 
-static void worker_thread(void);
+static void *worker_thread(void *unused);
 
 
 int main(int argc, char **argv) {
@@ -143,19 +136,11 @@ int main(int argc, char **argv) {
         return EXIT_FAILURE;
     }
     for (i = 0; i < thread_count; i++) {
-        int result;
-        pthread_t thread;
-
-        result = pthread_create(&thread, NULL,
-                                (void * (*)(void *))&worker_thread,
-                                NULL);
-        if (result != 0) {
-            fprintf(stderr, "failed to create worker thread: %s\n",
-                            strerror(result));
+        errno = pthread_create(threads + i, NULL, &worker_thread, NULL);
+        if (errno != 0) {
+            perror("failed to create worker thread");
             return EXIT_FAILURE;
         }
-
-        threads[i] = thread;
     }
 
 #ifdef USE_IPV4_ONLY
@@ -275,13 +260,13 @@ static void parse_arguments(int argc, char **argv) {
     while ((option = getopt(argc, argv, "d:p:t:uh?")) != -1) {
         switch (option) {
             case 'd': {
-                if (atoi(optarg) < 0) {
+                global_log_level = atoi(optarg);
+                if (global_log_level < 0) {
                     print_usage(argv[0]);
                     fprintf(stderr, "\n-d positive number required: '%s'\n",
                                     optarg);
                     exit(EXIT_FAILURE);
                 }
-                global_log_level = atoi(optarg);
                 break;
             }
             case 'p': {
@@ -359,18 +344,18 @@ static void print_usage(const char *argv) {
 
 static void initialize_gnutls(void) {
     int result;
-    gcry_error_t error = 0;
+    gcry_error_t error;
 
     /* Thread safe setup. Must be called before gnutls_global_init(). */
     error = gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
-    if (error) {
+    if (error != 0) {
         fprintf(stderr, "gcry_control(): %s/%s\n", gcry_strsource(error),
                                                    gcry_strerror(error));
         exit(EXIT_FAILURE);
     }
     /* Prevent usage of blocking /dev/random. */
     error = gcry_control(GCRYCTL_ENABLE_QUICK_RANDOM, 0);
-    if (error) {
+    if (error != 0) {
         fprintf(stderr, "gcry_control(): %s/%s\n", gcry_strsource(error),
                                                    gcry_strerror(error));
         exit(EXIT_FAILURE);
@@ -397,9 +382,11 @@ static void deinitialize_gnutls(void) {
     gnutls_global_deinit();
 }
 
-static void worker_thread(void) {
+static void *worker_thread(void *unused) {
     int client_socket;
 
+    (void)unused;
+
     for (;;) {
         /* Get next element from ring buffer. */
         P(ringbuffer_full);
@@ -416,4 +403,6 @@ static void worker_thread(void) {
 
         handle_connection(client_socket);
     }
+
+    return NULL;
 }