ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/netdisp.C
Revision: 1.4
Committed: Sat Jan 31 03:27:45 2004 UTC (20 years, 4 months ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: before_astyle
Changes since 1.3: +0 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
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 perror("socket");
58 return NULL;
59 }
60 if (ioctl(skfd, SIOCGIFCONF, &ifc) < 0) {
61 perror("SIOCGIFCONF");
62 close(skfd);
63 return NULL;
64 }
65 for (i = 0, ifr = ifc.ifc_req;
66 i < (ifc.ifc_len / sizeof(struct ifreq));
67 i++, ifr++) {
68 struct ifreq ifr2;
69
70 STRCPY(ifr2.ifr_name, ifr->ifr_name);
71 if (ioctl(skfd, SIOCGIFADDR, &ifr2) >= 0) {
72 unsigned long addr;
73 struct sockaddr_in *p_addr;
74
75 p_addr = (struct sockaddr_in *)&(ifr2.ifr_addr);
76 addr = htonl((unsigned long)p_addr->sin_addr.s_addr);
77
78 /*
79 * not "0.0.0.0" or "127.0.0.1" - so format the address
80 */
81 if (addr && addr != 0x7F000001) {
82 char *colon = STRCHR(display, ':');
83
84 if (colon == NULL)
85 colon = ":0.0";
86
87 rval = rxvt_malloc(STRLEN(colon) + 16);
88 sprintf(rval, "%d.%d.%d.%d%s",
89 (int)((addr >> 030) & 0xFF),
90 (int)((addr >> 020) & 0xFF),
91 (int)((addr >> 010) & 0xFF),
92 (int)(addr & 0xFF), colon);
93 break;
94 }
95 }
96 }
97
98 close(skfd);
99 return rval;
100 }
101 #endif /* DISPLAY_IS_IP */
102 /*----------------------- end-of-file (C source) -----------------------*/