00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "setup.h"
00025
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029
00030 #ifdef HAVE_UNISTD_H
00031 #include <unistd.h>
00032 #endif
00033
00034 #include "if2ip.h"
00035
00036
00037
00038
00039
00040 #if !defined(WIN32) && !defined(__BEOS__) && !defined(__CYGWIN__) && \
00041 !defined(__riscos__) && !defined(__INTERIX) && !defined(NETWARE) && \
00042 !defined(__AMIGA__) && !defined(__minix)
00043
00044 #ifdef HAVE_SYS_SOCKET_H
00045 #include <sys/socket.h>
00046 #endif
00047 #ifdef HAVE_NETINET_IN_H
00048 #include <netinet/in.h>
00049 #endif
00050 #ifdef HAVE_ARPA_INET_H
00051 #include <arpa/inet.h>
00052 #endif
00053
00054 #ifdef HAVE_SYS_TIME_H
00055
00056 #include <sys/time.h>
00057 #endif
00058 #ifdef HAVE_NET_IF_H
00059 #include <net/if.h>
00060 #endif
00061 #ifdef HAVE_SYS_IOCTL_H
00062 #include <sys/ioctl.h>
00063 #endif
00064
00065 #ifdef HAVE_NETDB_H
00066 #include <netdb.h>
00067 #endif
00068
00069 #ifdef HAVE_SYS_SOCKIO_H
00070 #include <sys/sockio.h>
00071 #endif
00072
00073 #ifdef VMS
00074 #include <inet.h>
00075 #endif
00076
00077 #include "inet_ntop.h"
00078 #include "memory.h"
00079
00080
00081 #include "memdebug.h"
00082
00083 #define SYS_ERROR -1
00084
00085 char *Curl_if2ip(const char *interface, char *buf, int buf_size)
00086 {
00087 int dummy;
00088 char *ip=NULL;
00089
00090 if(!interface)
00091 return NULL;
00092
00093 dummy = socket(AF_INET, SOCK_STREAM, 0);
00094 if (SYS_ERROR == dummy) {
00095 return NULL;
00096 }
00097 else {
00098 struct ifreq req;
00099 size_t len = strlen(interface);
00100 memset(&req, 0, sizeof(req));
00101 if(len >= sizeof(req.ifr_name)) {
00102 sclose(dummy);
00103 return NULL;
00104 }
00105 memcpy(req.ifr_name, interface, len+1);
00106 req.ifr_addr.sa_family = AF_INET;
00107 #ifdef IOCTL_3_ARGS
00108 if (SYS_ERROR == ioctl(dummy, SIOCGIFADDR, &req)) {
00109 #else
00110 if (SYS_ERROR == ioctl(dummy, SIOCGIFADDR, &req, sizeof(req))) {
00111 #endif
00112 sclose(dummy);
00113 return NULL;
00114 }
00115 else {
00116 struct in_addr in;
00117
00118 struct sockaddr_in *s = (struct sockaddr_in *)&req.ifr_dstaddr;
00119 memcpy(&in, &s->sin_addr, sizeof(in));
00120 ip = (char *) Curl_inet_ntop(s->sin_family, &in, buf, buf_size);
00121 }
00122 sclose(dummy);
00123 }
00124 return ip;
00125 }
00126
00127
00128 #else
00129 char *Curl_if2ip(const char *interf, char *buf, int buf_size)
00130 {
00131 (void) interf;
00132 (void) buf;
00133 (void) buf_size;
00134 return NULL;
00135 }
00136 #endif