"Fossies" - the Fresh Open Source Software Archive

Member "pktstat-1.8.5/udp.c" (4 Mar 2012, 2980 Bytes) of package /linux/privat/old/pktstat-1.8.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /* David Leonard, 2002. Public domain. */
    2 /* $Id: udp.c 1205 2012-03-04 14:10:49Z d $ */
    3 
    4 #if HAVE_CONFIG_H
    5 # include "config.h"
    6 #endif
    7 
    8 #if STDC_HEADERS
    9 # include <stdio.h>
   10 # include <stdlib.h>
   11 # include <string.h>
   12 #endif
   13 #if HAVE_NETDB_H
   14 # include <netdb.h>
   15 #endif
   16 #if HAVE_SYS_TYPES_H
   17 # include <sys/types.h>
   18 #endif
   19 #if defined(__linux__) || defined(__GLIBC__)
   20 # define __FAVOR_BSD
   21 #endif
   22 #if HAVE_NETINET_IN_H
   23 # include <netinet/in.h>
   24 #endif
   25 #if HAVE_NETINET_IN_SYSTM_H
   26 # include <netinet/in_systm.h>
   27 #endif
   28 #if HAVE_NETINET_IP_H
   29 # include <netinet/ip.h>
   30 #endif
   31 #if HAVE_NETINET_IP6_H
   32 # include <netinet/ip6.h>
   33 #endif
   34 #if HAVE_NETINET_UDP_H
   35 # include <netinet/udp.h>
   36 #endif
   37 #if HAVE_ARPA_INET_H
   38 # include <arpa/inet.h>
   39 #endif
   40 
   41 #include "compat.h"
   42 #include "tag.h"
   43 #include "flow.h"
   44 #include "hash.h"
   45 #include "main.h"
   46 #include "display.h"
   47 
   48 static int
   49 udp_cmp(a, b)
   50     const void *a;
   51     const void *b;
   52 {
   53     const u_int16_t*ia = (const u_int16_t*)a;
   54     const u_int16_t*ib = (const u_int16_t*)b;
   55 
   56     return *ia - *ib;
   57 }
   58 
   59 static unsigned int
   60 udp_hash(a)
   61     const void *a;
   62 {
   63     const u_int16_t*ia = (const u_int16_t*)a;
   64 
   65     return *ia;
   66 }
   67 
   68 static struct hash udp_hashtab = {
   69     udp_cmp,        /* cmp */
   70     udp_hash,       /* hashfn */
   71     (free_t)free,       /* freekey */
   72     (free_t)free        /* freedata */
   73 };
   74 
   75 void
   76 udp_reset()
   77 {
   78     hash_clear(&udp_hashtab);
   79 }
   80 
   81 /* Look up an IP address */
   82 const char *
   83 udp_lookup(port)
   84     u_int16_t port;
   85 {
   86     const char *result;
   87     static int oldnflag = -1;
   88 
   89     if (oldnflag != nflag) {
   90         udp_reset();
   91         oldnflag = nflag;
   92     }
   93 
   94     result = (const char *)hash_lookup(&udp_hashtab, &port);
   95     if (result == NULL) {
   96         struct servent *se;
   97         u_int16_t *a2;
   98         char buf[TAGLEN];
   99 
  100         if (nflag)
  101             se = NULL;
  102         else {
  103             display_message("resolving udp port %u", port);
  104 #if HAVE_GETSERVBYPORT
  105             se = getservbyport(htons(port), "udp");
  106             display_message("");
  107 #else
  108             se = NULL;
  109 #endif
  110         }
  111         if (se == NULL) {
  112             snprintf(buf, sizeof buf, "%u", port);
  113             result = buf;
  114         } else
  115             result = se->s_name;
  116         result = strdup(result);
  117         a2 = (u_int16_t *)malloc(sizeof (u_int16_t));
  118         *a2 = port;
  119         hash_store(&udp_hashtab, a2, result);
  120     }
  121     return result;
  122 }
  123 
  124 const char *
  125 udp_tag(p, end, ip, ip6)
  126     const char *p;
  127     const char *end;
  128     const struct ip *ip;
  129     const struct ip6_hdr *ip6;
  130 {
  131     static char src[TAGLEN], dst[TAGLEN];
  132     static char tag[TAGLEN];
  133     struct udphdr *udp = (struct udphdr *)p;
  134     u_int16_t sport = ntohs(udp->uh_sport);
  135     u_int16_t dport = ntohs(udp->uh_dport);
  136 
  137     if (ip) {
  138         snprintf(src, sizeof src, "%s:%s", 
  139             ip_lookup(&ip->ip_src), udp_lookup(sport));
  140         snprintf(dst, sizeof dst, "%s:%s", 
  141             ip_lookup(&ip->ip_dst), udp_lookup(dport));
  142         snprintf(tag, sizeof tag, "udp %s", tag_combine(src, dst));
  143     }
  144 #if HAVE_NETINET_IP6_H
  145     if (ip6) {
  146         snprintf(src, sizeof src, "%s,%s", 
  147             ip6_lookup(&ip6->ip6_src), udp_lookup(sport));
  148         snprintf(dst, sizeof dst, "%s,%s", 
  149             ip6_lookup(&ip6->ip6_dst), udp_lookup(dport));
  150         snprintf(tag, sizeof tag, "udp6 %s", tag_combine(src, dst));
  151     }
  152 #endif
  153 
  154     return tag;
  155 }
  156