gst_plugins_base/gst-libs/gst/netbuffer/gstnetbuffer.c
branchRCL_3
changeset 30 7e817e7e631c
parent 29 567bb019e3e3
equal deleted inserted replaced
29:567bb019e3e3 30:7e817e7e631c
   143  * @naddr: a network address
   143  * @naddr: a network address
   144  * @address: an IPv4 network address.
   144  * @address: an IPv4 network address.
   145  * @port: a port number to set.
   145  * @port: a port number to set.
   146  *
   146  *
   147  * Set @naddr with the IPv4 @address and @port pair.
   147  * Set @naddr with the IPv4 @address and @port pair.
   148  *
       
   149  * Note that @port and @address must be expressed in network byte order,
       
   150  * use g_htons() and g_htonl() to convert them to network byte order.
       
   151  */
   148  */
   152 #ifdef __SYMBIAN32__
   149 #ifdef __SYMBIAN32__
   153 EXPORT_C
   150 EXPORT_C
   154 #endif
   151 #endif
   155 
   152 
   169  * @naddr: a network address
   166  * @naddr: a network address
   170  * @address: an IPv6 network address.
   167  * @address: an IPv6 network address.
   171  * @port: a port number to set.
   168  * @port: a port number to set.
   172  *
   169  *
   173  * Set @naddr with the IPv6 @address and @port pair.
   170  * Set @naddr with the IPv6 @address and @port pair.
   174  *
       
   175  * Note that @port must be expressed in network byte order, use g_htons() to convert
       
   176  * it to network byte order.
       
   177  */
   171  */
   178 #ifdef __SYMBIAN32__
   172 #ifdef __SYMBIAN32__
   179 EXPORT_C
   173 EXPORT_C
   180 #endif
   174 #endif
   181 
   175 
   201 #ifdef __SYMBIAN32__
   195 #ifdef __SYMBIAN32__
   202 EXPORT_C
   196 EXPORT_C
   203 #endif
   197 #endif
   204 
   198 
   205 GstNetType
   199 GstNetType
   206 gst_netaddress_get_net_type (const GstNetAddress * naddr)
   200 gst_netaddress_get_net_type (GstNetAddress * naddr)
   207 {
   201 {
   208   g_return_val_if_fail (naddr != NULL, GST_NET_TYPE_UNKNOWN);
   202   g_return_val_if_fail (naddr != NULL, GST_NET_TYPE_UNKNOWN);
   209 
   203 
   210   return naddr->type;
   204   return naddr->type;
   211 }
   205 }
   214  * gst_netaddress_get_ip4_address:
   208  * gst_netaddress_get_ip4_address:
   215  * @naddr: a network address
   209  * @naddr: a network address
   216  * @address: a location to store the address.
   210  * @address: a location to store the address.
   217  * @port: a location to store the port.
   211  * @port: a location to store the port.
   218  *
   212  *
   219  * Get the IPv4 address stored in @naddr into @address. This function requires
   213  * Get the IPv4 address stored in @naddr into @address.
   220  * that the address type of @naddr is of type #GST_NET_TYPE_IP4.
       
   221  *
       
   222  * Note that @port and @address are expressed in network byte order, use
       
   223  * g_ntohs() and g_ntohl() to convert them to host order.
       
   224  *
   214  *
   225  * Returns: TRUE if the address could be retrieved.
   215  * Returns: TRUE if the address could be retrieved.
   226  */
   216  */
   227 #ifdef __SYMBIAN32__
   217 #ifdef __SYMBIAN32__
   228 EXPORT_C
   218 EXPORT_C
   229 #endif
   219 #endif
   230 
   220 
   231 gboolean
   221 gboolean
   232 gst_netaddress_get_ip4_address (const GstNetAddress * naddr, guint32 * address,
   222 gst_netaddress_get_ip4_address (GstNetAddress * naddr, guint32 * address,
   233     guint16 * port)
   223     guint16 * port)
   234 {
   224 {
   235   g_return_val_if_fail (naddr != NULL, FALSE);
   225   g_return_val_if_fail (naddr != NULL, FALSE);
   236 
   226 
   237   if (naddr->type == GST_NET_TYPE_UNKNOWN || naddr->type == GST_NET_TYPE_IP6)
   227   if (naddr->type == GST_NET_TYPE_UNKNOWN)
   238     return FALSE;
   228     return FALSE;
   239 
   229 
   240   if (address)
   230   if (address)
   241     *address = naddr->address.ip4;
   231     *address = naddr->address.ip4;
   242   if (port)
   232   if (port)
   251  * @address: a location to store the result.
   241  * @address: a location to store the result.
   252  * @port: a location to store the port.
   242  * @port: a location to store the port.
   253  *
   243  *
   254  * Get the IPv6 address stored in @naddr into @address.
   244  * Get the IPv6 address stored in @naddr into @address.
   255  *
   245  *
   256  * If @naddr is of type GST_NET_TYPE_IP4, the transitional IP6 address is
       
   257  * returned.
       
   258  *
       
   259  * Note that @port is expressed in network byte order, use g_ntohs() to convert
       
   260  * it to host order.
       
   261  *
       
   262  * Returns: TRUE if the address could be retrieved.
   246  * Returns: TRUE if the address could be retrieved.
   263  */
   247  */
   264 #ifdef __SYMBIAN32__
   248 #ifdef __SYMBIAN32__
   265 EXPORT_C
   249 EXPORT_C
   266 #endif
   250 #endif
   267 
   251 
   268 gboolean
   252 gboolean
   269 gst_netaddress_get_ip6_address (const GstNetAddress * naddr, guint8 address[16],
   253 gst_netaddress_get_ip6_address (GstNetAddress * naddr, guint8 address[16],
   270     guint16 * port)
   254     guint16 * port)
   271 {
   255 {
   272   static guint8 ip4_transition[16] =
       
   273       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF };
       
   274   g_return_val_if_fail (naddr != NULL, FALSE);
   256   g_return_val_if_fail (naddr != NULL, FALSE);
   275 
   257 
   276   if (naddr->type == GST_NET_TYPE_UNKNOWN)
   258   if (naddr->type == GST_NET_TYPE_UNKNOWN)
   277     return FALSE;
   259     return FALSE;
   278 
   260 
   279   if (address) {
   261   if (address)
   280     if (naddr->type == GST_NET_TYPE_IP6) {
   262     memcpy (address, naddr->address.ip6, 16);
   281       memcpy (address, naddr->address.ip6, 16);
       
   282     } else {                    /* naddr->type == GST_NET_TYPE_IP4 */
       
   283       memcpy (address, ip4_transition, 12);
       
   284       memcpy (address + 12, (guint8 *) & (naddr->address.ip4), 4);
       
   285     }
       
   286   }
       
   287   if (port)
   263   if (port)
   288     *port = naddr->port;
   264     *port = naddr->port;
   289 
   265 
   290   return TRUE;
   266   return TRUE;
   291 }
       
   292 
       
   293 /**
       
   294  * gst_netaddress_get_address_bytes:
       
   295  * @naddr: a network address
       
   296  * @address: a location to store the result.
       
   297  * @port: a location to store the port.
       
   298  *
       
   299  * Get just the address bytes stored in @naddr into @address.
       
   300  *
       
   301  * Note that @port is expressed in network byte order, use g_ntohs() to convert
       
   302  * it to host order. IP4 addresses are also stored in network byte order.
       
   303  *
       
   304  * Returns: number of bytes actually copied
       
   305  *
       
   306  * Since: 0.10.22
       
   307  */
       
   308 #ifdef __SYMBIAN32__
       
   309 EXPORT_C
       
   310 #endif
       
   311 
       
   312 gint
       
   313 gst_netaddress_get_address_bytes (const GstNetAddress * naddr,
       
   314     guint8 address[16], guint16 * port)
       
   315 {
       
   316   gint ret = 0;
       
   317 
       
   318   g_return_val_if_fail (naddr != NULL, FALSE);
       
   319 
       
   320   if (naddr->type == GST_NET_TYPE_UNKNOWN)
       
   321     return 0;
       
   322 
       
   323   if (address) {
       
   324     if (naddr->type == GST_NET_TYPE_IP6) {
       
   325       memcpy (address, naddr->address.ip6, 16);
       
   326       ret = 16;
       
   327     } else {                    /* naddr->type == GST_NET_TYPE_IP4 */
       
   328       memcpy (address, (guint8 *) & (naddr->address.ip4), 4);
       
   329       ret = 4;
       
   330     }
       
   331   }
       
   332   if (port)
       
   333     *port = naddr->port;
       
   334 
       
   335   return ret;
       
   336 }
       
   337 
       
   338 /**
       
   339  * gst_netaddress_set_address_bytes:
       
   340  * @naddr: a network address
       
   341  * @address: a location to store the result.
       
   342  * @port: a location to store the port.
       
   343  *
       
   344  * Set just the address bytes stored in @naddr into @address.
       
   345  *
       
   346  * Note that @port must be expressed in network byte order, use g_htons() to convert
       
   347  * it to network byte order order. IP4 address bytes must also be stored in
       
   348  * network byte order.
       
   349  *
       
   350  * Returns: number of bytes actually copied
       
   351  *
       
   352  * Since: 0.10.22
       
   353  */
       
   354 #ifdef __SYMBIAN32__
       
   355 EXPORT_C
       
   356 #endif
       
   357 
       
   358 gint
       
   359 gst_netaddress_set_address_bytes (GstNetAddress * naddr, GstNetType type,
       
   360     guint8 address[16], guint16 port)
       
   361 {
       
   362   gint len = 0;
       
   363 
       
   364   g_return_val_if_fail (naddr != NULL, 0);
       
   365 
       
   366   naddr->type = type;
       
   367   switch (naddr->type) {
       
   368     case GST_NET_TYPE_UNKNOWN:
       
   369     case GST_NET_TYPE_IP6:
       
   370       len = 16;
       
   371       memcpy (naddr->address.ip6, address, 16);
       
   372       break;
       
   373     case GST_NET_TYPE_IP4:
       
   374       len = 4;
       
   375       memcpy ((guint8 *) & (naddr->address.ip4), address, 4);
       
   376       break;
       
   377   }
       
   378 
       
   379   if (port)
       
   380     naddr->port = port;
       
   381 
       
   382   return len;
       
   383 }
   267 }
   384 
   268 
   385 /**
   269 /**
   386  * gst_netaddress_equal:
   270  * gst_netaddress_equal:
   387  * @naddr1: The first #GstNetAddress
   271  * @naddr1: The first #GstNetAddress
   423     default:
   307     default:
   424       break;
   308       break;
   425   }
   309   }
   426   return TRUE;
   310   return TRUE;
   427 }
   311 }
   428 
       
   429 /**
       
   430  * gst_netaddress_to_string:
       
   431  * @naddr: a #GstNetAddress
       
   432  * @dest: destination
       
   433  * @len: len of @dest
       
   434  *
       
   435  * Copies a string representation of @naddr into @dest. Up to @len bytes are
       
   436  * copied.
       
   437  *
       
   438  * Returns: the number of bytes which would be produced if the buffer was large
       
   439  * enough
       
   440  *
       
   441  * Since: 0.10.24
       
   442  */
       
   443  
       
   444 #ifdef __SYMBIAN32__
       
   445 EXPORT_C
       
   446 #endif
       
   447 
       
   448 gint
       
   449 gst_netaddress_to_string (const GstNetAddress * naddr, gchar * dest, gulong len)
       
   450 {
       
   451   gint result;
       
   452 
       
   453   g_return_val_if_fail (naddr != NULL, FALSE);
       
   454   g_return_val_if_fail (dest != NULL, FALSE);
       
   455 
       
   456   switch (naddr->type) {
       
   457     case GST_NET_TYPE_IP4:
       
   458     {
       
   459       guint32 address;
       
   460       guint16 port;
       
   461 
       
   462       gst_netaddress_get_ip4_address (naddr, &address, &port);
       
   463       address = g_ntohl (address);
       
   464 
       
   465       result = g_snprintf (dest, len, "%d.%d.%d.%d:%d", (address >> 24) & 0xff,
       
   466           (address >> 16) & 0xff, (address >> 8) & 0xff, address & 0xff,
       
   467           g_ntohs (port));
       
   468       break;
       
   469     }
       
   470     case GST_NET_TYPE_IP6:
       
   471     {
       
   472       guint8 address[16];
       
   473       guint16 port;
       
   474 
       
   475       gst_netaddress_get_ip6_address (naddr, address, &port);
       
   476 
       
   477       result =
       
   478           g_snprintf (dest, len, "[%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x]:%d",
       
   479           (address[0] << 8) | address[1], (address[2] << 8) | address[3],
       
   480           (address[4] << 8) | address[5], (address[6] << 8) | address[7],
       
   481           (address[8] << 8) | address[9], (address[10] << 8) | address[11],
       
   482           (address[12] << 8) | address[13], (address[14] << 8) | address[15],
       
   483           g_ntohs (port));
       
   484       break;
       
   485     }
       
   486     default:
       
   487       dest[0] = 0;
       
   488       result = 0;
       
   489       break;
       
   490   }
       
   491   return result;
       
   492 }