ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/netdisp.C
Revision: 1.6
Committed: Fri Feb 13 12:16:21 2004 UTC (20 years, 3 months ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_1_0, rel-2_4, rel-2_5, rel-2_2, rel-2_3, rel-2_0, rel-1_9, rel-2_7
Changes since 1.5: +21 -21 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.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 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 pcg 1.6 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 pcg 1.6 if (ioctl (skfd, SIOCGIFCONF, &ifc) < 0)
62 pcg 1.5 {
63 pcg 1.6 perror ("SIOCGIFCONF");
64     close (skfd);
65 pcg 1.5 return NULL;
66 pcg 1.1 }
67 pcg 1.5 for (i = 0, ifr = ifc.ifc_req;
68 pcg 1.6 i < (ifc.ifc_len / sizeof (struct ifreq));
69 pcg 1.5 i++, ifr++)
70     {
71     struct ifreq ifr2;
72    
73 pcg 1.6 STRCPY (ifr2.ifr_name, ifr->ifr_name);
74     if (ioctl (skfd, SIOCGIFADDR, &ifr2) >= 0)
75 pcg 1.5 {
76     unsigned long addr;
77     struct sockaddr_in *p_addr;
78    
79 pcg 1.6 p_addr = (struct sockaddr_in *)& (ifr2.ifr_addr);
80     addr = htonl ((unsigned long)p_addr->sin_addr.s_addr);
81 pcg 1.5
82     /*
83     * not "0.0.0.0" or "127.0.0.1" - so format the address
84     */
85     if (addr && addr != 0x7F000001)
86     {
87 pcg 1.6 char *colon = STRCHR (display, ':');
88 pcg 1.5
89     if (colon == NULL)
90     colon = ":0.0";
91    
92 pcg 1.6 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 pcg 1.5 break;
99     }
100     }
101 pcg 1.1 }
102    
103 pcg 1.6 close (skfd);
104 pcg 1.5 return rval;
105 pcg 1.1 }
106     #endif /* DISPLAY_IS_IP */
107     /*----------------------- end-of-file (C source) -----------------------*/