summaryrefslogtreecommitdiffstats
path: root/common/elapi/elapi_ioctl.c
blob: c2a42a17c701f08a572251467cb099e662465886 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
    ELAPI

    Special platform specific functions related to networking

    Copyright (C) Dmitri Pal <dpal@redhat.com> 2009

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#define _GNU_SOURCE

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <errno.h>

#include "elapi_ioctl.h"
#include "trace.h"
#include "config.h"

#ifndef HAVE_GETIFADDRS

/* These functions are taken form Stevens's
 * UNIX Network Programming Volume 1
 */

int elapi_get_addrlist(struct ifconf *ifc)
{
    int sockfd;
    int length, lastlen;
    int error;
    char *buffer;

    TRACE_FLOW_STRING("elapi_get_addrlist", "Entry");

    /* Open socket */
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);

    lastlen = 0;
    length = INTERFACE_NUM_GUESS * sizeof(struct ifreq);

    /* Allocate right amount of memory */
    /* This is a trick from Stevens book */
    /* Search web for "get_ifi_info" to get original code */
    for ( ; ; ) {

        buffer = malloc(length);
        ifc->ifc_len = length;
        ifc->ifc_buf = buffer;

        /* Read list */
        if (ioctl(sockfd, SIOCGIFCONF, ifc) < 0) {
            error = errno;
            TRACE_INFO_NUMBER("Ioctl call failed", error);
            if (error != EINVAL || lastlen != 0) {
                free(buffer);
                TRACE_ERROR_NUMBER("ioctl failed", error);
                return error;
            }
        } else {
            TRACE_INFO_NUMBER("Length returned", ifc->ifc_len);
            TRACE_INFO_NUMBER("Previous length", lastlen);
            /* Break if length is same */
            if (ifc->ifc_len == lastlen) break;
            lastlen = ifc->ifc_len;
        }

        /* Grow length */
        length += INTERFACE_NUM_INC * sizeof(struct ifreq);
        free(buffer);
    }

    TRACE_FLOW_STRING("elapi_get_addrlist", "Exit");
    return EOK;
}

/* Get the variable part of the size of the address */
static int elapi_get_addrlen(struct ifreq *ifr)
{
    int len;

    TRACE_FLOW_STRING("elapi_get_addrlen", "Entry");

#ifdef HAVE_SOCKADDR_SA_LEN
    len = max(sizeof(struct sockaddr), ifr->ifr_addr.sa_len);
#else
    switch (ifr->ifr_addr.sa_family) {
#ifdef IPV6
    case AF_INET6:
        len = sizeof(struct sockaddr_in6);
        break;
#endif
    case AF_INET:
        default:
        len = sizeof(struct sockaddr);
        break;
    }
#endif    /* HAVE_SOCKADDR_SA_LEN */

    TRACE_FLOW_NUMBER("elapi_get_addrlen Returning", len);
    return len;
}

/* Get next address */
struct ifreq *elapi_get_next_addr(struct ifconf *ifc, struct ifreq *current)
{
    char *ifr;

    TRACE_FLOW_STRING("elapi_get_next_addr", "Entry");

    TRACE_INFO_NUMBER("Current ifi", current);
    TRACE_INFO_NUMBER("Address", &current->ifr_addr);

    /* Move to the next item */
    ifr = (char *)current + sizeof(current->ifr_name) + elapi_get_addrlen(current);

    TRACE_INFO_NUMBER("New ifi", ifr);

    /* Check if we are beyond the end of the allocated area */
    /* Have to cast otherwise get warnings */
    if (ifr >= ((char *)ifc->ifc_buf + ifc->ifc_len)) ifr = NULL;

    TRACE_INFO_NUMBER("New ifi adjusted", ifr);

    TRACE_FLOW_STRING("elapi_get_next_addr", "Exit");

    return (struct ifreq *)ifr;
}


#endif /* HAVE_GETIFADDRS */