~cyphermox/ubuntu/natty/connman/release-0.64

« back to all changes in this revision

Viewing changes to gdhcp/common.c

  • Committer: Mathieu Trudel-Lapierre
  • Date: 2010-11-30 15:51:10 UTC
  • mfrom: (1.1.13 upstream)
  • Revision ID: mathieu.trudel-lapierre@canonical.com-20101130155110-32g0usyc4jbl131x
New upstream release 0.64.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
#include <config.h>
23
23
#endif
24
24
 
 
25
#include <stdio.h>
25
26
#include <errno.h>
26
27
#include <unistd.h>
 
28
#include <sys/ioctl.h>
27
29
#include <stdint.h>
28
30
#include <string.h>
29
31
#include <endian.h>
 
32
#include <net/if_arp.h>
 
33
#include <linux/if.h>
30
34
#include <netpacket/packet.h>
31
35
#include <net/ethernet.h>
32
36
 
44
48
        /* Options below will not be exposed to user */
45
49
        { OPTION_IP,                    0x32 }, /* requested-ip */
46
50
        { OPTION_U8,                    0x35 }, /* message-type */
 
51
        { OPTION_U32,                   0x36 }, /* server-id */
47
52
        { OPTION_U16,                   0x39 }, /* max-size */
48
53
        { OPTION_STRING,                0x3c }, /* vendor */
49
54
        { OPTION_STRING,                0x3d }, /* client-id */
416
421
 
417
422
        return fd;
418
423
}
 
424
 
 
425
char *get_interface_name(int index)
 
426
{
 
427
        struct ifreq ifr;
 
428
        int sk, err;
 
429
 
 
430
        if (index < 0)
 
431
                return NULL;
 
432
 
 
433
        sk = socket(PF_INET, SOCK_DGRAM, 0);
 
434
        if (sk < 0) {
 
435
                perror("Open socket error");
 
436
                return NULL;
 
437
        }
 
438
 
 
439
        memset(&ifr, 0, sizeof(ifr));
 
440
        ifr.ifr_ifindex = index;
 
441
 
 
442
        err = ioctl(sk, SIOCGIFNAME, &ifr);
 
443
        if (err < 0) {
 
444
                perror("Get interface name error");
 
445
                close(sk);
 
446
                return NULL;
 
447
        }
 
448
 
 
449
        close(sk);
 
450
 
 
451
        return g_strdup(ifr.ifr_name);
 
452
}
 
453
 
 
454
gboolean interface_is_up(int index)
 
455
{
 
456
        int sk, err;
 
457
        struct ifreq ifr;
 
458
        gboolean ret = FALSE;
 
459
 
 
460
        sk = socket(PF_INET, SOCK_DGRAM, 0);
 
461
        if (sk < 0) {
 
462
                perror("Open socket error");
 
463
                return FALSE;
 
464
        }
 
465
 
 
466
        memset(&ifr, 0, sizeof(ifr));
 
467
        ifr.ifr_ifindex = index;
 
468
 
 
469
        err = ioctl(sk, SIOCGIFNAME, &ifr);
 
470
        if (err < 0) {
 
471
                perror("Get interface name error");
 
472
                goto done;
 
473
        }
 
474
 
 
475
        err = ioctl(sk, SIOCGIFFLAGS, &ifr);
 
476
        if (err < 0) {
 
477
                perror("Get interface flags error");
 
478
                goto done;
 
479
        }
 
480
 
 
481
        if (ifr.ifr_flags & IFF_UP)
 
482
                ret = TRUE;
 
483
 
 
484
done:
 
485
        close(sk);
 
486
 
 
487
        return ret;
 
488
}