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