示例#1
0
/**
 * mate_vfs_resolve_next_address:
 * @handle: a #MateVFSResolveHandle.
 * @address: a pointer to a pointer to a #MateVFSAddress.
 * 
 * Stores the next #MateVFSAddress available in @handle of the
 * former lookup in @address.
 * 
 * Return value: %TRUE if the next address was stored in @address or
 * %FALSE if no other address is available.
 *
 * Since: 2.8
 */
gboolean
mate_vfs_resolve_next_address (MateVFSResolveHandle  *handle,
				MateVFSAddress       **address)
{
	g_return_val_if_fail (address != NULL, FALSE);
	g_return_val_if_fail (handle != NULL, FALSE);
	
	*address = NULL;
	
#ifdef HAVE_GETADDRINFO   
	while (*address == NULL && handle->current != NULL) {
#ifdef _AIX
		/* getaddrinfo() on AIX 4.3.2 and probably others don't set sa_family in
		   ai_addr so we have to copy it from ai_family */
		handle->current->ai_addr->sa_family = handle->current->ai_family;
#endif
		*address = mate_vfs_address_new_from_sockaddr (handle->current->ai_addr,
								handle->current->ai_addrlen);
		handle->current = handle->current->ai_next;
	}
#else
	if (handle->current) {
		*address = mate_vfs_address_dup (handle->current->data);
		handle->current = handle->current->next;
	}
#endif	
	return *address != NULL;	
}
int
main (int argc, char **argv)
{
	MateVFSAddress *v4_a;
	MateVFSAddress *v4_b;
	guint32          v4_numeric;
	gboolean         equal;
#ifdef ENABLE_IPV6
	MateVFSAddress *v6_a;
	MateVFSAddress *v6_b;
#endif
	fprintf (stderr, "Testing MateVFSAddress\n");
		
	mate_vfs_init ();

	/* v4 test cases */
	
	v4_a = mate_vfs_address_new_from_string ("127.3.2.1");

	/* generate a numeric 127.0.0.1 */
	v4_numeric = g_htonl (2130706433U);
	
	v4_b = mate_vfs_address_new_from_ipv4 (v4_numeric);
	
	g_assert (v4_a && v4_b);
	g_assert (mate_vfs_address_get_family_type (v4_a) == AF_INET);
	g_assert (mate_vfs_address_get_family_type (v4_b) == AF_INET);

	g_assert (mate_vfs_address_get_ipv4 (v4_b) == v4_numeric);
	
	/* compare the whole address for now */
	
	equal = mate_vfs_address_match (v4_a, v4_b, 32);
	g_assert (equal == FALSE);
	
	equal = mate_vfs_address_match (v4_a, v4_b, 24);
	g_assert (equal == FALSE);
	
	equal = mate_vfs_address_match (v4_a, v4_b, 16);
	g_assert (equal == FALSE);
	
	equal = mate_vfs_address_match (v4_a, v4_b, 8);
	g_assert (equal == TRUE);

	equal = mate_vfs_address_match (v4_a, v4_b, 0);
	g_assert (equal == FALSE);

	mate_vfs_address_free (v4_a);
	v4_a = mate_vfs_address_new_from_string ("127.0.0.1");

	equal = mate_vfs_address_match (v4_a, v4_b, 32);
	g_assert (equal == TRUE);

	mate_vfs_address_free (v4_b);
	v4_b = mate_vfs_address_dup (v4_a);

	equal = mate_vfs_address_equal (v4_a, v4_b);
	g_assert (equal == TRUE);
	
	equal = mate_vfs_address_match (v4_a, v4_b, 32);
	g_assert (equal == TRUE);

#ifdef INADDR_LOOPBACK
	v4_numeric = mate_vfs_address_get_ipv4 (v4_a);
	g_assert (INADDR_LOOPBACK == g_ntohl (v4_numeric));
#endif
	
#ifdef ENABLE_IPV6
	/* Mapped v4 test cases */

	v6_a = mate_vfs_address_new_from_string ("::ffff:127.0.0.1");

	g_assert (v6_a);
	g_assert (mate_vfs_address_get_family_type (v6_a) == AF_INET6);

	/* v4 mapped in v6 test (v4_a still is 127.0.0.1) */
	equal = mate_vfs_address_match (v4_a, v6_a, 32);
	g_assert (equal == TRUE);

	mate_vfs_address_free (v6_a);
	
	v6_a = mate_vfs_address_new_from_string ("fe80::dead:babe");
	v6_b = mate_vfs_address_new_from_string ("fe80::dead:beef");

	g_assert (v6_a && v6_b);
	g_assert (mate_vfs_address_get_family_type (v6_a) == AF_INET6);
	g_assert (mate_vfs_address_get_family_type (v6_b) == AF_INET6);

	equal = mate_vfs_address_match (v6_a, v6_b, 128);
	g_assert (equal == FALSE);

	/* fe80::dead:bx* */
	equal = mate_vfs_address_match (v6_a, v6_b, 120);
	g_assert (equal == FALSE);

	/* fe80::dead:b* */
	/* both address are equal from this mask on */
	equal = mate_vfs_address_match (v6_a, v6_b, 116);
	g_assert (equal == TRUE);

	/* fe80::dead:* */
	equal = mate_vfs_address_match (v6_a, v6_b, 112);
	g_assert (equal == TRUE);

	/* fe80::* */
	equal = mate_vfs_address_match (v6_a, v6_b, 64);
	g_assert (equal == TRUE);
	
	/* _dup test for v6 */	
	mate_vfs_address_free (v6_b);
	v6_b = mate_vfs_address_dup (v6_a);
	
	equal = mate_vfs_address_equal (v6_a, v6_b);
	g_assert (equal == TRUE);

	mate_vfs_address_free (v6_a);
	mate_vfs_address_free (v6_b);
	
#endif 	
	mate_vfs_address_free (v4_a);
	mate_vfs_address_free (v4_b);

	
	return 0;
}