~ubuntu-branches/ubuntu/vivid/curl/vivid

« back to all changes in this revision

Viewing changes to lib/if2ip.c

  • Committer: Package Import Robot
  • Author(s): Sebastien Bacher
  • Date: 2013-05-07 12:16:37 UTC
  • mfrom: (3.4.37 sid)
  • Revision ID: package-import@ubuntu.com-20130507121637-9t3i98qgsyr9dw5d
Tags: 7.30.0-1ubuntu1
* Resynchronize on Debian. Remaining changes:
  - Drop dependencies not in main:
    + Build-Depends: Drop stunnel4 and libssh2-1-dev.
    + Drop libssh2-1-dev from binary package Depends.
  - Add new libcurl3-udeb package.
  - Add new curl-udeb package.
* Add warning to debian/patches/series.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 *                            | (__| |_| |  _ <| |___
6
6
 *                             \___|\___/|_| \_\_____|
7
7
 *
8
 
 * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
 
8
 * Copyright (C) 1998 - 2013, 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
83
83
  return result;
84
84
}
85
85
 
86
 
char *Curl_if2ip(int af, const char *interf, char *buf, int buf_size)
 
86
if2ip_result_t Curl_if2ip(int af, unsigned int remote_scope,
 
87
                          const char *interf, char *buf, int buf_size)
87
88
{
88
89
  struct ifaddrs *iface, *head;
89
 
  char *ip = NULL;
 
90
  if2ip_result_t res = IF2IP_NOT_FOUND;
 
91
 
 
92
#ifndef ENABLE_IPV6
 
93
  (void) remote_scope;
 
94
#endif
90
95
 
91
96
  if(getifaddrs(&head) >= 0) {
92
97
    for(iface=head; iface != NULL; iface=iface->ifa_next) {
93
 
      if((iface->ifa_addr != NULL) &&
94
 
         (iface->ifa_addr->sa_family == af) &&
95
 
         curl_strequal(iface->ifa_name, interf)) {
96
 
        void *addr;
97
 
        char scope[12]="";
 
98
      if(iface->ifa_addr != NULL) {
 
99
        if(iface->ifa_addr->sa_family == af) {
 
100
          if(curl_strequal(iface->ifa_name, interf)) {
 
101
            void *addr;
 
102
            char *ip;
 
103
            char scope[12]="";
 
104
            char ipstr[64];
98
105
#ifdef ENABLE_IPV6
99
 
        if(af == AF_INET6) {
100
 
          unsigned int scopeid = 0;
101
 
          addr = &((struct sockaddr_in6 *)iface->ifa_addr)->sin6_addr;
 
106
            if(af == AF_INET6) {
 
107
              unsigned int scopeid = 0;
 
108
              addr = &((struct sockaddr_in6 *)iface->ifa_addr)->sin6_addr;
102
109
#ifdef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID
103
 
          /* Include the scope of this interface as part of the address */
104
 
          scopeid = ((struct sockaddr_in6 *)iface->ifa_addr)->sin6_scope_id;
105
 
#endif
106
 
          if(scopeid)
107
 
            snprintf(scope, sizeof(scope), "%%%u", scopeid);
108
 
        }
109
 
        else
110
 
#endif
111
 
          addr = &((struct sockaddr_in *)iface->ifa_addr)->sin_addr;
112
 
        ip = (char *) Curl_inet_ntop(af, addr, buf, buf_size);
113
 
        strlcat(buf, scope, buf_size);
114
 
        break;
 
110
              /* Include the scope of this interface as part of the address */
 
111
              scopeid =
 
112
                ((struct sockaddr_in6 *)iface->ifa_addr)->sin6_scope_id;
 
113
#endif
 
114
              if(scopeid != remote_scope) {
 
115
                /* We are interested only in interface addresses whose
 
116
                   scope ID matches the remote address we want to
 
117
                   connect to: global (0) for global, link-local for
 
118
                   link-local, etc... */
 
119
                if(res == IF2IP_NOT_FOUND) res = IF2IP_AF_NOT_SUPPORTED;
 
120
                continue;
 
121
              }
 
122
              if(scopeid)
 
123
                snprintf(scope, sizeof(scope), "%%%u", scopeid);
 
124
            }
 
125
            else
 
126
#endif
 
127
              addr = &((struct sockaddr_in *)iface->ifa_addr)->sin_addr;
 
128
            res = IF2IP_FOUND;
 
129
            ip = (char *) Curl_inet_ntop(af, addr, ipstr, sizeof(ipstr));
 
130
            snprintf(buf, buf_size, "%s%s", ip, scope);
 
131
            break;
 
132
          }
 
133
        }
 
134
        else if((res == IF2IP_NOT_FOUND) &&
 
135
                curl_strequal(iface->ifa_name, interf)) {
 
136
          res = IF2IP_AF_NOT_SUPPORTED;
 
137
        }
115
138
      }
116
139
    }
117
140
    freeifaddrs(head);
118
141
  }
119
 
  return ip;
 
142
  return res;
120
143
}
121
144
 
122
145
#elif defined(HAVE_IOCTL_SIOCGIFADDR)
126
149
  /* This is here just to support the old interfaces */
127
150
  char buf[256];
128
151
 
129
 
  char *ip = Curl_if2ip(AF_INET, interf, buf, sizeof(buf));
130
 
 
131
 
  return (ip != NULL) ? TRUE : FALSE;
 
152
  return (Curl_if2ip(AF_INET, 0, interf, buf, sizeof(buf)) ==
 
153
          IF2IP_NOT_FOUND) ? FALSE : TRUE;
132
154
}
133
155
 
