mirror of
https://github.com/ecki/net-tools.git
synced 2025-04-12 18:53:35 +00:00
Not all sockaddr structs have the same alignment. Instead, it depends on the fields contained in it. The way net-tools has written things though, it accepts sockaddr* everywhere which has 16bit alignment, even though it will cast it to other sockaddr types that have higher alignment. For example, `route` can crash on alpha because it declares sockaddr on the stack, but then casts it up to sockaddr_in6 (which has 32bits). It's also bad storage wise as we might try to cast the sockaddr to a type that is larger than sockaddr which means clobbering the stack. Instead, lets rewrite all the APIs to take a sockaddr_storage. This is guaranteed to have both the maximum alignment and size requirements for all other sockaddr types. Now we can safely cast that pointer to any other sockaddr type and not worry about it. It also has the nice effect of deleting a lot of casts in a lot of places when we only need the type of family. The vast majority of changes here are mechanical. There are a few places where we have to memcpy between a dedicated sockaddr_storage and a smaller struct because we're using an external embedded type (like arpreq). URL: https://bugs.gentoo.org/558436
141 lines
3.3 KiB
C
141 lines
3.3 KiB
C
/*
|
|
* lib/fddi.c This file contains an implementation of the "FDDI"
|
|
* support functions.
|
|
*
|
|
* Version: $Id: fddi.c,v 1.7 2000/03/05 11:26:02 philip Exp $
|
|
*
|
|
* Author: Lawrence V. Stefani, <stefani@lkg.dec.com>
|
|
*
|
|
* 1998-07-01 - Arnaldo Carvalho de Melo <acme@conectiva.com.br> GNU gettext
|
|
*
|
|
* 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.
|
|
*/
|
|
#include "config.h"
|
|
|
|
#include <features.h>
|
|
|
|
#if HAVE_HWFDDI
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <net/if_arp.h>
|
|
#ifndef ARPHRD_FDDI
|
|
#error "No FDDI Support in your current Kernelsource Tree."
|
|
#error "Disable HW Type FDDI"
|
|
#endif
|
|
#if __GLIBC__ >= 2
|
|
#include <netinet/if_fddi.h>
|
|
#else
|
|
#include <linux/if_fddi.h>
|
|
#endif
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <errno.h>
|
|
#include <ctype.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include "net-support.h"
|
|
#include "pathnames.h"
|
|
#include "intl.h"
|
|
#include "util.h"
|
|
|
|
extern struct hwtype fddi_hwtype;
|
|
|
|
|
|
/* Display an FDDI address in readable format. */
|
|
static const char *pr_fddi(const char *ptr)
|
|
{
|
|
static char buff[64];
|
|
|
|
snprintf(buff, sizeof(buff), "%02X-%02X-%02X-%02X-%02X-%02X",
|
|
(ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377),
|
|
(ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377)
|
|
);
|
|
return (buff);
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
#define _DEBUG 1
|
|
#else
|
|
#define _DEBUG 0
|
|
#endif
|
|
|
|
/* Input an FDDI address and convert to binary. */
|
|
static int in_fddi(char *bufp, struct sockaddr_storage *sasp)
|
|
{
|
|
struct sockaddr *sap = (struct sockaddr *)sasp;
|
|
char *ptr;
|
|
char c, *orig;
|
|
int i, val;
|
|
|
|
sap->sa_family = fddi_hwtype.type;
|
|
ptr = sap->sa_data;
|
|
|
|
i = 0;
|
|
orig = bufp;
|
|
while ((*bufp != '\0') && (i < FDDI_K_ALEN)) {
|
|
val = 0;
|
|
c = *bufp++;
|
|
if (isdigit(c))
|
|
val = c - '0';
|
|
else if (c >= 'a' && c <= 'f')
|
|
val = c - 'a' + 10;
|
|
else if (c >= 'A' && c <= 'F')
|
|
val = c - 'A' + 10;
|
|
else {
|
|
if (_DEBUG)
|
|
fprintf(stderr, _("in_fddi(%s): invalid fddi address!\n"), orig);
|
|
errno = EINVAL;
|
|
return (-1);
|
|
}
|
|
val <<= 4;
|
|
c = *bufp++;
|
|
if (isdigit(c))
|
|
val |= c - '0';
|
|
else if (c >= 'a' && c <= 'f')
|
|
val |= c - 'a' + 10;
|
|
else if (c >= 'A' && c <= 'F')
|
|
val |= c - 'A' + 10;
|
|
else {
|
|
if (_DEBUG)
|
|
fprintf(stderr, _("in_fddi(%s): invalid fddi address!\n"), orig);
|
|
errno = EINVAL;
|
|
return (-1);
|
|
}
|
|
*ptr++ = (unsigned char) (val & 0377);
|
|
i++;
|
|
|
|
/* We might get a semicolon here - not required. */
|
|
if (*bufp == ':') {
|
|
if (_DEBUG && i == FDDI_K_ALEN)
|
|
fprintf(stderr, _("in_fddi(%s): trailing : ignored!\n"),
|
|
orig);
|
|
bufp++;
|
|
}
|
|
}
|
|
|
|
/* That's it. Any trailing junk? */
|
|
if (_DEBUG && (i == FDDI_K_ALEN) && (*bufp != '\0')) {
|
|
fprintf(stderr, _("in_fddi(%s): trailing junk!\n"), orig);
|
|
errno = EINVAL;
|
|
return (-1);
|
|
}
|
|
if (_DEBUG)
|
|
fprintf(stderr, "in_fddi(%s): %s\n", orig, pr_fddi(sap->sa_data));
|
|
|
|
return (0);
|
|
}
|
|
|
|
|
|
struct hwtype fddi_hwtype =
|
|
{
|
|
"fddi", NULL, /*"Fiber Distributed Data Interface (FDDI)", */ ARPHRD_FDDI, FDDI_K_ALEN,
|
|
pr_fddi, in_fddi, NULL
|
|
};
|
|
|
|
|
|
#endif /* HAVE_HWFDDI */
|