mirror of
https://git.proxmox.com/git/mirror_iproute2
synced 2025-10-06 02:07:04 +00:00

The original intent was to make sure strings written by those functions are NUL-terminated at all times, though it was suggested to get rid of the 15 char protocol name limit as well which this patch accomplishes. In addition to that, simplify inet_proto_a2n() a bit: Use the error checking in get_u8() to find out whether passed 'buf' contains a valid decimal number instead of checking the first character's value manually. Signed-off-by: Phil Sutter <phil@nwl.cc>
72 lines
1.4 KiB
C
72 lines
1.4 KiB
C
/*
|
|
* inet_proto.c
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version
|
|
* 2 of the License, or (at your option) any later version.
|
|
*
|
|
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
|
|
*
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <syslog.h>
|
|
#include <fcntl.h>
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
#include <netdb.h>
|
|
#include <string.h>
|
|
|
|
#include "rt_names.h"
|
|
#include "utils.h"
|
|
|
|
const char *inet_proto_n2a(int proto, char *buf, int len)
|
|
{
|
|
static char *ncache;
|
|
static int icache = -1;
|
|
struct protoent *pe;
|
|
|
|
if (proto == icache)
|
|
return ncache;
|
|
|
|
pe = getprotobynumber(proto);
|
|
if (pe) {
|
|
if (icache != -1)
|
|
free(ncache);
|
|
icache = proto;
|
|
ncache = strdup(pe->p_name);
|
|
strncpy(buf, pe->p_name, len - 1);
|
|
buf[len - 1] = '\0';
|
|
return buf;
|
|
}
|
|
snprintf(buf, len, "ipproto-%d", proto);
|
|
return buf;
|
|
}
|
|
|
|
int inet_proto_a2n(const char *buf)
|
|
{
|
|
static char *ncache;
|
|
static int icache = -1;
|
|
struct protoent *pe;
|
|
__u8 ret;
|
|
|
|
if (icache != -1 && strcmp(ncache, buf) == 0)
|
|
return icache;
|
|
|
|
if (!get_u8(&ret, buf, 10))
|
|
return ret;
|
|
|
|
pe = getprotobyname(buf);
|
|
if (pe) {
|
|
if (icache != -1)
|
|
free(ncache);
|
|
icache = pe->p_proto;
|
|
ncache = strdup(pe->p_name);
|
|
return pe->p_proto;
|
|
}
|
|
return -1;
|
|
}
|