~ubuntu-branches/ubuntu/gutsy/wireshark/gutsy-security

« back to all changes in this revision

Viewing changes to epan/to_str.c

  • Committer: Bazaar Package Importer
  • Author(s): Frederic Peters
  • Date: 2007-04-01 08:58:40 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070401085840-or3qhrpv8alt1bwg
Tags: 0.99.5-1
* New upstream release.
* debian/patches/09_idl2wrs.dpatch: updated to patch idl2wrs.sh.in.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* to_str.c
2
2
 * Routines for utilities to convert various other types to strings.
3
3
 *
4
 
 * $Id: to_str.c 19524 2006-10-14 05:02:40Z sahlberg $
 
4
 * $Id: to_str.c 20670 2007-02-01 18:40:28Z gerald $
5
5
 *
6
6
 * Wireshark - Network traffic analyzer
7
7
 * By Gerald Combs <gerald@wireshark.org>
65
65
#include <time.h>
66
66
#include "emem.h"
67
67
 
 
68
/*
 
69
 * If a user _does_ pass in a too-small buffer, this is probably
 
70
 * going to be too long to fit.  However, even a partial string
 
71
 * starting with "[Buf" should provide enough of a clue to be
 
72
 * useful.
 
73
 */
 
74
#define BUF_TOO_SMALL_ERR "[Buffer too small]"
 
75
 
68
76
/* Routine to convert a sequence of bytes to a hex string, one byte/two hex
69
77
 * digits at at a time, with a specified punctuation character between
70
78
 * the bytes.
135
143
ip_to_str(const guint8 *ad) {
136
144
  gchar *buf;
137
145
 
138
 
  buf=ep_alloc(16);
139
 
  ip_to_str_buf(ad, buf);
 
146
  buf=ep_alloc(MAX_IP_STR_LEN);
 
147
  ip_to_str_buf(ad, buf, MAX_IP_STR_LEN);
140
148
  return buf;
141
149
}
142
150
 
179
187
"248", "249", "250", "251", "252", "253", "254", "255"
180
188
};
181
189
void
182
 
ip_to_str_buf(const guint8 *ad, gchar *buf)
 
190
ip_to_str_buf(const guint8 *ad, gchar *buf, int buf_len)
183
191
{
184
192
        register gchar const *p;
185
193
        register gchar *b=buf;
186
194
 
 
195
        if (buf_len < MAX_IP_STR_LEN) {
 
196
                g_snprintf ( buf, buf_len, BUF_TOO_SMALL_ERR );                 /* Let the unexpected value alert user */
 
197
                return;
 
198
        }
 
199
 
187
200
        p=fast_strings[*ad++];
188
201
        do {
189
202
                *b++=*p;
323
336
  if(pletohl(&addrp[0])==0xffffffff){
324
337
    g_snprintf(buf, buf_len, "host");
325
338
  } else {
326
 
    g_snprintf(buf, buf_len, "%d", pletohl(&addrp[0]));
 
339
    g_snprintf(buf, buf_len, "%d.%d", pletohl(&addrp[0]), pletohl(&addrp[4]));
327
340
  }
328
341
}
329
342
 
546
559
        }
547
560
}
548
561
 
 
562
 
 
563
void
 
564
display_epoch_time(gchar *buf, int buflen, time_t sec, gint32 frac,
 
565
    time_res_t units)
 
566
{
 
567
        const char *sign;
 
568
        double elapsed_secs;
 
569
 
 
570
        elapsed_secs = difftime(sec,(time_t)0);
 
571
 
 
572
        /* This code copied from display_signed_time; keep it in case anyone
 
573
           is looking at captures from before 1970 (???).
 
574
           If the fractional part of the time stamp is negative,
 
575
           print its absolute value and, if the seconds part isn't
 
576
           (the seconds part should be zero in that case), stick
 
577
           a "-" in front of the entire time stamp. */
 
578
        sign = "";
 
579
        if (frac < 0) {
 
580
                frac = -frac;
 
581
                if (elapsed_secs >= 0)
 
582
                        sign = "-";
 
583
        }
 
584
        switch (units) {
 
585
 
 
586
        case SECS:
 
587
                g_snprintf(buf, buflen, "%s%0.0f", sign, elapsed_secs);
 
588
                break;
 
589
 
 
590
        case DSECS:
 
591
                g_snprintf(buf, buflen, "%s%0.0f.%01d", sign, elapsed_secs, frac);
 
592
                break;
 
593
 
 
594
        case CSECS:
 
595
                g_snprintf(buf, buflen, "%s%0.0f.%02d", sign, elapsed_secs, frac);
 
596
                break;
 
597
 
 
598
        case MSECS:
 
599
                g_snprintf(buf, buflen, "%s%0.0f.%03d", sign, elapsed_secs, frac);
 
600
                break;
 
601
 
 
602
        case USECS:
 
603
                g_snprintf(buf, buflen, "%s%0.0f.%06d", sign, elapsed_secs, frac);
 
604
                break;
 
605
 
 
606
        case NSECS:
 
607
                g_snprintf(buf, buflen, "%s%0.0f.%09d", sign, elapsed_secs, frac);
 
608
                break;
 
609
        }
 
610
}
 
611
 
549
612
/*
550
613
 * Display a relative time as days/hours/minutes/seconds.
551
614
 */
774
837
{
775
838
  gchar *str;
776
839
 
777
 
  str=ep_alloc(256);
778
 
  address_to_str_buf(addr, str, 256);
 
840
  str=ep_alloc(MAX_ADDR_STR_LEN);
 
841
  address_to_str_buf(addr, str, MAX_ADDR_STR_LEN);
779
842
  return str;
780
843
}
781
844
 
784
847
{
785
848
  struct atalk_ddp_addr ddp_addr;
786
849
 
 
850
  if (!buf)
 
851
    return;
 
852
 
787
853
  switch(addr->type){
788
854
  case AT_NONE:
789
855
    g_snprintf(buf, buf_len, "%s", "");
792
858
    g_snprintf(buf, buf_len, "%02x:%02x:%02x:%02x:%02x:%02x", addr->data[0], addr->data[1], addr->data[2], addr->data[3], addr->data[4], addr->data[5]);
793
859
    break;
794
860
  case AT_IPv4:
795
 
    ip_to_str_buf(addr->data, buf);
 
861
    ip_to_str_buf(addr->data, buf, buf_len);
796
862
    break;
797
863
  case AT_IPv6:
798
 
    inet_ntop(AF_INET6, addr->data, buf, INET6_ADDRSTRLEN);
 
864
    if ( inet_ntop(AF_INET6, addr->data, buf, buf_len) == NULL ) /* Returns NULL if no space and does not touch buf */
 
865
        g_snprintf ( buf, buf_len, BUF_TOO_SMALL_ERR );                 /* Let the unexpected value alert user */
799
866
    break;
800
867
  case AT_IPX:
801
868
    g_snprintf(buf, buf_len, "%02x%02x%02x%02x.%02x%02x%02x%02x%02x%02x", addr->data[0], addr->data[1], addr->data[2], addr->data[3], addr->data[4], addr->data[5], addr->data[6], addr->data[7], addr->data[8], addr->data[9]);