| 1 |
root |
1.10 |
/*----------------------------------------------------------------------* |
| 2 |
pcg |
1.7 |
* File: netdisp.C |
| 3 |
pcg |
1.1 |
*----------------------------------------------------------------------* |
| 4 |
|
|
* |
| 5 |
|
|
* All portions of code are copyright by their respective author/s. |
| 6 |
|
|
* Copyright (c) 1996 Chuck Blake <cblake@BBN.COM> |
| 7 |
|
|
* - original version |
| 8 |
|
|
* Copyright (c) 1997 mj olesen <olesen@me.queensu.ca> |
| 9 |
|
|
* Copyright (c) 1997,1998 Oezguer Kesim <kesim@math.fu-berlin.de> |
| 10 |
|
|
* |
| 11 |
|
|
* This program is free software; you can redistribute it and/or modify |
| 12 |
|
|
* it under the terms of the GNU General Public License as published by |
| 13 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
| 14 |
|
|
* (at your option) any later version. |
| 15 |
|
|
* |
| 16 |
|
|
* This program is distributed in the hope that it will be useful, |
| 17 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
|
|
* GNU General Public License for more details. |
| 20 |
|
|
* |
| 21 |
|
|
* You should have received a copy of the GNU General Public License |
| 22 |
|
|
* along with this program; if not, write to the Free Software |
| 23 |
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 24 |
|
|
*----------------------------------------------------------------------*/ |
| 25 |
|
|
/*----------------------------------------------------------------------* |
| 26 |
|
|
* support for resolving the actual IP number of the host for remote |
| 27 |
|
|
* DISPLAYs. When the display is local (i.e. :0), we add support for |
| 28 |
|
|
* sending the first non-loopback interface IP number as the DISPLAY |
| 29 |
|
|
* instead of just sending the incorrect ":0". This way telnet/rlogin |
| 30 |
|
|
* shells can actually get the correct information into DISPLAY for |
| 31 |
|
|
* xclients. |
| 32 |
|
|
*----------------------------------------------------------------------*/ |
| 33 |
|
|
|
| 34 |
|
|
#include "../config.h" /* NECESSARY */ |
| 35 |
|
|
#include "rxvt.h" /* NECESSARY */ |
| 36 |
|
|
|
| 37 |
|
|
#ifdef DISPLAY_IS_IP |
| 38 |
|
|
#include "netdisp.h" |
| 39 |
|
|
|
| 40 |
|
|
/*----------------------------------------------------------------------*/ |
| 41 |
|
|
/* return NULL a pointer to buffer which may be freed */ |
| 42 |
|
|
char * |
| 43 |
pcg |
1.6 |
rxvt_network_display (const char *display) |
| 44 |
pcg |
1.1 |
{ |
| 45 |
pcg |
1.5 |
char buffer[1024], *rval = NULL; |
| 46 |
|
|
struct ifconf ifc; |
| 47 |
|
|
struct ifreq *ifr; |
| 48 |
|
|
int i, skfd; |
| 49 |
|
|
|
| 50 |
root |
1.8 |
if (display[0] != ':' && strncmp (display, "unix:", 5)) |
| 51 |
pcg |
1.5 |
return (char *) display; /* nothing to do */ |
| 52 |
|
|
|
| 53 |
pcg |
1.6 |
ifc.ifc_len = sizeof (buffer); /* Get names of all ifaces */ |
| 54 |
pcg |
1.5 |
ifc.ifc_buf = buffer; |
| 55 |
|
|
|
| 56 |
pcg |
1.6 |
if ((skfd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) |
| 57 |
pcg |
1.5 |
{ |
| 58 |
pcg |
1.6 |
perror ("socket"); |
| 59 |
pcg |
1.5 |
return NULL; |
| 60 |
pcg |
1.1 |
} |
| 61 |
root |
1.9 |
|
| 62 |
pcg |
1.6 |
if (ioctl (skfd, SIOCGIFCONF, &ifc) < 0) |
| 63 |
pcg |
1.5 |
{ |
| 64 |
pcg |
1.6 |
perror ("SIOCGIFCONF"); |
| 65 |
|
|
close (skfd); |
| 66 |
pcg |
1.5 |
return NULL; |
| 67 |
pcg |
1.1 |
} |
| 68 |
root |
1.9 |
|
| 69 |
pcg |
1.5 |
for (i = 0, ifr = ifc.ifc_req; |
| 70 |
pcg |
1.6 |
i < (ifc.ifc_len / sizeof (struct ifreq)); |
| 71 |
pcg |
1.5 |
i++, ifr++) |
| 72 |
|
|
{ |
| 73 |
root |
1.9 |
struct ifreq ifr2; |
| 74 |
pcg |
1.5 |
|
| 75 |
root |
1.8 |
strcpy (ifr2.ifr_name, ifr->ifr_name); |
| 76 |
root |
1.9 |
|
| 77 |
pcg |
1.6 |
if (ioctl (skfd, SIOCGIFADDR, &ifr2) >= 0) |
| 78 |
pcg |
1.5 |
{ |
| 79 |
root |
1.9 |
unsigned long addr; |
| 80 |
pcg |
1.5 |
struct sockaddr_in *p_addr; |
| 81 |
|
|
|
| 82 |
root |
1.9 |
p_addr = (struct sockaddr_in *) &ifr2.ifr_addr; |
| 83 |
pcg |
1.6 |
addr = htonl ((unsigned long)p_addr->sin_addr.s_addr); |
| 84 |
pcg |
1.5 |
|
| 85 |
|
|
/* |
| 86 |
|
|
* not "0.0.0.0" or "127.0.0.1" - so format the address |
| 87 |
|
|
*/ |
| 88 |
|
|
if (addr && addr != 0x7F000001) |
| 89 |
|
|
{ |
| 90 |
root |
1.9 |
char *colon = strchr (display, ':'); |
| 91 |
pcg |
1.5 |
|
| 92 |
|
|
if (colon == NULL) |
| 93 |
|
|
colon = ":0.0"; |
| 94 |
|
|
|
| 95 |
root |
1.8 |
rval = rxvt_malloc (strlen (colon) + 16); |
| 96 |
pcg |
1.6 |
sprintf (rval, "%d.%d.%d.%d%s", |
| 97 |
|
|
(int) ((addr >> 030) & 0xFF), |
| 98 |
|
|
(int) ((addr >> 020) & 0xFF), |
| 99 |
|
|
(int) ((addr >> 010) & 0xFF), |
| 100 |
|
|
(int) (addr & 0xFF), colon); |
| 101 |
pcg |
1.5 |
break; |
| 102 |
|
|
} |
| 103 |
|
|
} |
| 104 |
pcg |
1.1 |
} |
| 105 |
|
|
|
| 106 |
pcg |
1.6 |
close (skfd); |
| 107 |
root |
1.9 |
|
| 108 |
pcg |
1.5 |
return rval; |
| 109 |
pcg |
1.1 |
} |
| 110 |
|
|
#endif /* DISPLAY_IS_IP */ |
| 111 |
|
|
/*----------------------- end-of-file (C source) -----------------------*/ |