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