"SfR Fresh" - the SfR Freeware/Shareware Archive 
Member "tn3270-5.2.0-glibc/tn3270/api/api_bsd.c" of archive tn3270-5.2.0-glibc.tgz:
As a special service "SfR Fresh" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting with prefixed line numbers.
Alternatively you can here view or download the uninterpreted source code file.
That can be also achieved for any archive member file by clicking within an archive contents listing on the first character of the file(path) respectively on the according byte size field.
1 /*
2 * Copyright (c) 1988 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17
18 #ifndef lint
19 static char sccsid[] = "@(#)api_bsd.c 4.1 (Berkeley) 12/4/88";
20 #endif /* not lint */
21
22 #if defined(unix)
23
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <netdb.h>
28 #include <stdio.h>
29
30 #include "../ctlr/api.h"
31 #include "api_exch.h"
32
33
34 int
35 api_close_api()
36 {
37 if (api_exch_outcommand(EXCH_CMD_DISASSOCIATE) == -1) {
38 return -1;
39 } else if (api_exch_flush() == -1) {
40 return -1;
41 } else {
42 return 0;
43 }
44 }
45
46
47 int
48 api_open_api(string)
49 char *string; /* if non-zero, where to connect to */
50 {
51 struct sockaddr_in server;
52 struct hostent *hp;
53 struct storage_descriptor sd;
54 extern char *getenv();
55 char thehostname[100];
56 char keyname[100];
57 char inkey[100];
58 FILE *keyfile;
59 int sock;
60 unsigned int port;
61 int i;
62
63 if (string == 0) {
64 string = getenv("API3270"); /* Get API */
65 if (string == 0) {
66 fprintf(stderr,
67 "API3270 environmental variable not set - no API.\n");
68 return -1; /* Nothing */
69 }
70 }
71
72 if (sscanf(string, "%[^:]:%d:%s", thehostname,
73 (int *)&port, keyname) != 3) {
74 fprintf(stderr, "API3270 environmental variable has bad format.\n");
75 return -1;
76 }
77 /* Now, try to connect */
78 sock = socket(AF_INET, SOCK_STREAM, 0);
79 if (sock < 0) {
80 perror("opening API socket");
81 return -1;
82 }
83 server.sin_family = AF_INET;
84 hp = gethostbyname(thehostname);
85 if (hp == 0) {
86 fprintf(stderr, "%s specifies bad host name.\n", string);
87 return -1;
88 }
89 bcopy(hp->h_addr, (char *)&server.sin_addr, hp->h_length);
90 server.sin_port = htons(port);
91
92 if (connect(sock, (struct sockaddr *)&server, sizeof server) < 0) {
93 perror("connecting to API server");
94 return -1;
95 }
96 /* Now, try application level connection */
97 if (api_exch_init(sock, "client") == -1) {
98 return -1;
99 }
100 if (api_exch_outcommand(EXCH_CMD_ASSOCIATE) == -1) {
101 return -1;
102 }
103 keyfile = fopen(keyname, "r");
104 if (keyfile == 0) {
105 perror("fopen");
106 return -1;
107 }
108 if (fscanf(keyfile, "%s\n", inkey) != 1) {
109 perror("fscanf");
110 return -1;
111 }
112 sd.length = strlen(inkey)+1;
113 if (api_exch_outtype(EXCH_TYPE_STORE_DESC, sizeof sd, (char *)&sd) == -1) {
114 return -1;
115 }
116 if (api_exch_outtype(EXCH_TYPE_BYTES, sd.length, inkey) == -1) {
117 return -1;
118 }
119 while ((i = api_exch_nextcommand()) != EXCH_CMD_ASSOCIATED) {
120 int passwd_length;
121 char *passwd, *getpass();
122 char buffer[200];
123
124 switch (i) {
125 case EXCH_CMD_REJECTED:
126 if (api_exch_intype(EXCH_TYPE_STORE_DESC,
127 sizeof sd, (char *)&sd) == -1) {
128 return -1;
129 }
130 if (api_exch_intype(EXCH_TYPE_BYTES, sd.length, buffer) == -1) {
131 return -1;
132 }
133 buffer[sd.length] = 0;
134 fprintf(stderr, "%s\n", buffer);
135 if (api_exch_outcommand(EXCH_CMD_ASSOCIATE) == -1) {
136 return -1;
137 }
138 break;
139 case EXCH_CMD_SEND_AUTH:
140 if (api_exch_intype(EXCH_TYPE_STORE_DESC, sizeof sd, (char *)&sd) == -1) {
141 return -1;
142 }
143 if (api_exch_intype(EXCH_TYPE_BYTES, sd.length, buffer) == -1) {
144 return -1;
145 }
146 buffer[sd.length] = 0;
147 passwd = getpass(buffer); /* Go to terminal */
148 passwd_length = strlen(passwd);
149 if (api_exch_intype(EXCH_TYPE_STORE_DESC, sizeof sd, (char *)&sd) == -1) {
150 return -1;
151 }
152 if (api_exch_intype(EXCH_TYPE_BYTES, sd.length, buffer) == -1) {
153 return -1;
154 }
155 buffer[sd.length] = 0;
156 if (sd.length) {
157 char *ptr;
158
159 ptr = passwd;
160 i = 0;
161 while (*ptr) {
162 *ptr++ ^= buffer[i++];
163 if (i >= sd.length) {
164 i = 0;
165 }
166 }
167 }
168 sd.length = passwd_length;
169 if (api_exch_outcommand(EXCH_CMD_AUTH) == -1) {
170 return -1;
171 }
172 if (api_exch_outtype(EXCH_TYPE_STORE_DESC, sizeof sd, (char *)&sd) == -1) {
173 return -1;
174 }
175 if (api_exch_outtype(EXCH_TYPE_BYTES, passwd_length, passwd) == -1) {
176 return -1;
177 }
178 break;
179 case -1:
180 return -1;
181 default:
182 fprintf(stderr,
183 "Waiting for connection indicator, received 0x%x.\n", i);
184 break;
185 }
186 }
187 /* YEAH */
188 return 0; /* Happiness! */
189 }
190
191
192 api_exch_api(regs, sregs, parms, length)
193 union REGS *regs;
194 struct SREGS *sregs;
195 char *parms;
196 int length;
197 {
198 struct storage_descriptor sd;
199 int i;
200
201 if (api_exch_outcommand(EXCH_CMD_REQUEST) == -1) {
202 return -1;
203 }
204 if (api_exch_outtype(EXCH_TYPE_REGS, sizeof *regs, (char *)regs) == -1) {
205 return -1;
206 }
207 if (api_exch_outtype(EXCH_TYPE_SREGS, sizeof *sregs, (char *)sregs) == -1) {
208 return -1;
209 }
210 sd.length = length;
211 sd.location = (long) parms;
212 if (api_exch_outtype(EXCH_TYPE_STORE_DESC, sizeof sd, (char *)&sd) == -1) {
213 return -1;
214 }
215 if (api_exch_outtype(EXCH_TYPE_BYTES, length, parms) == -1) {
216 return -1;
217 }
218 while ((i = api_exch_nextcommand()) != EXCH_CMD_REPLY) {
219 switch (i) {
220 case EXCH_CMD_GIMME:
221 if (api_exch_intype(EXCH_TYPE_STORE_DESC, sizeof sd, (char *)&sd)
222 == -1) {
223 return -1;
224 }
225 /*XXX validity check GIMME? */
226 if (api_exch_outcommand(EXCH_CMD_HEREIS) == -1) {
227 return -1;
228 }
229 if (api_exch_outtype(EXCH_TYPE_STORE_DESC, sizeof sd, (char *)&sd)
230 == -1) {
231 return -1;
232 }
233 if (api_exch_outtype(EXCH_TYPE_BYTES, sd.length,
234 (char *)sd.location) == -1) {
235 return -1;
236 }
237 break;
238 case EXCH_CMD_HEREIS:
239 if (api_exch_intype(EXCH_TYPE_STORE_DESC, sizeof sd, (char *)&sd)
240 == -1) {
241 return -1;
242 }
243 /* XXX Validty check HEREIS? */
244 if (api_exch_intype(EXCH_TYPE_BYTES, sd.length,
245 (char *)sd.location) == -1) {
246 return -1;
247 }
248 break;
249 default:
250 fprintf(stderr, "Waiting for reply command, we got command %d.\n",
251 i);
252 return -1;
253 }
254 }
255 if (api_exch_intype(EXCH_TYPE_REGS, sizeof *regs, (char *)regs) == -1) {
256 return -1;
257 }
258 if (api_exch_intype(EXCH_TYPE_SREGS, sizeof *sregs, (char *)sregs) == -1) {
259 return -1;
260 }
261 /* YEAH */
262 return 0; /* Happiness! */
263 }
264
265 #endif /* unix */