提交 f4712b48c86c9886dc6f71d8a55c976816b55c5a

作者 LJH 李佳桓
1 个父辈 112fbabd

add

正在显示 1 个修改的文件 包含 268 行增加0 行删除
  1 +/*
  2 + * Copyright 2008 Stephen Liu
  3 + * For license terms, see the file COPYING along with this library.
  4 + */
  5 +
  6 +#include <stdio.h>
  7 +#include <stdarg.h>
  8 +
  9 +#include "spwin32port.hpp"
  10 +
  11 +#include <lm.h>
  12 +#include <tlhelp32.h>
  13 +
  14 +/* Windows doesn't have writev() but does have WSASend */
  15 +int spwin32_writev(SOCKET sock, const struct iovec *vector, DWORD count)
  16 +{
  17 + DWORD sent = -1;
  18 + WSASend(sock, (LPWSABUF)vector, count, &sent, 0, NULL, NULL);
  19 + return sent;
  20 +}
  21 +
  22 +int spwin32_inet_aton(const char *c, struct in_addr* addr)
  23 +{
  24 + unsigned int r;
  25 + if (strcmp(c, "255.255.255.255") == 0) {
  26 + addr->s_addr = 0xFFFFFFFFu;
  27 + return 1;
  28 + }
  29 + r = inet_addr(c);
  30 + if (r == INADDR_NONE)
  31 + return 0;
  32 + addr->s_addr = r;
  33 + return 1;
  34 +}
  35 +
  36 +int spwin32_socketpair(int d, int type, int protocol, int socks[2])
  37 +{
  38 + struct sockaddr_in addr;
  39 + SOCKET listener;
  40 + int e;
  41 + int addrlen = sizeof(addr);
  42 + DWORD flags = 0;
  43 +
  44 + if (socks == 0) {
  45 + WSASetLastError(WSAEINVAL);
  46 + return SOCKET_ERROR;
  47 + }
  48 +
  49 + socks[0] = socks[1] = INVALID_SOCKET;
  50 + if ((listener = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
  51 + return SOCKET_ERROR;
  52 +
  53 + memset(&addr, 0, sizeof(addr));
  54 + addr.sin_family = AF_INET;
  55 + addr.sin_addr.s_addr = htonl(0x7f000001);
  56 + addr.sin_port = 0;
  57 +
  58 + e = bind(listener, (const struct sockaddr*) &addr, sizeof(addr));
  59 + if (e == SOCKET_ERROR) {
  60 + e = WSAGetLastError();
  61 + closesocket(listener);
  62 + WSASetLastError(e);
  63 + return SOCKET_ERROR;
  64 + }
  65 + e = getsockname(listener, (struct sockaddr*) &addr, &addrlen);
  66 + if (e == SOCKET_ERROR) {
  67 + e = WSAGetLastError();
  68 + closesocket(listener);
  69 + WSASetLastError(e);
  70 + return SOCKET_ERROR;
  71 + }
  72 +
  73 + do {
  74 + if (listen(listener, 1) == SOCKET_ERROR) break;
  75 + if ((socks[0] = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, flags)) == INVALID_SOCKET) break;
  76 + if (connect(socks[0], (const struct sockaddr*) &addr, sizeof(addr)) == SOCKET_ERROR) break;
  77 + if ((socks[1] = accept(listener, NULL, NULL)) == INVALID_SOCKET) break;
  78 + closesocket(listener);
  79 + return 0;
  80 + } while (0);
  81 + e = WSAGetLastError();
  82 + closesocket(listener);
  83 + closesocket(socks[0]);
  84 + closesocket(socks[1]);
  85 + WSASetLastError(e);
  86 + return SOCKET_ERROR;
  87 +}
  88 +
  89 +int spwin32_gettimeofday(struct timeval* tv, void * )
  90 +{
  91 +#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
  92 + #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
  93 +#else
  94 + #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
  95 +#endif
  96 +
  97 + FILETIME ft;
  98 + unsigned __int64 tmpres = 0;
  99 + static int tzflag;
  100 +
  101 + if (NULL != tv)
  102 + {
  103 + GetSystemTimeAsFileTime(&ft);
  104 +
  105 + tmpres |= ft.dwHighDateTime;
  106 + tmpres <<= 32;
  107 + tmpres |= ft.dwLowDateTime;
  108 +
  109 + /*converting file time to unix epoch*/
  110 + tmpres /= 10; /*convert into microseconds*/
  111 + tmpres -= DELTA_EPOCH_IN_MICROSECS;
  112 + tv->tv_sec = (long)(tmpres / 1000000UL);
  113 + tv->tv_usec = (long)(tmpres % 1000000UL);
  114 + }
  115 +
  116 + return 0;
  117 +}
  118 +
  119 +//-------------------------------------------------------------------
  120 +
  121 +spwin32_logger_t g_spwin32_syslog = spwin32_syslog;
  122 +
  123 +void spwin32_syslog (int priority, const char * format, ...)
  124 +{
  125 + char logTemp[ 1024 ] = { 0 };
  126 +
  127 + va_list vaList;
  128 + va_start( vaList, format );
  129 + _vsnprintf( logTemp, sizeof( logTemp ), format, vaList );
  130 + va_end ( vaList );
  131 +
  132 + if( strchr( logTemp, '\n' ) ) {
  133 + printf( "#%d %s", GetCurrentThreadId(), logTemp );
  134 + } else {
  135 + printf( "#%d %s\n", GetCurrentThreadId(), logTemp );
  136 + }
  137 +}
  138 +
  139 +void spwin32_closelog (void)
  140 +{
  141 +}
  142 +
  143 +void spwin32_openlog (const char *ident , int option , int facility)
  144 +{
  145 +}
  146 +
  147 +int spwin32_setlogmask (int priority)
  148 +{
  149 + return 0;
  150 +}
  151 +
  152 +//-------------------------------------------------------------------
  153 +
  154 +int spwin32_initsocket()
  155 +{
  156 + WSADATA wsaData;
  157 +
  158 + int err = WSAStartup( MAKEWORD( 2, 0 ), &wsaData );
  159 + if ( err != 0 ) {
  160 + spwin32_syslog( LOG_EMERG, "Couldn't find a useable winsock.dll." );
  161 + return -1;
  162 + }
  163 +
  164 + return 0;
  165 +}
  166 +
  167 +DWORD spwin32_getppid(void)
  168 +{
  169 + HANDLE snapshot;
  170 + PROCESSENTRY32 entry;
  171 + DWORD myid = GetCurrentProcessId(), parentid = 0;
  172 + int found = FALSE;
  173 +
  174 + snapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
  175 + if (snapshot == INVALID_HANDLE_VALUE) {
  176 + sp_syslog( LOG_ERR, "couldn't take process snapshot in getppid()" );
  177 + return parentid;
  178 + }
  179 +
  180 + entry.dwSize = sizeof(PROCESSENTRY32);
  181 + if (!Process32First(snapshot, &entry)) {
  182 + CloseHandle(snapshot);
  183 + sp_syslog( LOG_ERR, "Process32First failed in getppid()" );
  184 + return parentid;
  185 + }
  186 +
  187 + do {
  188 + if (entry.th32ProcessID == myid) {
  189 + parentid = entry.th32ParentProcessID;
  190 + found = TRUE;
  191 + break;
  192 + }
  193 + } while (Process32Next(snapshot, &entry));
  194 +
  195 + CloseHandle(snapshot);
  196 +
  197 + if (!found) {
  198 + sp_syslog( LOG_ERR, "couldn't find the current process entry in getppid()" );
  199 + }
  200 +
  201 + return parentid;
  202 +}
  203 +
  204 +const char * spwin32_strerror( DWORD lastError, char * errmsg, size_t len )
  205 +{
  206 + if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, lastError, 0,
  207 + errmsg, len - 1, NULL)) {
  208 + /* if we fail, call ourself to find out why and return that error */
  209 + return spwin32_strerror( GetLastError(), errmsg, len );
  210 + }
  211 +
  212 + return errmsg;
  213 +}
  214 +
  215 +int spwin32_getexefile( DWORD pid, char * path, int size )
  216 +{
  217 + HANDLE snapshot;
  218 + MODULEENTRY32 entry;
  219 +
  220 + snapshot = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, pid );
  221 + if (snapshot == INVALID_HANDLE_VALUE) {
  222 + sp_syslog( LOG_ERR, "couldn't take process snapshot in getexefile()" );
  223 + return -1;
  224 + }
  225 +
  226 + entry.dwSize = sizeof(MODULEENTRY32);
  227 + if (!Module32First(snapshot, &entry)) {
  228 + CloseHandle(snapshot);
  229 + char errmsg[ 256 ] = { 0 };
  230 + sp_syslog( LOG_ERR, "Module32First failed in getexefile(), errno %d, %s",
  231 + GetLastError(), spwin32_strerror( GetLastError(), errmsg, sizeof( errmsg ) ) );
  232 + return -1;
  233 + }
  234 +
  235 + ::strncpy( path, entry.szExePath, size );
  236 + path[ size - 1 ] = '\0';
  237 +
  238 + CloseHandle(snapshot);
  239 +
  240 + return 0;
  241 +}
  242 +
  243 +void spwin32_pwd( char * path, int size )
  244 +{
  245 + spwin32_getexefile( GetCurrentProcessId(), path, size );
  246 +
  247 + char * pos = strrchr( path, '\\' );
  248 + if( NULL != pos ) *pos = '\0';
  249 +}
  250 +
  251 +void spwin32_pause_console()
  252 +{
  253 + DWORD ppid = spwin32_getppid();
  254 + if( ppid > 0 ) {
  255 + char filePath[ 256 ] = { 0 };
  256 + spwin32_getexefile( ppid, filePath, sizeof( filePath ) );
  257 +
  258 + char * pos = strrchr( filePath, '\\' );
  259 + if( NULL == pos ) pos = filePath;
  260 +
  261 + if( 0 == strcasecmp( pos + 1, "msdev.exe" )
  262 + || 0 == strcasecmp( pos + 1, "explorer.exe" ) )
  263 + {
  264 + printf( "\npress any key to exit ...\n" );
  265 + getchar();
  266 + }
  267 + }
  268 +}
... ...
注册登录 后发表评论