Introduce ip6_to_str_buf_len (little cleaner version of inet_ntop6 from wsutil/inet_n...
[obnox/wireshark/wip.git] / epan / address_to_str.c
1 /* address_to_str.c
2  * Routines for utilities to convert addresses to strings.
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <stdlib.h>
30 #include <string.h>
31
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>         /* needed for <netinet/in.h> */
34 #endif
35
36 #ifdef HAVE_NETINET_IN_H
37 # include <netinet/in.h>        /* needed for <arpa/inet.h> on some platforms */
38 #endif
39
40 #ifdef HAVE_ARPA_INET_H
41 #include <arpa/inet.h>
42 #endif
43
44 #ifdef HAVE_SYS_SOCKET_H
45 #include <sys/socket.h>         /* needed to define AF_ values on UNIX */
46 #endif
47
48 #ifdef HAVE_WINSOCK2_H
49 #include <winsock2.h>           /* needed to define AF_ values on Windows */
50 #endif
51
52 #ifdef NEED_INET_V6DEFS_H
53 # include "wsutil/inet_v6defs.h"
54 #endif
55
56 #include "to_str.h"
57 #include "value_string.h"
58 #include "addr_resolv.h"
59 #include "pint.h"
60 #include "atalk-utils.h"
61 #include "sna-utils.h"
62 #include "osi-utils.h"
63 #include <epan/dissectors/packet-mtp3.h>
64 #include <stdio.h>
65 #include "emem.h"
66
67 /* private to_str.c API, don't export to .h! */
68 char *word_to_hex(char *out, guint16 word);
69 char *word_to_hex_npad(char *out, guint16 word);
70 char *dword_to_hex_punct(char *out, guint32 dword, char punct);
71 char *dword_to_hex(char *out, guint32 dword);
72 char *bytes_to_hexstr(char *out, const guint8 *ad, guint32 len);
73 char *bytes_to_hexstr_punct(char *out, const guint8 *ad, guint32 len, char punct);
74
75 /*
76  * If a user _does_ pass in a too-small buffer, this is probably
77  * going to be too long to fit.  However, even a partial string
78  * starting with "[Buf" should provide enough of a clue to be
79  * useful.
80  */
81 #define BUF_TOO_SMALL_ERR "[Buffer too small]"
82
83 /* Wrapper for the most common case of asking
84  * for a string using a colon as the hex-digit separator.
85  */
86 /* XXX FIXME
87 remove this one later when every call has been converted to ep_address_to_str()
88 */
89 gchar *
90 ether_to_str(const guint8 *ad)
91 {
92         return bytestring_to_str(ad, 6, ':');
93 }
94
95 gchar *
96 tvb_ether_to_str(tvbuff_t *tvb, const gint offset)
97 {
98         return bytestring_to_str(tvb_get_ptr(tvb, offset, 6), 6, ':');
99 }
100
101 /*
102  This function is very fast and this function is called a lot.
103  XXX update the ep_address_to_str stuff to use this function.
104 */
105 const gchar *
106 ip_to_str(const guint8 *ad) {
107   gchar *buf;
108
109   buf=ep_alloc(MAX_IP_STR_LEN);
110   ip_to_str_buf(ad, buf, MAX_IP_STR_LEN);
111   return buf;
112 }
113
114 #define IPV4_LENGTH 4
115 const gchar *
116 tvb_ip_to_str(tvbuff_t *tvb, const gint offset)
117 {
118   gchar *buf;
119
120   buf=ep_alloc(MAX_IP_STR_LEN);
121   ip_to_str_buf(tvb_get_ptr(tvb, offset, IPV4_LENGTH), buf, MAX_IP_STR_LEN);
122   return buf;
123 }
124
125 /* XXX FIXME
126 remove this one later when every call has been converted to ep_address_to_str()
127 */
128 gchar *
129 ip6_to_str(const struct e_in6_addr *ad) {
130   gchar *str;
131
132   str=ep_alloc(MAX_IP6_STR_LEN);
133   ip6_to_str_buf(ad, str);
134   return str;
135 }
136
137 #define IPV6_LENGTH 16
138 gchar *
139 tvb_ip6_to_str(tvbuff_t *tvb, const gint offset)
140 {
141   gchar *buf;
142
143   buf=ep_alloc(MAX_IP6_STR_LEN);
144   ip6_to_str_buf((const struct e_in6_addr *)tvb_get_ptr(tvb, offset, IPV6_LENGTH), buf);
145   return buf;
146 }
147
148 /* const char *
149  * inet_ntop6(src, dst, size)
150  *      convert IPv6 binary address into presentation (printable) format
151  * author:
152  *      Paul Vixie, 1996.
153  */
154 static void
155 ip6_to_str_buf_len(const guchar* src, char *buf, size_t buf_len)
156 {
157         struct { int base, len; } best, cur;
158         guint words[8];
159         int i;
160
161         if (buf_len < MAX_IP6_STR_LEN) {        /* buf_len < 40 */
162                 g_strlcpy(buf, BUF_TOO_SMALL_ERR, buf_len);     /* Let the unexpected value alert user */
163                 return;
164         }
165
166         /*
167          * Preprocess:
168          *      Copy the input (bytewise) array into a wordwise array.
169          *      Find the longest run of 0x00's in src[] for :: shorthanding.
170          */
171         for (i = 0; i < 16; i += 2) {
172                 words[i / 2] = (src[i+1] << 0);
173                 words[i / 2] |= (src[i] << 8);
174         }
175         best.base = -1;
176         cur.base = -1;
177         for (i = 0; i < 8; i++) {
178                 if (words[i] == 0) {
179                         if (cur.base == -1)
180                                 cur.base = i, cur.len = 1;
181                         else
182                                 cur.len++;
183                 } else {
184                         if (cur.base != -1) {
185                                 if (best.base == -1 || cur.len > best.len)
186                                         best = cur;
187                                 cur.base = -1;
188                         }
189                 }
190         }
191         if (cur.base != -1) {
192                 if (best.base == -1 || cur.len > best.len)
193                         best = cur;
194         }
195         if (best.base != -1 && best.len < 2)
196                 best.base = -1;
197
198         /* Is this address an encapsulated IPv4? */
199         if (best.base == 0 && (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
200         {
201                 /* best.len == 6 -> ::IPv4; 5 -> ::ffff:IPv4 */
202                 buf = g_stpcpy(buf, "::");
203                 if (best.len == 5)
204                 buf = g_stpcpy(buf, "ffff:");
205                 ip_to_str_buf(src + 12, buf, MAX_IP_STR_LEN);
206                 /* max: 2 + 5 + 16 == 23 bytes */
207                 return;
208         }
209
210         /*
211          * Format the result.
212          */
213         for (i = 0; i < 8; i++) {
214                 /* Are we inside the best run of 0x00's? */
215                 if (i == best.base) {
216                         *buf++ = ':';
217                         i += best.len;
218
219                         /* Was it a trailing run of 0x00's? */
220                         if (i == 8) {
221                                 *buf++ = ':';
222                                 break;
223                         }
224                 }
225                 /* Are we following an initial run of 0x00s or any real hex? */
226                 if (i != 0)
227                         *buf++ = ':';
228
229                 buf = word_to_hex_npad(buf, words[i]); /* max: 4B */
230                 /* max: 8 * 4 + 7 == 39 bytes */
231         }
232         *buf = '\0';    /* 40 byte */
233 }
234
235 void
236 ip6_to_str_buf(const struct e_in6_addr *ad, gchar *buf)
237 {
238   ip6_to_str_buf_len((const guchar*)ad, buf, MAX_IP6_STR_LEN);
239 }
240
241 gchar*
242 ipx_addr_to_str(const guint32 net, const guint8 *ad)
243 {
244         gchar   *buf;
245         char    *name;
246
247         name = get_ether_name_if_known(ad);
248
249         if (name) {
250                 buf = ep_strdup_printf("%s.%s", get_ipxnet_name(net), name);
251         }
252         else {
253                 buf = ep_strdup_printf("%s.%s", get_ipxnet_name(net),
254                     bytestring_to_str(ad, 6, '\0'));
255         }
256         return buf;
257 }
258
259 gchar*
260 ipxnet_to_string(const guint8 *ad)
261 {
262         guint32 addr = pntohl(ad);
263         return ipxnet_to_str_punct(addr, ' ');
264 }
265
266 gchar *
267 ipxnet_to_str_punct(const guint32 ad, const char punct)
268 {
269   gchar *buf = ep_alloc(12);
270
271   *dword_to_hex_punct(buf, ad, punct) = '\0';
272   return buf;
273 }
274
275 static void
276 vines_addr_to_str_buf(const guint8 *addrp, gchar *buf, int buf_len)
277 {
278   if (buf_len < 14) {
279      g_strlcpy(buf, BUF_TOO_SMALL_ERR, buf_len);        /* Let the unexpected value alert user */
280      return;
281   }
282
283   buf    = dword_to_hex(buf, pntohl(&addrp[0]));        /* 8 bytes */
284   *buf++ = '.';                                         /* 1 byte */
285   buf    = word_to_hex(buf, pntohs(&addrp[4]));         /* 4 bytes */
286   *buf   = '\0';                                        /* 1 byte */
287 }
288
289 gchar *
290 tvb_vines_addr_to_str(tvbuff_t *tvb, const gint offset)
291 {
292   gchar *buf;
293
294   buf=ep_alloc(214); /* XXX, 14 here? */
295
296   vines_addr_to_str_buf(tvb_get_ptr(tvb, offset, VINES_ADDR_LEN), buf, 214);
297   return buf;
298 }
299
300 static void
301 usb_addr_to_str_buf(const guint8 *addrp, gchar *buf, int buf_len)
302 {
303   if(pletohl(&addrp[0])==0xffffffff){
304     g_snprintf(buf, buf_len, "host");
305   } else {
306     g_snprintf(buf, buf_len, "%d.%d", pletohl(&addrp[0]), pletohl(&addrp[4]));
307   }
308 }
309
310 static void
311 tipc_addr_to_str_buf( const guint8 *data, gchar *buf, int buf_len){
312         guint8 zone;
313         guint16 subnetwork;
314         guint16 processor;
315         guint32 tipc_address;
316
317         tipc_address = data[0];
318         tipc_address = (tipc_address << 8) ^ data[1];
319         tipc_address = (tipc_address << 8) ^ data[2];
320         tipc_address = (tipc_address << 8) ^ data[3];
321
322         processor = tipc_address & 0x0fff;
323
324         tipc_address = tipc_address >> 12;
325         subnetwork = tipc_address & 0x0fff;
326
327         tipc_address = tipc_address >> 12;
328         zone = tipc_address & 0xff;
329
330         g_snprintf(buf,buf_len,"%u.%u.%u",zone,subnetwork,processor);
331 }
332
333 static void
334 ib_addr_to_str_buf( const address *addr, gchar *buf, int buf_len){
335         if (addr->len >= 16) {  /* GID is 128bits */
336                 #define PREAMBLE_STR_LEN                (sizeof("GID: ") - 1)
337                 g_snprintf(buf,buf_len,"GID: ");
338                 if (buf_len < (int)PREAMBLE_STR_LEN ||
339                                 inet_ntop(AF_INET6, addr->data, buf + PREAMBLE_STR_LEN, 
340                                                   buf_len - PREAMBLE_STR_LEN) == NULL ) /* Returns NULL if no space and does not touch buf */
341                         g_snprintf ( buf, buf_len, BUF_TOO_SMALL_ERR ); /* Let the unexpected value alert user */
342         } else {        /* this is a LID (16 bits) */
343                 guint16 lid_number = *((guint16*) addr->data);
344                 g_snprintf(buf,buf_len,"LID: %u",lid_number);
345         }
346 }
347
348 /* XXX FIXME
349 remove this one later when every call has been converted to ep_address_to_str()
350 */
351 gchar *
352 fc_to_str(const guint8 *ad)
353 {
354     return bytestring_to_str (ad, 3, '.');
355 }
356
357 gchar *
358 tvb_fc_to_str(tvbuff_t *tvb, const gint offset)
359 {
360     return bytestring_to_str (tvb_get_ptr(tvb, offset, 3), 3, '.');
361 }
362
363 /* FC Network Header Network Address Authority Identifiers */
364
365 #define FC_NH_NAA_IEEE          1       /* IEEE 802.1a */
366 #define FC_NH_NAA_IEEE_E        2       /* IEEE Exteneded */
367 #define FC_NH_NAA_LOCAL         3
368 #define FC_NH_NAA_IP            4       /* 32-bit IP address */
369 #define FC_NH_NAA_IEEE_R        5       /* IEEE Registered */
370 #define FC_NH_NAA_IEEE_R_E      6       /* IEEE Registered Exteneded */
371 /* according to FC-PH 3 draft these are now reclaimed and reserved */
372 #define FC_NH_NAA_CCITT_INDV    12      /* CCITT 60 bit individual address */
373 #define FC_NH_NAA_CCITT_GRP     14      /* CCITT 60 bit group address */
374
375 gchar *
376 fcwwn_to_str (const guint8 *ad)
377 {
378     int fmt;
379     guint8 oui[6];
380     gchar *ethstr;
381     gchar *ethptr;
382
383     if (ad == NULL) return NULL;
384
385     ethstr=ep_alloc(512);
386     ethptr = bytes_to_hexstr_punct(ethstr, ad, 8, ':'); /* 23 bytes */
387
388     fmt = (ad[0] & 0xF0) >> 4;
389
390     switch (fmt) {
391
392     case FC_NH_NAA_IEEE:
393     case FC_NH_NAA_IEEE_E:
394         memcpy (oui, &ad[2], 6);
395
396         g_snprintf (ethptr, 512-23, " (%s)", get_manuf_name (oui));
397         break;
398
399     case FC_NH_NAA_IEEE_R:
400         oui[0] = ((ad[0] & 0x0F) << 4) | ((ad[1] & 0xF0) >> 4);
401         oui[1] = ((ad[1] & 0x0F) << 4) | ((ad[2] & 0xF0) >> 4);
402         oui[2] = ((ad[2] & 0x0F) << 4) | ((ad[3] & 0xF0) >> 4);
403         oui[3] = ((ad[3] & 0x0F) << 4) | ((ad[4] & 0xF0) >> 4);
404         oui[4] = ((ad[4] & 0x0F) << 4) | ((ad[5] & 0xF0) >> 4);
405         oui[5] = ((ad[5] & 0x0F) << 4) | ((ad[6] & 0xF0) >> 4);
406
407         g_snprintf (ethptr, 512-23, " (%s)", get_manuf_name (oui));
408         break;
409
410     default:
411         *ethptr = '\0';
412         break;
413     }
414     return (ethstr);
415 }
416
417 gchar *
418 tvb_fcwwn_to_str(tvbuff_t *tvb, const gint offset)
419 {
420         return fcwwn_to_str (tvb_get_ptr(tvb, offset, 8));
421 }
422
423 /*XXX FIXME the code below may be called very very frequently in the future.
424   optimize it for speed and get rid of the slow sprintfs */
425 /* XXX - perhaps we should have individual address types register
426    a table of routines to do operations such as address-to-name translation,
427    address-to-string translation, and the like, and have this call them,
428    and also have an address-to-string-with-a-name routine */
429 /* XXX - use this, and that future address-to-string-with-a-name routine,
430    in "col_set_addr()"; it might also be useful to have address types
431    export the names of the source and destination address fields, so
432    that "col_set_addr()" need know nothing whatsoever about particular
433    address types */
434 /* convert an address struct into a printable string */
435
436 gchar*
437 ep_address_to_str(const address *addr)
438 {
439   gchar *str;
440
441   str=ep_alloc(MAX_ADDR_STR_LEN);
442   address_to_str_buf(addr, str, MAX_ADDR_STR_LEN);
443   return str;
444 }
445
446 /* The called routines use se_alloc'ed memory */
447 gchar*
448 se_address_to_str(const address *addr)
449 {
450   gchar *str;
451
452   str=se_alloc(MAX_ADDR_STR_LEN);
453   address_to_str_buf(addr, str, MAX_ADDR_STR_LEN);
454   return str;
455 }
456
457 void
458 address_to_str_buf(const address *addr, gchar *buf, int buf_len)
459 {
460   const guint8 *addrdata;
461   struct atalk_ddp_addr ddp_addr;
462
463   char temp[32];
464   char *tempptr = temp;
465
466   if (!buf || !buf_len)
467     return;
468
469   switch(addr->type){
470   case AT_NONE:
471     buf[0] = '\0';
472     break;
473   case AT_ETHER:                                        /* 18 bytes */
474     tempptr = bytes_to_hexstr_punct(tempptr, addr->data, 6, ':');       /* 17 bytes */
475     break;
476   case AT_IPv4:
477     ip_to_str_buf(addr->data, buf, buf_len);
478     break;
479   case AT_IPv6:
480     ip6_to_str_buf_len(addr->data, buf, buf_len);
481     break;
482   case AT_IPX:                                          /* 22 bytes */
483     addrdata = addr->data;
484     tempptr = bytes_to_hexstr(tempptr, &addrdata[0], 4);                /*  8 bytes */
485     *tempptr++ = '.';                                                   /*  1 byte  */
486     tempptr = bytes_to_hexstr(tempptr, &addrdata[4], 6);                /* 12 bytes */
487     break;
488   case AT_SNA:
489     sna_fid_to_str_buf(addr, buf, buf_len);
490     break;
491   case AT_ATALK:
492     memcpy(&ddp_addr, addr->data, sizeof ddp_addr);
493     atalk_addr_to_str_buf(&ddp_addr, buf, buf_len);
494     break;
495   case AT_VINES:
496     vines_addr_to_str_buf(addr->data, buf, buf_len);
497     break;
498   case AT_USB:
499     usb_addr_to_str_buf(addr->data, buf, buf_len);
500     break;
501   case AT_OSI:
502     print_nsap_net_buf(addr->data, addr->len, buf, buf_len);
503     break;
504   case AT_ARCNET:                                       /* 5 bytes */
505     tempptr = g_stpcpy(tempptr, "0x");                                  /* 2 bytes */
506     tempptr = bytes_to_hexstr(tempptr, addr->data, 1);                  /* 2 bytes */
507     break;
508   case AT_FC:                                           /* 9 bytes */
509     tempptr = bytes_to_hexstr_punct(tempptr, addr->data, 3, '.');       /* 8 bytes */
510     break;
511   case AT_SS7PC:
512     mtp3_addr_to_str_buf((const mtp3_addr_pc_t *)addr->data, buf, buf_len);
513     break;
514   case AT_STRINGZ:
515     g_strlcpy(buf, addr->data, buf_len);
516     break;
517   case AT_EUI64:                                        /* 24 bytes */
518     tempptr = bytes_to_hexstr_punct(tempptr, addr->data, 8, ':');       /* 23 bytes */
519     break;
520   case AT_URI: {
521     int copy_len = addr->len < (buf_len - 1) ? addr->len : (buf_len - 1);
522     memcpy(buf, addr->data, copy_len );
523     buf[copy_len] = '\0';
524     }
525     break;
526   case AT_TIPC:
527     tipc_addr_to_str_buf(addr->data, buf, buf_len);
528     break;
529   case AT_IB:
530     ib_addr_to_str_buf(addr, buf, buf_len);
531     break;
532   default:
533     g_assert_not_reached();
534   }
535
536   /* copy to output buffer */
537   if (tempptr != temp) {
538     size_t temp_len = (size_t) (tempptr - temp);
539
540     if (temp_len < (size_t) buf_len) {
541       memcpy(buf, temp, temp_len);
542       buf[temp_len] = '\0';
543     } else
544      g_strlcpy(buf, BUF_TOO_SMALL_ERR, buf_len);/* Let the unexpected value alert user */
545   }
546 }
547