~vcs-imports/gnome-media/main

« back to all changes in this revision

Viewing changes to cddb-slave2/inetaddr.c

  • Committer: thomasvs
  • Date: 2004-05-10 08:52:25 UTC
  • Revision ID: vcs-imports@canonical.com-20040510085225-zb6tqc12yi1h08gd
remove unneccesary whitespace

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
 * Library General Public License for more details.
14
14
 *
15
15
 * You should have received a copy of the GNU Library General Public
16
 
 * License along with this library; if not, write to the 
 
16
 * License along with this library; if not, write to the
17
17
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
18
 * Boston, MA  02111-1307, USA.
19
19
 */
62
62
    len = 1024;
63
63
    buf = g_new(gchar, len);
64
64
 
65
 
    while ((res = gethostbyname_r (hostname, &result_buf, buf, len, &result, &herr)) 
 
65
    while ((res = gethostbyname_r (hostname, &result_buf, buf, len, &result, &herr))
66
66
           == ERANGE)
67
67
      {
68
68
        len *= 2;
139
139
            sa->sin_family = result.h_addrtype;
140
140
            memcpy(&sa->sin_addr, result.h_addr_list[0], result.h_length);
141
141
          }
142
 
        
 
142
 
143
143
        if (nicename && result.h_name)
144
144
          *nicename = g_strdup(result.h_name);
145
145
 
147
147
      }
148
148
  }
149
149
 
150
 
#else 
 
150
#else
151
151
#ifdef HAVE_GETHOSTBYNAME_R_GLIB_MUTEX
152
152
  {
153
153
    struct hostent* he;
188
188
            sa->sin_family = result->h_addrtype;
189
189
            memcpy(&sa->sin_addr, result->h_addr_list[0], result->h_length);
190
190
          }
191
 
        
 
191
 
192
192
        if (nicename && result->h_name)
193
193
          *nicename = g_strdup(result->h_name);
194
194
 
195
195
        ReleaseMutex(gnet_hostent_Mutex);
196
196
        rv = TRUE;
197
 
   
 
197
 
198
198
      }
199
199
  }
200
200
#else
225
225
  return rv;
226
226
}
227
227
 
228
 
/* 
 
228
/*
229
229
 
230
230
   Thread safe gethostbyaddr (we assume that gethostbyaddr_r follows
231
231
   the same pattern as gethostbyname_r, so we don't have special
250
250
    len = 1024;
251
251
    buf = g_new(gchar, len);
252
252
 
253
 
    while ((res = gethostbyaddr_r (addr, length, type, &result_buf, buf, len, &result, &herr)) 
 
253
    while ((res = gethostbyaddr_r (addr, length, type, &result_buf, buf, len, &result, &herr))
254
254
           == ERANGE)
255
255
      {
256
256
        len *= 2;
306
306
      rv = g_strdup (result.h_name);
307
307
  }
308
308
 
309
 
#else 
 
309
#else
310
310
#ifdef HAVE_GETHOSTBYNAME_R_GLIB_MUTEX
311
311
  {
312
312
    struct hostent* he;
370
370
 *  @name: a nice name (eg, mofo.eecs.umich.edu) or a dotted decimal name
371
371
 *    (eg, 141.213.8.59).  You can delete the after the function is called.
372
372
 *  @port: port number (0 if the port doesn't matter)
373
 
 * 
 
373
 *
374
374
 *  Create an internet address from a name and port.  This function
375
375
 *  may block.
376
376
 *
377
377
 *  Returns: a new #GInetAddr, or NULL if there was a failure.
378
378
 *
379
379
 **/
380
 
GInetAddr* 
 
