1 | /*--------------------------------*-C-*---------------------------------* |
1 | /*--------------------------------*-C-*---------------------------------* |
2 | * File: netdisp.c |
2 | * File: netdisp.C |
3 | *----------------------------------------------------------------------* |
3 | *----------------------------------------------------------------------* |
4 | * |
4 | * |
5 | * All portions of code are copyright by their respective author/s. |
5 | * All portions of code are copyright by their respective author/s. |
6 | * Copyright (c) 1996 Chuck Blake <cblake@BBN.COM> |
6 | * Copyright (c) 1996 Chuck Blake <cblake@BBN.COM> |
7 | * - original version |
7 | * - original version |
… | |
… | |
38 | #include "netdisp.h" |
38 | #include "netdisp.h" |
39 | |
39 | |
40 | /*----------------------------------------------------------------------*/ |
40 | /*----------------------------------------------------------------------*/ |
41 | /* return NULL a pointer to buffer which may be freed */ |
41 | /* return NULL a pointer to buffer which may be freed */ |
42 | char * |
42 | char * |
43 | rxvt_network_display(const char *display) |
43 | rxvt_network_display (const char *display) |
44 | { |
44 | { |
45 | char buffer[1024], *rval = NULL; |
45 | char buffer[1024], *rval = NULL; |
46 | struct ifconf ifc; |
46 | struct ifconf ifc; |
47 | struct ifreq *ifr; |
47 | struct ifreq *ifr; |
48 | int i, skfd; |
48 | int i, skfd; |
49 | |
49 | |
50 | if (display[0] != ':' && STRNCMP(display, "unix:", 5)) |
50 | if (display[0] != ':' && strncmp (display, "unix:", 5)) |
51 | return (char *) display; /* nothing to do */ |
51 | return (char *) display; /* nothing to do */ |
52 | |
52 | |
53 | ifc.ifc_len = sizeof(buffer); /* Get names of all ifaces */ |
53 | ifc.ifc_len = sizeof (buffer); /* Get names of all ifaces */ |
54 | ifc.ifc_buf = buffer; |
54 | ifc.ifc_buf = buffer; |
55 | |
55 | |
56 | if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
56 | if ((skfd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) |
|
|
57 | { |
57 | perror("socket"); |
58 | perror ("socket"); |
58 | return NULL; |
59 | 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 | } |
60 | } |
97 | |
61 | |
|
|
62 | if (ioctl (skfd, SIOCGIFCONF, &ifc) < 0) |
|
|
63 | { |
|
|
64 | perror ("SIOCGIFCONF"); |
98 | close(skfd); |
65 | close (skfd); |
|
|
66 | return NULL; |
|
|
67 | } |
|
|
68 | |
|
|
69 | for (i = 0, ifr = ifc.ifc_req; |
|
|
70 | i < (ifc.ifc_len / sizeof (struct ifreq)); |
|
|
71 | i++, ifr++) |
|
|
72 | { |
|
|
73 | struct ifreq ifr2; |
|
|
74 | |
|
|
75 | strcpy (ifr2.ifr_name, ifr->ifr_name); |
|
|
76 | |
|
|
77 | if (ioctl (skfd, SIOCGIFADDR, &ifr2) >= 0) |
|
|
78 | { |
|
|
79 | unsigned long addr; |
|
|
80 | struct sockaddr_in *p_addr; |
|
|
81 | |
|
|
82 | p_addr = (struct sockaddr_in *) &ifr2.ifr_addr; |
|
|
83 | addr = htonl ((unsigned long)p_addr->sin_addr.s_addr); |
|
|
84 | |
|
|
85 | /* |
|
|
86 | * not "0.0.0.0" or "127.0.0.1" - so format the address |
|
|
87 | */ |
|
|
88 | if (addr && addr != 0x7F000001) |
|
|
89 | { |
|
|
90 | char *colon = strchr (display, ':'); |
|
|
91 | |
|
|
92 | if (colon == NULL) |
|
|
93 | colon = ":0.0"; |
|
|
94 | |
|
|
95 | rval = rxvt_malloc (strlen (colon) + 16); |
|
|
96 | sprintf (rval, "%d.%d.%d.%d%s", |
|
|
97 | (int) ((addr >> 030) & 0xFF), |
|
|
98 | (int) ((addr >> 020) & 0xFF), |
|
|
99 | (int) ((addr >> 010) & 0xFF), |
|
|
100 | (int) (addr & 0xFF), colon); |
|
|
101 | break; |
|
|
102 | } |
|
|
103 | } |
|
|
104 | } |
|
|
105 | |
|
|
106 | close (skfd); |
|
|
107 | |
99 | return rval; |
108 | return rval; |
100 | } |
109 | } |
101 | #endif /* DISPLAY_IS_IP */ |
110 | #endif /* DISPLAY_IS_IP */ |
102 | /*----------------------- end-of-file (C source) -----------------------*/ |
111 | /*----------------------- end-of-file (C source) -----------------------*/ |