This version connects to the other side.
authorGregory Minshall <minshall@ucbvax.Berkeley.EDU>
Sat, 13 Jun 1987 04:55:46 +0000 (20:55 -0800)
committerGregory Minshall <minshall@ucbvax.Berkeley.EDU>
Sat, 13 Jun 1987 04:55:46 +0000 (20:55 -0800)
SCCS-vsn: usr.bin/tn3270/api/api_bsd.c 1.2
SCCS-vsn: usr.bin/tn3270/api/api_exch.c 1.2
SCCS-vsn: usr.bin/tn3270/api/api_exch.h 1.2

usr/src/usr.bin/tn3270/api/api_bsd.c
usr/src/usr.bin/tn3270/api/api_exch.c
usr/src/usr.bin/tn3270/api/api_exch.h

index 508b772..d2cf1d7 100644 (file)
@@ -5,10 +5,9 @@
 #include <stdio.h>
 
 #include "../api/api.h"
 #include <stdio.h>
 
 #include "../api/api.h"
+#include "api_exch.h"
 
 
 
 
-static int sock = -1;
-
 api_open_api(string)
 char   *string;                /* if non-zero, where to connect to */
 {
 api_open_api(string)
 char   *string;                /* if non-zero, where to connect to */
 {
@@ -16,7 +15,9 @@ char  *string;                /* if non-zero, where to connect to */
     struct hostent *hp;
     char *getenv();
     char thehostname[100];
     struct hostent *hp;
     char *getenv();
     char thehostname[100];
+    int sock;
     int port;
     int port;
+    int i;
 
     if (string == 0) {
        string = getenv("API3270");     /* Get API */
 
     if (string == 0) {
        string = getenv("API3270");     /* Get API */
@@ -50,6 +51,85 @@ char *string;                /* if non-zero, where to connect to */
        perror("connecting to API server");
        return -1;
     }
        perror("connecting to API server");
        return -1;
     }
+    /* Now, try application level connection */
+    if (api_exch_init(sock) == -1) {
+       return -1;
+    }
+    if (api_exch_outcommand(EXCH_ASSOCIATE) == -1) {
+       return -1;
+    }
+    while ((i = api_exch_inbyte()) != EXCH_ASSOCIATED) {
+       struct storage_descriptor sd;
+       int passwd_length;
+       char *passwd, *getpass();
+       char buffer[200];
+
+       switch (i) {
+       case EXCH_REJECTED:
+           if (api_exch_intype(EXCH_TYPE_STORE_DESC,
+                                       sizeof sd, (char *)&sd) == -1) {
+               return -1;
+           }
+           sd.length = ntohs(sd.length);
+           if (api_exch_intype(EXCH_TYPE_BYTES, sd.length, buffer) == -1) {
+               return -1;
+           }
+           buffer[sd.length] = 0;
+           fprintf(stderr, "%s\n", buffer);
+           if (api_exch_outcommand(EXCH_ASSOCIATE) == -1) {
+               return -1;
+           }
+           break;
+       case EXCH_SEND_AUTH:
+           if (api_exch_intype(EXCH_TYPE_STORE_DESC, sizeof sd, (char *)&sd) == -1) {
+               return -1;
+           }
+           sd.length = ntohs(sd.length);
+           if (api_exch_intype(EXCH_TYPE_BYTES, sd.length, buffer) == -1) {
+               return -1;
+           }
+           buffer[sd.length] = 0;
+           passwd = getpass(buffer);           /* Go to terminal */
+           passwd_length = strlen(passwd);
+           if (api_exch_intype(EXCH_TYPE_STORE_DESC, sizeof sd, (char *)&sd) == -1) {
+               return -1;
+           }
+           sd.length = ntohs(sd.length);
+           if (api_exch_intype(EXCH_TYPE_BYTES, sd.length, buffer) == -1) {
+               return -1;
+           }
+           buffer[sd.length] = 0;
+           if (sd.length) {
+               char *ptr;
+
+               ptr = passwd;
+               i = 0;
+               while (*ptr) {
+                   *ptr++ ^= buffer[i++];
+                   if (i >= sd.length) {
+                       i = 0;
+                   }
+               }
+           }
+           sd.length = htons(passwd_length);
+           if (api_exch_outcommand(EXCH_AUTH) == -1) {
+               return -1;
+           }
+           if (api_exch_outtype(EXCH_TYPE_STORE_DESC, sizeof sd, (char *)&sd) == -1) {
+               return -1;
+           }
+           if (api_exch_outtype(EXCH_TYPE_BYTES, passwd_length, passwd) == -1) {
+               return -1;
+           }
+           break;
+       case -1:
+           return -1;
+       default:
+           fprintf(stderr,
+                   "Waiting for connection indicator, received 0x%x.\n", i);
+           break;
+       }
+    }
     /* YEAH */
     return 0;          /* Happiness! */
 }
     /* YEAH */
     return 0;          /* Happiness! */
 }
