1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
/* $Xorg: XUrls.c,v 1.4 2001/02/09 02:05:58 xorgcvs Exp $ */
/*
Copyright 1996, 1998 The Open Group
Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABIL-
ITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
Except as contained in this notice, the name of The Open Group shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization from
The Open Group.
*/
/* $XFree86: xc/programs/xrx/rx/XUrls.c,v 1.11 2001/12/14 20:02:19 dawes Exp $ */
#include "RxI.h"
#include "XUrls.h"
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <limits.h> /* for MAXHOSTNAMELEN */
#include <errno.h>
/* and in case we didn't get it from the headers above */
#ifndef MAXHOSTNAMELEN
# define MAXHOSTNAMELEN 256
#endif
/*
* Return a good hostname, even on multi-homed hosts, e.g. machines
* connected to the internet via PPP which also have a local network
* are multi-homed.
*
* This might still fail to provide the right name if netscape is
* running on a different host than it is being displayed on. E.g,
*
* netscapehost <--ethernet--> display host <--ppp--//--> webserver
*
* The display host is multi-homed and has the nodename myhost.mynet
* as far as netscape and the netscape host are concerned, but has
* the name pppX.myispnet to the outside world. When this happens the
* user is going to have to set the XREALDISPLAY environment variable
* when they run netscape like this.
*/
static char*
MyBestHostname (
char* myname,
int myname_len,
char* display_name,
char* dest_url)
{
struct utsname host;
*myname = '\0';
/* if of the form ":0.0" get the real hostname */
if (display_name[0] == ':') {
/* for some reason this doesn't work on Solaris 2.x */
#if !(defined(sun) && defined(SVR4))
struct sockaddr_in local, remote;
struct hostent* hp;
int s, rv, namelen;
char dest_hostname[MAXHOSTNAMELEN + 1];
ParseHostname (dest_url, dest_hostname, sizeof dest_hostname);
hp = gethostbyname (dest_hostname);
if (hp) {
memcpy (&remote.sin_addr, hp->h_addr_list[0], sizeof remote.sin_addr);
remote.sin_port = htons(60000);
remote.sin_family = AF_INET;
#ifdef BSD44SOCKETS
remote.sin_len = sizeof remote;
#endif
local.sin_addr.s_addr = htonl(INADDR_ANY);
local.sin_port = htons(60000);
local.sin_family = AF_INET;
#ifdef BSD44SOCKETS
local.sin_len = sizeof remote;
#endif
s = socket(PF_INET, SOCK_DGRAM, 0);
if (s != -1) {
do {
rv = bind (s, (struct sockaddr*) &local, sizeof local);
local.sin_port = htons (ntohs (local.sin_port) + 1);
} while (rv == -1 && errno == EADDRINUSE);
if (rv != -1) {
do {
rv = connect (s, (struct sockaddr*) &remote, sizeof remote);
remote.sin_port = htons (ntohs (remote.sin_port) + 1);
} while (rv == -1 && errno == EADDRINUSE);
if (rv != -1) {
namelen = sizeof local;
rv = getsockname (s, (struct sockaddr*) &local, (void *)&namelen);
if (rv != -1) {
hp = gethostbyaddr ((char*) &local.sin_addr.s_addr,
sizeof local.sin_addr.s_addr,
AF_INET);
if (hp) {
strncpy (myname, hp->h_name, myname_len);
myname[MAXHOSTNAMELEN] = '\0';
close (s);
return display_name;
}
}
}
}
close (s);
}
}
#endif
/* none of the above worked, punt */
uname(&host);
strncpy (myname, host.nodename, myname_len);
myname[MAXHOSTNAMELEN] = '\0';
} else { /* otherwise believe the display_name */
char *ptr;
ptr = strchr(display_name, ':');
if (ptr == NULL) {
/* if there's no ":0" in the name, just copy it */
strncpy(myname, display_name, myname_len);
myname[MAXHOSTNAMELEN] = '\0';
} else {
/* otherwise copy everthing up to the ":0" */
strncpy(myname, display_name, ptr - display_name);
myname[ptr - display_name] = '\0';
return ptr;
}
}
return display_name;
}
/* return the Url of the user's X display
* this must be of the form:
* x11: [protocol/] [hostname] : [:] displaynumber [.screennumber] \
* [ ; auth= auth_data ]
*/
char *
GetXUrl(char *display_name, char *auth, char* dest_url)
{
char *dpy_name, *proto = NULL;
char *url, *ptr;
char *name;
struct hostent *host;
int len, proto_len, dpy_len, name_len, auth_len;
char hostname[MAXHOSTNAMELEN + 1];
len = 5; /* this is for "x11:" */
/* if of the form "x11:display" change it to "display" */
if (strncmp(display_name, "x11:", 4) == 0)
dpy_name = display_name + 4;
else
dpy_name = display_name;
/* if protocol is specified store it and remove it from display name */
ptr = strchr(dpy_name, '/');
if (ptr != NULL) {
proto = dpy_name;
proto_len = ptr - dpy_name;
dpy_name = ptr + 1;
/* if local forget it */
if (strncmp(proto, "local", proto_len) == 0)
proto_len = 0;
} else
proto_len = 0;
/* if of the form "unix:0.0" change it to ":0.0" */
if (strncmp(dpy_name, "unix", 4) == 0)
dpy_name += 4;
dpy_name = MyBestHostname (hostname, MAXHOSTNAMELEN, dpy_name, dest_url);
host = gethostbyname(hostname);
name = host->h_name;
name_len = strlen(name);
dpy_len = dpy_name == NULL ? 0 : strlen(dpy_name);
auth_len = auth == NULL ? 0 : 6 + strlen(auth); /* 6 for ";auth=" */
len += proto_len + 1 + name_len + dpy_len + auth_len;
url = ptr = (char *)Malloc(len);
if (url == NULL)
return NULL;
strcpy(ptr, "x11:");
ptr += 4;
if (proto_len != 0) {
strncpy(ptr, proto, proto_len + 1);
ptr += proto_len + 1;
}
if (name_len != 0) {
strcpy(ptr, name);
ptr += name_len;
}
if (dpy_len != 0) {
strcpy(ptr, dpy_name);
ptr += dpy_len;
}
if (auth_len != 0)
sprintf(ptr, ";auth=%s", auth);
else
*ptr = '\0';
return url;
}
/* return the Url of the user's XPrint server
* this must be of the form:
* xprint: [printername@] [protocol/] [hostname] : [:] displaynumber \
* [ ; auth= auth_data ]
*/
char *
GetXPrintUrl(char *display_name, char *printer, char *auth, char* dest_url)
{
char *dpy_name, *proto = NULL;
char *url, *ptr;
char *name;
struct hostent *host;
int len, proto_len, dpy_len, name_len, auth_len, printer_len;
char hostname[MAXHOSTNAMELEN + 1];
len = 7; /* this is for "xprint:" */
/* if of the form "xprint:display" change it to "display" */
if (strncmp(display_name, "xprint:", 7) == 0)
dpy_name = display_name + 7;
else
dpy_name = display_name;
/* if protocol is specified store it and remove it from display name */
ptr = strchr(dpy_name, '/');
if (ptr != NULL) {
proto = dpy_name;
proto_len = ptr - dpy_name;
dpy_name = ptr + 1;
/* if local forget it */
if (strncmp(proto, "local", proto_len) == 0)
proto_len = 0;
} else
proto_len = 0;
/* if of the form "unix:0.0" change it to ":0.0" */
if (strncmp(dpy_name, "unix", 4) == 0)
dpy_name += 4;
dpy_name = MyBestHostname (hostname, MAXHOSTNAMELEN, dpy_name, dest_url);
host = gethostbyname(hostname);
name = host->h_name;
/* if a screen number is specified strip it - not valid for printing */
ptr = strchr(dpy_name, '.');
if (ptr != NULL)
dpy_len = ptr - dpy_name;
else
dpy_len = strlen(dpy_name);
name_len = strlen(name);
printer_len = printer ? strlen(printer) : 0;
auth_len = auth == NULL ? 0 : 6 + strlen(auth); /* 6 for ";auth=" */
len += printer_len + 1 + proto_len + 1 + name_len + dpy_len + auth_len;
url = ptr = (char *)Malloc(len);
if (url == NULL)
return NULL;
strcpy(ptr, "xprint:");
ptr += 7;
if (printer_len != 0) {
strcpy(ptr, printer);
ptr += printer_len;
*ptr++ = '@';
}
if (proto_len != 0) {
strncpy(ptr, proto, proto_len + 1);
ptr += proto_len + 1;
}
if (name_len != 0) {
strcpy(ptr, name);
ptr += name_len;
}
if (dpy_len != 0) {
strncpy(ptr, dpy_name, dpy_len);
ptr += dpy_len;
}
if (auth_len != 0)
sprintf(ptr, ";auth=%s", auth);
else
*ptr = '\0';
return url;
}
/*
* Extract the hostname of a given url. The hostname is copied to the given
* buffer if it is big enough and its length is returned.
* This relies strongly on the following expected syntax:
* scheme : [//] hostname [: port] [/path]
*/
int
ParseHostname(char *url, char *buf, int buflen)
{
char *ptr, *begin;
if (url == NULL)
return 0;
/* skip scheme part */
ptr = strchr(url, ':');
if (ptr != NULL)
ptr++;
else
ptr = url;
/* skip possible "//" */
while (*ptr && *ptr == '/')
ptr++;
begin = ptr;
/* look for possible port specification */
ptr = strchr(begin, ':');
if (ptr == NULL) {
/* look for possible path */
ptr = strchr(begin, '/');
if (ptr == NULL)
ptr += strlen(begin);
}
if (ptr - begin < buflen) {
strncpy(buf, begin, ptr - begin);
buf[ptr - begin] = '\0';
return ptr - begin;
} else
return 0;
}
|