00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "rsync.h"
00019
00020 #define NS_INT16SZ 2
00021 #define NS_INADDRSZ 4
00022 #define NS_IN6ADDRSZ 16
00023
00024
00025
00026
00027
00028
00029 static int inet_pton4(const char *src, unsigned char *dst);
00030 static int inet_pton6(const char *src, unsigned char *dst);
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 int
00044 inet_pton(int af,
00045 const char *src,
00046 void *dst)
00047 {
00048 switch (af) {
00049 case AF_INET:
00050 return (inet_pton4(src, dst));
00051 #ifdef INET6
00052 case AF_INET6:
00053 return (inet_pton6(src, dst));
00054 #endif
00055 default:
00056 errno = EAFNOSUPPORT;
00057 return (-1);
00058 }
00059
00060 }
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 static int
00073 inet_pton4(src, dst)
00074 const char *src;
00075 unsigned char *dst;
00076 {
00077 static const char digits[] = "0123456789";
00078 int saw_digit, octets, ch;
00079 unsigned char tmp[NS_INADDRSZ], *tp;
00080
00081 saw_digit = 0;
00082 octets = 0;
00083 *(tp = tmp) = 0;
00084 while ((ch = *src++) != '\0') {
00085 const char *pch;
00086
00087 if ((pch = strchr(digits, ch)) != NULL) {
00088 unsigned int new = *tp * 10 + (pch - digits);
00089
00090 if (new > 255)
00091 return (0);
00092 *tp = new;
00093 if (! saw_digit) {
00094 if (++octets > 4)
00095 return (0);
00096 saw_digit = 1;
00097 }
00098 } else if (ch == '.' && saw_digit) {
00099 if (octets == 4)
00100 return (0);
00101 *++tp = 0;
00102 saw_digit = 0;
00103 } else
00104 return (0);
00105 }
00106 if (octets < 4)
00107 return (0);
00108 memcpy(dst, tmp, NS_INADDRSZ);
00109 return (1);
00110 }
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125 #ifdef INET6
00126 static int
00127 inet_pton6(src, dst)
00128 const char *src;
00129 unsigned char *dst;
00130 {
00131 static const char xdigits_l[] = "0123456789abcdef",
00132 xdigits_u[] = "0123456789ABCDEF";
00133 unsigned char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
00134 const char *xdigits, *curtok;
00135 int ch, saw_xdigit;
00136 unsigned int val;
00137
00138 memset((tp = tmp), '\0', NS_IN6ADDRSZ);
00139 endp = tp + NS_IN6ADDRSZ;
00140 colonp = NULL;
00141
00142 if (*src == ':')
00143 if (*++src != ':')
00144 return (0);
00145 curtok = src;
00146 saw_xdigit = 0;
00147 val = 0;
00148 while ((ch = *src++) != '\0') {
00149 const char *pch;
00150
00151 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
00152 pch = strchr((xdigits = xdigits_u), ch);
00153 if (pch != NULL) {
00154 val <<= 4;
00155 val |= (pch - xdigits);
00156 if (val > 0xffff)
00157 return (0);
00158 saw_xdigit = 1;
00159 continue;
00160 }
00161 if (ch == ':') {
00162 curtok = src;
00163 if (!saw_xdigit) {
00164 if (colonp)
00165 return (0);
00166 colonp = tp;
00167 continue;
00168 }
00169 if (tp + NS_INT16SZ > endp)
00170 return (0);
00171 *tp++ = (unsigned char) (val >> 8) & 0xff;
00172 *tp++ = (unsigned char) val & 0xff;
00173 saw_xdigit = 0;
00174 val = 0;
00175 continue;
00176 }
00177 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
00178 inet_pton4(curtok, tp) > 0) {
00179 tp += NS_INADDRSZ;
00180 saw_xdigit = 0;
00181 break;
00182 }
00183 return (0);
00184 }
00185 if (saw_xdigit) {
00186 if (tp + NS_INT16SZ > endp)
00187 return (0);
00188 *tp++ = (unsigned char) (val >> 8) & 0xff;
00189 *tp++ = (unsigned char) val & 0xff;
00190 }
00191 if (colonp != NULL) {
00192
00193
00194
00195
00196 const int n = tp - colonp;
00197 int i;
00198
00199 for (i = 1; i <= n; i++) {
00200 endp[- i] = colonp[n - i];
00201 colonp[n - i] = 0;
00202 }
00203 tp = endp;
00204 }
00205 if (tp != endp)
00206 return (0);
00207 memcpy(dst, tmp, NS_IN6ADDRSZ);
00208 return (1);
00209 }
00210 #endif