/[cmucl]/src/clx/socket.c
ViewVC logotype

Contents of /src/clx/socket.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (show annotations)
Wed Jun 17 18:22:46 2009 UTC (4 years, 10 months ago) by rtoy
Branch: MAIN
CVS Tags: sparc-tramp-assem-base, post-merge-intl-branch, intl-branch-working-2010-02-19-1000, unicode-string-buffer-impl-base, release-20b-pre1, release-20b-pre2, unicode-string-buffer-base, sparc-tramp-assem-2010-07-19, amd64-dd-start, intl-2-branch-base, GIT-CONVERSION, cross-sol-x86-merged, intl-branch-working-2010-02-11-1000, RELEASE_20b, release-20a-base, cross-sol-x86-base, snapshot-2010-12, snapshot-2010-11, snapshot-2011-09, snapshot-2011-06, snapshot-2011-07, snapshot-2011-04, snapshot-2011-02, snapshot-2011-03, snapshot-2011-01, pre-merge-intl-branch, snapshot-2010-05, snapshot-2010-04, snapshot-2010-07, snapshot-2010-06, snapshot-2010-01, snapshot-2010-03, snapshot-2010-02, snapshot-2010-08, cross-sol-x86-2010-12-20, intl-branch-2010-03-18-1300, RELEASE_20a, release-20a-pre1, snapshot-2009-11, snapshot-2009-12, cross-sparc-branch-base, intl-branch-base, snapshot-2009-08, snapshot-2009-07, HEAD
Branch point for: cross-sparc-branch, RELEASE-20B-BRANCH, unicode-string-buffer-branch, sparc-tramp-assem-branch, RELEASE-20A-BRANCH, amd64-dd-branch, unicode-string-buffer-impl-branch, intl-branch, cross-sol-x86-branch, intl-2-branch
Changes since 1.4: +1 -1 lines
File MIME type: text/plain
Merge portable-clx (2009-06-16) to main branch.  Tested by running
src/contrib/games/feebs and hemlock which works (in non-unicode
builds).
1 /* Copyright Massachusetts Institute of Technology 1988 */
2 /*
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.
5 *
6 * This code was cribbed from lib/X/XConnDis.c.
7 * Compile using
8 * % cc -c socket.c -DUNIXCONN
9 *
10 * $Id: socket.c,v 1.5 2009/06/17 18:22:46 rtoy Rel $
11 */
12
13 #include <stdio.h>
14 #include <X11/Xos.h>
15 #include <X11/Xproto.h>
16 #include <errno.h>
17 #include <netinet/in.h>
18 #include <sys/ioctl.h>
19 #include <netdb.h>
20 #include <sys/socket.h>
21 #ifndef hpux
22 #include <netinet/tcp.h>
23 #endif
24
25 extern int errno; /* Certain (broken) OS's don't have this */
26 /* decl in errno.h */
27
28 #ifdef UNIXCONN
29 #include <sys/un.h>
30 #ifndef X_UNIX_PATH
31 #ifdef hpux
32 #define X_UNIX_PATH "/usr/spool/sockets/X11/"
33 #define OLD_UNIX_PATH "/tmp/.X11-unix/X"
34 #else /* hpux */
35 #define X_UNIX_PATH "/tmp/.X11-unix/X"
36 #endif /* hpux */
37 #endif /* X_UNIX_PATH */
38 #endif /* UNIXCONN */
39
40 #ifndef hpux
41 void bcopy();
42 #endif /* hpux */
43
44 /*
45 * Attempts to connect to server, given host and display. Returns file
46 * descriptor (network socket) or 0 if connection fails.
47 */
48
49 int connect_to_server (host, display)
50 char *host;
51 int display;
52 {
53 struct sockaddr_in inaddr; /* INET socket address. */
54 struct sockaddr *addr; /* address to connect to */
55 struct hostent *host_ptr;
56 int addrlen; /* length of address */
57 #ifdef UNIXCONN
58 struct sockaddr_un unaddr; /* UNIX socket address. */
59 #endif
60 extern char *getenv();
61 extern struct hostent *gethostbyname();
62 int fd; /* Network socket */
63 {
64 #ifdef UNIXCONN
65 if ((host[0] == '\0') || (strcmp("unix", host) == 0)) {
66 /* Connect locally using Unix domain. */
67 unaddr.sun_family = AF_UNIX;
68 (void) strcpy(unaddr.sun_path, X_UNIX_PATH);
69 (void) sprintf(&unaddr.sun_path[strlen(unaddr.sun_path)], "%d", display);
70 addr = (struct sockaddr *) &unaddr;
71 addrlen = strlen(unaddr.sun_path) + 2;
72 /*
73 * Open the network connection.
74 */
75 if ((fd = socket((int) addr->sa_family, SOCK_STREAM, 0)) < 0) {
76 #ifdef hpux /* this is disgusting */ /* cribbed from X11R4 xlib source */
77 if (errno == ENOENT) { /* No such file or directory */
78 (void) sprintf(unaddr.sun_path, "%s%d", OLD_UNIX_PATH, display);
79 addrlen = strlen(unaddr.sun_path) + 2;
80 if ((fd = socket ((int) addr->sa_family, SOCK_STREAM, 0)) < 0)
81 return(-1); /* errno set by most recent system call. */
82 } else
83 #endif /* hpux */
84 return(-1); /* errno set by system call. */
85 }
86 } else
87 #endif /* UNIXCONN */
88 {
89 /* Get the statistics on the specified host. */
90 if ((inaddr.sin_addr.s_addr = inet_addr(host)) == -1)
91 {
92 if ((host_ptr = gethostbyname(host)) == NULL)
93 {
94 /* No such host! */
95 errno = EINVAL;
96 return(-1);
97 }
98 /* Check the address type for an internet host. */
99 if (host_ptr->h_addrtype != AF_INET)
100 {
101 /* Not an Internet host! */
102 errno = EPROTOTYPE;
103 return(-1);
104 }
105 /* Set up the socket data. */
106 inaddr.sin_family = host_ptr->h_addrtype;
107 #ifdef hpux
108 (void) memcpy((char *)&inaddr.sin_addr,
109 (char *)host_ptr->h_addr,
110 sizeof(inaddr.sin_addr));
111 #else /* hpux */
112 (void) bcopy((char *)host_ptr->h_addr,
113 (char *)&inaddr.sin_addr,
114 sizeof(inaddr.sin_addr));
115 #endif /* hpux */
116 }
117 else
118 {
119 inaddr.sin_family = AF_INET;
120 }
121 addr = (struct sockaddr *) &inaddr;
122 addrlen = sizeof (struct sockaddr_in);
123 inaddr.sin_port = display + X_TCP_PORT;
124 inaddr.sin_port = htons(inaddr.sin_port);
125 /*
126 * Open the network connection.
127 */
128 if ((fd = socket((int) addr->sa_family, SOCK_STREAM, 0)) < 0){
129 return(-1); /* errno set by system call. */}
130 /* make sure to turn off TCP coalescence */
131 #ifdef TCP_NODELAY
132 {
133 int mi = 1;
134 setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &mi, sizeof (int));
135 }
136 #endif
137 }
138
139 /*
140 * Changed 9/89 to retry connection if system call was interrupted. This
141 * is necessary for multiprocessing implementations that use timers,
142 * since the timer results in a SIGALRM. -- jdi
143 */
144 while (connect(fd, addr, addrlen) == -1) {
145 if (errno != EINTR) {
146 (void) close (fd);
147 return(-1); /* errno set by system call. */
148 }
149 }
150 }
151 /*
152 * Return the id if the connection succeeded.
153 */
154 return(fd);
155 }

  ViewVC Help
Powered by ViewVC 1.1.5