home *** CD-ROM | disk | FTP | other *** search
/ PC World 2004 November / PCWorld_2004-11_cd.bin / software / temacd / poweroff / pwroff30.exe / src / sockets.c < prev    next >
C/C++ Source or Header  |  2002-02-23  |  13KB  |  504 lines

  1. #include "poweroff.h"
  2. #include <winsock2.h>
  3.  
  4. void ShowSocketError(PowerSettings *ps,char *function)
  5. {
  6.   char str[100],error[100];
  7.  
  8.   Log("ShowSocketError start");
  9.   switch (WSAGetLastError())
  10.   {
  11.     case WSAEINTR:
  12.       strcpy(error,"Interrupted function call");
  13.       break;
  14.     case WSAEBADF:
  15.       strcpy(error,"WSAEBADF");
  16.       break;
  17.     case WSAEACCES:
  18.       strcpy(error,"Permission denied");
  19.       break;
  20.     case WSAEFAULT:
  21.       strcpy(error,"Bad address");
  22.       break;
  23.     case WSAEINVAL:
  24.       strcpy(error,"Invalid argument");
  25.       break;
  26.     case WSAEMFILE:
  27.       strcpy(error,"Too many open files");
  28.       break;
  29.     case WSAEWOULDBLOCK:
  30.       strcpy(error,"Resource temporarily unavailable");
  31.       break;
  32.     case WSAEINPROGRESS:
  33.       strcpy(error,"Operation now in progress");
  34.       break;
  35.     case WSAEALREADY:
  36.       strcpy(error,"Operation already in progress");
  37.       break;
  38.     case WSAENOTSOCK:
  39.       strcpy(error,"Socket operation on non-socket");
  40.       break;
  41.     case WSAEDESTADDRREQ:
  42.       strcpy(error,"Destination address required");
  43.       break;
  44.     case WSAEMSGSIZE:
  45.       strcpy(error,"Message too long");
  46.       break;
  47.     case WSAEPROTOTYPE:
  48.       strcpy(error,"Protocol wrong type for socket");
  49.       break;
  50.     case WSAENOPROTOOPT:
  51.       strcpy(error,"Bad protocol option");
  52.       break;
  53.     case WSAEPROTONOSUPPORT:
  54.       strcpy(error,"Protocol not supported");
  55.       break;
  56.     case WSAESOCKTNOSUPPORT:
  57.       strcpy(error,"Socket type not supported");
  58.       break;
  59.     case WSAEOPNOTSUPP:
  60.       strcpy(error,"Operation not supported");
  61.       break;
  62.     case WSAEPFNOSUPPORT:
  63.       strcpy(error,"Protocol family not supported");
  64.       break;
  65.     case WSAEAFNOSUPPORT:
  66.       strcpy(error,"Address family not supported by protocol family");
  67.       break;
  68.     case WSAEADDRINUSE:
  69.       strcpy(error,"Address already in use");
  70.       break;
  71.     case WSAEADDRNOTAVAIL:
  72.       strcpy(error,"Cannot assign requested address");
  73.       break;
  74.     case WSAENETDOWN:
  75.       strcpy(error,"Network is down");
  76.       break;
  77.     case WSAENETUNREACH:
  78.       strcpy(error,"Network is unreachable");
  79.       break;
  80.     case WSAENETRESET:
  81.       strcpy(error,"Network dropped connection on reset");
  82.       break;
  83.     case WSAECONNABORTED:
  84.       strcpy(error,"Software caused connection abort");
  85.       break;
  86.     case WSAECONNRESET:
  87.       strcpy(error,"Connection reset by peer");
  88.       break;
  89.     case WSAENOBUFS:
  90.       strcpy(error,"No buffer space available");
  91.       break;
  92.     case WSAEISCONN:
  93.       strcpy(error,"Socket is already connected");
  94.       break;
  95.     case WSAENOTCONN:
  96.       strcpy(error,"Socket is not connected");
  97.       break;
  98.     case WSAESHUTDOWN:
  99.       strcpy(error,"Cannot send after socket shutdown");
  100.       break;
  101.     case WSAETOOMANYREFS:
  102.       strcpy(error,"WSAETOOMANYREFS");
  103.       break;
  104.     case WSAETIMEDOUT:
  105.       strcpy(error,"Connection timed out");
  106.       break;
  107.     case WSAECONNREFUSED:
  108.       strcpy(error,"Connection refused");
  109.       break;
  110.     case WSAELOOP:
  111.       strcpy(error,"WSAELOOP");
  112.       break;
  113.     case WSAENAMETOOLONG:
  114.       strcpy(error,"WSAENAMETOOLONG");
  115.       break;
  116.     case WSAEHOSTDOWN:
  117.       strcpy(error,"Host is down");
  118.       break;
  119.     case WSAEHOSTUNREACH:
  120.       strcpy(error,"No route to host");
  121.       break;
  122.     case WSAENOTEMPTY:
  123.       strcpy(error,"WSAENOTEMPTY");
  124.       break;
  125.     case WSAEPROCLIM:
  126.       strcpy(error,"Too many processes");
  127.       break;
  128.     case WSAEUSERS:
  129.       strcpy(error,"WSAEUSERS");
  130.       break;
  131.     case WSAEDQUOT:
  132.       strcpy(error,"WSAEDQUOT");
  133.       break;
  134.     case WSAESTALE:
  135.       strcpy(error,"WSAESTALE");
  136.       break;
  137.     case WSAEREMOTE:
  138.       strcpy(error,"WSAEREMOTE");
  139.       break;
  140.     case WSASYSNOTREADY:
  141.       strcpy(error,"Network subsystem is unavailable");
  142.       break;
  143.     case WSAVERNOTSUPPORTED:
  144.       strcpy(error,"WINSOCK.DLL version out of range");
  145.       break;
  146.     case WSANOTINITIALISED:
  147.       strcpy(error,"Successful WSAStartup not yet performed");
  148.       break;
  149.     case WSAEDISCON:
  150.       strcpy(error,"Graceful shutdown in progress");
  151.       break;
  152.     case WSAENOMORE:
  153.       strcpy(error,"WSAENOMORE");
  154.       break;
  155.     case WSAECANCELLED:
  156.       strcpy(error,"WSAECANCELLED");
  157.       break;
  158.     case WSAEINVALIDPROCTABLE:
  159.       strcpy(error,"WSAEINVALIDPROCTABLE");
  160.       break;
  161.     case WSAEINVALIDPROVIDER:
  162.       strcpy(error,"WSAEINVALIDPROVIDER");
  163.       break;
  164.     case WSAEPROVIDERFAILEDINIT:
  165.       strcpy(error,"WSAEPROVIDERFAILEDINIT");
  166.       break;
  167.     case WSASYSCALLFAILURE:
  168.       strcpy(error,"System call failure");
  169.       break;
  170.     case WSASERVICE_NOT_FOUND:
  171.       strcpy(error,"WSASERVICE_NOT_FOUND");
  172.       break;
  173.     case WSATYPE_NOT_FOUND:
  174.       strcpy(error,"Class type not found");
  175.       break;
  176.     case WSA_E_NO_MORE:
  177.       strcpy(error,"WSA_E_NO_MORE");
  178.       break;
  179.     case WSA_E_CANCELLED:
  180.       strcpy(error,"WSA_E_CANCELLED");
  181.       break;
  182.     case WSAEREFUSED:
  183.       strcpy(error,"WSAEREFUSED");
  184.       break;
  185.     case WSAHOST_NOT_FOUND:
  186.       strcpy(error,"Host not found");
  187.       break;
  188.     case WSATRY_AGAIN:
  189.       strcpy(error,"Non-authoritative host not found");
  190.       break;
  191.     case WSANO_RECOVERY:
  192.       strcpy(error,"This is a non-recoverable error");
  193.       break;
  194.     case WSANO_DATA:
  195.       strcpy(error,"Valid name, no data record of requested type");
  196.       break;
  197.     case WSA_QOS_RECEIVERS:
  198.       strcpy(error,"WSA_QOS_RECEIVERS");
  199.       break;
  200.     case WSA_QOS_SENDERS:
  201.       strcpy(error,"WSA_QOS_SENDERS");
  202.       break;
  203.     case WSA_QOS_NO_SENDERS:
  204.       strcpy(error,"WSA_QOS_NO_SENDERS");
  205.       break;
  206.     case WSA_QOS_NO_RECEIVERS:
  207.       strcpy(error,"WSA_QOS_NO_RECEIVERS");
  208.       break;
  209.     case WSA_QOS_REQUEST_CONFIRMED:
  210.       strcpy(error,"WSA_QOS_REQUEST_CONFIRMED");
  211.       break;
  212.     case WSA_QOS_ADMISSION_FAILURE:
  213.       strcpy(error,"WSA_QOS_ADMISSION_FAILURE");
  214.       break;
  215.     case WSA_QOS_POLICY_FAILURE:
  216.       strcpy(error,"WSA_QOS_POLICY_FAILURE");
  217.       break;
  218.     case WSA_QOS_BAD_STYLE:
  219.       strcpy(error,"WSA_QOS_BAD_STYLE");
  220.       break;
  221.     case WSA_QOS_BAD_OBJECT:
  222.       strcpy(error,"WSA_QOS_BAD_OBJECT");
  223.       break;
  224.     case WSA_QOS_TRAFFIC_CTRL_ERROR:
  225.       strcpy(error,"WSA_QOS_TRAFFIC_CTRL_ERROR");
  226.       break;
  227.     case WSA_QOS_GENERIC_ERROR:
  228.       strcpy(error,"WSA_QOS_GENERIC_ERROR");
  229.       break;
  230.     default:
  231.       strcpy(error,"Unknown error");
  232.       break;
  233.   }
  234.   sprintf(str,"%s(): %s (%d)",function,error,WSAGetLastError());
  235.   Error(ps,NULL,str);
  236.   Log("ShowSocketError end");
  237. }
  238.  
  239. int InitializeSockets(void)
  240. {
  241.   WORD wVersionRequested;
  242.   WSADATA wsaData;
  243.   int err;
  244.  
  245.   Log("InitializeSockets start");
  246.   wVersionRequested=MAKEWORD(1,1);
  247.  
  248.   err=WSAStartup(wVersionRequested,&wsaData);
  249.   if (err!=0) 
  250.   {
  251.     Log("InitializeSockets end because WSAStartup failed");
  252.     return -1;
  253.   }
  254.   
  255.   if (LOBYTE(wsaData.wVersion)!=1 ||
  256.       HIBYTE(wsaData.wVersion)!=1) 
  257.   {
  258.     WSACleanup( );
  259.     Log("InitializeSockets end, version=%d.%d",HIBYTE(wsaData.wVersion),LOBYTE(wsaData.wVersion));
  260.     return -2; 
  261.   }
  262.   Log("InitializeSockets end");
  263.   return 0;
  264. }
  265.  
  266. void DestroySockets(HWND hWnd,PowerSettings *ps)
  267. {
  268.   Log("DestroySockets start");
  269.   if (ps)
  270.   {
  271.     CloseServerSocket(hWnd,ps);
  272.     CloseClientSocket(hWnd,ps);
  273.   }
  274.   WSACleanup();
  275.   Log("DestroySockets end");
  276. }
  277.  
  278. int CreateServerSocket(HWND hWnd,PowerSettings *ps)
  279. {
  280.   struct sockaddr_in sa;
  281.  
  282.   Log("CreateServerSocket start");
  283.   if (ps->serversocket!=INVALID_SOCKET)
  284.     CloseServerSocket(hWnd,ps);
  285.   ps->serversocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  286.   if (ps->serversocket==INVALID_SOCKET)
  287.   {
  288.     ShowSocketError(ps,"socket");
  289.     return -1;
  290.   }
  291.  
  292.   sa.sin_family = AF_INET;
  293.   sa.sin_port = htons((unsigned short)ps->remote_control.port);
  294.   sa.sin_addr.s_addr = htonl(INADDR_ANY);   
  295.   if (bind(ps->serversocket,(struct sockaddr*)&sa,sizeof(sa))==SOCKET_ERROR)
  296.   {
  297.     ShowSocketError(ps,"bind");
  298.     return -1;
  299.   }
  300.   if (listen(ps->serversocket,LISTEN_CLIENTS)==SOCKET_ERROR)
  301.   {
  302.     ShowSocketError(ps,"listen");
  303.     return -1;
  304.   }
  305.   if (WSAAsyncSelect(ps->serversocket,hWnd,UWM_ACCEPT,FD_ACCEPT)==SOCKET_ERROR)
  306.   {
  307.     ShowSocketError(ps,"WSAAsyncSelect");
  308.     return -1;
  309.   }
  310.   Log("CreateServerSocket end");
  311.   return 0;
  312. }
  313.  
  314. int AcceptClientSocket(HWND hWnd,PowerSettings *ps)
  315. {
  316.   Log("AcceptClientSocket start");
  317.   if (ps->clientsocket!=INVALID_SOCKET)
  318.     CloseClientSocket(hWnd,ps);
  319.   if ((ps->clientsocket=accept(ps->serversocket,NULL,NULL))==INVALID_SOCKET)
  320.   {
  321.     ShowSocketError(ps,"accept");
  322.     return -1;
  323.   }
  324.   if (ps->remote_control.password[0]=='\0')
  325.     ps->authenticated=1;
  326.   else
  327.     ps->authenticated=0;
  328.   if (WSAAsyncSelect(ps->clientsocket,hWnd,UWM_READ,FD_READ|FD_CLOSE)==SOCKET_ERROR)
  329.   {
  330.     ShowSocketError(ps,"WSAAsyncSelect");
  331.     return -1;
  332.   }
  333.   ReturnClientInfo(hWnd,ps,"Welcome to Poweroff %s created by Jorgen Bosman",POWEROFF_VERSION);
  334.   Log("AcceptClientSocket end");
  335.   return 0;
  336. }
  337.  
  338. int ReadFromClientSocket(HWND hWnd,PowerSettings *ps)
  339. {
  340.   char buf[101]="",tmp[2];
  341.   static char command[501]="";
  342.   int bytes_received,x;
  343.  
  344.   Log("ReadFromClientSocket start");
  345.   bytes_received=recv(ps->clientsocket,buf,100,0);
  346.   if (bytes_received==0)
  347.     CloseClientSocket(hWnd,ps);
  348.   else if (bytes_received==SOCKET_ERROR)
  349.   {
  350.     ShowSocketError(ps,"send");
  351.     CloseClientSocket(hWnd,ps);
  352.     return -1;
  353.   }
  354.   else
  355.   {
  356.     buf[bytes_received]=0;
  357.     Log("Received data from client: %s",buf);
  358.     for (x=0;x<bytes_received;x++)
  359.     {
  360.       if (buf[x]=='\n')
  361.       {
  362.         ProcessClientCommand(hWnd,ps,command);
  363.         command[0]='\0';
  364.       }
  365.       else if (buf[x]=='\r')
  366.         continue;
  367.       else
  368.       {
  369.         if (strlen(command)>=500)
  370.         {
  371.           ReturnClientError(hWnd,ps,"Command too long");
  372.           command[0]='\0';
  373.         }
  374.         else
  375.         {
  376.           tmp[0]=buf[x];
  377.           tmp[1]='\0';
  378.           strcat(command,tmp);
  379.         }
  380.       }
  381.     }
  382.   }
  383.   Log("ReadFromClientSocket end");
  384.   return 0;
  385. }
  386.  
  387. int CloseClientSocket(HWND hWnd,PowerSettings *ps)
  388. {
  389.   Log("CloseClientSocket start");
  390.   if (ps->clientsocket!=INVALID_SOCKET)
  391.   {
  392.     shutdown(ps->clientsocket,SD_BOTH);
  393.     closesocket(ps->clientsocket);
  394.     ps->clientsocket=INVALID_SOCKET;
  395.     ps->authenticated=0;
  396.   }
  397.   Log("CloseClientSocket end");
  398.   return 0;
  399. }
  400.  
  401. int CloseServerSocket(HWND hWnd,PowerSettings *ps)
  402. {
  403.   Log("CloseServerSocket start");
  404.   if (ps->serversocket!=INVALID_SOCKET)
  405.   {
  406.     shutdown(ps->serversocket,SD_BOTH);
  407.     closesocket(ps->serversocket);
  408.     ps->serversocket=INVALID_SOCKET;
  409.   }
  410.   CloseClientSocket(hWnd,ps);
  411.   Log("CloseServerSocket end");
  412.   return 0;
  413. }
  414.  
  415. int OpenServerConnection(HWND hWnd,PowerSettings *ps,char *server,char *errormsg)
  416. {
  417.   struct sockaddr_in sa;
  418.   struct hostent *hp; 
  419.   unsigned long ia;
  420.   char str[500],*value;
  421.   int ret;
  422.  
  423.   Log("OpenServerConnection start, server=%s",server);
  424.   if (ps->clientsocket!=INVALID_SOCKET)
  425.     CloseClientSocket(hWnd,ps);
  426.   ps->clientsocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  427.   if (ps->clientsocket==INVALID_SOCKET)
  428.   {
  429.     ShowSocketError(ps,"socket");
  430.     return -1;
  431.   }
  432.  
  433.   sa.sin_family = AF_INET;
  434.   sa.sin_port = 0;
  435.   sa.sin_addr.s_addr = htonl(INADDR_ANY);   
  436.   if (bind(ps->clientsocket,(struct sockaddr*)&sa,sizeof(sa))==SOCKET_ERROR)
  437.   {
  438.     ShowSocketError(ps,"bind");
  439.     return -1;
  440.   }
  441.  
  442.   if ((ia=inet_addr(server))==INADDR_NONE)
  443.   {
  444.     hp=gethostbyname(server);
  445.     if (hp == NULL)
  446.     {
  447.       ShowSocketError(ps,"gethostbyname");
  448.       return -1;
  449.     }
  450.     memset(&sa,0,sizeof(sa));
  451.     memcpy(&sa.sin_addr,hp->h_addr,hp->h_length);
  452.     sa.sin_family=hp->h_addrtype;
  453.   }
  454.   else
  455.   {
  456. /*    hp=gethostbyaddr((char*)&ia,sizeof(ia),AF_INET);
  457.     if (hp == NULL)
  458.     {
  459.       ShowSocketError(ps,"gethostbyaddr");
  460.       return -1;
  461.     }*/
  462.     memset(&sa,0,sizeof(sa));
  463.     memcpy(&sa.sin_addr,&ia,sizeof(ia));
  464.     sa.sin_family=AF_INET;
  465.   }
  466.   sa.sin_port=htons((unsigned short)ps->remote.port);
  467.   if (connect(ps->clientsocket,(struct sockaddr*)&sa,sizeof(sa))==SOCKET_ERROR)
  468.   {
  469.     ShowSocketError(ps,"connect");
  470.     return -1;
  471.   }
  472.   Log("Waiting from response from server");
  473.   if ((ret=recv(ps->clientsocket,str,500,0))==SOCKET_ERROR || ret<=0)
  474.   {
  475.     ShowSocketError(ps,"recv");
  476.     CloseClientSocket(hWnd,ps);
  477.     return -1;
  478.   }
  479.   str[ret]=0;
  480.   Log("response=%s",str);
  481.   while (strlen(str)>0 && (str[strlen(str)-1]=='\n' || str[strlen(str)-1]=='\r'))
  482.     str[strlen(str)-1]='\0';
  483.   value=strchr(str,' ');
  484.   if (value)
  485.   {
  486.     *value='\0';
  487.     value++;
  488.     Trim(value);
  489.   }
  490.   if (str[0]=='2')
  491.     return 0;
  492.   else if (str[0]=='5')
  493.   {
  494.     strcpy(errormsg,value);
  495.     return -2;
  496.   }
  497.   else
  498.   {
  499.     strcpy(errormsg,"Unknown response received");
  500.     return -2;
  501.   }
  502.   Log("OpenServerConnection end");
  503.   return 0;
  504. }