File: netdisp.c

package info (click to toggle)
rxvt 1%3A2.4.5-16
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,128 kB
  • ctags: 1,296
  • sloc: ansic: 12,852; makefile: 521; sh: 411
file content (128 lines) | stat: -rw-r--r-- 4,046 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*--------------------------------*-C-*---------------------------------*
 * File:	netdisp.c
 *----------------------------------------------------------------------*
 * Copyright (C) 1997 1998 Oezguer Kesim <kesim@math.fu-berlin.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *----------------------------------------------------------------------*
 * Originally written by Chuck Blake <cblake@BBN.COM>
 * Modifications by mj olesen <olesen@me.queensu.ca>
 *----------------------------------------------------------------------*
 * support for resolving the actual IP number of the host for remote
 * DISPLAYs.  When the display is local (i.e. :0), we add support for
 * sending the first non-loopback interface IP number as the DISPLAY
 * instead of just sending the incorrect ":0".  This way telnet/rlogin
 * shells can actually get the correct information into DISPLAY for
 * xclients.
 *----------------------------------------------------------------------*/

#include "rxvt.h"		/* NECESSARY */

/* compile an empty file if this option is disabled. */
#ifndef DISPLAY_IS_IP
/* char * network_display (const char * display) { return display; } */
#endif
#ifdef DISPLAY_IS_IP
/* On Solaris link with -lsocket and -lnsl */
#include <sys/types.h>
#include <sys/socket.h>

/* these next two are probably only on Sun (not Solaris) */
#ifdef HAVE_SYS_SOCKIO_H
#include <sys/sockio.h>
#endif
#ifdef HAVE_SYS_BYTEORDER_H
#include <sys/byteorder.h>
#endif

#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <net/if_arp.h>
/*}}} */
/* extern functions referenced */
/* extern variables referenced */
/* extern variables declared here */
/* local variables */
/* local functions referenced */

/*----------------------------------------------------------------------*/
/* return a pointer to a static buffer */
/* PROTO */
char           *
network_display(const char *display)
{
    static char     ipaddress[32] = "";
    char            buffer[1024], *rval = NULL;
    struct ifconf   ifc;
    struct ifreq   *ifr;
    int             i, skfd;

    if (display[0] != ':' && strncmp(display, "unix:", 5))
	return display;		/* nothing to do */

    ifc.ifc_len = sizeof(buffer);	/* Get names of all ifaces */
    ifc.ifc_buf = buffer;

    if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
	perror("socket");
	return NULL;
    }
    if (ioctl(skfd, SIOCGIFCONF, &ifc) < 0) {
	perror("SIOCGIFCONF");
	close(skfd);
	return NULL;
    }
    for (i = 0, ifr = ifc.ifc_req;
	 i < (ifc.ifc_len / sizeof(struct ifreq));

	 i++, ifr++) {
	struct ifreq    ifr2;

	strcpy(ifr2.ifr_name, ifr->ifr_name);
	if (ioctl(skfd, SIOCGIFADDR, &ifr2) >= 0) {
	    unsigned long   addr;
	    struct sockaddr_in *p_addr;

	    p_addr = (struct sockaddr_in *)&(ifr2.ifr_addr);
	    addr = htonl((unsigned long)p_addr->sin_addr.s_addr);

	/*
	 * not "0.0.0.0" or "127.0.0.1" - so format the address
	 */
	    if (addr && addr != 0x7F000001) {
		char           *colon = strchr(display, ':');

		if (colon == NULL)
		    colon = ":0.0";

		sprintf(ipaddress, "%d.%d.%d.%d%s",
			(int)((addr >> 030) & 0xFF),
			(int)((addr >> 020) & 0xFF),
			(int)((addr >> 010) & 0xFF),
			(int)(addr & 0xFF), colon);

		rval = ipaddress;
		break;
	    }
	}
    }

    close(skfd);
    return rval;
}
#endif				/* DISPLAY_IS_IP */
/*----------------------- end-of-file (C source) -----------------------*/