static void test_ipv6 (void) { NiceAddress addr, other, v4addr; gchar str[NICE_ADDRESS_STRING_LEN]; struct sockaddr_in6 sin, sin2; g_assert (nice_address_set_from_string (&v4addr, "172.1.0.1") == TRUE); memset (&sin, 0, sizeof (sin)); memset (&sin2, 0, sizeof (sin2)); memset (&addr, 0, sizeof (NiceAddress)); memset (&other, 0, sizeof (NiceAddress)); nice_address_init (&addr); nice_address_init (&other); nice_address_set_ipv6 (&addr, (guchar *) "\x00\x11\x22\x33" "\x44\x55\x66\x77" "\x88\x99\xaa\xbb" "\xcc\xdd\xee\xff"); g_assert (addr.s.ip6.sin6_family == AF_INET6); nice_address_to_string (&addr, str); g_assert (0 == strcmp (str, "11:2233:4455:6677:8899:aabb:ccdd:eeff")); nice_address_set_port (&addr, 9876); /* in native byte order */ nice_address_set_from_string (&other, "11:2233:4455:6677:8899:aabb:ccdd:eeff"); nice_address_set_port (&other, 9876); /* in native byte order */ nice_address_copy_to_sockaddr (&other, (struct sockaddr*)&sin2); nice_address_copy_to_sockaddr (&addr, (struct sockaddr*)&sin); g_assert (nice_address_equal (&addr, &other) == TRUE); nice_address_to_string (&addr, str); nice_address_to_string (&other, str); g_assert (memcmp (&sin, &sin2, sizeof(sin)) == 0); /* private IPv6 address */ nice_address_set_ipv6 (&addr, (guchar *) "\xfc\x00\x00\x00" "\x00\x00\x00\x00" "\x00\x00\x00\x00" "\x00\x00\x00\x01"); g_assert (nice_address_is_private (&addr) == TRUE); nice_address_set_ipv6 (&addr, (guchar *) "\x00\x00\x00\x00" "\x00\x00\x00\x00" "\x00\x00\x00\x00" "\x00\x00\x00\x01"); g_assert (nice_address_is_private (&addr) == TRUE); /* mismatching address families */ g_assert (nice_address_equal (&addr, &v4addr) != TRUE); /* mismatched type */ addr.s.addr.sa_family = AF_UNSPEC; /*g_assert (nice_address_equal (&addr, &v4addr) != TRUE);*/ }
static void test_ipv4 (void) { NiceAddress addr; NiceAddress other; gchar str[NICE_ADDRESS_STRING_LEN]; nice_address_init (&addr); nice_address_init (&other); nice_address_set_ipv4 (&addr, 0x01020304); g_assert (addr.s.ip4.sin_family == AF_INET); nice_address_to_string (&addr, str); g_assert (0 == strcmp (str, "1.2.3.4")); nice_address_to_string (&addr, str); /* same address */ nice_address_set_ipv4 (&other, 0x01020304); g_assert (TRUE == nice_address_equal (&addr, &other)); /* from sockaddr_in */ nice_address_set_port (&other, 9876); /* in native byte order */ other.s.ip4.sin_family = AF_INET; nice_address_set_from_string (&addr, "1.2.3.4"); nice_address_set_port (&addr, 9876); /* in native byte order */ nice_address_to_string (&addr, str); nice_address_to_string (&other, str); g_assert (TRUE == nice_address_equal (&addr, &other)); /* different IP */ nice_address_set_ipv4 (&other, 0x01020305); g_assert (FALSE == nice_address_equal (&addr, &other)); /* different port */ nice_address_set_ipv4 (&other, 0x01020304); nice_address_set_port (&addr, 1); g_assert (FALSE == nice_address_equal (&addr, &other)); /* test private address check */ { NiceAddress *heap_addr = nice_address_new (); g_assert (nice_address_set_from_string (heap_addr, "127.0.0.1") == TRUE); g_assert (nice_address_is_private (heap_addr) == TRUE); g_assert (nice_address_set_from_string (heap_addr, "127.0.0.1.1") != TRUE); nice_address_free (heap_addr); } }
/* * Adds a new local candidate. Implements the candidate pruning * defined in ICE spec section 4.1.3 "Eliminating Redundant * Candidates" (ID-19). */ static gboolean priv_add_local_candidate_pruned (NiceAgent *agent, guint stream_id, NiceComponent *component, NiceCandidate *candidate) { GSList *i; g_assert (candidate != NULL); for (i = component->local_candidates; i ; i = i->next) { NiceCandidate *c = i->data; if (nice_address_equal (&c->base_addr, &candidate->base_addr) && nice_address_equal (&c->addr, &candidate->addr) && c->transport == candidate->transport) { nice_debug ("Candidate %p (component-id %u) redundant, ignoring.", candidate, component->id); return FALSE; } } component->local_candidates = g_slist_append (component->local_candidates, candidate); conn_check_add_for_local_candidate(agent, stream_id, component, candidate); return TRUE; }
/* * Finds a remote candidate with matching address and * transport. * * @return pointer to candidate or NULL if not found */ NiceCandidate * component_find_remote_candidate (const Component *component, const NiceAddress *addr, NiceCandidateTransport transport) { GSList *i; for (i = component->remote_candidates; i; i = i->next) { NiceCandidate *candidate = i->data; if (nice_address_equal(&candidate->addr, addr) && candidate->transport == transport) return candidate; } return NULL; }
/* * Creates a server reflexive candidate for 'component_id' of stream * 'stream_id' for each TCP_PASSIVE and TCP_ACTIVE candidates for each * base address. * * @return pointer to the created candidate, or NULL on error */ void discovery_discover_tcp_server_reflexive_candidates ( NiceAgent *agent, guint stream_id, guint component_id, NiceAddress *address, NiceSocket *base_socket) { NiceComponent *component; NiceStream *stream; NiceAddress base_addr = base_socket->addr; GSList *i; if (!agent_find_component (agent, stream_id, component_id, &stream, &component)) return; nice_address_set_port (&base_addr, 0); for (i = component->local_candidates; i; i = i ->next) { NiceCandidate *c = i->data; NiceAddress caddr; caddr = c->addr; nice_address_set_port (&caddr, 0); if (agent->force_relay == FALSE && c->transport != NICE_CANDIDATE_TRANSPORT_UDP && c->type == NICE_CANDIDATE_TYPE_HOST && nice_address_equal (&base_addr, &caddr)) { nice_address_set_port (address, nice_address_get_port (&c->addr)); discovery_add_server_reflexive_candidate ( agent, stream_id, component_id, address, c->transport, c->sockptr, FALSE); } } }
gint main (void) { NiceAgent *agent; NiceAddress addr_local, addr_remote; NiceCandidate *candidate; GSList *candidates, *i; guint stream_id; #ifdef G_OS_WIN32 WSADATA w; WSAStartup(0x0202, &w); #endif nice_address_init (&addr_local); nice_address_init (&addr_remote); g_type_init (); #if !GLIB_CHECK_VERSION(2,31,8) g_thread_init(NULL); #endif g_assert (nice_address_set_from_string (&addr_local, "127.0.0.1")); g_assert (nice_address_set_from_string (&addr_remote, "127.0.0.1")); nice_address_set_port (&addr_remote, 2345); agent = nice_agent_new ( NULL, NICE_COMPATIBILITY_RFC5245); g_assert (agent->local_addresses == NULL); /* add one local address */ nice_agent_add_local_address (agent, &addr_local); g_assert (agent->local_addresses != NULL); g_assert (g_slist_length (agent->local_addresses) == 1); g_assert (nice_address_equal (agent->local_addresses->data, &addr_local)); /* add a stream */ stream_id = nice_agent_add_stream (agent, 1); nice_agent_gather_candidates (agent, stream_id); /* adding a stream should cause host candidates to be generated */ candidates = nice_agent_get_local_candidates (agent, stream_id, 1); g_assert (g_slist_length (candidates) == 1); candidate = candidates->data; /* socket manager uses random port number */ nice_address_set_port (&addr_local, 1); nice_address_set_port (&(candidate->addr), 1); g_assert (nice_address_equal (&(candidate->addr), &addr_local)); g_assert (strncmp (candidate->foundation, "1", 1) == 0); for (i = candidates; i; i = i->next) nice_candidate_free ((NiceCandidate *) i->data); g_slist_free (candidates); /* clean up */ g_object_unref (agent); #ifdef G_OS_WIN32 WSACleanup(); #endif return 0; }