Example #1
0
    static bool parse_address(const char *in, uint8_t *out)
    {
        size_t count = 0, j;
        char *i = const_cast<char *>(in);
        auto addr = reinterpret_cast<struct ether_addr *>(out);

        if (!in)
            return false;

        if (ether_hostton(in, addr) == 0)
            return true;

        if (strnlen(in, m_input_len + 1) != m_input_len)
            throw std::runtime_error("invalid address length");

        for (j = 1; j < ETH_ALEN; ++j)
            if (in[j*3 - 1] != ':')
                throw std::runtime_error("invalid address format");

        while (i && *i && count < ETH_ALEN)
            if (*i != ':')
                out[count++] = strtol(i, &i, m_hex_base);
            else
                i++;

        return true;
    }
Example #2
0
static int
ethers(int argc, char *argv[])
{
	char		hostname[MAXHOSTNAMELEN], *hp;
	int		i, rv = RV_OK;
	struct ether_addr ea, *eap;

	if (argc == 2) {
		fprintf(stderr, "%s: Enumeration not supported on ethers\n",
		    __progname);
		rv = RV_NOENUM;
	} else {
		for (i = 2; i < argc; i++) {
			if ((eap = ether_aton(argv[i])) == NULL) {
				eap = &ea;
				hp = argv[i];
				if (ether_hostton(hp, eap) != 0) {
					rv = RV_NOTFOUND;
					break;
				}
			} else {
				hp = hostname;
				if (ether_ntohost(hp, eap) != 0) {
					rv = RV_NOTFOUND;
					break;
				}
			}
			ETHERSPRINT;
		}
	}
	return rv;
}
Example #3
0
/*
 * Lookup an Ethernet address and return it.
 * Return NULL if addr not found.
 */
u_char *
lookup_hwa(char *hostname, int htype)
{
	switch (htype) {

		/* XXX - How is this done on other systems? -gwr */
#ifdef	ETC_ETHERS
	case HTYPE_ETHERNET:
	case HTYPE_IEEE802:
		{
			static struct ether_addr ea;
			/* This does a lookup in /etc/ethers */
			if (ether_hostton(hostname, &ea)) {
				report(LOG_ERR, "no HW addr for host \"%s\"",
					   hostname);
				return (u_char *) 0;
			}
			return (u_char *) & ea;
		}
#endif /* ETC_ETHERS */

	default:
		report(LOG_ERR, "no lookup for HW addr type %d", htype);
	}							/* switch */

	/* If the system can't do it, just return an error. */
	return (u_char *) 0;
}
Example #4
0
CckBool
transportAddressFromString_ethernet (CckTransportAddress *out, const char *address)
{

    int i;
    int digit;
    int octet;
    unsigned char *addr = (unsigned char*) address;

    clearTransportAddress(out);
    out->family = TT_FAMILY_ETHERNET;

    if(!ether_hostton(address, &out->addr.ether)) {
	return CCK_TRUE;
    }

    if(strlen(address) > TT_STRADDRLEN_ETHERNET) {
	CCK_ERROR("transportAddressFromString: Ethernet address too long: %s\n", address);
	return CCK_FALSE;
    }

    /* ...because ether_aton_r... */
    for(i = 0; i < 6; i++) {

	if((digit = hexDigitToInt(*addr++)) < 0) {
	    break;
	}

	if(!*addr || (*addr == ':')) {
	    octet = digit;
	    out->addr.ether.ether_addr_octet[i] = octet;
	} else {
	    octet = digit << 4;
	    if((digit = hexDigitToInt(*addr++)) < 0) {
		break;
	    }
	    octet += digit;
	    out->addr.ether.ether_addr_octet[i] = octet;
	}

	if(*addr == ':') {
	    addr++;
	}

    }

    if(i == 6 ) return CCK_TRUE;

    CCK_ERROR("Could not resolve / encode Ethernet address: %s\n",
		address);

    return CCK_FALSE;

}
Example #5
0
/*
 * Use the OS-supplied routine.
 * This *should* be thread-safe; the API doesn't have a static buffer.
 */
