mirror of
https://github.com/ecki/net-tools.git
synced 2025-04-12 00:29:43 +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
100 lines
2.4 KiB
C
100 lines
2.4 KiB
C
/*
|
|
* lib/unix.c This file contains the general hardware types.
|
|
*
|
|
* Version: $Id: unix.c,v 1.6 1998/11/19 13:02:04 philip Exp $
|
|
*
|
|
* Author: Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
|
|
* Copyright 1993 MicroWalt Corporation
|
|
*
|
|
* 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 <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#if HAVE_AFUNIX
|
|
#include <sys/un.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"
|
|
|
|
|
|
/* Display an UNSPEC address. */
|
|
static const char *UNSPEC_print(const char *ptr)
|
|
{
|
|
static char buff[64];
|
|
char *pos;
|
|
unsigned int i;
|
|
|
|
pos = buff;
|
|
for (i = 0; i < sizeof(struct sockaddr); i++) {
|
|
pos += sprintf(pos, "%02X-", (*ptr++ & 0377));
|
|
}
|
|
buff[strlen(buff) - 1] = '\0';
|
|
return (buff);
|
|
}
|
|
|
|
|
|
/* Display an UNSPEC socket address. */
|
|
static const char *UNSPEC_sprint(const struct sockaddr_storage *sasp, int numeric)
|
|
{
|
|
const struct sockaddr *sap = (const struct sockaddr *)sasp;
|
|
static char buf[64];
|
|
|
|
if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
|
|
return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
|
|
return (UNSPEC_print(sap->sa_data));
|
|
}
|
|
|
|
|
|
#if HAVE_AFUNIX
|
|
|
|
/* Display a UNIX domain address. */
|
|
static const char *UNIX_print(const char *ptr)
|
|
{
|
|
return (ptr);
|
|
}
|
|
|
|
|
|
/* Display a UNIX domain address. */
|
|
static const char *UNIX_sprint(const struct sockaddr_storage *sasp, int numeric)
|
|
{
|
|
const struct sockaddr *sap = (const struct sockaddr *)sasp;
|
|
static char buf[64];
|
|
|
|
if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
|
|
return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
|
|
return (UNIX_print(sap->sa_data));
|
|
}
|
|
|
|
|
|
struct aftype unix_aftype =
|
|
{
|
|
"unix", NULL, /*"UNIX Domain", */ AF_UNIX, 0,
|
|
UNIX_print, UNIX_sprint, NULL, NULL,
|
|
NULL, NULL, NULL,
|
|
-1,
|
|
"/proc/net/unix"
|
|
};
|
|
#endif /* HAVE_AFUNIX */
|
|
|
|
|
|
struct aftype unspec_aftype =
|
|
{
|
|
"unspec", NULL, /*"UNSPEC", */ AF_UNSPEC, 0,
|
|
UNSPEC_print, UNSPEC_sprint, NULL, NULL,
|
|
NULL,
|
|
};
|