1
/* Copyright Massachusetts Institute of Technology 1988 */
3
* THIS IS AN OS DEPENDENT FILE! It should work on 4.2BSD derived
4
* systems. VMS and System V should plan to have their own version.
6
* This code was cribbed from lib/X/XConnDis.c.
8
* % cc -c socket.c -DUNIXCONN
22
#include <sys/param.h>
24
#include <X11/Xproto.h>
26
#include <sys/types.h>
28
#include <sys/ioctl.h>
29
#include <sys/socket.h>
31
#include <netinet/in.h>
34
#include <netinet/tcp.h>
37
extern int errno; /* Certain (broken) OS's don't have this */
44
#define X_UNIX_PATH "/usr/spool/sockets/X11/"
45
#define OLD_UNIX_PATH "/tmp/.X11-unix/X"
47
#define X_UNIX_PATH "/tmp/.X11-unix/X"
49
#endif /* X_UNIX_PATH */
55
* Attempts to connect to server, given host and display. Returns file
56
* descriptor (network socket) or 0 if connection fails.
59
int connect_to_server (host, display)
63
struct sockaddr_in inaddr; /* INET socket address. */
64
struct sockaddr *addr; /* address to connect to */
65
struct hostent *host_ptr;
66
int addrlen; /* length of address */
68
struct sockaddr_un unaddr; /* UNIX socket address. */
70
extern char *getenv();
71
extern struct hostent *gethostbyname();
72
int fd; /* Network socket */
75
if ((host[0] == '\0') || (strcmp("unix", host) == 0)) {
76
/* Connect locally using Unix domain. */
77
unaddr.sun_family = AF_UNIX;
78
(void) strcpy(unaddr.sun_path, X_UNIX_PATH);
79
(void) sprintf(&unaddr.sun_path[strlen(unaddr.sun_path)], "%d", display);
80
addr = (struct sockaddr *) &unaddr;
81
addrlen = strlen(unaddr.sun_path) + 2;
83
* Open the network connection.
85
if ((fd = socket((int) addr->sa_family, SOCK_STREAM, 0)) < 0) {
86
#ifdef hpux /* this is disgusting */ /* cribbed from X11R4 xlib source */
87
if (errno == ENOENT) { /* No such file or directory */
88
(void) sprintf(unaddr.sun_path, "%s%d", OLD_UNIX_PATH, display);
89
addrlen = strlen(unaddr.sun_path) + 2;
90
if ((fd = socket ((int) addr->sa_family, SOCK_STREAM, 0)) < 0)
91
return(-1); /* errno set by most recent system call. */
94
return(-1); /* errno set by system call. */
99
/* Get the statistics on the specified host. */
100
if ((inaddr.sin_addr.s_addr = inet_addr(host)) == -1)
102
if ((host_ptr = gethostbyname(host)) == NULL)
108
/* Check the address type for an internet host. */
109
if (host_ptr->h_addrtype != AF_INET)
111
/* Not an Internet host! */
115
/* Set up the socket data. */
116
inaddr.sin_family = host_ptr->h_addrtype;
118
(void) memcpy((char *)&inaddr.sin_addr,
119
(char *)host_ptr->h_addr,
120
sizeof(inaddr.sin_addr));
122
(void) bcopy((char *)host_ptr->h_addr,
123
(char *)&inaddr.sin_addr,
124
sizeof(inaddr.sin_addr));
129
inaddr.sin_family = AF_INET;
131
addr = (struct sockaddr *) &inaddr;
132
addrlen = sizeof (struct sockaddr_in);
133
inaddr.sin_port = display + X_TCP_PORT;
134
inaddr.sin_port = htons(inaddr.sin_port);
136
* Open the network connection.
138
if ((fd = socket((int) addr->sa_family, SOCK_STREAM, 0)) < 0){
139
return(-1); /* errno set by system call. */}
140
/* make sure to turn off TCP coalescence */
144
setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &mi, sizeof (int));
150
* Changed 9/89 to retry connection if system call was interrupted. This
151
* is necessary for multiprocessing implementations that use timers,
152
* since the timer results in a SIGALRM. -- jdi
154
while (connect(fd, addr, addrlen) == -1) {
155
if (errno != EINTR) {
157
return(-1); /* errno set by system call. */
162
* Return the id if the connection succeeded.