u_char *
pcap_ether_hostton(const char *name)
{
	register u_char *ap;
	u_char a[6];

	ap = NULL;
	if (ether_hostton(name, (struct ether_addr *)a) == 0) {
		ap = (u_char *)malloc(6);
		if (ap != NULL)
			memcpy((char *)ap, (char *)a, 6);
	}
	return (ap);
}
/* Convert the host ID string to a MAC address.
 * The string may be a:
 *    Host name
 *    IP address string
 *    MAC address string
*/
static inline void get_dest_addr(const char *hostid, struct ether_addr *eaddr)
{
	struct ether_addr *eap;

	eap = ether_aton(hostid);
	if (eap) {
		*eaddr = *eap;
		bb_debug_msg("The target station address is %s\n\n", ether_ntoa(eaddr));
#if !defined(__UCLIBC__)
	} else if (ether_hostton(hostid, eaddr) == 0) {
		bb_debug_msg("Station address for hostname %s is %s\n\n", hostid, ether_ntoa(eaddr));
#endif
	} else
		bb_show_usage();
}
Example #7
0
/* Convert the host ID string to a MAC address.
 * The string may be a:
 *    Host name
 *    IP address string
 *    MAC address string
 */
static void get_dest_addr(const char *hostid, struct ether_addr *eaddr)
{
	struct ether_addr *eap;

	eap = ether_aton_r(hostid, eaddr);
	if (eap) {
		bb_debug_msg("The target station address is %s\n\n", ether_ntoa(eap));
#if !defined(__UCLIBC__) || UCLIBC_VERSION >= KERNEL_VERSION(0, 9, 30)
	} else if (ether_hostton(hostid, eaddr) == 0) {
		bb_debug_msg("Station address for hostname %s is %s\n\n", hostid, ether_ntoa(eaddr));
#endif
	} else {
		bb_show_usage();
	}
}
static int
_stringToEtherAddr(struct ether_addr *address, const char *buffer)
{
    int count = sscanf(buffer, "%hhx%*[:-]%hhx%*[:-]%hhx%*[:-]%hhx%*[:-]%hhx%*[:-]%hhx",
                       &(address->ether_addr_octet[0]), &(address->ether_addr_octet[1]),
                       &(address->ether_addr_octet[2]), &(address->ether_addr_octet[3]),
                       &(address->ether_addr_octet[4]), &(address->ether_addr_octet[5]));
    if (count != 6) {
        if (ether_hostton(buffer, address) != 0) {
            errno = EINVAL;
            return -1;
        }
    }
    return 0;
}
Example #9
0
/* Convert the host ID string to a MAC address.
 * The string may be a:
 *    Host name
 *    IP address string
 *    MAC address string
 */
static void get_dest_addr(const char *hostid, struct ether_addr *eaddr)
{
	struct ether_addr *eap;

	eap = ether_aton_r(hostid, eaddr);
	if (eap) {
		bb_debug_msg("The target station address is %s\n\n", ether_ntoa(eap));
#if !defined(__UCLIBC_MAJOR__) \
 || __UCLIBC_MAJOR__ > 0 \
 || __UCLIBC_MINOR__ > 9 \
 || (__UCLIBC_MINOR__ == 9 && __UCLIBC_SUBLEVEL__ >= 30)
	} else if (ether_hostton(hostid, eaddr) == 0) {
		bb_debug_msg("Station address for hostname %s is %s\n\n", hostid, ether_ntoa(eaddr));
#endif
	} else {
		bb_show_usage();
	}
}
Example #10
0
/*
 * ethers
 */
