Bruce Cran | 9277ec1 | 2012-02-20 19:28:39 +0000 | [diff] [blame^] | 1 | /* This file contains functions which implement those POSIX and Linux functions |
| 2 | * that MinGW and Microsoft don't provide. The implementations contain just enough |
| 3 | * functionality to support fio. |
| 4 | */ |
| 5 | |
| 6 | #include <arpa/inet.h> |
| 7 | #include <netinet/in.h> |
| 8 | #include <windows.h> |
| 9 | #include <stddef.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <unistd.h> |
| 12 | #include <dirent.h> |
| 13 | #include <pthread.h> |
| 14 | #include <semaphore.h> |
| 15 | #include <sys/shm.h> |
| 16 | #include <sys/mman.h> |
| 17 | #include <sys/uio.h> |
| 18 | #include <sys/resource.h> |
| 19 | #include <sys/poll.h> |
| 20 | |
| 21 | #include "../os-windows.h" |
| 22 | |
| 23 | long sysconf(int name) |
| 24 | { |
| 25 | long long val = -1; |
| 26 | DWORD len; |
| 27 | SYSTEM_LOGICAL_PROCESSOR_INFORMATION processorInfo; |
| 28 | SYSTEM_INFO sysInfo; |
| 29 | MEMORYSTATUSEX status; |
| 30 | |
| 31 | switch (name) |
| 32 | { |
| 33 | case _SC_NPROCESSORS_ONLN: |
| 34 | len = sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); |
| 35 | GetLogicalProcessorInformation(&processorInfo, &len); |
| 36 | val = len / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); |
| 37 | break; |
| 38 | |
| 39 | case _SC_PAGESIZE: |
| 40 | GetSystemInfo(&sysInfo); |
| 41 | val = sysInfo.dwPageSize; |
| 42 | break; |
| 43 | |
| 44 | case _SC_PHYS_PAGES: |
| 45 | status.dwLength = sizeof(status); |
| 46 | GlobalMemoryStatusEx(&status); |
| 47 | val = status.ullTotalPhys; |
| 48 | break; |
| 49 | default: |
| 50 | log_err("sysconf(%d) is not implemented\n", name); |
| 51 | break; |
| 52 | } |
| 53 | |
| 54 | return val; |
| 55 | } |
| 56 | |
| 57 | char *dl_error = NULL; |
| 58 | |
| 59 | int dlclose(void *handle) |
| 60 | { |
| 61 | return !FreeLibrary((HMODULE)handle); |
| 62 | } |
| 63 | |
| 64 | void *dlopen(const char *file, int mode) |
| 65 | { |
| 66 | HMODULE hMod; |
| 67 | |
| 68 | hMod = LoadLibrary(file); |
| 69 | if (hMod == INVALID_HANDLE_VALUE) |
| 70 | dl_error = (char*)"LoadLibrary failed"; |
| 71 | else |
| 72 | dl_error = NULL; |
| 73 | |
| 74 | return hMod; |
| 75 | } |
| 76 | |
| 77 | void *dlsym(void *handle, const char *name) |
| 78 | { |
| 79 | FARPROC fnPtr; |
| 80 | |
| 81 | fnPtr = GetProcAddress((HMODULE)handle, name); |
| 82 | if (fnPtr == NULL) |
| 83 | dl_error = (char*)"GetProcAddress failed"; |
| 84 | else |
| 85 | dl_error = NULL; |
| 86 | |
| 87 | return fnPtr; |
| 88 | } |
| 89 | |
| 90 | char *dlerror(void) |
| 91 | { |
| 92 | return dl_error; |
| 93 | } |
| 94 | |
| 95 | int gettimeofday(struct timeval *restrict tp, void *restrict tzp) |
| 96 | { |
| 97 | FILETIME fileTime; |
| 98 | unsigned long long unix_time, windows_time; |
| 99 | const time_t MILLISECONDS_BETWEEN_1601_AND_1970 = 11644473600000; |
| 100 | |
| 101 | // Ignore the timezone parameter |
| 102 | (void)tzp; |
| 103 | |
| 104 | /* |
| 105 | * Windows time is stored as the number 100 ns intervals since January 1 1601. |
| 106 | * Conversion details from http://www.informit.com/articles/article.aspx?p=102236&seqNum=3 |
| 107 | * Its precision is 100 ns but accuracy is only one clock tick, or normally around 15 ms. |
| 108 | */ |
| 109 | GetSystemTimeAsFileTime(&fileTime); |
| 110 | windows_time = ((unsigned long long)fileTime.dwHighDateTime << 32) + fileTime.dwLowDateTime; |
| 111 | /* Divide by 10,000 to convert to ms and subtract the time between 1601 and 1970 */ |
| 112 | unix_time = (((windows_time)/10000) - MILLISECONDS_BETWEEN_1601_AND_1970); |
| 113 | /* unix_time is now the number of milliseconds since 1970 (the Unix epoch) */ |
| 114 | tp->tv_sec = unix_time / 1000; |
| 115 | tp->tv_usec = (unix_time % 1000) * 1000; |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | void syslog(int priority, const char *message, ... /* argument */) |
| 120 | { |
| 121 | log_err("%s is not implemented\n", __func__); |
| 122 | } |
| 123 | |
| 124 | int sigaction(int sig, const struct sigaction *act, |
| 125 | struct sigaction *oact) |
| 126 | { |
| 127 | errno = ENOSYS; |
| 128 | return (-1); |
| 129 | } |
| 130 | |
| 131 | int lstat(const char * path, struct stat * buf) |
| 132 | { |
| 133 | return stat(path, buf); |
| 134 | } |
| 135 | |
| 136 | void *mmap(void *addr, size_t len, int prot, int flags, |
| 137 | int fildes, off_t off) |
| 138 | { |
| 139 | DWORD vaProt = 0; |
| 140 | void* allocAddr = NULL; |
| 141 | |
| 142 | if (prot & PROT_NONE) |
| 143 | vaProt |= PAGE_NOACCESS; |
| 144 | |
| 145 | if ((prot & PROT_READ) && !(prot & PROT_WRITE)) |
| 146 | vaProt |= PAGE_READONLY; |
| 147 | |
| 148 | if (prot & PROT_WRITE) |
| 149 | vaProt |= PAGE_READWRITE; |
| 150 | |
| 151 | if ((flags & MAP_ANON) | (flags & MAP_ANONYMOUS)) |
| 152 | { |
| 153 | allocAddr = VirtualAlloc(addr, len, MEM_COMMIT, vaProt); |
| 154 | } |
| 155 | |
| 156 | return allocAddr; |
| 157 | } |
| 158 | |
| 159 | int munmap(void *addr, size_t len) |
| 160 | { |
| 161 | return !VirtualFree(addr, 0, MEM_RELEASE); |
| 162 | } |
| 163 | |
| 164 | int fork(void) |
| 165 | { |
| 166 | log_err("%s is not implemented\n", __func__); |
| 167 | errno = ENOSYS; |
| 168 | return (-1); |
| 169 | } |
| 170 | |
| 171 | pid_t setsid(void) |
| 172 | { |
| 173 | log_err("%s is not implemented\n", __func__); |
| 174 | errno = ENOSYS; |
| 175 | return (-1); |
| 176 | } |
| 177 | |
| 178 | void openlog(const char *ident, int logopt, int facility) |
| 179 | { |
| 180 | log_err("%s is not implemented\n", __func__); |
| 181 | } |
| 182 | |
| 183 | void closelog(void) |
| 184 | { |
| 185 | log_err("%s is not implemented\n", __func__); |
| 186 | } |
| 187 | |
| 188 | int kill(pid_t pid, int sig) |
| 189 | { |
| 190 | errno = ESRCH; |
| 191 | return (-1); |
| 192 | } |
| 193 | |
| 194 | // This is assumed to be used only by the network code, |
| 195 | // and so doesn't try and handle any of the other cases |
| 196 | int fcntl(int fildes, int cmd, ...) |
| 197 | { |
| 198 | // non-blocking mode doesn't work the same as in BSD sockets, |
| 199 | // so ignore it. |
| 200 | #if 0 |
| 201 | va_list ap; |
| 202 | int val, opt, status; |
| 203 | |
| 204 | if (cmd == F_GETFL) |
| 205 | return 0; |
| 206 | else if (cmd != F_SETFL) { |
| 207 | errno = EINVAL; |
| 208 | return (-1); |
| 209 | } |
| 210 | |
| 211 | va_start(ap, 1); |
| 212 | |
| 213 | opt = va_arg(ap, int); |
| 214 | if (opt & O_NONBLOCK) |
| 215 | val = 1; |
| 216 | else |
| 217 | val = 0; |
| 218 | |
| 219 | status = ioctlsocket((SOCKET)fildes, opt, &val); |
| 220 | |
| 221 | if (status == SOCKET_ERROR) { |
| 222 | errno = EINVAL; |
| 223 | val = -1; |
| 224 | } |
| 225 | |
| 226 | va_end(ap); |
| 227 | |
| 228 | return val; |
| 229 | #endif |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * Get the value of a local clock source. |
| 235 | * This implementation supports 2 clocks: CLOCK_MONOTONIC provides high-accuracy |
| 236 | * relative time, while CLOCK_REALTIME provides a low-accuracy wall time. |
| 237 | */ |
| 238 | int clock_gettime(clockid_t clock_id, struct timespec *tp) |
| 239 | { |
| 240 | int rc = 0; |
| 241 | |
| 242 | if (clock_id == CLOCK_MONOTONIC) |
| 243 | { |
| 244 | static LARGE_INTEGER freq = {{0,0}}; |
| 245 | LARGE_INTEGER counts; |
| 246 | |
| 247 | QueryPerformanceCounter(&counts); |
| 248 | if (freq.QuadPart == 0) |
| 249 | QueryPerformanceFrequency(&freq); |
| 250 | |
| 251 | tp->tv_sec = counts.QuadPart / freq.QuadPart; |
| 252 | /* Get the difference between the number of ns stored |
| 253 | * in 'tv_sec' and that stored in 'counts' */ |
| 254 | unsigned long long t = tp->tv_sec * freq.QuadPart; |
| 255 | t = counts.QuadPart - t; |
| 256 | /* 't' now contains the number of cycles since the last second. |
| 257 | * We want the number of nanoseconds, so multiply out by 1,000,000,000 |
| 258 | * and then divide by the frequency. */ |
| 259 | t *= 1000000000; |
| 260 | tp->tv_nsec = t / freq.QuadPart; |
| 261 | } |
| 262 | else if (clock_id == CLOCK_REALTIME) |
| 263 | { |
| 264 | /* clock_gettime(CLOCK_REALTIME,...) is just an alias for gettimeofday with a |
| 265 | * higher-precision field. */ |
| 266 | struct timeval tv; |
| 267 | gettimeofday(&tv, NULL); |
| 268 | tp->tv_sec = tv.tv_sec; |
| 269 | tp->tv_nsec = tv.tv_usec * 1000; |
| 270 | } else { |
| 271 | errno = EINVAL; |
| 272 | rc = -1; |
| 273 | } |
| 274 | |
| 275 | return rc; |
| 276 | } |
| 277 | |
| 278 | int mlock(const void * addr, size_t len) |
| 279 | { |
| 280 | return !VirtualLock((LPVOID)addr, len); |
| 281 | } |
| 282 | |
| 283 | int munlock(const void * addr, size_t len) |
| 284 | { |
| 285 | return !VirtualUnlock((LPVOID)addr, len); |
| 286 | } |
| 287 | |
| 288 | pid_t waitpid(pid_t pid, int *stat_loc, int options) |
| 289 | { |
| 290 | log_err("%s is not implemented\n", __func__); |
| 291 | errno = ENOSYS; |
| 292 | return -1; |
| 293 | } |
| 294 | |
| 295 | int usleep(useconds_t useconds) |
| 296 | { |
| 297 | Sleep(useconds / 1000); |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | char *basename(char *path) |
| 302 | { |
| 303 | static char name[MAX_PATH]; |
| 304 | int i; |
| 305 | |
| 306 | if (path == NULL || strlen(path) == 0) |
| 307 | return (char*)"."; |
| 308 | |
| 309 | i = strlen(path) - 1; |
| 310 | |
| 311 | while (name[i] != '\\' && name[i] != '/' && i >= 0) |
| 312 | i--; |
| 313 | |
| 314 | strcpy(name, path + i); |
| 315 | |
| 316 | return name; |
| 317 | } |
| 318 | |
| 319 | int posix_fallocate(int fd, off_t offset, off_t len) |
| 320 | { |
| 321 | log_err("%s is not implemented\n", __func__); |
| 322 | errno = ENOSYS; |
| 323 | return (-1); |
| 324 | } |
| 325 | |
| 326 | int ftruncate(int fildes, off_t length) |
| 327 | { |
| 328 | BOOL bSuccess; |
| 329 | int old_pos = tell(fildes); |
| 330 | lseek(fildes, length, SEEK_SET); |
| 331 | HANDLE hFile = (HANDLE)_get_osfhandle(fildes); |
| 332 | bSuccess = SetEndOfFile(hFile); |
| 333 | lseek(fildes, old_pos, SEEK_SET); |
| 334 | return !bSuccess; |
| 335 | } |
| 336 | |
| 337 | int fsync(int fildes) |
| 338 | { |
| 339 | HANDLE hFile = (HANDLE)_get_osfhandle(fildes); |
| 340 | return !FlushFileBuffers(hFile); |
| 341 | } |
| 342 | |
| 343 | int nFileMappings = 0; |
| 344 | HANDLE fileMappings[1024]; |
| 345 | |
| 346 | int shmget(key_t key, size_t size, int shmflg) |
| 347 | { |
| 348 | int mapid = -1; |
| 349 | HANDLE hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, (PAGE_EXECUTE_READWRITE | SEC_RESERVE), size >> 32, size & 0xFFFFFFFF, NULL); |
| 350 | if (hMapping != NULL) { |
| 351 | fileMappings[nFileMappings] = hMapping; |
| 352 | mapid = nFileMappings; |
| 353 | nFileMappings++; |
| 354 | } else { |
| 355 | errno = ENOSYS; |
| 356 | } |
| 357 | |
| 358 | return mapid; |
| 359 | } |
| 360 | |
| 361 | void *shmat(int shmid, const void *shmaddr, int shmflg) |
| 362 | { |
| 363 | void* mapAddr; |
| 364 | MEMORY_BASIC_INFORMATION memInfo; |
| 365 | mapAddr = MapViewOfFile(fileMappings[shmid], FILE_MAP_ALL_ACCESS, 0, 0, 0); |
| 366 | VirtualQuery(mapAddr, &memInfo, sizeof(memInfo)); |
| 367 | mapAddr = VirtualAlloc(mapAddr, memInfo.RegionSize, MEM_COMMIT, PAGE_READWRITE); |
| 368 | return mapAddr; |
| 369 | } |
| 370 | |
| 371 | int shmdt(const void *shmaddr) |
| 372 | { |
| 373 | return !UnmapViewOfFile(shmaddr); |
| 374 | } |
| 375 | |
| 376 | int shmctl(int shmid, int cmd, struct shmid_ds *buf) |
| 377 | { |
| 378 | if (cmd == IPC_RMID) { |
| 379 | fileMappings[shmid] = INVALID_HANDLE_VALUE; |
| 380 | return 0; |
| 381 | } else { |
| 382 | log_err("%s is not implemented\n", __func__); |
| 383 | } |
| 384 | return (-1); |
| 385 | } |
| 386 | |
| 387 | int setuid(uid_t uid) |
| 388 | { |
| 389 | log_err("%s is not implemented\n", __func__); |
| 390 | errno = ENOSYS; |
| 391 | return (-1); |
| 392 | } |
| 393 | |
| 394 | int setgid(gid_t gid) |
| 395 | { |
| 396 | log_err("%s is not implemented\n", __func__); |
| 397 | errno = ENOSYS; |
| 398 | return (-1); |
| 399 | } |
| 400 | |
| 401 | int nice(int incr) |
| 402 | { |
| 403 | if (incr != 0) { |
| 404 | errno = EINVAL; |
| 405 | return -1; |
| 406 | } |
| 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | int getrusage(int who, struct rusage *r_usage) |
| 412 | { |
| 413 | const time_t SECONDS_BETWEEN_1601_AND_1970 = 11644473600; |
| 414 | FILETIME cTime, eTime, kTime, uTime; |
| 415 | time_t time; |
| 416 | |
| 417 | memset(r_usage, 0, sizeof(*r_usage)); |
| 418 | |
| 419 | HANDLE hProcess = GetCurrentProcess(); |
| 420 | GetProcessTimes(hProcess, &cTime, &eTime, &kTime, &uTime); |
| 421 | time = ((unsigned long long)uTime.dwHighDateTime << 32) + uTime.dwLowDateTime; |
| 422 | /* Divide by 10,000,000 to get the number of seconds and move the epoch from |
| 423 | * 1601 to 1970 */ |
| 424 | time = (time_t)(((time)/10000000) - SECONDS_BETWEEN_1601_AND_1970); |
| 425 | r_usage->ru_utime.tv_sec = time; |
| 426 | /* getrusage() doesn't care about anything other than seconds, so set tv_usec to 0 */ |
| 427 | r_usage->ru_utime.tv_usec = 0; |
| 428 | time = ((unsigned long long)kTime.dwHighDateTime << 32) + kTime.dwLowDateTime; |
| 429 | /* Divide by 10,000,000 to get the number of seconds and move the epoch from |
| 430 | * 1601 to 1970 */ |
| 431 | time = (time_t)(((time)/10000000) - SECONDS_BETWEEN_1601_AND_1970); |
| 432 | r_usage->ru_stime.tv_sec = time; |
| 433 | r_usage->ru_stime.tv_usec = 0; |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | int posix_madvise(void *addr, size_t len, int advice) |
| 438 | { |
| 439 | log_err("%s is not implemented\n", __func__); |
| 440 | return ENOSYS; |
| 441 | } |
| 442 | |
| 443 | // Windows doesn't support advice for memory pages. Just ignore it. |
| 444 | int msync(void *addr, size_t len, int flags) |
| 445 | { |
| 446 | log_err("%s is not implemented\n", __func__); |
| 447 | errno = ENOSYS; |
| 448 | return -1; |
| 449 | } |
| 450 | |
| 451 | int fdatasync(int fildes) |
| 452 | { |
| 453 | return fsync(fildes); |
| 454 | } |
| 455 | |
| 456 | ssize_t pwrite(int fildes, const void *buf, size_t nbyte, |
| 457 | off_t offset) |
| 458 | { |
| 459 | long pos = tell(fildes); |
| 460 | ssize_t len = write(fildes, buf, nbyte); |
| 461 | lseek(fildes, pos, SEEK_SET); |
| 462 | return len; |
| 463 | } |
| 464 | |
| 465 | ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset) |
| 466 | { |
| 467 | long pos = tell(fildes); |
| 468 | ssize_t len = read(fildes, buf, nbyte); |
| 469 | lseek(fildes, pos, SEEK_SET); |
| 470 | return len; |
| 471 | } |
| 472 | |
| 473 | ssize_t readv(int fildes, const struct iovec *iov, int iovcnt) |
| 474 | { |
| 475 | log_err("%s is not implemented\n", __func__); |
| 476 | errno = ENOSYS; |
| 477 | return (-1); |
| 478 | } |
| 479 | |
| 480 | ssize_t writev(int fildes, const struct iovec *iov, int iovcnt) |
| 481 | { |
| 482 | log_err("%s is not implemented\n", __func__); |
| 483 | errno = ENOSYS; |
| 484 | return (-1); |
| 485 | } |
| 486 | |
| 487 | long long strtoll(const char *restrict str, char **restrict endptr, |
| 488 | int base) |
| 489 | { |
| 490 | return _strtoi64(str, endptr, base); |
| 491 | } |
| 492 | |
| 493 | char *strsep(char **stringp, const char *delim) |
| 494 | { |
| 495 | char *orig = *stringp; |
| 496 | BOOL gotMatch = FALSE; |
| 497 | int i = 0; |
| 498 | int j = 0; |
| 499 | |
| 500 | if (*stringp == NULL) |
| 501 | return NULL; |
| 502 | |
| 503 | while ((*stringp)[i] != '\0') { |
| 504 | j = 0; |
| 505 | while (delim[j] != '\0') { |
| 506 | if ((*stringp)[i] == delim[j]) { |
| 507 | gotMatch = TRUE; |
| 508 | (*stringp)[i] = '\0'; |
| 509 | *stringp = *stringp + i + 1; |
| 510 | break; |
| 511 | } |
| 512 | j++; |
| 513 | } |
| 514 | if (gotMatch) |
| 515 | break; |
| 516 | |
| 517 | i++; |
| 518 | } |
| 519 | |
| 520 | if (!gotMatch) |
| 521 | *stringp = NULL; |
| 522 | |
| 523 | return orig; |
| 524 | } |
| 525 | |
| 526 | int poll(struct pollfd fds[], nfds_t nfds, int timeout) |
| 527 | { |
| 528 | struct timeval tv; |
| 529 | struct timeval *to = NULL; |
| 530 | fd_set readfds, writefds, exceptfds; |
| 531 | int i; |
| 532 | int rc; |
| 533 | |
| 534 | if (timeout != -1) |
| 535 | to = &tv; |
| 536 | |
| 537 | to->tv_sec = timeout / 1000; |
| 538 | to->tv_usec = (timeout % 1000) * 1000; |
| 539 | |
| 540 | FD_ZERO(&readfds); |
| 541 | FD_ZERO(&writefds); |
| 542 | FD_ZERO(&exceptfds); |
| 543 | |
| 544 | for (i = 0; i < nfds; i++) |
| 545 | { |
| 546 | if (fds[i].fd < 0) { |
| 547 | fds[i].revents = 0; |
| 548 | continue; |
| 549 | } |
| 550 | |
| 551 | if (fds[i].events & POLLIN) |
| 552 | FD_SET(fds[i].fd, &readfds); |
| 553 | |
| 554 | if (fds[i].events & POLLOUT) |
| 555 | FD_SET(fds[i].fd, &writefds); |
| 556 | |
| 557 | FD_SET(fds[i].fd, &exceptfds); |
| 558 | } |
| 559 | |
| 560 | rc = select(nfds, &readfds, &writefds, &exceptfds, to); |
| 561 | |
| 562 | if (rc != SOCKET_ERROR) { |
| 563 | for (i = 0; i < nfds; i++) |
| 564 | { |
| 565 | if (fds[i].fd < 0) { |
| 566 | continue; |
| 567 | } |
| 568 | |
| 569 | if ((fds[i].events & POLLIN) && FD_ISSET(fds[i].fd, &readfds)) |
| 570 | fds[i].revents |= POLLIN; |
| 571 | |
| 572 | if ((fds[i].events & POLLOUT) && FD_ISSET(fds[i].fd, &writefds)) |
| 573 | fds[i].revents |= POLLOUT; |
| 574 | |
| 575 | if (FD_ISSET(fds[i].fd, &exceptfds)) |
| 576 | fds[i].revents |= POLLHUP; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | return rc; |
| 581 | } |
| 582 | |
| 583 | int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) |
| 584 | { |
| 585 | log_err("%s is not implemented\n", __func__); |
| 586 | errno = ENOSYS; |
| 587 | return -1; |
| 588 | } |
| 589 | |
| 590 | DIR *opendir(const char *dirname) |
| 591 | { |
| 592 | log_err("%s is not implemented\n", __func__); |
| 593 | errno = ENOSYS; |
| 594 | return NULL; |
| 595 | } |
| 596 | |
| 597 | int closedir(DIR *dirp) |
| 598 | { |
| 599 | log_err("%s is not implemented\n", __func__); |
| 600 | errno = ENOSYS; |
| 601 | return -1; |
| 602 | } |
| 603 | |
| 604 | struct dirent *readdir(DIR *dirp) |
| 605 | { |
| 606 | log_err("%s is not implemented\n", __func__); |
| 607 | errno = ENOSYS; |
| 608 | return NULL; |
| 609 | } |
| 610 | |
| 611 | uid_t geteuid(void) |
| 612 | { |
| 613 | log_err("%s is not implemented\n", __func__); |
| 614 | errno = ENOSYS; |
| 615 | return -1; |
| 616 | } |
| 617 | |
| 618 | int inet_aton(char *addr) |
| 619 | { |
| 620 | log_err("%s is not implemented\n", __func__); |
| 621 | errno = ENOSYS; |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | const char* inet_ntop(int af, const void *restrict src, |
| 626 | char *restrict dst, socklen_t size) |
| 627 | { |
| 628 | INT status = SOCKET_ERROR; |
| 629 | WSADATA wsd; |
| 630 | char *ret = NULL; |
| 631 | |
| 632 | if (af != AF_INET && af != AF_INET6) { |
| 633 | errno = EAFNOSUPPORT; |
| 634 | return NULL; |
| 635 | } |
| 636 | |
| 637 | WSAStartup(MAKEWORD(2,2), &wsd); |
| 638 | |
| 639 | if (af == AF_INET) { |
| 640 | struct sockaddr_in si; |
| 641 | DWORD len = size; |
| 642 | memset(&si, 0, sizeof(si)); |
| 643 | si.sin_family = af; |
| 644 | memcpy(&si.sin_addr, src, sizeof(si.sin_addr)); |
| 645 | status = WSAAddressToString((struct sockaddr*)&si, sizeof(si), NULL, dst, &len); |
| 646 | } else if (af == AF_INET6) { |
| 647 | struct sockaddr_in6 si6; |
| 648 | DWORD len = size; |
| 649 | memset(&si6, 0, sizeof(si6)); |
| 650 | si6.sin6_family = af; |
| 651 | memcpy(&si6.sin6_addr, src, sizeof(si6.sin6_addr)); |
| 652 | status = WSAAddressToString((struct sockaddr*)&si6, sizeof(si6), NULL, dst, &len); |
| 653 | } |
| 654 | |
| 655 | if (status != SOCKET_ERROR) |
| 656 | ret = dst; |
| 657 | else |
| 658 | errno = ENOSPC; |
| 659 | |
| 660 | WSACleanup(); |
| 661 | return ret; |
| 662 | } |
| 663 | |
| 664 | int inet_pton(int af, const char *restrict src, void *restrict dst) |
| 665 | { |
| 666 | INT status = SOCKET_ERROR; |
| 667 | WSADATA wsd; |
| 668 | int ret = 1; |
| 669 | |
| 670 | if (af != AF_INET && af != AF_INET6) { |
| 671 | errno = EAFNOSUPPORT; |
| 672 | return -1; |
| 673 | } |
| 674 | |
| 675 | WSAStartup(MAKEWORD(2,2), &wsd); |
| 676 | |
| 677 | if (af == AF_INET) { |
| 678 | struct sockaddr_in si; |
| 679 | INT len = sizeof(si); |
| 680 | memset(&si, 0, sizeof(si)); |
| 681 | si.sin_family = af; |
| 682 | status = WSAStringToAddressA((char*)src, af, NULL, (struct sockaddr*)&si, &len); |
| 683 | if (status != SOCKET_ERROR) |
| 684 | memcpy(dst, &si.sin_addr, sizeof(si.sin_addr)); |
| 685 | } else if (af == AF_INET6) { |
| 686 | struct sockaddr_in6 si6; |
| 687 | INT len = sizeof(si6); |
| 688 | memset(&si6, 0, sizeof(si6)); |
| 689 | si6.sin6_family = af; |
| 690 | status = WSAStringToAddressA((char*)src, af, NULL, (struct sockaddr*)&si6, &len); |
| 691 | if (status != SOCKET_ERROR) |
| 692 | memcpy(dst, &si6.sin6_addr, sizeof(si6.sin6_addr)); |
| 693 | } |
| 694 | |
| 695 | if (status == SOCKET_ERROR) { |
| 696 | errno = ENOSPC; |
| 697 | ret = 0; |
| 698 | } |
| 699 | |
| 700 | WSACleanup(); |
| 701 | |
| 702 | return ret; |
| 703 | } |