~ubuntu-branches/ubuntu/karmic/ucarp/karmic

« back to all changes in this revision

Viewing changes to src/fillmac.c

  • Committer: Bazaar Package Importer
  • Author(s): Eric Evans
  • Date: 2004-12-24 12:39:30 UTC
  • Revision ID: james.westby@ubuntu.com-20041224123930-9qnzcd6o5j0jjjvm
Tags: 1.1-2
Patched to correct a bug that caused upscript to also be applied
when state changed to BACKUP, if --dowscript was passed before
--upscript, (Closes: #284891). Thanks Michail Bachmann
<michail.bachmann@cms.hu-berlin.de>

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
#ifdef HAVE_NET_IF_TYPES_H
12
12
# include <net/if_types.h>
13
13
#endif
 
14
#ifdef HAVE_SYS_SOCKIO_H
 
15
# include <sys/sockio.h>
 
16
#endif
 
17
#ifndef HAVE_NET_IF_ARP_H
 
18
# include <net/if_arp.h>
 
19
#endif
 
20
#include "log.h"
14
21
 
15
22
#ifdef WITH_DMALLOC
16
23
# include <dmalloc.h>
17
24
#endif
18
25
 
 
26
#ifdef PF_PACKET
 
27
# define HWINFO_DOMAIN PF_PACKET
 
28
#else
 
29
# define HWINFO_DOMAIN PF_INET
 
30
#endif
 
31
#ifdef SOCK_PACKET
 
32
# define HWINFO_TYPE SOCK_PACKET
 
33
#else
 
34
# define HWINFO_TYPE SOCK_DGRAM
 
35
#endif   
 
36
 
19
37
int fill_mac_address(void)
20
38
{
21
39
    int s;
22
 
    
23
 
    if ((s = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
24
 
        perror("socket");
 
40
 
 
41
    if ((s = socket(HWINFO_DOMAIN, HWINFO_TYPE, 0)) == -1) {
 
42
        logfile(LOG_ERR, _("Unable to open raw device: [%s]"),
 
43
                strerror(errno));
25
44
        return -1;
26
45
    }
27
46
#ifdef SIOCGIFHWADDR
28
47
    {
29
 
        struct ifreq ifr;
30
 
        
31
 
        if (strlen(interface) >= sizeof ifr.ifr_name) {
32
 
            fprintf(stderr, "Interface name too long\n");
33
 
            return -1;
34
 
        }
35
 
        strcpy(ifr.ifr_name, interface);                
 
48
        struct ifreq ifr;
 
49
        
 
50
        if (strlen(interface) >= sizeof ifr.ifr_name) {
 
51
            logfile(LOG_ERR, _("Interface name too long"));
 
52
            return -1;
 
53
        }
 
54
        strcpy(ifr.ifr_name, interface);                
36
55
        if (ioctl(s, SIOCGIFHWADDR, &ifr) != 0) {
37
 
            perror("SIOCGIFHWADDR");
 
56
            logfile(LOG_ERR,
 
57
                    _("Unable to get hardware info about an interface: %s"),
 
58
                    strerror(errno));
38
59
            (void) close(s);
39
60
            return -1;
40
61
        }
43
64
        case ARPHRD_IEEE802:
44
65
            break;
45
66
        default:
46
 
            fprintf(stderr, "Warning : unknown hardware type [%u]\n",
 
67
            logfile(LOG_ERR, _("Unknown hardware type [%u]"),
47
68
                    (unsigned int) ifr.ifr_hwaddr.sa_family);
48
69
        }
49
70
        memcpy(hwaddr, &ifr.ifr_hwaddr.sa_data, sizeof hwaddr);
50
71
    }
51
72
#elif defined(HAVE_GETIFADDRS)
52
 
    {   
53
 
        struct ifaddrs *ifas;
54
 
        struct ifaddrs *ifa;
55
 
        struct sockaddr_dl *sadl;
56
 
        struct ether_addr *ea;
57
 
        
58
 
        if (getifaddrs(&ifas) != 0) {
59
 
            perror("getifaddrs");
60
 
            return -1;
61
 
        }
62
 
        ifa = ifas;
63
 
        while (ifa != NULL) {
64
 
            if (strcmp(ifa->ifa_name, interface) == 0 &&
65
 
                ifa->ifa_addr->sa_family == AF_LINK) {
66
 
                sadl = (struct sockaddr_dl *) ifa->ifa_addr;
67
 
                if (sadl == NULL || sadl->sdl_type != IFT_ETHER ||
68
 
                    sadl->sdl_alen <= 0) {
69
 
                    fprintf(stderr,
70
 
                            "Invalid media / hardware address for [%s]\n",
71
 
                            interface);
72
 
                    return -1;
73
 
                }
74
 
                ea = (struct ether_addr *) LLADDR(sadl);
75
 
                memcpy(hwaddr, ea, sizeof hwaddr);
 
73
    {   
 
74
        struct ifaddrs *ifas;
 
75
        struct ifaddrs *ifa;
 
76
        struct sockaddr_dl *sadl;
 
77
        struct ether_addr *ea;
 
78
        
 
79
        if (getifaddrs(&ifas) != 0) {
 
80
            logfile(LOG_ERR, _("Unable to get interface address: %s"),
 
81
                    strerror(errno));
 
82
            return -1;
 
83
        }
 
84
        ifa = ifas;
 
85
        while (ifa != NULL) {
 
86
            if (strcmp(ifa->ifa_name, interface) == 0 &&
 
87
                ifa->ifa_addr->sa_family == AF_LINK) {
 
88
                sadl = (struct sockaddr_dl *) ifa->ifa_addr;
 
89
                if (sadl == NULL || sadl->sdl_type != IFT_ETHER ||
 
90
                    sadl->sdl_alen <= 0) {
 
91
                    logfile(LOG_ERR,
 
92
                            _("Invalid media / hardware address for [%s]"),
 
93
                            interface);
 
94
                    return -1;
 
95
                }
 
96
                ea = (struct ether_addr *) LLADDR(sadl);
 
97
                memcpy(hwaddr, ea, sizeof hwaddr);
76
98
                
77
 
                return 0;
78
 
            }
79
 
            ifa = ifa->ifa_next;
80
 
        }
81
 
        return -1;
 
99
                return 0;
 
100
            }
 
101
            ifa = ifa->ifa_next;
 
102
        }
 
103
        return -1;
 
104
    }
 
105
#elif defined(SIOCGLIFNUM)
 
106
    {
 
107
        struct lifconf lifc;
 
108
        struct lifnum lifn;
 
109
        struct lifreq *lifr;
 
110
        caddr_t *lifrspace;
 
111
        struct arpreq arpreq;
 
112
        
 
113
        lifn.lifn_flags = 0;
 
114
        lifn.lifn_family = AF_INET;
 
115
        if (ioctl(s, SIOCGLIFNUM, &lifn) < 0) {
 
116
            logfile(LOG_ERR, _("ioctl SIOCGLIFNUM error"));
 
117
            return -1;
 
118
        }
 
119
        if (lifn.lifn_count <= 0) {
 
120
            logfile(LOG_ERR, _("No interface found"));
 
121
            return -1;            
 
122
        }
 
123
        lifc.lifc_family = lifn.lifn_family;
 
124
        lifc.lifc_len = lifn.lifn_count * sizeof *lifr;
 
125
        lifrspace = ALLOCA(lifc.lifc_len);
 
126
        lifc.lifc_buf = (caddr_t) lifrspace;
 
127
        if (ioctl(s, SIOCGLIFCONF, &lifc) < 0) {
 
128
            logfile(LOG_ERR, _("ioctl SIOCGLIFCONF error"));
 
129
            ALLOCA_FREE(lifrspace);
 
130
            return -1;
 
131
        }        
 
132
        lifr = lifc.lifc_req;
 
133
        while (--lifn.lifn_count > 0) {
 
134
            if (strcmp(lifr->lifr_name, interface) == 0) {
 
135
                break;
 
136
            }      
 
137
            lifr++;
 
138
        }
 
139
        if (lifn.lifn_count <= 0) {
 
140
            logfile(LOG_ERR, _("Interface [%s] not found"), interface);
 
141
            ALLOCA_FREE(lifrspace);
 
142
            return -1;            
 
143
        }
 
144
        memcpy(&arpreq.arp_pa, &lifr->lifr_addr, sizeof arpreq.arp_pa);
 
145
        ALLOCA_FREE(lifrspace);
 
146
        if (ioctl(s, SIOCGARP, &arpreq) != 0) {
 
147
            logfile(LOG_ERR, _("Unable to get hardware info about [%s]"),
 
148
                    interface);
 
149
            return -1;
 
150
        }       
 
151
        memcpy(hwaddr, &arpreq.arp_ha.sa_data, sizeof hwaddr);
82
152
    }
83
153
#endif
84
154