annotate lib-src/gnuslib.c @ 5481:5008a40f7460

Added tag r21-5-31 for changeset f5a0b32f4687
author Stephen J. Turnbull <stephen@xemacs.org>
date Fri, 29 Apr 2011 22:57:14 +0900
parents b9167d522a9a
children 3cc7470ea71c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1 /* -*-C-*-
613
023b83f4e54b [xemacs-hg @ 2001-06-10 10:42:16 by ben]
ben
parents: 458
diff changeset
2 Common library code for the XEmacs server and client.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
3
5420
b9167d522a9a Rebase with 21.5 trunk.
Mats Lidell <matsl@xemacs.org>
parents: 5406 5290
diff changeset
4
5290
e6508b64ee08 More permission consistency.
Stephen J. Turnbull <stephen@xemacs.org>
parents: 3556
diff changeset
5
613
023b83f4e54b [xemacs-hg @ 2001-06-10 10:42:16 by ben]
ben
parents: 458
diff changeset
6 This file is part of XEmacs.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
7
5406
061f4f90f874 Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 3556
diff changeset
8 XEmacs is free software: you can redistribute it and/or modify it
061f4f90f874 Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 3556
diff changeset
9 under the terms of the GNU General Public License as published by the
061f4f90f874 Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 3556
diff changeset
10 Free Software Foundation, either version 3 of the License, or (at your
061f4f90f874 Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 3556
diff changeset
11 option) any later version.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
12
5406
061f4f90f874 Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 3556
diff changeset
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
061f4f90f874 Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 3556
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
061f4f90f874 Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 3556
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
061f4f90f874 Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 3556
diff changeset
16 for more details.
5290
e6508b64ee08 More permission consistency.
Stephen J. Turnbull <stephen@xemacs.org>
parents: 3556
diff changeset
17
5406
061f4f90f874 Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 3556
diff changeset
18 You should have received a copy of the GNU General Public License
061f4f90f874 Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents: 3556
diff changeset
19 along with XEmacs. If not, see <http://www.gnu.org/licenses/>.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
20
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
21 Copyright (C) 1989 Free Software Foundation, Inc.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
22
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
23 Author: Andy Norman (ange@hplb.hpl.hp.com), based on
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
24 'etc/server.c' and 'etc/emacsclient.c' from the 18.52 GNU
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
25 Emacs distribution.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
26
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
27 Please mail bugs and suggestions to the author at the above address.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
28 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
29
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
30 /* HISTORY
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
31 * 11-Nov-1990 bristor@simba
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
32 * Added EOT stuff.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
33 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
34
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
35 /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
36 * This file incorporates new features added by Bob Weiner <weiner@mot.com>,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
37 * Darrell Kindred <dkindred@cmu.edu> and Arup Mukherjee <arup@cmu.edu>.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
38 * Please see the note at the end of the README file for details.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
39 *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
40 * (If gnuserv came bundled with your emacs, the README file is probably
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
41 * ../etc/gnuserv.README relative to the directory containing this file)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
42 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
43
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
44 #if 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
45 static char rcsid [] = "!Header: gnuslib.c,v 2.4 95/02/16 11:57:37 arup alpha !";
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
46 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
47
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
48 #include "gnuserv.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
49 #include <errno.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
50
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
51 #ifdef SYSV_IPC
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
52 static int connect_to_ipc_server (void);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
53 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
54 #ifdef UNIX_DOMAIN_SOCKETS
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
55 static int connect_to_unix_server (void);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
56 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
57 #ifdef INTERNET_DOMAIN_SOCKETS
458
c33ae14dd6d0 Import from CVS: tag r21-2-44
cvs
parents: 442
diff changeset
58 static int connect_to_internet_server (char *serverhost, unsigned short port);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
59 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
60
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
61 /* On some systems, e.g. DGUX, inet_addr returns a 'struct in_addr'. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
62 #ifdef HAVE_BROKEN_INET_ADDR
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
63 # define IN_ADDR struct in_addr
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
64 # define NUMERIC_ADDR_ERROR (numeric_addr.s_addr == -1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
65 #else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
66 # if (LONGBITS > 32)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
67 # define IN_ADDR unsigned int
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
68 # else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
69 # define IN_ADDR unsigned long
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
70 # endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
71 # define NUMERIC_ADDR_ERROR (numeric_addr == (IN_ADDR) -1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
72 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
73
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
74 #include <stdlib.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
75 #include <stdio.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
76 #include <sys/types.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
77 #include <sys/stat.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
78 #ifdef HAVE_UNISTD_H
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
79 #include <unistd.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
80 #endif /* HAVE_UNISTD_H */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
81 #ifdef HAVE_STRING_H
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
82 #include <string.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
83 #endif /* HAVE_STRING_H */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
84
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
85 #include <arpa/inet.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
86
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
87 char *tmpdir = NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
88
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
89 char *progname = NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
90
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
91 int
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
92 make_connection (char *hostarg, int portarg, int *s)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
93 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
94 #ifdef INTERNET_DOMAIN_SOCKETS
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
95 char *ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
96 if (hostarg == NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
97 hostarg = getenv("GNU_HOST");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
98 if (portarg == 0 && (ptr=getenv("GNU_PORT")) != NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
99 portarg = atoi(ptr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
100 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
101
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
102 if (hostarg != NULL) {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
103 /* hostname was given explicitly, via cmd line arg or GNU_HOST,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
104 * so obey it. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
105 #ifdef UNIX_DOMAIN_SOCKETS
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
106 if (!strcmp(hostarg, "unix")) {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
107 *s = connect_to_unix_server();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
108 return (int) CONN_UNIX;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
109 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
110 #endif /* UNIX_DOMAIN_SOCKETS */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
111 #ifdef INTERNET_DOMAIN_SOCKETS
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
112 *s = connect_to_internet_server(hostarg, portarg);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
113 return (int) CONN_INTERNET;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
114 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
115 #ifdef SYSV_IPC
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
116 return -1; /* hostarg should always be NULL for SYSV_IPC */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
117 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
118 } else {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
119 /* no hostname given. Use unix-domain/sysv-ipc, or
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
120 * internet-domain connection to local host if they're not available. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
121 #if defined(UNIX_DOMAIN_SOCKETS)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
122 *s = connect_to_unix_server();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
123 return (int) CONN_UNIX;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
124 #elif defined(SYSV_IPC)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
125 *s = connect_to_ipc_server();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
126 return (int) CONN_IPC;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
127 #elif defined(INTERNET_DOMAIN_SOCKETS)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
128 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
129 char localhost[HOSTNAMSZ];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
130 gethostname(localhost,HOSTNAMSZ); /* use this host by default */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
131 *s = connect_to_internet_server(localhost, portarg);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
132 return (int) CONN_INTERNET;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
133 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
134 #endif /* IPC type */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
135 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
136 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
137
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
138 #ifdef SYSV_IPC
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
139 /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
140 connect_to_ipc_server -- establish connection with server process via SYSV IPC
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
141 Returns msqid for server if successful.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
142 */
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
143 static int
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
144 connect_to_ipc_server (void)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
145 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
146 int s; /* connected msqid */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
147 key_t key; /* message key */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
148 char buf[GSERV_BUFSZ+1]; /* buffer for filename */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
149
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
150 sprintf(buf,"%s/gsrv%d",tmpdir,(int)geteuid());
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
151 creat(buf,0600);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
152 if ((key = ftok(buf,1)) == -1) {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
153 perror(progname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
154 fprintf(stderr, "%s: unable to get ipc key from %s\n",
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
155 progname, buf);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
156 exit(1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
157 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
158
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
159 if ((s = msgget(key,0600)) == -1) {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
160 perror(progname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
161 fprintf(stderr,"%s: unable to access msg queue\n",progname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
162 exit(1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
163 }; /* if */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
164
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
165 return(s);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
166
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
167 } /* connect_to_ipc_server */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
168
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
169
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
170 /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
171 disconnect_from_ipc_server -- inform the server that sending has finished,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
172 and wait for its reply.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
173 */
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
174 void
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
175 disconnect_from_ipc_server (int s, struct msgbuf *msgp, int echo)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
176 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
177 int len; /* length of received message */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
178
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
179 send_string(s,EOT_STR); /* EOT terminates this message */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
180 msgp->mtype = 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
181
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
182 if(msgsnd(s,msgp,strlen(msgp->mtext)+1,0) < 0) {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
183 perror(progname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
184 fprintf(stderr,"%s: unable to send message to server\n",progname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
185 exit(1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
186 }; /* if */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
187
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
188 if((len = msgrcv(s,msgp,GSERV_BUFSZ,getpid(),0)) < 0) {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
189 perror(progname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
190 fprintf(stderr,"%s: unable to receive message from server\n",progname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
191 exit(1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
192 }; /* if */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
193
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
194 if (echo) {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
195 msgp->mtext[len] = '\0'; /* string terminate message */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
196 fputs(msgp->mtext, stdout);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
197 if (msgp->mtext[len-1] != '\n') putchar ('\n');
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
198 }; /* if */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
199
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
200 } /* disconnect_from_ipc_server */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
201 #endif /* SYSV_IPC */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
202
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
203
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
204 #if defined(INTERNET_DOMAIN_SOCKETS) || defined(UNIX_DOMAIN_SOCKETS)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
205 /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
206 send_string -- send string to socket.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
207 */
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
208 void
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
209 send_string (int s, const char *msg)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
210 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
211 #if 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
212 if (send(s,msg,strlen(msg),0) < 0) {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
213 perror(progname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
214 fprintf(stderr,"%s: unable to send\n",progname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
215 exit(1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
216 }; /* if */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
217 #else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
218 int len, left=strlen(msg);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
219 while (left > 0) {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
220 if ((len=write(s,msg,min2(left,GSERV_BUFSZ))) < 0) {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
221 /* XEmacs addition: robertl@arnet.com */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
222 if (errno == EPIPE) {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
223 return ;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
224 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
225 perror(progname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
226 fprintf(stderr,"%s: unable to send\n",progname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
227 exit(1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
228 }; /* if */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
229 left -= len;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
230 msg += len;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
231 }; /* while */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
232 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
233 } /* send_string */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
234
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
235 /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
236 read_line -- read a \n terminated line from a socket
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
237 */
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
238 int
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
239 read_line (int s, char *dest)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
240 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
241 int length;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
242 int offset=0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
243 char buffer[GSERV_BUFSZ+1];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
244
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
245 while ((length=read(s,buffer+offset,1)>0) && buffer[offset]!='\n'
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
246 && buffer[offset] != EOT_CHR) {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
247 offset += length;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
248 if (offset >= GSERV_BUFSZ)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
249 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
250 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
251 buffer[offset] = '\0';
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
252 strcpy(dest,buffer);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
253 return 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
254 } /* read_line */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
255 #endif /* INTERNET_DOMAIN_SOCKETS || UNIX_DOMAIN_SOCKETS */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
256
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
257
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
258 #ifdef UNIX_DOMAIN_SOCKETS
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
259 /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
260 connect_to_unix_server -- establish connection with server process via a unix-
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
261 domain socket. Returns socket descriptor for server
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
262 if successful.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
263 */
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
264 static int
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
265 connect_to_unix_server (void)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
266 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
267 int s; /* connected socket descriptor */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
268 struct sockaddr_un server; /* for unix connections */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
269
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
270 if ((s = socket(AF_UNIX,SOCK_STREAM,0)) < 0) {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
271 perror(progname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
272 fprintf(stderr,"%s: unable to create socket\n",progname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
273 exit(1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
274 }; /* if */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
275
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
276 server.sun_family = AF_UNIX;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
277 #ifdef HIDE_UNIX_SOCKET
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
278 sprintf(server.sun_path,"%s/gsrvdir%d/gsrv",tmpdir,(int)geteuid());
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
279 #else /* HIDE_UNIX_SOCKET */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
280 sprintf(server.sun_path,"%s/gsrv%d",tmpdir,(int)geteuid());
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
281 #endif /* HIDE_UNIX_SOCKET */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
282 if (connect(s,(struct sockaddr *)&server,strlen(server.sun_path)+2) < 0) {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
283 perror(progname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
284 fprintf(stderr,"%s: unable to connect to local\n",progname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
285 exit(1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
286 }; /* if */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
287
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
288 return(s);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
289
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
290 } /* connect_to_unix_server */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
291 #endif /* UNIX_DOMAIN_SOCKETS */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
292
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
293
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
294 #ifdef INTERNET_DOMAIN_SOCKETS
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
295 /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
296 internet_addr -- return the internet addr of the hostname or
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
297 internet address passed. Return -1 on error.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
298 */
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
299 int
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
300 internet_addr (char *host)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
301 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
302 struct hostent *hp; /* pointer to host info for remote host */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
303 IN_ADDR numeric_addr; /* host address */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
304
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
305 numeric_addr = inet_addr(host);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
306 if (!NUMERIC_ADDR_ERROR)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
307 return numeric_addr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
308 else if ((hp = gethostbyname(host)) != NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
309 return ((struct in_addr *)(hp->h_addr))->s_addr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
310 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
311 return -1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
312
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
313 } /* internet_addr */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
314
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
315 #ifdef AUTH_MAGIC_COOKIE
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
316 # include <X11/X.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
317 # include <X11/Xauth.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
318
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
319 static Xauth *server_xauth = NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
320 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
321
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
322 /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
323 connect_to_internet_server -- establish connection with server process via
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
324 an internet domain socket. Returns socket
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
325 descriptor for server if successful.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
326 */
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
327 static int
458
c33ae14dd6d0 Import from CVS: tag r21-2-44
cvs
parents: 442
diff changeset
328 connect_to_internet_server (char *serverhost, unsigned short port)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
329 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
330 int s; /* connected socket descriptor */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
331 struct servent *sp; /* pointer to service information */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
332 struct sockaddr_in peeraddr_in; /* for peer socket address */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
333 char buf[512]; /* temporary buffer */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
334
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
335 /* clear out address structures */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
336 memset((char *)&peeraddr_in,0,sizeof(struct sockaddr_in));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
337
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
338 /* Set up the peer address to which we will connect. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
339 peeraddr_in.sin_family = AF_INET;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
340
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
341 /* look up the server host's internet address */
647
b39c14581166 [xemacs-hg @ 2001-08-13 04:45:47 by ben]
ben
parents: 613
diff changeset
342 if ((peeraddr_in.sin_addr.s_addr = internet_addr (serverhost)) ==
b39c14581166 [xemacs-hg @ 2001-08-13 04:45:47 by ben]
ben
parents: 613
diff changeset
343 (unsigned int) -1)
b39c14581166 [xemacs-hg @ 2001-08-13 04:45:47 by ben]
ben
parents: 613
diff changeset
344 {
b39c14581166 [xemacs-hg @ 2001-08-13 04:45:47 by ben]
ben
parents: 613
diff changeset
345 fprintf (stderr, "%s: unable to find %s in /etc/hosts or from YP\n",
b39c14581166 [xemacs-hg @ 2001-08-13 04:45:47 by ben]
ben
parents: 613
diff changeset
346 progname, serverhost);
b39c14581166 [xemacs-hg @ 2001-08-13 04:45:47 by ben]
ben
parents: 613
diff changeset
347 exit(1);
b39c14581166 [xemacs-hg @ 2001-08-13 04:45:47 by ben]
ben
parents: 613
diff changeset
348 }
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
349
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
350 if (port == 0) {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
351 if ((sp = getservbyname ("gnuserv","tcp")) == NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
352 peeraddr_in.sin_port = htons(DEFAULT_PORT+getuid());
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
353 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
354 peeraddr_in.sin_port = sp->s_port;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
355 } /* if */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
356 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
357 peeraddr_in.sin_port = htons(port);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
358
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
359 /* Create the socket. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
360 if ((s = socket (AF_INET,SOCK_STREAM, 0))== -1) {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
361 perror(progname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
362 fprintf(stderr,"%s: unable to create socket\n",progname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
363 exit(1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
364 }; /* if */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
365
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
366 /* Try to connect to the remote server at the address
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
367 * which was just built into peeraddr.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
368 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
369 if (connect(s, (struct sockaddr *)&peeraddr_in,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
370 sizeof(struct sockaddr_in)) == -1) {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
371 perror(progname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
372 fprintf(stderr, "%s: unable to connect to remote\n",progname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
373 exit(1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
374 }; /* if */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
375
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
376 #ifdef AUTH_MAGIC_COOKIE
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
377
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
378 /* send credentials using MIT-MAGIC-COOKIE-1 protocol */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
379
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
380 server_xauth =
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
381 XauGetAuthByAddr(FamilyInternet,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
382 sizeof(peeraddr_in.sin_addr.s_addr),
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
383 (char *) &peeraddr_in.sin_addr.s_addr,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
384 strlen(MCOOKIE_SCREEN), MCOOKIE_SCREEN,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
385 strlen(MCOOKIE_X_NAME), MCOOKIE_X_NAME);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
386
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
387 if (server_xauth && server_xauth->data) {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
388 sprintf(buf, "%s\n%d\n", MCOOKIE_NAME, server_xauth->data_length);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
389 write (s, buf, strlen(buf));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
390 write (s, server_xauth->data, server_xauth->data_length);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
391
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
392 return (s);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
393 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
394
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
395 #endif /* AUTH_MAGIC_COOKIE */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
396
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
397 sprintf (buf, "%s\n", DEFAUTH_NAME);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
398 write (s, buf, strlen(buf));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
399
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
400 return(s);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
401
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
402 } /* connect_to_internet_server */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
403 #endif /* INTERNET_DOMAIN_SOCKETS */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
404
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
405
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
406 #if defined(INTERNET_DOMAIN_SOCKETS) || defined(UNIX_DOMAIN_SOCKETS)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
407 /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
408 disconnect_from_server -- inform the server that sending has finished, and wait for
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
409 its reply.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
410 */
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
411 void
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
412 disconnect_from_server (int s, int echo)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
413 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
414 #if 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
415 char buffer[REPLYSIZ+1];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
416 #else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
417 char buffer[GSERV_BUFSZ+1];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
418 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
419 int add_newline = 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
420 int length;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
421
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
422 send_string(s,EOT_STR); /* make sure server gets string */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
423
3556
2161ac78b41e [xemacs-hg @ 2006-08-11 17:37:17 by james]
james
parents: 647
diff changeset
424 #ifndef _SCO_DS
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
425 /*
3556
2161ac78b41e [xemacs-hg @ 2006-08-11 17:37:17 by james]
james
parents: 647
diff changeset
426 * There used to be a comment here complaining about ancient Linux
2161ac78b41e [xemacs-hg @ 2006-08-11 17:37:17 by james]
james
parents: 647
diff changeset
427 * versions. It is no longer relevant. I don't know why _SCO_DS is
2161ac78b41e [xemacs-hg @ 2006-08-11 17:37:17 by james]
james
parents: 647
diff changeset
428 * verboten here, as the original comment did not say.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
429 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
430
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
431 if (shutdown(s,1) == -1) {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
432 perror(progname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
433 fprintf(stderr, "%s: unable to shutdown socket\n",progname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
434 exit(1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
435 }; /* if */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
436 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
437
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
438 #if 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
439 while((length = recv(s,buffer,REPLYSIZ,0)) > 0) {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
440 buffer[length] = '\0';
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
441 if (echo) fputs(buffer,stdout);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
442 add_newline = (buffer[length-1] != '\n');
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
443 }; /* while */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
444 #else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
445 while ((length = read(s,buffer,GSERV_BUFSZ)) > 0 ||
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
446 (length == -1 && errno == EINTR)) {
3556
2161ac78b41e [xemacs-hg @ 2006-08-11 17:37:17 by james]
james
parents: 647
diff changeset
447 if (length > 0) {
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
448 buffer[length] = '\0';
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
449 if (echo) {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
450 fputs(buffer,stdout);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
451 add_newline = (buffer[length-1] != '\n');
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
452 }; /* if */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
453 }; /* if */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
454 }; /* while */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
455 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
456
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
457 if (echo && add_newline) putchar('\n');
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
458
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
459 if(length < 0) {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
460 perror(progname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
461 fprintf(stderr,"%s: unable to read the reply from the server\n",progname);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
462 exit(1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
463 }; /* if */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
464
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
465 } /* disconnect_from_server */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
466 #endif /* INTERNET_DOMAIN_SOCKETS || UNIX_DOMAIN_SOCKETS */