Esempio n. 1
0
File: util.c Progetto: imp/slist
/*
 * util_create_guid -- generate GUID based on the guid type
 * id_type:	SPC_INQUIRY_ID_TYPE_EUI -
 *		EUI-64 based 16-byte designator format;
 *		SPC_INQUIRY_ID_TYPE_NAA -
 *		NAA IEEE Registered Extended designator format.
 *
 * SPC-4 revision 11 section 7.6.3.5.4 and 7.6.3.6.5.
 *
 * Note that now this function is always called with parameter
 * id_type SPC_INQUIRY_ID_TYPE_NAA, therefore the code for creating
 * EUI-64 based 16-byte format GUID is no longer used. But in order
 * to keep backward compatiability and for future extension, all the
 * code that has been used for creating old GUIDs should be kept, to
 * make the format clear for all possible GUIDs targets might have.
 */
Boolean_t
util_create_guid(char **guid, uchar_t id_type)
{
	eui_16_t	eui;
	/*
	 * We only have room for 32bits of data in the GUID. The hiword/loword
	 * macros will not work on 64bit variables. The work, but produce
	 * invalid results on Big Endian based machines.
	 */
	uint32_t	tval = (uint_t)time((time_t *)0);
	size_t		guid_size;
	int		i, fd;

	/*
	 * Create the NAA (6) GUID.
	 */
	if (id_type == SPC_INQUIRY_ID_TYPE_NAA) {
		return (util_create_guid_naa(guid));
	}

	if ((mac_len == 0) && (if_find_mac(NULL) == False)) {

		/*
		 * By default strict GUID generation is enforced. This can
		 * be disabled by using the correct XML tag in the configuration
		 * file.
		 */
		if (enforce_strict_guid == True)
			return (False);

		/*
		 * There's no MAC address available and we've even tried
		 * a second time to get one. So fallback to using a random
		 * number for the MAC address.
		 */
		if ((fd = open("/dev/random", O_RDONLY)) < 0)
			return (False);
		if (read(fd, &eui, sizeof (eui)) != sizeof (eui))
			return (False);
		(void) close(fd);

		eui.e_vers		= SUN_EUI_16_VERS;
		eui.e_company_id[0]	= (SUN_EN >> 16) & 0xff;
		eui.e_company_id[1]	= (SUN_EN >> 8) & 0xff;
		eui.e_company_id[2]	= SUN_EN & 0xff;

	} else {
Esempio n. 2
0
Boolean_t
util_create_guid(char **guid)
{
	eui_16_t	eui;
	/*
	 * We only have room for 32bits of data in the GUID. The hiword/loword
	 * macros will not work on 64bit variables. The work, but produce
	 * invalid results on Big Endian based machines.
	 */
	uint32_t	tval = (uint_t)time((time_t *)0);
	size_t		guid_size;
	int		i, fd;

	if ((mac_len == 0) && (if_find_mac(NULL) == False)) {

		/*
		 * By default strict GUID generation is enforced. This can
		 * be disabled by using the correct XML tag in the configuration
		 * file.
		 */
		if (enforce_strict_guid == True)
			return (False);

		/*
		 * There's no MAC address available and we've even tried
		 * a second time to get one. So fallback to using a random
		 * number for the MAC address.
		 */
		if ((fd = open("/dev/random", O_RDONLY)) < 0)
			return (False);
		if (read(fd, &eui, sizeof (eui)) != sizeof (eui))
			return (False);
		(void) close(fd);

		eui.e_vers		= SUN_EUI_16_VERS;
		eui.e_company_id[0]	= 0;
		eui.e_company_id[1]	= 0;
		eui.e_company_id[2]	= SUN_EN;

	} else {
		bzero(&eui, sizeof (eui));

		eui.e_vers	= SUN_EUI_16_VERS;
		/* ---- [0] & [1] are zero for Sun's IEEE identifier ---- */
		eui.e_company_id[2]	= SUN_EN;
		eui.e_timestamp[0]	= hibyte(hiword(tval));
		eui.e_timestamp[1]	= lobyte(hiword(tval));
		eui.e_timestamp[2]	= hibyte(loword(tval));
		eui.e_timestamp[3]	= lobyte(loword(tval));
		for (i = 0; i < min(mac_len, sizeof (eui.e_mac)); i++) {
			eui.e_mac[i] = mac_addr[i];
		}

		/*
		 * To prevent duplicate GUIDs we need to sleep for one
		 * second here since part of the GUID is a time stamp with
		 * a one second resolution.
		 */
		sleep(1);
	}

	if (tgt_xml_encode((uint8_t *)&eui, sizeof (eui), guid,
	    &guid_size) == False) {
		return (False);
	} else
		return (True);
}