134
 
char *Curl_if2ip(int af, const char *interf, char *buf, int buf_size)
 
156
if2ip_result_t Curl_if2ip(int af, unsigned int remote_scope,
 
157
                          const char *interf, char *buf, int buf_size)
135
158
{
136
159
  struct ifreq req;
137
160
  struct in_addr in;
138
161
  struct sockaddr_in *s;
139
162
  curl_socket_t dummy;
140
163
  size_t len;
141
 
  char *ip;
 
164
 
 
165
  (void)remote_scope;
142
166
 
143
167
  if(!interf || (af != AF_INET))
144
 
    return NULL;
 
168
    return IF2IP_NOT_FOUND;
145
169
 
146
170
  len = strlen(interf);
147
171
  if(len >= sizeof(req.ifr_name))
148
 
    return NULL;
 
172
    return IF2IP_NOT_FOUND;
149
173
 
150
174
  dummy = socket(AF_INET, SOCK_STREAM, 0);
151
175
  if(CURL_SOCKET_BAD == dummy)
152
 
    return NULL;
 
176
    return IF2IP_NOT_FOUND;
153
177
 
154
178
  memset(&req, 0, sizeof(req));
155
179
  memcpy(req.ifr_name, interf, len+1);
157
181
 
158
182
  if(ioctl(dummy, SIOCGIFADDR, &req) < 0) {
159
183
    sclose(dummy);
160
 
    return NULL;
 
184
    /* With SIOCGIFADDR, we cannot tell the difference between an interface
 
185
       that does not exist and an interface that has no address of the
 
186
       correct family. Assume the interface does not exist */
 
187
    return IF2IP_NOT_FOUND;
161
188
  }
162
189
 
163
190
  s = (struct sockaddr_in *)&req.ifr_addr;
164
191
  memcpy(&in, &s->sin_addr, sizeof(in));
165
 
  ip = (char *) Curl_inet_ntop(s->sin_family, &in, buf, buf_size);
 
192
  Curl_inet_ntop(s->sin_family, &in, buf, buf_size);
166
193
 
167
194
  sclose(dummy);
168
 
  return ip;
 
195
  return IF2IP_FOUND;
169
196
}
170
197
 
171
198
#else
177
204
  return FALSE;
178
205
}
179
206
 
180
 
char *Curl_if2ip(int af, const char *interf, char *buf, int buf_size)
 
207
if2ip_result_t Curl_if2ip(int af, unsigned int remote_scope,
 
208
                          const char *interf, char *buf, int buf_size)
181
209
{
182
210
    (void) af;
 
211
    (void) remote_scope;
183
212
    (void) interf;
184
213
    (void) buf;
185
214
    (void) buf_size;
186
 
    return NULL;
 
215
    return IF2IP_NOT_FOUND;
187
216
}
188
217
 
189
218
#endif