2 * Routines for utilities to convert addresses to strings.
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h> /* needed for <netinet/in.h> */
36 #ifdef HAVE_NETINET_IN_H
37 # include <netinet/in.h> /* needed for <arpa/inet.h> on some platforms */
40 #ifdef HAVE_ARPA_INET_H
41 #include <arpa/inet.h>
44 #ifdef HAVE_SYS_SOCKET_H
45 #include <sys/socket.h> /* needed to define AF_ values on UNIX */
48 #ifdef HAVE_WINSOCK2_H
49 #include <winsock2.h> /* needed to define AF_ values on Windows */
52 #ifdef NEED_INET_V6DEFS_H
53 # include "wsutil/inet_v6defs.h"
57 #include "value_string.h"
58 #include "addr_resolv.h"
60 #include "atalk-utils.h"
61 #include "sna-utils.h"
62 #include "osi-utils.h"
63 #include <epan/dissectors/packet-mtp3.h>
67 /* private to_str.c API, don't export to .h! */
68 char *word_to_hex(char *out, guint16 word);
69 char *dword_to_hex_punct(char *out, guint32 dword, char punct);
70 char *dword_to_hex(char *out, guint32 dword);
71 char *bytes_to_hexstr(char *out, const guint8 *ad, guint32 len);
72 char *bytes_to_hexstr_punct(char *out, const guint8 *ad, guint32 len, char punct);
75 * If a user _does_ pass in a too-small buffer, this is probably
76 * going to be too long to fit. However, even a partial string
77 * starting with "[Buf" should provide enough of a clue to be
80 #define BUF_TOO_SMALL_ERR "[Buffer too small]"
82 /* Wrapper for the most common case of asking
83 * for a string using a colon as the hex-digit separator.
86 remove this one later when every call has been converted to ep_address_to_str()
89 ether_to_str(const guint8 *ad)
91 return bytestring_to_str(ad, 6, ':');
95 tvb_ether_to_str(tvbuff_t *tvb, const gint offset)
97 return bytestring_to_str(tvb_get_ptr(tvb, offset, 6), 6, ':');
101 This function is very fast and this function is called a lot.
102 XXX update the ep_address_to_str stuff to use this function.
105 ip_to_str(const guint8 *ad) {
108 buf=ep_alloc(MAX_IP_STR_LEN);
109 ip_to_str_buf(ad, buf, MAX_IP_STR_LEN);
113 #define IPV4_LENGTH 4
115 tvb_ip_to_str(tvbuff_t *tvb, const gint offset)
119 buf=ep_alloc(MAX_IP_STR_LEN);
120 ip_to_str_buf(tvb_get_ptr(tvb, offset, IPV4_LENGTH), buf, MAX_IP_STR_LEN);
125 remove this one later when every call has been converted to ep_address_to_str()
128 ip6_to_str(const struct e_in6_addr *ad) {
129 #ifndef INET6_ADDRSTRLEN
130 #define INET6_ADDRSTRLEN 46
134 str=ep_alloc(INET6_ADDRSTRLEN+1);
136 ip6_to_str_buf(ad, str);
140 #define IPV6_LENGTH 16
142 tvb_ip6_to_str(tvbuff_t *tvb, const gint offset)
146 buf=ep_alloc(INET6_ADDRSTRLEN+1);
147 ip6_to_str_buf((const struct e_in6_addr *)tvb_get_ptr(tvb, offset, IPV6_LENGTH), buf);
152 ip6_to_str_buf(const struct e_in6_addr *ad, gchar *buf)
154 inet_ntop(AF_INET6, (const guchar*)ad, buf, INET6_ADDRSTRLEN);
158 ipx_addr_to_str(const guint32 net, const guint8 *ad)
163 name = get_ether_name_if_known(ad);
166 buf = ep_strdup_printf("%s.%s", get_ipxnet_name(net), name);
169 buf = ep_strdup_printf("%s.%s", get_ipxnet_name(net),
170 bytestring_to_str(ad, 6, '\0'));
176 ipxnet_to_string(const guint8 *ad)
178 guint32 addr = pntohl(ad);
179 return ipxnet_to_str_punct(addr, ' ');
183 ipxnet_to_str_punct(const guint32 ad, const char punct)
185 gchar *buf = ep_alloc(12);
187 *dword_to_hex_punct(buf, ad, punct) = '\0';
192 vines_addr_to_str_buf(const guint8 *addrp, gchar *buf, int buf_len)
195 g_strlcpy(buf, BUF_TOO_SMALL_ERR, buf_len); /* Let the unexpected value alert user */
199 buf = dword_to_hex(buf, pntohl(&addrp[0])); /* 8 bytes */
200 *buf++ = '.'; /* 1 byte */
201 buf = word_to_hex(buf, pntohs(&addrp[4])); /* 4 bytes */
202 *buf = '\0'; /* 1 byte */
206 tvb_vines_addr_to_str(tvbuff_t *tvb, const gint offset)
210 buf=ep_alloc(214); /* XXX, 14 here? */
212 vines_addr_to_str_buf(tvb_get_ptr(tvb, offset, VINES_ADDR_LEN), buf, 214);
217 usb_addr_to_str_buf(const guint8 *addrp, gchar *buf, int buf_len)
219 if(pletohl(&addrp[0])==0xffffffff){
220 g_snprintf(buf, buf_len, "host");
222 g_snprintf(buf, buf_len, "%d.%d", pletohl(&addrp[0]), pletohl(&addrp[4]));
227 tipc_addr_to_str_buf( const guint8 *data, gchar *buf, int buf_len){
231 guint32 tipc_address;
233 tipc_address = data[0];
234 tipc_address = (tipc_address << 8) ^ data[1];
235 tipc_address = (tipc_address << 8) ^ data[2];
236 tipc_address = (tipc_address << 8) ^ data[3];
238 processor = tipc_address & 0x0fff;
240 tipc_address = tipc_address >> 12;
241 subnetwork = tipc_address & 0x0fff;
243 tipc_address = tipc_address >> 12;
244 zone = tipc_address & 0xff;
246 g_snprintf(buf,buf_len,"%u.%u.%u",zone,subnetwork,processor);
250 ib_addr_to_str_buf( const address *addr, gchar *buf, int buf_len){
251 if (addr->len >= 16) { /* GID is 128bits */
252 #define PREAMBLE_STR_LEN (sizeof("GID: ") - 1)
253 g_snprintf(buf,buf_len,"GID: ");
254 if ( inet_ntop(AF_INET6, addr->data, buf + PREAMBLE_STR_LEN,
255 buf_len + PREAMBLE_STR_LEN) == NULL ) /* Returns NULL if no space and does not touch buf */
256 g_snprintf ( buf, buf_len, BUF_TOO_SMALL_ERR ); /* Let the unexpected value alert user */
257 } else { /* this is a LID (16 bits) */
258 guint16 lid_number = *((guint16*) addr->data);
259 g_snprintf(buf,buf_len,"LID: %u",lid_number);
264 remove this one later when every call has been converted to ep_address_to_str()
267 fc_to_str(const guint8 *ad)
269 return bytestring_to_str (ad, 3, '.');
273 tvb_fc_to_str(tvbuff_t *tvb, const gint offset)
275 return bytestring_to_str (tvb_get_ptr(tvb, offset, 3), 3, '.');
278 /* FC Network Header Network Address Authority Identifiers */
280 #define FC_NH_NAA_IEEE 1 /* IEEE 802.1a */
281 #define FC_NH_NAA_IEEE_E 2 /* IEEE Exteneded */
282 #define FC_NH_NAA_LOCAL 3
283 #define FC_NH_NAA_IP 4 /* 32-bit IP address */
284 #define FC_NH_NAA_IEEE_R 5 /* IEEE Registered */
285 #define FC_NH_NAA_IEEE_R_E 6 /* IEEE Registered Exteneded */
286 /* according to FC-PH 3 draft these are now reclaimed and reserved */
287 #define FC_NH_NAA_CCITT_INDV 12 /* CCITT 60 bit individual address */
288 #define FC_NH_NAA_CCITT_GRP 14 /* CCITT 60 bit group address */
291 fcwwn_to_str (const guint8 *ad)
298 if (ad == NULL) return NULL;
300 ethstr=ep_alloc(512);
301 ethptr = bytes_to_hexstr_punct(ethstr, ad, 8, ':'); /* 23 bytes */
303 fmt = (ad[0] & 0xF0) >> 4;
308 case FC_NH_NAA_IEEE_E:
309 memcpy (oui, &ad[2], 6);
311 g_snprintf (ethptr, 512-23, " (%s)", get_manuf_name (oui));
314 case FC_NH_NAA_IEEE_R:
315 oui[0] = ((ad[0] & 0x0F) << 4) | ((ad[1] & 0xF0) >> 4);
316 oui[1] = ((ad[1] & 0x0F) << 4) | ((ad[2] & 0xF0) >> 4);
317 oui[2] = ((ad[2] & 0x0F) << 4) | ((ad[3] & 0xF0) >> 4);
318 oui[3] = ((ad[3] & 0x0F) << 4) | ((ad[4] & 0xF0) >> 4);
319 oui[4] = ((ad[4] & 0x0F) << 4) | ((ad[5] & 0xF0) >> 4);
320 oui[5] = ((ad[5] & 0x0F) << 4) | ((ad[6] & 0xF0) >> 4);
322 g_snprintf (ethptr, 512-23, " (%s)", get_manuf_name (oui));
333 tvb_fcwwn_to_str(tvbuff_t *tvb, const gint offset)
335 return fcwwn_to_str (tvb_get_ptr(tvb, offset, 8));
338 /*XXX FIXME the code below may be called very very frequently in the future.
339 optimize it for speed and get rid of the slow sprintfs */
340 /* XXX - perhaps we should have individual address types register
341 a table of routines to do operations such as address-to-name translation,
342 address-to-string translation, and the like, and have this call them,
343 and also have an address-to-string-with-a-name routine */
344 /* XXX - use this, and that future address-to-string-with-a-name routine,
345 in "col_set_addr()"; it might also be useful to have address types
346 export the names of the source and destination address fields, so
347 that "col_set_addr()" need know nothing whatsoever about particular
349 /* convert an address struct into a printable string */
352 ep_address_to_str(const address *addr)
356 str=ep_alloc(MAX_ADDR_STR_LEN);
357 address_to_str_buf(addr, str, MAX_ADDR_STR_LEN);
361 /* The called routines use se_alloc'ed memory */
363 se_address_to_str(const address *addr)
367 str=se_alloc(MAX_ADDR_STR_LEN);
368 address_to_str_buf(addr, str, MAX_ADDR_STR_LEN);
373 address_to_str_buf(const address *addr, gchar *buf, int buf_len)
375 const guint8 *addrdata;
376 struct atalk_ddp_addr ddp_addr;
379 char *tempptr = temp;
381 if (!buf || !buf_len)
388 case AT_ETHER: /* 18 bytes */
389 tempptr = bytes_to_hexstr_punct(tempptr, addr->data, 6, ':'); /* 17 bytes */
392 ip_to_str_buf(addr->data, buf, buf_len);
395 if ( inet_ntop(AF_INET6, addr->data, buf, buf_len) == NULL ) /* Returns NULL if no space and does not touch buf */
396 g_snprintf ( buf, buf_len, BUF_TOO_SMALL_ERR ); /* Let the unexpected value alert user */
398 case AT_IPX: /* 22 bytes */
399 addrdata = addr->data;
400 tempptr = bytes_to_hexstr(tempptr, &addrdata[0], 4); /* 8 bytes */
401 *tempptr++ = '.'; /* 1 byte */
402 tempptr = bytes_to_hexstr(tempptr, &addrdata[4], 6); /* 12 bytes */
405 sna_fid_to_str_buf(addr, buf, buf_len);
408 memcpy(&ddp_addr, addr->data, sizeof ddp_addr);
409 atalk_addr_to_str_buf(&ddp_addr, buf, buf_len);
412 vines_addr_to_str_buf(addr->data, buf, buf_len);
415 usb_addr_to_str_buf(addr->data, buf, buf_len);
418 print_nsap_net_buf(addr->data, addr->len, buf, buf_len);
420 case AT_ARCNET: /* 5 bytes */
421 tempptr = g_stpcpy(tempptr, "0x"); /* 2 bytes */
422 tempptr = bytes_to_hexstr(tempptr, addr->data, 1); /* 2 bytes */
424 case AT_FC: /* 9 bytes */
425 tempptr = bytes_to_hexstr_punct(tempptr, addr->data, 3, '.'); /* 8 bytes */
428 mtp3_addr_to_str_buf((const mtp3_addr_pc_t *)addr->data, buf, buf_len);
431 g_strlcpy(buf, addr->data, buf_len);
433 case AT_EUI64: /* 24 bytes */
434 tempptr = bytes_to_hexstr_punct(tempptr, addr->data, 8, ':'); /* 23 bytes */
437 int copy_len = addr->len < (buf_len - 1) ? addr->len : (buf_len - 1);
438 memcpy(buf, addr->data, copy_len );
439 buf[copy_len] = '\0';
443 tipc_addr_to_str_buf(addr->data, buf, buf_len);
446 ib_addr_to_str_buf(addr, buf, buf_len);
449 g_assert_not_reached();
452 /* copy to output buffer */
453 if (tempptr != temp) {
454 size_t temp_len = (size_t) (tempptr - temp);
456 if (temp_len < (size_t) buf_len) {
457 memcpy(buf, temp, temp_len);
458 buf[temp_len] = '\0';
460 g_strlcpy(buf, BUF_TOO_SMALL_ERR, buf_len);/* Let the unexpected value alert user */