xref: /csrg-svn/usr.bin/tn3270/api/api_bsd.c (revision 31459)
1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <netinet/in.h>
4 #include <netdb.h>
5 #include <stdio.h>
6 
7 #include "../api/api.h"
8 
9 
10 static int sock = -1;
11 
12 api_open_api(string)
13 char	*string;		/* if non-zero, where to connect to */
14 {
15     struct sockaddr_in server;
16     struct hostent *hp;
17     char *getenv();
18     char thehostname[100];
19     int port;
20 
21     if (string == 0) {
22 	string = getenv("API3270");	/* Get API */
23 	if (string == 0) {
24 	    fprintf(stderr,
25 			"API3270 environmental variable not set - no API.\n");
26 	    return -1;			/* Nothing */
27 	}
28     }
29 
30     if (sscanf(string, "%[^:]:%d", thehostname, &port) != 2) {
31 	fprintf(stderr, "API3270 environmental variable has bad format.\n");
32 	return -1;
33     }
34     /* Now, try to connect */
35     sock = socket(AF_INET, SOCK_STREAM, 0);
36     if (sock < 0) {
37 	perror("opening API socket");
38 	return -1;
39     }
40     server.sin_family = AF_INET;
41     hp = gethostbyname(thehostname);
42     if (hp == 0) {
43 	fprintf(stderr, "%s specifies bad host name.\n", string);
44 	return -1;
45     }
46     bcopy(hp->h_addr, &server.sin_addr, hp->h_length);
47     server.sin_port = htons(port);
48 
49     if (connect(sock, &server, sizeof server) < 0) {
50 	perror("connecting to API server");
51 	return -1;
52     }
53     /* YEAH */
54     return 0;		/* Happiness! */
55 }
56 
57 
58 api_exch_api(regs, sregs)
59 union REGS *regs;
60 struct SREGS *sregs;
61 {
62 }
63