Fix handling of errors with strtol(), factor out port parsing
[trust_router.git] / gsscon / test / gsscon_server.c
index 1e9232b..3ca69c4 100644 (file)
  */
 
 #include <gsscon.h>
+#include <assert.h>
 
+static int cb_print_names(gss_name_t clientName,
+                         gss_buffer_t displayName,
+                         void *data)
+{
+  assert(clientName != NULL);
+  assert(data == NULL);
+  printf("Gss name: %-*s\n",
+        displayName->length, displayName->value);
+  return 0;
+}
+
+  
 /* --------------------------------------------------------------------------- */
 
 static int SetupListeningSocket (int  inPort, 
@@ -116,14 +129,22 @@ int main (int argc, const char *argv[])
     int err = 0;
     OM_uint32 minorStatus;
     int port = kDefaultPort;
+    long tmp;
     int listenFD = -1;
     gss_ctx_id_t gssContext = GSS_C_NO_CONTEXT;
     int i = 0;
-        
+    gss_buffer_desc nameBuffer = {0, "trustidentity"};
+
     for (i = 1; (i < argc) && !err; i++) {
         if ((strcmp (argv[i], "--port") == 0) && (i < (argc - 1))) {
-            port = strtol (argv[++i], NULL, 0);
-            if (port == 0) { err = errno; }
+            errno = 0; /* ensure this starts off at 0 */
+            tmp = strtol (argv[++i], NULL, 0);
+            if (errno)
+                err = errno;
+            else if ((tmp <= 0) || (tmp > 65535))
+                err = ERANGE;
+            else
+                port = (int) tmp;
         } else if ((strcmp(argv[i], "--service") == 0) && (i < (argc - 1))) {
             gServiceName = argv[++i];
         } else {
@@ -144,6 +165,7 @@ int main (int argc, const char *argv[])
         int authorizationError = 0;
         
         connectionFD = accept (listenFD, NULL, NULL);
+
         if (connectionFD < 0) {
             if (errno != EINTR) { 
                 err = errno;
@@ -152,7 +174,8 @@ int main (int argc, const char *argv[])
         }
         
         printf ("Accepting new connection...\n");
-        connectionErr = gsscon_passive_authenticate (connectionFD, &gssContext);
+        connectionErr = gsscon_passive_authenticate (connectionFD, nameBuffer, &gssContext,
+                                                    cb_print_names, NULL);
         
         if (!connectionErr) {
             connectionErr = gsscon_authorize (gssContext,