HACK: pinfo->private_data points to smb_info again
[wireshark-wip.git] / epan / address_to_str.c
blob1c444568e6e5d560044695e48e5d2c23ca59b22e
1 /* address_to_str.c
2 * Routines for utilities to convert addresses to strings.
4 * $Id$
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "config.h"
27 #include <stdlib.h>
28 #include <string.h>
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h> /* needed for <netinet/in.h> */
32 #endif
34 #ifdef HAVE_NETINET_IN_H
35 # include <netinet/in.h> /* needed for <arpa/inet.h> on some platforms */
36 #endif
38 #ifdef HAVE_ARPA_INET_H
39 #include <arpa/inet.h>
40 #endif
42 #ifdef HAVE_SYS_SOCKET_H
43 #include <sys/socket.h> /* needed to define AF_ values on UNIX */
44 #endif
46 #ifdef HAVE_WINSOCK2_H
47 #include <winsock2.h> /* needed to define AF_ values on Windows */
48 #endif
50 #ifdef NEED_INET_V6DEFS_H
51 # include "wsutil/inet_v6defs.h"
52 #endif
54 #include "to_str-int.h"
55 #include "to_str.h"
56 #include "value_string.h"
57 #include "addr_resolv.h"
58 #include "wsutil/pint.h"
59 #include "atalk-utils.h"
60 #include "sna-utils.h"
61 #include "osi-utils.h"
62 #include <epan/dissectors/packet-mtp3.h>
63 #include <stdio.h>
64 #include "emem.h"
67 * If a user _does_ pass in a too-small buffer, this is probably
68 * going to be too long to fit. However, even a partial string
69 * starting with "[Buf" should provide enough of a clue to be
70 * useful.
72 #define BUF_TOO_SMALL_ERR "[Buffer too small]"
74 /* Wrapper for the most common case of asking
75 * for a string using a colon as the hex-digit separator.
77 /* XXX FIXME
78 remove this one later when every call has been converted to ep_address_to_str()
80 const gchar *
81 ether_to_str(const guint8 *ad)
83 return bytestring_to_str(ad, 6, ':');
86 const gchar *
87 tvb_ether_to_str(tvbuff_t *tvb, const gint offset)
89 return bytestring_to_str(tvb_get_ptr(tvb, offset, 6), 6, ':');
93 This function is very fast and this function is called a lot.
94 XXX update the ep_address_to_str stuff to use this function.
96 const gchar *
97 ip_to_str(const guint8 *ad) {
98 gchar *buf;
100 buf=(gchar *)ep_alloc(MAX_IP_STR_LEN);
101 ip_to_str_buf(ad, buf, MAX_IP_STR_LEN);
102 return buf;
105 #define IPV4_LENGTH 4
106 const gchar *
107 tvb_ip_to_str(tvbuff_t *tvb, const gint offset)
109 gchar *buf;
111 buf=(gchar *)ep_alloc(MAX_IP_STR_LEN);
112 ip_to_str_buf(tvb_get_ptr(tvb, offset, IPV4_LENGTH), buf, MAX_IP_STR_LEN);
113 return buf;
116 /* XXX FIXME
117 remove this one later when every call has been converted to ep_address_to_str()
119 gchar *
120 ip6_to_str(const struct e_in6_addr *ad) {
121 gchar *str;
123 str=(gchar *)ep_alloc(MAX_IP6_STR_LEN);
124 ip6_to_str_buf(ad, str);
125 return str;
128 #define IPV6_LENGTH 16
129 gchar *
130 tvb_ip6_to_str(tvbuff_t *tvb, const gint offset)
132 gchar *buf;
134 buf=(gchar *)ep_alloc(MAX_IP6_STR_LEN);
135 ip6_to_str_buf((const struct e_in6_addr *)tvb_get_ptr(tvb, offset, IPV6_LENGTH), buf);
136 return buf;
139 /* const char *
140 * inet_ntop6(src, dst, size)
141 * convert IPv6 binary address into presentation (printable) format
142 * author:
143 * Paul Vixie, 1996.
145 static void
146 ip6_to_str_buf_len(const guchar* src, char *buf, size_t buf_len)
148 struct { int base, len; } best, cur;
149 guint words[8];
150 int i;
152 if (buf_len < MAX_IP6_STR_LEN) { /* buf_len < 40 */
153 g_strlcpy(buf, BUF_TOO_SMALL_ERR, buf_len); /* Let the unexpected value alert user */
154 return;
158 * Preprocess:
159 * Copy the input (bytewise) array into a wordwise array.
160 * Find the longest run of 0x00's in src[] for :: shorthanding.
162 for (i = 0; i < 16; i += 2) {
163 words[i / 2] = (src[i+1] << 0);
164 words[i / 2] |= (src[i] << 8);
166 best.base = -1; best.len = 0;
167 cur.base = -1; cur.len = 0;
168 for (i = 0; i < 8; i++) {
169 if (words[i] == 0) {
170 if (cur.base == -1) {
171 cur.base = i;
172 cur.len = 1;
173 } else
174 cur.len++;
175 } else {
176 if (cur.base != -1) {
177 if (best.base == -1 || cur.len > best.len)
178 best = cur;
179 cur.base = -1;
183 if (cur.base != -1) {
184 if (best.base == -1 || cur.len > best.len)
185 best = cur;
187 if (best.base != -1 && best.len < 2)
188 best.base = -1;
190 /* Is this address an encapsulated IPv4? */
191 /* XXX,
192 * Orginal code dated 1996 uses ::/96 as a valid IPv4-compatible addresses
193 * but since Feb 2006 ::/96 is deprecated one.
194 * Quoting wikipedia [0]:
195 * > The 96-bit zero-value prefix ::/96, originally known as IPv4-compatible
196 * > addresses, was mentioned in 1995[35] but first described in 1998.[41]
197 * > This class of addresses was used to represent IPv4 addresses within
198 * > an IPv6 transition technology. Such an IPv6 address has its first
199 * > (most significant) 96 bits set to zero, while its last 32 bits are the
200 * > IPv4 address that is represented.
201 * > In February 2006 the Internet Engineering Task Force (IETF) has deprecated
202 * > the use of IPv4-compatible addresses.[1] The only remaining use of this address
203 * > format is to represent an IPv4 address in a table or database with fixed size
204 * > members that must also be able to store an IPv6 address.
206 * If needed it can be fixed by changing next line:
207 * if (best.base == 0 && (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
208 * to:
209 * if (best.base == 0 && best.len == 5 && words[5] == 0xffff)
211 * [0] http://en.wikipedia.org/wiki/IPv6_address#Historical_notes
214 if (best.base == 0 && (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
216 /* best.len == 6 -> ::IPv4; 5 -> ::ffff:IPv4 */
217 buf = g_stpcpy(buf, "::");
218 if (best.len == 5)
219 buf = g_stpcpy(buf, "ffff:");
220 ip_to_str_buf(src + 12, buf, MAX_IP_STR_LEN);
221 /* max: 2 + 5 + 16 == 23 bytes */
222 return;
226 * Format the result.
228 for (i = 0; i < 8; i++) {
229 /* Are we inside the best run of 0x00's? */
230 if (i == best.base) {
231 *buf++ = ':';
232 i += best.len;
234 /* Was it a trailing run of 0x00's? */
235 if (i == 8) {
236 *buf++ = ':';
237 break;
240 /* Are we following an initial run of 0x00s or any real hex? */
241 if (i != 0)
242 *buf++ = ':';
244 buf = word_to_hex_npad(buf, words[i]); /* max: 4B */
245 /* max: 8 * 4 + 7 == 39 bytes */
247 *buf = '\0'; /* 40 byte */
250 void
251 ip6_to_str_buf(const struct e_in6_addr *ad, gchar *buf)
253 ip6_to_str_buf_len((const guchar*)ad, buf, MAX_IP6_STR_LEN);
256 gchar*
257 ipx_addr_to_str(const guint32 net, const guint8 *ad)
259 gchar *buf;
260 char *name;
262 name = get_ether_name_if_known(ad);
264 if (name) {
265 buf = ep_strdup_printf("%s.%s", get_ipxnet_name(net), name);
267 else {
268 buf = ep_strdup_printf("%s.%s", get_ipxnet_name(net),
269 bytestring_to_str(ad, 6, '\0'));
271 return buf;
274 gchar*
275 ipxnet_to_string(const guint8 *ad)
277 guint32 addr = pntohl(ad);
278 return ipxnet_to_str_punct(addr, ' ');
281 gchar *
282 ipxnet_to_str_punct(const guint32 ad, const char punct)
284 gchar *buf = (gchar *)ep_alloc(12);
286 *dword_to_hex_punct(buf, ad, punct) = '\0';
287 return buf;
290 static void
291 vines_addr_to_str_buf(const guint8 *addrp, gchar *buf, int buf_len)
293 if (buf_len < 14) {
294 g_strlcpy(buf, BUF_TOO_SMALL_ERR, buf_len); /* Let the unexpected value alert user */
295 return;
298 buf = dword_to_hex(buf, pntohl(&addrp[0])); /* 8 bytes */
299 *buf++ = '.'; /* 1 byte */
300 buf = word_to_hex(buf, pntohs(&addrp[4])); /* 4 bytes */
301 *buf = '\0'; /* 1 byte */
304 gchar *
305 tvb_vines_addr_to_str(tvbuff_t *tvb, const gint offset)
307 gchar *buf;
309 buf=(gchar *)ep_alloc(214); /* XXX, 14 here? */
311 vines_addr_to_str_buf(tvb_get_ptr(tvb, offset, VINES_ADDR_LEN), buf, 214);
312 return buf;
316 This function is very fast and this function is called a lot.
317 XXX update the ep_address_to_str stuff to use this function.
319 gchar *
320 eui64_to_str(const guint64 ad) {
321 gchar *buf;
322 guint8 *p_eui64;
324 p_eui64 = (guint8 *)ep_alloc(8);
325 buf=(gchar *)ep_alloc(EUI64_STR_LEN);
327 /* Copy and convert the address to network byte order. */
328 *(guint64 *)(void *)(p_eui64) = pntoh64(&(ad));
330 g_snprintf(buf, EUI64_STR_LEN, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
331 p_eui64[0], p_eui64[1], p_eui64[2], p_eui64[3],
332 p_eui64[4], p_eui64[5], p_eui64[6], p_eui64[7] );
333 return buf;
335 gchar *
336 tvb_eui64_to_str(tvbuff_t *tvb, const gint offset, const guint encoding)
338 if(encoding)
340 return eui64_to_str(tvb_get_letoh64(tvb, offset));
341 }else {
342 return eui64_to_str(tvb_get_ntoh64(tvb, offset));
346 static void
347 usb_addr_to_str_buf(const guint8 *addrp, gchar *buf, int buf_len)
349 if(pletohl(&addrp[0])==0xffffffff){
350 g_snprintf(buf, buf_len, "host");
351 } else {
352 g_snprintf(buf, buf_len, "%d.%d", pletohl(&addrp[0]), pletohl(&addrp[4]));
356 static void
357 tipc_addr_to_str_buf( const guint8 *data, gchar *buf, int buf_len){
358 guint8 zone;
359 guint16 subnetwork;
360 guint16 processor;
361 guint32 tipc_address;
363 tipc_address = data[0];
364 tipc_address = (tipc_address << 8) ^ data[1];
365 tipc_address = (tipc_address << 8) ^ data[2];
366 tipc_address = (tipc_address << 8) ^ data[3];
368 processor = tipc_address & 0x0fff;
370 tipc_address = tipc_address >> 12;
371 subnetwork = tipc_address & 0x0fff;
373 tipc_address = tipc_address >> 12;
374 zone = tipc_address & 0xff;
376 g_snprintf(buf,buf_len,"%u.%u.%u",zone,subnetwork,processor);
379 static void
380 ib_addr_to_str_buf( const address *addr, gchar *buf, int buf_len){
381 if (addr->len >= 16) { /* GID is 128bits */
382 #define PREAMBLE_STR_LEN ((int)(sizeof("GID: ") - 1))
383 g_snprintf(buf,buf_len,"GID: ");
384 if (buf_len < PREAMBLE_STR_LEN ||
385 inet_ntop(AF_INET6, addr->data, buf + PREAMBLE_STR_LEN,
386 buf_len - PREAMBLE_STR_LEN) == NULL ) /* Returns NULL if no space and does not touch buf */
387 g_snprintf ( buf, buf_len, BUF_TOO_SMALL_ERR ); /* Let the unexpected value alert user */
388 } else { /* this is a LID (16 bits) */
389 guint16 lid_number;
391 memcpy((void *)&lid_number, addr->data, sizeof lid_number);
392 g_snprintf(buf,buf_len,"LID: %u",lid_number);
396 /* XXX FIXME
397 remove this one later when every call has been converted to ep_address_to_str()
399 const gchar *
400 fc_to_str(const guint8 *ad)
402 return bytestring_to_str (ad, 3, '.');
405 const gchar *
406 tvb_fc_to_str(tvbuff_t *tvb, const gint offset)
408 return bytestring_to_str (tvb_get_ptr(tvb, offset, 3), 3, '.');
411 /* FC Network Header Network Address Authority Identifiers */
413 #define FC_NH_NAA_IEEE 1 /* IEEE 802.1a */
414 #define FC_NH_NAA_IEEE_E 2 /* IEEE Exteneded */
415 #define FC_NH_NAA_LOCAL 3
416 #define FC_NH_NAA_IP 4 /* 32-bit IP address */
417 #define FC_NH_NAA_IEEE_R 5 /* IEEE Registered */
418 #define FC_NH_NAA_IEEE_R_E 6 /* IEEE Registered Exteneded */
419 /* according to FC-PH 3 draft these are now reclaimed and reserved */
420 #define FC_NH_NAA_CCITT_INDV 12 /* CCITT 60 bit individual address */
421 #define FC_NH_NAA_CCITT_GRP 14 /* CCITT 60 bit group address */
423 gchar *
424 fcwwn_to_str (const guint8 *ad)
426 int fmt;
427 guint8 oui[6];
428 gchar *ethstr;
429 gchar *ethptr;
431 if (ad == NULL) return NULL;
433 ethstr=(gchar *)ep_alloc(512);
434 ethptr = bytes_to_hexstr_punct(ethstr, ad, 8, ':'); /* 23 bytes */
436 fmt = (ad[0] & 0xF0) >> 4;
438 switch (fmt) {
440 case FC_NH_NAA_IEEE:
441 case FC_NH_NAA_IEEE_E:
442 memcpy (oui, &ad[2], 6);
444 g_snprintf (ethptr, 512-23, " (%s)", get_manuf_name (oui));
445 break;
447 case FC_NH_NAA_IEEE_R:
448 oui[0] = ((ad[0] & 0x0F) << 4) | ((ad[1] & 0xF0) >> 4);
449 oui[1] = ((ad[1] & 0x0F) << 4) | ((ad[2] & 0xF0) >> 4);
450 oui[2] = ((ad[2] & 0x0F) << 4) | ((ad[3] & 0xF0) >> 4);
451 oui[3] = ((ad[3] & 0x0F) << 4) | ((ad[4] & 0xF0) >> 4);
452 oui[4] = ((ad[4] & 0x0F) << 4) | ((ad[5] & 0xF0) >> 4);
453 oui[5] = ((ad[5] & 0x0F) << 4) | ((ad[6] & 0xF0) >> 4);
455 g_snprintf (ethptr, 512-23, " (%s)", get_manuf_name (oui));
456 break;
458 default:
459 *ethptr = '\0';
460 break;
462 return (ethstr);
465 gchar *
466 tvb_fcwwn_to_str(tvbuff_t *tvb, const gint offset)
468 return fcwwn_to_str (tvb_get_ptr(tvb, offset, 8));
471 /* XXX FIXME
472 remove this one later when every call has been converted to address_to_str()
474 const gchar *
475 ax25_to_str(const guint8 *ad)
477 return bytestring_to_str(ad, 7, ':');
480 /* XXX FIXME
481 remove this one later when every call has been converted to address_to_str()
483 gchar *
484 get_ax25_name(const guint8 *ad)
486 address addr;
488 addr.type = AT_AX25;
489 addr.len = 7;
490 addr.data = ad;
492 return address_to_str( &addr );
495 /*XXX FIXME the code below may be called very very frequently in the future.
496 optimize it for speed and get rid of the slow sprintfs */
497 /* XXX - perhaps we should have individual address types register
498 a table of routines to do operations such as address-to-name translation,
499 address-to-string translation, and the like, and have this call them,
500 and also have an address-to-string-with-a-name routine */
501 /* XXX - use this, and that future address-to-string-with-a-name routine,
502 in "col_set_addr()"; it might also be useful to have address types
503 export the names of the source and destination address fields, so
504 that "col_set_addr()" need know nothing whatsoever about particular
505 address types */
506 /* convert an address struct into a printable string */
508 gchar*
509 ep_address_to_str(const address *addr)
511 gchar *str;
513 str=(gchar *)ep_alloc(MAX_ADDR_STR_LEN);
514 address_to_str_buf(addr, str, MAX_ADDR_STR_LEN);
515 return str;
518 /* The called routines use se_alloc'ed memory */
519 gchar*
520 se_address_to_str(const address *addr)
522 gchar *str;
524 str=(gchar *)se_alloc(MAX_ADDR_STR_LEN);
525 address_to_str_buf(addr, str, MAX_ADDR_STR_LEN);
526 return str;
529 void
530 address_to_str_buf(const address *addr, gchar *buf, int buf_len)
532 const guint8 *addrdata;
533 struct atalk_ddp_addr ddp_addr;
534 guint16 ieee_802_15_4_short_addr;
536 char temp[32];
537 char *tempptr = temp;
539 if (!buf || !buf_len)
540 return;
542 switch(addr->type){
543 case AT_NONE:
544 buf[0] = '\0';
545 break;
546 case AT_ETHER: /* 18 bytes */
547 tempptr = bytes_to_hexstr_punct(tempptr, (const guint8 *)addr->data, 6, ':'); /* 17 bytes */
548 break;
549 case AT_IPv4:
550 ip_to_str_buf((const guint8 *)addr->data, buf, buf_len);
551 break;
552 case AT_IPv6:
553 ip6_to_str_buf_len((const guchar *)addr->data, buf, buf_len);
554 break;
555 case AT_IPX: /* 22 bytes */
556 addrdata = (const guint8 *)addr->data;
557 tempptr = bytes_to_hexstr(tempptr, &addrdata[0], 4); /* 8 bytes */
558 *tempptr++ = '.'; /*1 byte */
559 tempptr = bytes_to_hexstr(tempptr, &addrdata[4], 6); /* 12 bytes */
560 break;
561 case AT_SNA:
562 sna_fid_to_str_buf(addr, buf, buf_len);
563 break;
564 case AT_ATALK:
565 memcpy(&ddp_addr, addr->data, sizeof ddp_addr);
566 atalk_addr_to_str_buf(&ddp_addr, buf, buf_len);
567 break;
568 case AT_VINES:
569 vines_addr_to_str_buf((const guint8 *)addr->data, buf, buf_len);
570 break;
571 case AT_USB:
572 usb_addr_to_str_buf((const guint8 *)addr->data, buf, buf_len);
573 break;
574 case AT_OSI:
575 print_nsap_net_buf((const guint8 *)addr->data, addr->len, buf, buf_len);
576 break;
577 case AT_ARCNET: /* 5 bytes */
578 tempptr = g_stpcpy(tempptr, "0x"); /* 2 bytes */
579 tempptr = bytes_to_hexstr(tempptr, (const guint8 *)addr->data, 1); /* 2 bytes */
580 break;
581 case AT_FC: /* 9 bytes */
582 tempptr = bytes_to_hexstr_punct(tempptr, (const guint8 *)addr->data, 3, '.'); /* 8 bytes */
583 break;
584 case AT_SS7PC:
585 mtp3_addr_to_str_buf((const mtp3_addr_pc_t *)addr->data, buf, buf_len);
586 break;
587 case AT_STRINGZ:
588 g_strlcpy(buf, (const gchar *)addr->data, buf_len);
589 break;
590 case AT_EUI64: /* 24 bytes */
591 tempptr = bytes_to_hexstr_punct(tempptr, (const guint8 *)addr->data, 8, ':'); /* 23 bytes */
592 break;
593 case AT_URI: {
594 int copy_len = addr->len < (buf_len - 1) ? addr->len : (buf_len - 1);
595 memcpy(buf, addr->data, copy_len );
596 buf[copy_len] = '\0';
598 break;
599 case AT_TIPC:
600 tipc_addr_to_str_buf((const guint8 *)addr->data, buf, buf_len);
601 break;
602 case AT_IB:
603 ib_addr_to_str_buf(addr, buf, buf_len);
604 break;
605 case AT_AX25:
606 addrdata = (const guint8 *)addr->data;
607 g_snprintf(buf, buf_len, "%c%c%c%c%c%c-%02d",
608 (addrdata[0] >> 1) & 0x7f, (addrdata[1] >> 1) & 0x7f, (addrdata[2] >> 1) & 0x7f,
609 (addrdata[3] >> 1) & 0x7f, (addrdata[4] >> 1) & 0x7f, (addrdata[5] >> 1) & 0x7f,
610 (addrdata[6] >> 1) & 0x0f );
611 break;
612 case AT_IEEE_802_15_4_SHORT:
613 ieee_802_15_4_short_addr = pletohs(addr->data);
614 if (ieee_802_15_4_short_addr == 0xffff)
615 g_snprintf(buf, buf_len, "Broadcast");
616 else
617 g_snprintf(buf, buf_len, "0x%04x", ieee_802_15_4_short_addr);
618 break;
619 default:
620 g_assert_not_reached();
623 /* copy to output buffer */
624 if (tempptr != temp) {
625 size_t temp_len = (size_t) (tempptr - temp);
627 if (temp_len < (size_t) buf_len) {
628 memcpy(buf, temp, temp_len);
629 buf[temp_len] = '\0';
630 } else
631 g_strlcpy(buf, BUF_TOO_SMALL_ERR, buf_len);/* Let the unexpected value alert user */
636 * Editor modelines - http://www.wireshark.org/tools/modelines.html
638 * Local variables:
639 * c-basic-offset: 4
640 * tab-width: 8
641 * indent-tabs-mode: nil
642 * End:
644 * vi: set shiftwidth=4 tabstop=8 expandtab:
645 * :indentSize=4:tabSize=8:noTabs=true: