1 |
/*--------------------------------*-C-*---------------------------------* |
2 |
* File: netdisp.c |
3 |
*----------------------------------------------------------------------* |
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 |
rxvt_network_display (const char *display) |
44 |
{ |
45 |
char buffer[1024], *rval = NULL; |
46 |
struct ifconf ifc; |
47 |
struct ifreq *ifr; |
48 |
int i, skfd; |
49 |
|
50 |
if (display[0] != ':' && STRNCMP (display, "unix:", 5)) |
51 |
return (char *) display; /* nothing to do */ |
52 |
|
53 |
ifc.ifc_len = sizeof (buffer); /* Get names of all ifaces */ |
54 |
ifc.ifc_buf = buffer; |
55 |
|
56 |
if ((skfd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) |
57 |
{ |
58 |
perror ("socket"); |
59 |
return NULL; |
60 |
} |
61 |
if (ioctl (skfd, SIOCGIFCONF, &ifc) < 0) |
62 |
{ |
63 |
perror ("SIOCGIFCONF"); |
64 |
close (skfd); |
65 |
return NULL; |
66 |
} |
67 |
for (i = 0, ifr = ifc.ifc_req; |
68 |
i < (ifc.ifc_len / sizeof (struct ifreq)); |
69 |
i++, ifr++) |
70 |
{ |
71 |
struct ifreq ifr2; |
72 |
|
73 |
STRCPY (ifr2.ifr_name, ifr->ifr_name); |
74 |
if (ioctl (skfd, SIOCGIFADDR, &ifr2) >= 0) |
75 |
{ |
76 |
unsigned long addr; |
77 |
struct sockaddr_in *p_addr; |
78 |
|
79 |
p_addr = (struct sockaddr_in *)& (ifr2.ifr_addr); |
80 |
addr = htonl ((unsigned long)p_addr->sin_addr.s_addr); |
81 |
|
82 |
/* |
83 |
* not "0.0.0.0" or "127.0.0.1" - so format the address |
84 |
*/ |
85 |
if (addr && addr != 0x7F000001) |
86 |
{ |
87 |
char *colon = STRCHR (display, ':'); |
88 |
|
89 |
if (colon == NULL) |
90 |
colon = ":0.0"; |
91 |
|
92 |
rval = rxvt_malloc (STRLEN (colon) + 16); |
93 |
sprintf (rval, "%d.%d.%d.%d%s", |
94 |
(int) ((addr >> 030) & 0xFF), |
95 |
(int) ((addr >> 020) & 0xFF), |
96 |
(int) ((addr >> 010) & 0xFF), |
97 |
(int) (addr & 0xFF), colon); |
98 |
break; |
99 |
} |
100 |
} |
101 |
} |
102 |
|
103 |
close (skfd); |
104 |
return rval; |
105 |
} |
106 |
#endif /* DISPLAY_IS_IP */ |
107 |
/*----------------------- end-of-file (C source) -----------------------*/ |