index 84e5d24..81fae9a 100644 (file)
@@ -2,6 +2,8 @@
 
 #include "api_exch.h"
 
 
 #include "api_exch.h"
 
+static int sock;               /* Socket number */
+
 static char ibuffer[40], *ibuf_next, *ibuf_last;
 #define        IBUFADDED(i)            ibuf_last += (i)
 #define        IBUFAVAILABLE()         (ibuf_last -ibuf_next)
 static char ibuffer[40], *ibuf_next, *ibuf_last;
 #define        IBUFADDED(i)            ibuf_last += (i)
 #define        IBUFAVAILABLE()         (ibuf_last -ibuf_next)
@@ -61,7 +63,19 @@ int count;
     return 0;
 }
 
     return 0;
 }
 
-static int
+int
+api_exch_inbyte()
+{
+    if (IBUFAVAILABLE() < 1) {
+       if (infill(1) == -1) {
+           return -1;
+       }
+    }
+    return IBUFGETCHAR();
+}
+
+
+int
 api_exch_incommand(command)
 int command;
 {
 api_exch_incommand(command)
 int command;
 {
@@ -82,7 +96,7 @@ int command;
 }
 
 
 }
 
 
-static int
+int
 api_exch_outcommand(command)
 int command;
 {
 api_exch_outcommand(command)
 int command;
 {
@@ -96,7 +110,7 @@ int command;
 }
 
 
 }
 
 
-static int
+int
 api_exch_outtype(type, length, location)
 int
     type,
 api_exch_outtype(type, length, location)
 int
     type,
@@ -127,7 +141,7 @@ char
 }
 
 
 }
 
 
-static int
+int
 api_exch_intype(type, length, location)
 int
     type,
 api_exch_intype(type, length, location)
 int
     type,
@@ -161,3 +175,15 @@ char
     }
     return 0;
 }
     }
     return 0;
 }
+
+int
+api_exch_init(sock_number)
+int sock_number;
+{
+    sock = sock_number;
+
+    IBUFRESET();
+    OBUFRESET();
+
+    return 0;
+}
index a30852b..647dde4 100644 (file)
@@ -2,10 +2,13 @@
  * This file describes the structures passed back and forth
  * between the API client and API server on a Unix-based
  * tn3270 implementation.
  * This file describes the structures passed back and forth
  * between the API client and API server on a Unix-based
  * tn3270 implementation.
+ *
+ * A command is: <command code> <sequence number> <parameter>*
+ *
  */
 
 
  */
 
 
-#define        EXCH_CONNECT    23      /* Connect request [client->server] */
+#define        EXCH_ASSOCIATE  23      /* Connect request [client->server] */
 #define        EXCH_SEND_AUTH  44      /* Send auth (password) [server->client] */
        /*
         * struct storeage_desc
 #define        EXCH_SEND_AUTH  44      /* Send auth (password) [server->client] */
        /*
         * struct storeage_desc
@@ -18,7 +21,7 @@
         * struct storeage_desc
         * char authenticator[]
         */
         * struct storeage_desc
         * char authenticator[]
         */
-#define        EXCH_CONNECTED  78      /* You are now connected [server->client] */
+#define        EXCH_ASSOCIATED 78      /* You are now connected [server->client] */
 #define        EXCH_REJECTED   93      /* Too bad [server->client] */
        /*
         * struct storeage_desc
 #define        EXCH_REJECTED   93      /* Too bad [server->client] */
        /*
         * struct storeage_desc
@@ -52,7 +55,7 @@
 #define        EXCH_TYPE_BYTES         67
 
 /*
 #define        EXCH_TYPE_BYTES         67
 
 /*
- * each structure that comes over looks like:
+ * each parameter that comes over looks like:
  *
  *     char                    type of following
  *     short (2 bytes)         length of following (network byte order)
  *
  *     char                    type of following
  *     short (2 bytes)         length of following (network byte order)