380
GInetAddr*
381
381
gnet_inetaddr_new (const gchar* name, gint port)
382
382
{
383
383
  struct sockaddr_in* sa_in;
408
408
          ia = g_new0(GInetAddr, 1);
409
409
          ia->name = g_strdup(name);
410
410
          ia->ref_count = 1;
411
 
          
 
411
 
412
412
          sa_in = (struct sockaddr_in*) &ia->sa;
413
413
          sa_in->sin_family = AF_INET;
414
414
          sa_in->sin_port = g_htons(port);
430
430
 *  @port: port number (0 if the port doesn't matter)
431
431
 *  @func: Callback function.
432
432
 *  @data: User data passed when callback function is called.
433
 
 * 
 
433
 *
434
434
 *  Create a GInetAddr from a name and port asynchronously.  Once the
435
435
 *  structure is created, it will call the callback.  It may call the
436
436
 *  callback if there is a failure.
438
438
 *  The Unix version creates a pthread thread which does the lookup.
439
439
 *  If pthreads aren't available, it forks and does the lookup.
440
440
 *  Forking will be slow or even fail when using operating systems
441
 
 *  that copy the entire process when forking.  
 
441
 *  that copy the entire process when forking.
442
442
 *
443
443
 *  If you need to lookup hundreds of addresses, we recommend calling
444
444
 *  g_main_iteration(FALSE) between calls.  This will help prevent an
457
457
 *
458
458
 **/
459
459
GInetAddrNewAsyncID
460
 
gnet_inetaddr_new_async (const gchar* name, gint port, 
 
460
gnet_inetaddr_new_async (const gchar* name, gint port,
461
461
                         GInetAddrNewAsyncFunc func, gpointer data)
462
462
{
463
463
  int pipes[2];
558
558
  /* Add a watch */
559
559
  state->watch = g_io_add_watch(g_io_channel_unix_new(pipes[0]),
560
560
                                (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL),
561
 
                                gnet_inetaddr_new_async_cb, 
 
561
                                gnet_inetaddr_new_async_cb,
562
562
                                state);
563
563
 
564
564
  return state;
578
578
  if (gnet_gethostbyname(name, &sa, NULL))
579
579
    {
580
580
      guchar size = 4;  /* FIX for IPv6 */
581
 
      
 
581
 
582
582
      if ( (write(outfd, &size, sizeof(guchar)) == -1) ||
583
583
           (write(outfd, &sa.sin_addr, size) == -1) )
584
584
        g_warning ("Problem writing to pipe\n");
601
601
}
602
602
 
603
603
 
604
 
gboolean 
605
 
gnet_inetaddr_new_async_cb (GIOChannel* iochannel, 
606
 
                            GIOCondition condition, 
 
604
gboolean
 
605
gnet_inetaddr_new_async_cb (GIOChannel* iochannel,
 
606
                            GIOCondition condition,
607
607
                            gpointer data)
608
608
{
609
609
  GInetAddrAsyncState* state = (GInetAddrAsyncState*) data;
675
675
 *
676
676
 *  Cancel an asynchronous GInetAddr creation that was started with
677
677
 *  gnet_inetaddr_new_async().
678
 
 * 
 
678
 *
679
679
 */
680
680
void
681
681
gnet_inetaddr_new_async_cancel (GInetAddrNewAsyncID id)
703
703
 *  @name: a nice name (eg, mofo.eecs.umich.edu) or a dotted decimal name
704
704
 *    (eg, 141.213.8.59).  You can delete the after the function is called.
705
705
 *  @port: port number (0 if the port doesn't matter)
706
 
 * 
 
706
 *
707
707
 *  Create an internet address from a name and port, but don't block
708
708
 *  and fail if success would require blocking.  This is, if the name
709
709
 *  is a canonical name or "localhost", it returns the address.
712
712
 *  Returns: a new #GInetAddr, or NULL if there was a failure.
713
713
 *
714
714
 **/
715
 
GInetAddr* 
 
715
GInetAddr*
716
716
gnet_inetaddr_new_nonblock (const gchar* name, gint port)
717
717
{
718
718
  struct sockaddr_in* sa_in;
749
749
 *   gnet_inetaddr_clone:
750
750
 *   @ia: Address to clone
751
751
 *
752
 
 *   Create an internet address from another one.  
 
752
 *   Create an internet address from another one.
753
753
 *
754
754
 *   Returns: a new InetAddr, or NULL if there was a failure.
755
755
 *
756
756
 **/
757
 
GInetAddr* 
 
757
GInetAddr*
758
758
gnet_inetaddr_clone(const GInetAddr* ia)
759
759
{
760
760
  GInetAddr* cia;
764
764
  cia = g_new0(GInetAddr, 1);
765
765
  cia->ref_count = 1;
766
766
  cia->sa = ia->sa;
767
 
  if (ia->name != NULL) 
 
767
  if (ia->name != NULL)
768
768
    cia->name = g_strdup(ia->name);
769
769
 
770
770
  return cia;
771
771
}
772
772
 
773
773
 
774
 
/** 
 
774
/**
775
775
 *  gnet_inetaddr_delete:
776
776
 *  @ia: GInetAddr to delete
777
777
 *
819
819
 
820
820
  if (ia->ref_count == 0)
821
821
    {
822
 
      if (ia->name != NULL) 
 
822
      if (ia->name != NULL)
823
823
        g_free (ia->name);
824
824
      g_free (ia);
825
825
    }
846
846
 *
847
847
 **/
848
848
GInetAddrGetNameAsyncID
849
 
gnet_inetaddr_get_name_async (GInetAddr* ia, 
 
849
gnet_inetaddr_get_name_async (GInetAddr* ia,
850
850
                              GInetAddrGetNameAsyncFunc func,
851
851
                              gpointer data)
852
852
{
858
858
    {
859
859
      (func)(ia, GINETADDR_ASYNC_STATUS_OK, g_strdup(ia->name), data);
860
860
    }
861
 
  
 
861
 
862
862
  /* Otherwise, fork and look it up */
863
863
  else
864
864
    {
883
883
 
884
884
          /* Write the name to the pipe.  If we didn't get a name, we
885
885
             just write the canonical name. */
886
 
          if ((name = gnet_gethostbyaddr((char*) &((struct sockaddr_in*)&ia->sa)->sin_addr, 
 
886
          if ((name = gnet_gethostbyaddr((char*) &((struct sockaddr_in*)&ia->sa)->sin_addr,
887
887
                                        sizeof(struct in_addr), AF_INET)) != NULL)
888
888
            {
889
889
              guint lenint = strlen(name);
908
908
              gchar buffer[INET_ADDRSTRLEN];    /* defined in netinet/in.h */
909
909
              guchar* p = (guchar*) &(GNET_SOCKADDR_IN(ia->sa).sin_addr);
910
910
 
911
 
              g_snprintf(buffer, sizeof(buffer), 
 
911
              g_snprintf(buffer, sizeof(buffer),
912
912
                         "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
913
913
              len = strlen(buffer);
914
914
 
941
941
          /* Add a watch */
942
942
          state->watch = g_io_add_watch(g_io_channel_unix_new(pipes[0]),
943
943
                                        (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL),
944
 
                                        gnet_inetaddr_get_name_async_cb, 
 
944
                                        gnet_inetaddr_get_name_async_cb,
945
945
                                        state);
946
946
          return state;
947
947
        }
966
966
 
967
967
 
968
968
 
969
 
gboolean 
970
 
gnet_inetaddr_get_name_async_cb (GIOChannel* iochannel, 
971
 
                                 GIOCondition condition, 
 
969
gboolean
 
970
gnet_inetaddr_get_name_async_cb (GIOChannel* iochannel,
 
971
                                 GIOCondition condition,
972
972
                                 gpointer data)
973
973
{
974
974
  GInetAddrReverseAsyncState* state = (GInetAddrReverseAsyncState*) data;
1032
1032
 *
1033
1033
 *  Cancel an asynchronous nice name lookup that was started with
1034
1034
 *  gnet_inetaddr_get_name_async().
1035
 
 * 
 
1035
 *
1036
1036
 */
1037
1037
void
1038
1038
gnet_inetaddr_get_name_async_cancel(GInetAddrGetNameAsyncID id)
1063
1063
 *  for deleting the returned string.
1064
1064
 *
1065
1065
 **/
1066
 
gchar* 
 
1066
gchar*
1067
1067
gnet_inetaddr_get_canonical_name(const GInetAddr* ia)
1068
1068
{
1069
1069
  gchar buffer[INET_ADDRSTRLEN];        /* defined in netinet/in.h */
1070
1070
  guchar* p = (guchar*) &(GNET_SOCKADDR_IN(ia->sa).sin_addr);
1071
 
  
 
1071
 
1072
1072
  g_return_val_if_fail (ia != NULL, NULL);
1073
1073
 
1074
 
  g_snprintf(buffer, sizeof(buffer), 
 
1074
  g_snprintf(buffer, sizeof(buffer),
1075
1075
             "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
1076
 
  
 
1076
 
1077
1077
  return g_strdup(buffer);
1078
1078
}
1079
1079
 
1140
1140
/**
1141
1141
 *  gnet_inetaddr_is_internet:
1142
1142
 *  @inetaddr: Address to check
1143
 
 * 
 
1143
 *
1144
1144
 *  Check if the address is a sensible internet address.  This mean it
1145
1145
 *  is not private, reserved, loopback, multicast, or broadcast.
1146
1146
 *
1184
1184
 *   192.168.0.0     -   192.168.255.255 (192.168/16 prefix)
1185
1185
 *
1186
1186
 *  (from RFC 1918.  See also draft-manning-dsua-02.txt)
1187
 
 * 
 
1187
 *
1188
1188
 *  Returns: TRUE if the address is reserved for private networks;
1189
1189
 *  FALSE otherwise.
1190
1190
 *
1254
1254
 *
1255
1255
 *  Check if the address is a loopback address.  Loopback addresses
1256
1256
 *  have the prefix 127.0.0.1/16.
1257
 
 * 
 
1257
 *
1258
1258
 *  Returns: TRUE if the address is a loopback address; FALSE
1259
1259
 *  otherwise.
1260
1260
 *
1283
1283
 *  Check if the address is a multicast address.  Multicast address
1284
1284
 *  are in the range 224.0.0.1 - 239.255.255.255 (ie, the top four
1285
1285
 *  bits are 1110).
1286
 
 * 
 
1286
 *
1287
1287
 *  Returns: TRUE if the address is a multicast address; FALSE
1288
1288
 *  otherwise.
1289
1289
 *
1290
1290
 **/
1291
 
gboolean 
 
1291
gboolean
1292
1292
gnet_inetaddr_is_multicast (const GInetAddr* inetaddr)
1293
1293
{
1294
1294
  guint addr;
1312
1312
 *  Check if the address is a broadcast address.  The broadcast
1313
1313
 *  address is 255.255.255.255.  (Network broadcast address are
1314
1314
 *  network dependent.)
1315
 
 * 
 
1315
 *
1316
1316
 *  Returns: TRUE if the address is a broadcast address; FALSE
1317
1317
 *  otherwise.
1318
1318
 *
1319
1319
 **/
1320
 
gboolean 
 
1320
gboolean
1321
1321
gnet_inetaddr_is_broadcast (const GInetAddr* inetaddr)
1322
1322
{
1323
1323
  guint addr;
1348
1348
 *  Returns: hash value.
1349
1349
 *
1350
1350
 **/
1351
 
guint 
 
1351
guint
1352
1352
gnet_inetaddr_hash (gconstpointer p)
1353
1353
{
1354
1354
  const GInetAddr* ia;
1373
1373
 *  @p1: Pointer to first #GInetAddr.
1374
1374
 *  @p2: Pointer to second #GInetAddr.
1375
1375
 *
1376
 
 *  Compare two #GInetAddr's.  
 
1376
 *  Compare two #GInetAddr's.
1377
1377
 *
1378
1378
 *  Returns: 1 if they are the same; 0 otherwise.
1379
1379
 *
1380
1380
 **/
1381
 
gint 
 
1381
gint
1382
1382
gnet_inetaddr_equal(gconstpointer p1, gconstpointer p2)
1383
1383
{
1384
1384
  const GInetAddr* ia1 = (const GInetAddr*) p1;
1404
1404
 *  Returns: 1 if they are the same; 0 otherwise.
1405
1405
 *
1406
1406
 **/
1407
 
gint 
 
1407
gint
1408
1408
gnet_inetaddr_noport_equal(gconstpointer p1, gconstpointer p2)
1409
1409
{
1410
1410
  const GInetAddr* ia1 = (const GInetAddr*) p1;
1423
1423
 
1424
1424
/**
1425
1425
 *  gnet_inetaddr_gethostname:
1426
 
 * 
 
1426
 *
1427
1427
 *  Get the primary host's name.
1428
1428
 *
1429
1429
 *  Returns: the name of the host; NULL if there was an error.  The
1448
1448
 
1449
1449
/**
1450
1450
 *  gnet_inetaddr_gethostaddr:
1451
 
 * 
 
1451
 *
1452
1452
 *  Get the primary host's #GInetAddr.
1453
1453
 *
1454
1454
 *  Returns: the #GInetAddr of the host; NULL if there was an error.
1455
1455
 *  The caller is responsible for deleting the returned #GInetAddr.
1456
1456
 *
1457
1457
 **/
1458
 
GInetAddr* 
 
1458
GInetAddr*
1459
1459
gnet_inetaddr_gethostaddr(void)
1460
1460
{
1461
1461
  gchar* name;
1463
1463
 
1464
1464
  name = gnet_inetaddr_gethostname();
1465
1465
  if (name != NULL)
1466
 
    {  
 
1466
    {
1467
1467
      ia = gnet_inetaddr_new(name, 0);
1468
1468
      g_free(name);
1469
1469
    }
1487
1487
 *  Returns: INADDR_ANY #GInetAddr.
1488
1488
 *
1489
1489
 **/
1490
 
GInetAddr* 
 
1490
GInetAddr*
1491
1491
gnet_inetaddr_new_any (void)
1492
1492
{
1493
1493
  GInetAddr* ia;
1516
1516
 *  find one or there was an error.
1517
1517
 *
1518
1518
 **/
1519
 
GInetAddr* 
 
1519
GInetAddr*
1520
1520
gnet_inetaddr_autodetect_internet_interface (void)
1521
1521
{
1522
1522
  GInetAddr* jm_addr;
1558
1558
 *  this check.
1559
1559
 *
1560
1560
 **/
1561
 
GInetAddr* 
 
1561
GInetAddr*
1562
1562
gnet_inetaddr_get_interface_to (const GInetAddr* addr)
1563
1563
{
1564
1564
  int sockfd;
1607
1607
 *  such interface or the system does not support this check.
1608
1608
 *
1609
1609
 **/
1610
 
GInetAddr* 
 
1610
GInetAddr*
1611
1611
gnet_inetaddr_get_internet_interface (void)
1612
1612
{
1613
1613
  GInetAddr* iface = NULL;
1659
1659
 *  The caller should delete the list and the addresses.
1660
1660
 *
1661
1661
 **/
1662
 
GList* 
 
1662
GList*
1663
1663
gnet_inetaddr_list_interfaces (void)
1664
1664
{
1665
1665
  GList* list = NULL;
1682
1682
  while(1)
1683
1683
    {
1684
1684
      buf = g_new0(gchar, len);
1685
 
      
 
1685
 
1686
1686
      ifc.ifc_len = len;
1687
1687
      ifc.ifc_buf = buf;
1688
1688
      if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0)
1717
1717
      struct sockaddr addr;
1718
1718
      GInetAddr* ia;
1719
1719
 
1720
 
#ifdef HAVE_SOCKADDR_SA_LEN      
 
1720
#ifdef HAVE_SOCKADDR_SA_LEN
1721
1721
      ptr += sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len;
1722
1722
#else
1723
1723
      ptr += sizeof(struct ifreq);
1731
1731
 
1732
1732
      /* Save the address - the next call will clobber it */
1733
1733
      memcpy(&addr, &ifr->ifr_addr, sizeof(addr));
1734
 
      
 
1734
 
1735
1735
      /* Get the flags */
1736
1736
      ioctl(sockfd, SIOCGIFFLAGS, ifr);
1737
1737