1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
/*
* named.c - Databases of named values
*
* Written 2001,2002 by Werner Almesberger
* Copyright 2001 Network Robots
* Copyright 2002 Bivio Networks, Werner Almesberger
*/
#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <arpa/inet.h>
#include <u128.h>
#include "error.h"
#include "named.h"
const static struct ether_proto {
const char *in_name; /* protocol name for name->number lookup */
uint16_t number; /* protocol number, in host byte order */
const char *out_name; /* protocol name for number->name lookup;
NULL if translation is not supported */
} ether_proto_db[] = {
{ "ip", ETH_P_IP, "ip" },
{ "ipv4", ETH_P_IP, "ip" },
{ "ipv6", ETH_P_IPV6, "ipv6" },
{ NULL, 0 }
};
uint16_t ether_proto(const char *name)
{
const struct ether_proto *ep;
for (ep = ether_proto_db; ep->in_name; ep++)
if (!strcmp(ep->in_name,name)) return ep->number;
yyerrorf("unknown ether protocol \"%s\"",name);
}
const char *ether_proto_print(uint16_t proto)
{
static char buf[7]; /* 0xffff\0 */
const struct ether_proto *ep;
for (ep = ether_proto_db; ep->in_name; ep++)
if (ep->number == proto) {
if (ep->out_name) return ep->out_name;
break;
}
sprintf(buf,"0x%x",(unsigned) proto);
return buf;
}
uint8_t ip_proto(const char *name)
{
struct protoent *pe;
pe = getprotobyname(name);
if (!pe) yyerrorf("unknown IP protocol \"%s\"",name);
return pe->p_proto;
}
/* @@@ gross hack ... should know L4 protocol too */
uint16_t ip_port(const char *name)
{
struct servent *se;
se = getservbyname(name,"tcp");
if (!se) se = getservbyname(name,"udp");
if (!se) yyerrorf("unknown service/port \"%s\"",name);
return ntohs(se->s_port);
}
|