ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/netdisp.C
Revision: 1.2
Committed: Mon Nov 24 17:31:27 2003 UTC (20 years, 6 months ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: rel-1-3, rel-1-2
Changes since 1.1: +0 -0 lines
Log Message:
*** empty log message ***

File Contents

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