~vorlon/ubuntu/natty/curl/multiarch

« back to all changes in this revision

Viewing changes to lib/if2ip.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2009-04-29 11:10:29 UTC
  • mfrom: (3.2.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090429111029-2j5eiyokfw2bw049
Tags: 7.19.4-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Drop build dependencies: stunnel, libdb4.6-dev, libssh2-1-dev
  - Add build-dependency on openssh-server
  - Drop libssh2-1-dev from libcurl4-openssl-dev's Depends.
  - Call automake-1.9 with --add-missing --copy --force
* drop debian/patches/security_CVE-2009-0037.patch 
  - this patch is part of 7.19.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 *                            | (__| |_| |  _ <| |___
6
6
 *                             \___|\___/|_| \_\_____|
7
7
 *
8
 
 * Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
 
8
 * Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
9
9
 *
10
10
 * This software is licensed as described in the file COPYING, which
11
11
 * you should have received as part of this distribution. The terms
18
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
19
 * KIND, either express or implied.
20
20
 *
21
 
 * $Id: if2ip.c,v 1.53 2008-04-22 22:53:54 danf Exp $
 
21
 * $Id: if2ip.c,v 1.63 2008-12-30 08:05:38 gknauf Exp $
22
22
 ***************************************************************************/
23
23
 
24
24
#include "setup.h"
25
25
 
26
 
#include <stdio.h>
27
 
#include <stdlib.h>
28
 
#include <string.h>
29
 
 
30
26
#ifdef HAVE_UNISTD_H
31
 
#include <unistd.h>
 
27
#  include <unistd.h>
32
28
#endif
33
 
 
34
 
#include "if2ip.h"
35
 
 
36
 
/*
37
 
 * This test can probably be simplified to #if defined(SIOCGIFADDR) and
38
 
 * moved after the following includes.
39
 
 */
40
 
#if !defined(WIN32) && !defined(__BEOS__) && !defined(__CYGWIN__) && \
41
 
    !defined(__riscos__) && !defined(__INTERIX) && !defined(NETWARE) && \
42
 
    !defined(__AMIGA__) && !defined(__minix) && !defined(__SYMBIAN32__)
43
 
 
44
29
#ifdef HAVE_SYS_SOCKET_H
45
 
#include <sys/socket.h>
 
30
#  include <sys/socket.h>
46
31
#endif
47
32
#ifdef HAVE_NETINET_IN_H
48
 
#include <netinet/in.h>
 
33
#  include <netinet/in.h>
49
34
#endif
50
35
#ifdef HAVE_ARPA_INET_H
51
 
#include <arpa/inet.h>
52
 
#endif
53
 
 
54
 
#ifdef HAVE_SYS_TIME_H
55
 
/* This must be before net/if.h for AIX 3.2 to enjoy life */
56
 
#include <sys/time.h>
 
36
#  include <arpa/inet.h>
57
37
#endif
58
38
#ifdef HAVE_NET_IF_H
59
 
#include <net/if.h>
 
39
#  include <net/if.h>
60
40
#endif
61
41
#ifdef HAVE_SYS_IOCTL_H
62
 
#include <sys/ioctl.h>
 
42
#  include <sys/ioctl.h>
63
43
#endif
64
 
 
65
44
#ifdef HAVE_NETDB_H
66
 
#include <netdb.h>
 
45
#  include <netdb.h>
67
46
#endif
68
 
 
69
47
#ifdef HAVE_SYS_SOCKIO_H
70
 
#include <sys/sockio.h>
71
 
#endif
72
 
 
 
48
#  include <sys/sockio.h>
 
49
#endif
 
50
#ifdef HAVE_IFADDRS_H
 
51
#  include <ifaddrs.h>
 
52
#endif
 
53
#ifdef HAVE_STROPTS_H
 
54
#  include <stropts.h>
 
55
#endif
73
56
#ifdef VMS
74
 
#include <inet.h>
 
57
#  include <inet.h>
75
58
#endif
76
59
 
77
60
#include "inet_ntop.h"
 
61
#include "strequal.h"
 
62
#include "if2ip.h"
 
63
 
 
64
#define _MPRINTF_REPLACE /* use our functions only */
 
65
#include <curl/mprintf.h>
 
66
 
78
67
#include "memory.h"
79
 
 
80
68
/* The last #include file should be: */
81
69
#include "memdebug.h"
82
70
 
 
71
/* ------------------------------------------------------------------ */
 
72
 
 
73
#if defined(HAVE_GETIFADDRS)
 
74
 
 
75
char *Curl_if2ip(int af, const char *interface, char *buf, int buf_size)
 
76
{
 
77
  struct ifaddrs *iface, *head;
 
78
  char *ip=NULL;
 
79
 
 
80
  if (getifaddrs(&head) >= 0) {
 
81
    for (iface=head; iface != NULL; iface=iface->ifa_next) {
 
82
      if ((iface->ifa_addr != NULL) &&
 
83
          (iface->ifa_addr->sa_family == af) &&
 
84
          curl_strequal(iface->ifa_name, interface)) {
 
85
        void *addr;
 
86
        char scope[12]="";
 
87
#ifdef ENABLE_IPV6
 
88
        if (af == AF_INET6) {
 
89
          unsigned int scopeid = 0;
 
90
          addr = &((struct sockaddr_in6 *)iface->ifa_addr)->sin6_addr;
 
91
#ifdef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID
 
92
          /* Include the scope of this interface as part of the address */
 
93
          scopeid = ((struct sockaddr_in6 *)iface->ifa_addr)->sin6_scope_id;
 
94
#endif
 
95
          if (scopeid)
 
96
            snprintf(scope, sizeof(scope), "%%%u", scopeid);
 
97
        }
 
98
        else
 
99
#endif
 
100
          addr = &((struct sockaddr_in *)iface->ifa_addr)->sin_addr;
 
101
        ip = (char *) Curl_inet_ntop(af, addr, buf, buf_size);
 
102
        strlcat(buf, scope, buf_size);
 
103
        break;
 
104
      }
 
105
    }
 
106
    freeifaddrs(head);
 
107
  }
 
108
  return ip;
 
109
}
 
110
 
 
111
#elif defined(HAVE_IOCTL_SIOCGIFADDR)
 
112
 
83
113
#define SYS_ERROR -1
84
114
 
85
 
char *Curl_if2ip(const char *interface, char *buf, int buf_size)
 
115
char *Curl_if2ip(int af, const char *interface, char *buf, int buf_size)
86
116
{
87
117
  int dummy;
88
118
  char *ip=NULL;
89
119
 
90
 
  if(!interface)
 
120
  if(!interface || (af != AF_INET))
91
121
    return NULL;
92
122
 
93
123
  dummy = socket(AF_INET, SOCK_STREAM, 0);
104
134
    }
105
135
    memcpy(req.ifr_name, interface, len+1);
106
136
    req.ifr_addr.sa_family = AF_INET;
107
 
#ifdef IOCTL_3_ARGS
108
137
    if(SYS_ERROR == ioctl(dummy, SIOCGIFADDR, &req)) {
109
 
#else
110
 
    if(SYS_ERROR == ioctl(dummy, SIOCGIFADDR, &req, sizeof(req))) {
111
 
#endif
112
138
      sclose(dummy);
113
139
      return NULL;
114
140
    }
115
141
    else {
116
142
      struct in_addr in;
117
143
 
118
 
      struct sockaddr_in *s = (struct sockaddr_in *)&req.ifr_dstaddr;
 
144
      struct sockaddr_in *s = (struct sockaddr_in *)&req.ifr_addr;
119
145
      memcpy(&in, &s->sin_addr, sizeof(in));
120
146
      ip = (char *) Curl_inet_ntop(s->sin_family, &in, buf, buf_size);
121
147
    }
124
150
  return ip;
125
151
}
126
152
 
127
 
/* -- end of if2ip() -- */
128
153
#else
129
 
char *Curl_if2ip(const char *interf, char *buf, int buf_size)
 
154
 
 
155
char *Curl_if2ip(int af, const char *interf, char *buf, int buf_size)
130
156
{
 
157
    (void) af;
131
158
    (void) interf;
132
159
    (void) buf;
133
160
    (void) buf_size;
134
161
    return NULL;
135
162
}
 
163
 
136
164
#endif