static int
ethers(int argc, char *argv[])
{
	char		hostname[MAXHOSTNAMELEN + 1], *hp;
	struct ether_addr ea, *eap;
	int		i, rv;

	assert(argc > 1);
	assert(argv != NULL);

#define ETHERSPRINT	printf("%-17s  %s\n", ether_ntoa(eap), hp)

	rv = RV_OK;
	if (argc == 2) {
		fprintf(stderr, "Enumeration not supported on ethers\n");
		rv = RV_NOENUM;
	} else {
		for (i = 2; i < argc; i++) {
			if ((eap = ether_aton(argv[i])) == NULL) {
				eap = &ea;
				hp = argv[i];
				if (ether_hostton(hp, eap) != 0) {
					rv = RV_NOTFOUND;
					break;
				}
			} else {
				hp = hostname;
				if (ether_ntohost(hp, eap) != 0) {
					rv = RV_NOTFOUND;
					break;
				}
			}
			ETHERSPRINT;
		}
	}
	return rv;
}
Example #11
0
static int get_dest_addr(const char *hostid, struct ether_addr *eaddr)
{
	struct ether_addr *eap;

	eap = ether_aton(hostid);
	if (eap) {
		*eaddr = *eap;
		if (debug)
			fprintf(stderr, "The target station address is %s.\n",
					ether_ntoa(eaddr));
	} else if (ether_hostton(hostid, eaddr) == 0) {
		if (debug)
			fprintf(stderr, "Station address for hostname %s is %s.\n",
					hostid, ether_ntoa(eaddr));
	} else {
		(void)fprintf(stderr,
					  "ether-wake: The Magic Packet host address must be "
					  "specified as\n"
					  "  - a station address, 00:11:22:33:44:55, or\n"
					  "  - a hostname with a known 'ethers' entry.\n");
		return -1;
	}
	return 0;
}
Example #12
0
int
magic_assemble (struct magic *magic_buf, const char *mac_str,
								const char *passwd_str)
{
	int m[MAC_LEN];
	int j, k;


	if (mac_str == NULL || magic_buf == NULL) return -1;

	/* split the MAC address string into it's hex components */
	if (sscanf (mac_str, "%2x:%2x:%2x:%2x:%2x:%2x",
							&m[0], &m[1], &m[2], &m[3], &m[4], &m[5]) != MAC_LEN)
		{
			struct ether_addr ea;

			/* lets parse /etc/ethers for ethernet name resolving
			 * FIXME: ether_hostton() is not implemented on every platform
			 */
			if (ether_hostton (mac_str, &ea))
				{
					errno = EINVAL;
					return -1;
				}

			for (j = 0; j < MAC_LEN; ++j)
				m[j] = ea.ETHER_ADDR_OCTET[j];
		}

	/* accommodate the packet chunk's size to the packet type */
	if (passwd_str && magic_buf->size != sizeof (struct secureon))
		{
			magic_buf->packet = \
									(unsigned char *) xrealloc ((void *) magic_buf->packet,
																							sizeof (struct secureon));
			magic_buf->size = sizeof (struct secureon);
		}
	else if (passwd_str == NULL && magic_buf->size != sizeof (struct packet))
		{
			magic_buf->packet = \
									(unsigned char *) xrealloc ((void *) magic_buf->packet,
																							sizeof (struct packet));
			magic_buf->size = sizeof (struct packet);
		}


	/* assemble magic packet header */
	memset ((void *) magic_buf->packet, 0xff, MAGIC_HEADER);

	/* and now the data */
	for (j = 0; j < MAGIC_TIMES; j++)
		{
			for (k = 0; k < MAC_LEN; k++)
				((struct packet *) magic_buf->packet)->addr[j][k] = \
																							(unsigned char) m[k];
		}

	/* add the SecureON passwd */
	if (passwd_str)
		{
			int s[MAGIC_SECUREON];

			/* split the password string into it's hex components */
			if (sscanf (passwd_str, "%2x-%2x-%2x-%2x-%2x-%2x",
									&s[0], &s[1], &s[2], &s[3], &s[4], &s[5]) != MAGIC_SECUREON)
				{
					errno = EINVAL;
					return -2;
				}

			for (j = 0; j < MAGIC_SECUREON; j++)
				((struct secureon *) magic_buf->packet)->passwd[j] = \
																									(unsigned char) s[j];
		}

	return 0;
}