0
0
mirror of https://github.com/ecki/net-tools.git synced 2025-04-09 19:14:21 +00:00
Files
net-tools/lib/strip.c
Mike Frysinger 5c9e1e7615 use sockaddr_storage everywhere
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
2015-11-24 09:34:47 -05:00

118 lines
2.5 KiB
C

/*
* lib/strip.c This file contains an implementation of the STRIP
* support functions.
*
* Version: strip.c 1.20 1999/04/22 eswierk
*
* Author: Stuart Cheshire <cheshire@cs.stanford.edu>
*
* 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"
#if HAVE_HWSTRIP
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if_arp.h>
#include <linux/types.h>
#include <linux/if_strip.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include "net-support.h"
#include "pathnames.h"
#include "util.h"
#include "intl.h"
extern struct hwtype strip_hwtype;
static const char *
pr_strip(const char *ptr)
{
static char buff[64];
if(ptr[1])
sprintf(buff, "%02x-%02x%02x-%02x%02x", *(ptr+1), *(ptr+2), *(ptr+3),
*(ptr+4), *(ptr+5));
else
sprintf(buff, "%02x%02x-%02x%02x", *(ptr+2), *(ptr+3), *(ptr+4),
*(ptr+5));
return buff;
}
static int
in_strip(char *bufp, struct sockaddr_storage *sasp)
{
struct sockaddr *sap = (struct sockaddr *)sasp;
int i,i0;
MetricomAddress *haddr = (MetricomAddress *) (sap->sa_data);
sap->sa_family = strip_hwtype.type;
/* figure out what the device-address should be */
i0 = i = (bufp[0] == '*') ? 1 : 0;
while (bufp[i] && (bufp[i] != '-'))
i++;
if (bufp[i] != '-')
return -1;
if(i-i0 == 2)
{
haddr->c[1] = strtol(&bufp[i0], 0, 16);
i++;
if(bufp[i] == 0) return -1;
}else{
haddr->c[1] = 0;
i=i0;
}
haddr->c[2] = strtol(&bufp[i], 0, 16) >> 8;
haddr->c[3] = strtol(&bufp[i], 0, 16) & 0xFF;
while (bufp[i] && (bufp[i] != '-'))
i++;
if (bufp[i] != '-')
return -1;
haddr->c[4] = strtol(&bufp[i+1], 0, 16) >> 8;
haddr->c[5] = strtol(&bufp[i+1], 0, 16) & 0xFF;
haddr->c[0] = 0;
return 0;
}
/* Start the STRIP encapsulation on the file descriptor. */
static int do_strip(int fd)
{
int disc = N_STRIP;
if (ioctl(fd, TIOCSETD, &disc) < 0)
{
fprintf(stderr, "STRIP_set_disc(%d): %s\n", disc, strerror(errno));
return(-errno);
}
return(0);
}
struct hwtype strip_hwtype = {
"strip", "Metricom Starmode IP", ARPHRD_METRICOM, sizeof(MetricomAddress),
pr_strip, in_strip, do_strip, 0
};
#endif /* HAVE_HWSTRIP */