00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 #include <unistd.h>
00060 #include <sys/time.h>
00061 #include <ctype.h>
00062 #include <dlfcn.h>
00063 #include <io.h>
00064 #include <arpa/inet.h>
00065 #include <stdio.h>
00066
00067 #include "defs.h"
00068 #include "common/string.h"
00069 #include "os_time.h"
00070
00071 static unsigned int RandState;
00072
00073 void
00074 srandom(unsigned int Seed)
00075 {
00076 RandState = Seed;
00077 }
00078
00079 unsigned int
00080 random(void)
00081 {
00082 RandState = RandState * 1103515245 + 12345;
00083
00084 return (RandState ^ (RandState >> 16)) & RAND_MAX;
00085 }
00086
00087 int
00088 getpid(void)
00089 {
00090 HANDLE h = GetCurrentThread();
00091 return (int)h;
00092 }
00093
00094 char *
00095 win32_strerror(unsigned int ErrNo)
00096 {
00097 static char Msg[1000];
00098
00099 #if !defined WINCE
00100 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, ErrNo, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), Msg, sizeof(Msg), NULL);
00101 #else
00102 short WideMsg[1000];
00103
00104 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, ErrNo,
00105 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), WideMsg, sizeof(WideMsg) / 2, NULL);
00106
00107 if (WideCharToMultiByte(CP_ACP, 0, WideMsg, -1, Msg, sizeof(Msg), NULL, NULL) == 0)
00108 strscpy(Msg, "[cannot convert string]", sizeof(Msg));
00109 #endif
00110
00111 return Msg;
00112 }
00113
00114
00115 void *
00116 dlopen(const char *Name, int Flags __attribute__ ((unused)))
00117 {
00118 #if !defined WINCE
00119 return (void *)LoadLibrary(Name);
00120 #else
00121 short WideName[1000];
00122
00123 MultiByteToWideChar(CP_ACP, 0, Name, -1, WideName, sizeof(WideName));
00124 return (void *)LoadLibrary(WideName);
00125 #endif
00126 }
00127
00128 int
00129 dlclose(void *Handle)
00130 {
00131 FreeLibrary((HMODULE) Handle);
00132 return 0;
00133 }
00134
00135 void *
00136 dlsym(void *Handle, const char *Name)
00137 {
00138 #if !defined WINCE
00139 return GetProcAddress((HMODULE) Handle, Name);
00140 #else
00141 short WideName[1000];
00142
00143 MultiByteToWideChar(CP_ACP, 0, Name, -1, WideName, sizeof(WideName));
00144 return GetProcAddress((HMODULE) Handle, WideName);
00145 #endif
00146 }
00147
00148 char *
00149 dlerror(void)
00150 {
00151 return win32_strerror(GetLastError());
00152 }
00153
00154 #define NS_INADDRSZ 4
00155 #define NS_IN6ADDRSZ 16
00156 #define NS_INT16SZ 2
00157
00158 static int
00159 inet_pton4(const char *src, unsigned char *dst)
00160 {
00161 int saw_digit, octets, ch;
00162 u_char tmp[NS_INADDRSZ], *tp;
00163
00164 saw_digit = 0;
00165 octets = 0;
00166 *(tp = tmp) = 0;
00167
00168 while ((ch = *src++) != '\0') {
00169 if (ch >= '0' && ch <= '9') {
00170 unsigned int new = *tp * 10 + (ch - '0');
00171
00172 if (new > 255)
00173 return (0);
00174
00175 *tp = new;
00176
00177 if (!saw_digit) {
00178 if (++octets > 4)
00179 return (0);
00180
00181 saw_digit = 1;
00182 }
00183 }
00184
00185 else if (ch == '.' && saw_digit) {
00186 if (octets == 4)
00187 return (0);
00188
00189 *++tp = 0;
00190
00191 saw_digit = 0;
00192 }
00193
00194 else
00195 return (0);
00196 }
00197
00198 if (octets < 4)
00199 return (0);
00200
00201 memcpy(dst, tmp, NS_INADDRSZ);
00202 return (1);
00203 }
00204
00205 static int
00206 inet_pton6(const char *src, unsigned char *dst)
00207 {
00208 static const char xdigits[] = "0123456789abcdef";
00209 u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
00210 const char *curtok;
00211 int ch, saw_xdigit;
00212 u_int val;
00213
00214 tp = memset(tmp, '\0', NS_IN6ADDRSZ);
00215 endp = tp + NS_IN6ADDRSZ;
00216 colonp = NULL;
00217
00218 if (*src == ':')
00219 if (*++src != ':')
00220 return (0);
00221
00222 curtok = src;
00223 saw_xdigit = 0;
00224 val = 0;
00225
00226 while ((ch = tolower(*src++)) != '\0') {
00227 const char *pch;
00228
00229 pch = strchr(xdigits, ch);
00230
00231 if (pch != NULL) {
00232 val <<= 4;
00233 val |= (pch - xdigits);
00234
00235 if (val > 0xffff)
00236 return (0);
00237
00238 saw_xdigit = 1;
00239 continue;
00240 }
00241
00242 if (ch == ':') {
00243 curtok = src;
00244
00245 if (!saw_xdigit) {
00246 if (colonp)
00247 return (0);
00248
00249 colonp = tp;
00250 continue;
00251 }
00252
00253 else if (*src == '\0') {
00254 return (0);
00255 }
00256
00257 if (tp + NS_INT16SZ > endp)
00258 return (0);
00259
00260 *tp++ = (u_char) (val >> 8) & 0xff;
00261 *tp++ = (u_char) val & 0xff;
00262 saw_xdigit = 0;
00263 val = 0;
00264 continue;
00265 }
00266
00267 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) && inet_pton4(curtok, tp) > 0) {
00268 tp += NS_INADDRSZ;
00269 saw_xdigit = 0;
00270 break;
00271 }
00272
00273 return (0);
00274 }
00275
00276 if (saw_xdigit) {
00277 if (tp + NS_INT16SZ > endp)
00278 return (0);
00279
00280 *tp++ = (u_char) (val >> 8) & 0xff;
00281 *tp++ = (u_char) val & 0xff;
00282 }
00283
00284 if (colonp != NULL) {
00285 const int n = tp - colonp;
00286 int i;
00287
00288 if (tp == endp)
00289 return (0);
00290
00291 for (i = 1; i <= n; i++) {
00292 endp[-i] = colonp[n - i];
00293 colonp[n - i] = 0;
00294 }
00295
00296 tp = endp;
00297 }
00298
00299 if (tp != endp)
00300 return (0);
00301
00302 memcpy(dst, tmp, NS_IN6ADDRSZ);
00303 return (1);
00304 }
00305
00306 int
00307 inet_pton(int af, const char *src, void *dst)
00308 {
00309 switch (af) {
00310 case AF_INET:
00311 return (inet_pton4(src, dst));
00312
00313 case AF_INET6:
00314 return (inet_pton6(src, dst));
00315
00316 default:
00317 return -1;
00318 }
00319 }
00320
00321 static char *
00322 inet_ntop4(const unsigned char *src, char *dst, int size)
00323 {
00324 static const char fmt[] = "%u.%u.%u.%u";
00325 char tmp[sizeof "255.255.255.255"];
00326
00327 if (sprintf(tmp, fmt, src[0], src[1], src[2], src[3]) > size)
00328 return (NULL);
00329
00330 return strscpy(dst, tmp, size);
00331 }
00332
00333 static char *
00334 inet_ntop6(const unsigned char *src, char *dst, int size)
00335 {
00336 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
00337 struct {
00338 int base, len;
00339 } best, cur;
00340 u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
00341 int i;
00342
00343 memset(words, '\0', sizeof words);
00344
00345 for (i = 0; i < NS_IN6ADDRSZ; i += 2)
00346 words[i / 2] = (src[i] << 8) | src[i + 1];
00347
00348 best.base = -1;
00349 cur.base = -1;
00350
00351 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
00352 if (words[i] == 0) {
00353 if (cur.base == -1)
00354 cur.base = i, cur.len = 1;
00355
00356 else
00357 cur.len++;
00358 }
00359
00360 else {
00361 if (cur.base != -1) {
00362 if (best.base == -1 || cur.len > best.len)
00363 best = cur;
00364
00365 cur.base = -1;
00366 }
00367 }
00368 }
00369
00370 if (cur.base != -1) {
00371 if (best.base == -1 || cur.len > best.len)
00372 best = cur;
00373 }
00374
00375 if (best.base != -1 && best.len < 2)
00376 best.base = -1;
00377
00378 tp = tmp;
00379
00380 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
00381 if (best.base != -1 && i >= best.base && i < (best.base + best.len)) {
00382 if (i == best.base)
00383 *tp++ = ':';
00384
00385 continue;
00386 }
00387
00388 if (i != 0)
00389 *tp++ = ':';
00390
00391
00392 if (i == 6 && best.base == 0 && (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
00393 if (!inet_ntop4(src + 12, tp, sizeof tmp - (tp - tmp)))
00394 return (NULL);
00395
00396 tp += strlen(tp);
00397
00398 break;
00399 }
00400
00401 tp += sprintf(tp, "%x", words[i]);
00402 }
00403
00404 if (best.base != -1 && (best.base + best.len) == (NS_IN6ADDRSZ / NS_INT16SZ))
00405 *tp++ = ':';
00406
00407 *tp++ = '\0';
00408
00409 if ((tp - tmp) > size)
00410 return (NULL);
00411
00412 return strscpy(dst, tmp, size);
00413 }
00414
00415 char *
00416 inet_ntop(int af, const void *src, char *dst, int size)
00417 {
00418 switch (af) {
00419 case AF_INET:
00420 return (inet_ntop4(src, dst, size));
00421
00422 case AF_INET6:
00423 return (inet_ntop6(src, dst, size));
00424
00425 default:
00426 return (NULL);
00427 }
00428 }
00429
00430 int
00431 isatty(int fd)
00432 {
00433 #if !defined WINCE
00434 HANDLE Hand;
00435 CONSOLE_SCREEN_BUFFER_INFO Info;
00436 unsigned long Events;
00437
00438 if (fd == 0) {
00439 Hand = GetStdHandle(STD_INPUT_HANDLE);
00440 return GetNumberOfConsoleInputEvents(Hand, &Events);
00441 }
00442
00443 else if (fd == 1) {
00444 Hand = GetStdHandle(STD_OUTPUT_HANDLE);
00445 return GetConsoleScreenBufferInfo(Hand, &Info);
00446 }
00447
00448 else if (fd == 2) {
00449 Hand = GetStdHandle(STD_ERROR_HANDLE);
00450 return GetConsoleScreenBufferInfo(Hand, &Info);
00451 }
00452
00453 return -1;
00454 #else
00455 return 0;
00456 #endif
00457 }
00458
00459 #define CHUNK_SIZE 512
00460
00461
00462 int
00463 write(int fd, const void *buf, unsigned int count)
00464 {
00465 size_t written = 0;
00466 while (written < count) {
00467 ssize_t rc = send(fd, (const void *)((const unsigned char *)buf + written),
00468 min(count - written, CHUNK_SIZE), 0);
00469 if (rc <= 0) {
00470 break;
00471 }
00472 written += rc;
00473 }
00474 return written;
00475 }
00476
00477
00478
00479
00480
00481
00482