~ubuntu-branches/ubuntu/quantal/gclcvs/quantal

« back to all changes in this revision

Viewing changes to o/clxsocket.c

  • Committer: Bazaar Package Importer
  • Author(s): Camm Maguire
  • Date: 2004-06-24 15:13:46 UTC
  • Revision ID: james.westby@ubuntu.com-20040624151346-xh0xaaktyyp7aorc
Tags: 2.7.0-26
C_GC_OFFSET is 2 on m68k-linux

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
11
#include "include.h"
 
12
 
 
13
#ifdef HAVE_X11
 
14
 
 
15
 
 
16
#undef PAGESIZE
 
17
#undef MAXPATHLEN
 
18
#ifndef NO_UNIXCONN
 
19
#define UNIXCONN
 
20
#endif
 
21
 
 
22
#include <sys/param.h>
 
23
#include <X11/Xos.h>
 
24
#include <X11/Xproto.h>
 
25
#include <errno.h>
 
26
#include <sys/types.h>
 
27
 
 
28
#include <sys/ioctl.h>
 
29
#include <sys/socket.h>
 
30
#include <netdb.h>
 
31
#include <netinet/in.h>
 
32
 
 
33
#ifndef hpux
 
34
#include <netinet/tcp.h>
 
35
#endif
 
36
 
 
37
extern int errno;               /* Certain (broken) OS's don't have this */
 
38
                                /* decl in errno.h */
 
39
 
 
40
#ifdef UNIXCONN
 
41
#include <sys/un.h>
 
42
#ifndef X_UNIX_PATH
 
43
#ifdef hpux
 
44
#define X_UNIX_PATH "/usr/spool/sockets/X11/"
 
45
#define OLD_UNIX_PATH "/tmp/.X11-unix/X"
 
46
#else /* hpux */
 
47
#define X_UNIX_PATH "/tmp/.X11-unix/X"
 
48
#endif /* hpux */
 
49
#endif /* X_UNIX_PATH */
 
50
#endif /* UNIXCONN */
 
51
 
 
52
 
 
53
 
 
54
/* 
 
55
 * Attempts to connect to server, given host and display. Returns file 
 
56
 * descriptor (network socket) or 0 if connection fails.
 
57
 */
 
58
 
 
59
int connect_to_server (host, display)
 
60
     char *host;
 
61
     int display;
 
62
{
 
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 */
 
67
#ifdef UNIXCONN
 
68
  struct sockaddr_un unaddr;    /* UNIX socket address. */
 
69
#endif
 
70
  extern char *getenv();
 
71
  extern struct hostent *gethostbyname();
 
72
  int fd;                               /* Network socket */
 
73
  {
 
74
#ifdef UNIXCONN
 
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;
 
82
        /*
 
83
         * Open the network connection.
 
84
         */
 
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. */
 
92
            } else 
 
93
#endif /* hpux */
 
94
            return(-1);     /* errno set by system call. */
 
95
        }
 
96
    } else 
 
97
#endif /* UNIXCONN */
 
98
    {
 
99
      /* Get the statistics on the specified host. */
 
100
      if ((inaddr.sin_addr.s_addr = inet_addr(host)) == -1) 
 
101
        {
 
102
          if ((host_ptr = gethostbyname(host)) == NULL) 
 
103
            {
 
104
              /* No such host! */
 
105
              errno = EINVAL;
 
106
              return(-1);
 
107
            }
 
108
          /* Check the address type for an internet host. */
 
109
          if (host_ptr->h_addrtype != AF_INET) 
 
110
            {
 
111
              /* Not an Internet host! */
 
112
              errno = EPROTOTYPE;
 
113
              return(-1);
 
114
            }
 
115
          /* Set up the socket data. */
 
116
          inaddr.sin_family = host_ptr->h_addrtype;
 
117
#ifdef hpux
 
118
          (void) memcpy((char *)&inaddr.sin_addr, 
 
119
                        (char *)host_ptr->h_addr, 
 
120
                        sizeof(inaddr.sin_addr));
 
121
#else /* hpux */
 
122
          (void) bcopy((char *)host_ptr->h_addr, 
 
123
                       (char *)&inaddr.sin_addr, 
 
124
                       sizeof(inaddr.sin_addr));
 
125
#endif /* hpux */
 
126
        } 
 
127
      else 
 
128
        {
 
129
          inaddr.sin_family = AF_INET;
 
130
        }
 
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);
 
135
      /*
 
136
       * Open the network connection.
 
137
       */
 
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 */
 
141
#ifdef TCP_NODELAY
 
142
      {
 
143
        int mi = 1;
 
144
        setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &mi, sizeof (int));
 
145
      }
 
146
#endif
 
147
    }
 
148
 
 
149
    /*
 
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
 
153
     */
 
154
    while (connect(fd, addr, addrlen) == -1) {
 
155
        if (errno != EINTR) {
 
156
            (void) close (fd);
 
157
            return(-1);             /* errno set by system call. */
 
158
        }
 
159
      }
 
160
  }
 
161
  /*
 
162
   * Return the id if the connection succeeded.
 
163
   */
 
164
  return(fd);
 
165
}
 
166
#endif