?login_element?

Subversion Repositories NedoOS

Rev

Rev 2145 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. void delayLong(unsigned long counter)
  2. {
  3.   unsigned long start, finish;
  4.   counter = counter / 20;
  5.   if (counter < 1)
  6.   {
  7.     counter = 1;
  8.   }
  9.   start = time();
  10.   finish = start + counter;
  11.  
  12.   while (start < finish)
  13.   {
  14.     start = time();
  15.     YIELD();
  16.   }
  17. }
  18. int httpError(void)
  19. {
  20.   unsigned char *httpRes;
  21.   unsigned int httpErr;
  22.   httpRes = strstr(netbuf, "HTTP/1.1 ");
  23.  
  24.   if (httpRes != NULL)
  25.   {
  26.     httpErr = atol(httpRes + 9);
  27.   }
  28.   else
  29.   {
  30.     httpErr = 0;
  31.   }
  32.   return httpErr;
  33. }
  34.  
  35. void errorPrint(unsigned int error)
  36. {
  37.   switch (error)
  38.   {
  39.   case 2:
  40.     printf("02 SHUT_RDWR");
  41.     break;
  42.   case 4:
  43.     printf("04 ERR_INTR");
  44.     break;
  45.   case 23:
  46.     printf("23 ERR_NFILE");
  47.     break;
  48.   case 35:
  49.     printf("35 ERR_EAGAIN");
  50.     break;
  51.   case 37:
  52.     printf("37 ERR_ALREADY");
  53.     break;
  54.   case 38:
  55.     printf("38 ERR_NOTSOCK");
  56.     break;
  57.   case 40:
  58.     printf("40 ERR_EMSGSIZE");
  59.     break;
  60.   case 41:
  61.     printf("41 ERR_PROTOTYPE");
  62.     break;
  63.   case 47:
  64.     printf("47 ERR_AFNOSUPPORT");
  65.     break;
  66.   case 53:
  67.     printf("53 ERR_ECONNABORTED");
  68.     break;
  69.   case 54:
  70.     printf("54 ERR_CONNRESET");
  71.     break;
  72.   case 57:
  73.     printf("57 ERR_NOTCONN");
  74.     break;
  75.   case 65:
  76.     printf("65 ERR_HOSTUNREACH");
  77.     break;
  78.   default:
  79.     printf("%u UNKNOWN ERROR", error);
  80.     break;
  81.   }
  82. }
  83. void testOperation(unsigned char *process, int socket)
  84. {
  85.   if (socket < 0)
  86.   {
  87.     printf("%s: [ERROR:", process);
  88.     errorPrint(-socket);
  89.     printf("]\r\n");
  90.     exit(0);
  91.   }
  92. }
  93. char OpenSock(unsigned char family, unsigned char protocol)
  94. {
  95.   signed char socket;
  96.   unsigned int todo;
  97.   todo = OS_NETSOCKET((family << 8) + protocol);
  98.   if (todo > 32767)
  99.   {
  100.     return 0 - (todo & 255);
  101.   }
  102.   else
  103.   {
  104.     socket = ((todo & 65280) >> 8);
  105.     // printf("OS_NETSOCKET: Socket #%d created\n\r", socket);
  106.   }
  107.   return socket;
  108. }
  109.  
  110. char netShutDown(signed char socket, unsigned char type)
  111. {
  112.   unsigned int todo;
  113.   todo = OS_NETSHUTDOWN(socket, type);
  114.   if (todo > 32767)
  115.   {
  116.     return 0 - (todo & 255);
  117.   }
  118.   else
  119.   {
  120.     // printf("Socket #%d closed.\n\r", socket);
  121.   }
  122.   return socket;
  123. }
  124.  
  125. char netConnect(signed char socket, unsigned char retry)
  126. {
  127.   unsigned int todo;
  128.  
  129.   while (retry != 0)
  130.   {
  131.     todo = OS_NETCONNECT(socket, &targetadr);
  132.  
  133.     if (todo > 32767)
  134.     {
  135.       retry--;
  136.       delayLong(500);
  137.       netShutDown(socket, 0);
  138.       socket = OpenSock(AF_INET, SOCK_STREAM);
  139.       testOperation("OS_NETSOCKET", socket);
  140.     }
  141.     else
  142.     {
  143.       // printf("OS_NETCONNECT: connection successful, %u\n\r", (todo & 255));
  144.       return socket;
  145.     }
  146.   }
  147.   return 0 - (todo & 255);
  148. }
  149.  
  150. int tcpSend(signed char socket, unsigned int messageadr, unsigned int size, unsigned char retry)
  151. {
  152.   unsigned int todo;
  153.   readStruct.socket = socket;
  154.   readStruct.BufAdr = messageadr;
  155.   readStruct.bufsize = size;
  156.   readStruct.protocol = SOCK_STREAM;
  157.   while (retry > 0)
  158.   {
  159.     todo = OS_WIZNETWRITE(&readStruct);
  160.     if (todo > 32767)
  161.     {
  162.       retry--;
  163.       delayLong(500);
  164.     }
  165.     else
  166.     {
  167.       // printf("OS_WIZNETWRITE: %u bytes written. \n\r", todo);
  168.       return todo;
  169.     }
  170.   }
  171.   return 0 - (todo & 255);
  172. }
  173.  
  174. int tcpRead(signed char socket, unsigned char retry)
  175. {
  176.   unsigned int todo;
  177.  
  178.   readStruct.socket = socket;
  179.   readStruct.BufAdr = (unsigned int)&netbuf;
  180.   readStruct.bufsize = sizeof(netbuf);
  181.   readStruct.protocol = SOCK_STREAM;
  182.  
  183.   while (retry > 0)
  184.   {
  185.     todo = OS_WIZNETREAD(&readStruct);
  186.  
  187.     if (todo > 32767)
  188.     {
  189.       if ((todo & 255) != ERR_EAGAIN)
  190.       {
  191.         retry--;
  192.         delayLong(500);
  193.       }
  194.     }
  195.     else
  196.     {
  197.       // printf("OS_WIZNETREAD: %u bytes read. \n\r", todo);
  198.       return todo;
  199.     }
  200.   }
  201.   return 0 - (todo & 255);
  202. }
  203.  
  204. unsigned char dnsResolve(unsigned char *domainName)
  205. {
  206.   unsigned char socket, retry, retryInv;
  207.   unsigned int todo, queryPos, queryType, queryLng, domainLng, comaCount, reqSize;
  208.   unsigned int loop;
  209.  
  210.   unsigned char dnsQuery1[] = {0x11, 0x22, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  211.   unsigned char dnsQuery2[] = {0x00, 0x00, 0x01, 0x00, 0x01};
  212.  
  213.   domainLng = strlen(domainName);
  214.   comaCount = 0;
  215.   loop = domainLng;
  216.   cmd[loop + 1] = 0;
  217.  
  218.   do
  219.   {
  220.     if (domainName[loop - 1] == '.')
  221.     {
  222.       cmd[loop] = comaCount;
  223.       comaCount = 0;
  224.     }
  225.     else
  226.     {
  227.       cmd[loop] = domainName[loop - 1];
  228.       comaCount++;
  229.     }
  230.     loop--;
  231.   } while (loop != 0);
  232.   cmd[0] = comaCount;
  233.  
  234.   memcpy(netbuf, dnsQuery1, sizeof(dnsQuery1));
  235.   memcpy(netbuf + sizeof(dnsQuery1), cmd, domainLng + 1);
  236.   memcpy(netbuf + domainLng + sizeof(dnsQuery1) + 1, dnsQuery2, sizeof(dnsQuery2));
  237.   reqSize = sizeof(dnsQuery1) + sizeof(dnsQuery2) + domainLng + 1;
  238.  
  239.   socket = OpenSock(AF_INET, SOCK_DGRAM);
  240.   readStruct.socket = socket;
  241.   readStruct.BufAdr = (unsigned int)&netbuf;
  242.   readStruct.bufsize = (unsigned int)reqSize;
  243.   readStruct.protocol = SOCK_DGRAM;
  244.  
  245.   todo = OS_WIZNETWRITE_UDP(&readStruct, &dnsaddress);
  246.   if (todo > 32767)
  247.   {
  248.     errorPrint(todo & 255);
  249.     return 0;
  250.   }
  251.   else
  252.   {
  253.     // printf("OS_WIZNETWRITE_UDP: %u bytes written. \n\r", todo);
  254.   }
  255.  
  256.   readStruct.BufAdr = (unsigned int)&netbuf;
  257.   readStruct.bufsize = (unsigned int)sizeof(netbuf);
  258.   retry = 20;
  259.   retryInv = retry;
  260.  
  261.   do
  262.   {
  263.     todo = OS_WIZNETREAD_UDP(&readStruct, &dnsaddress);
  264.     if (todo > 32767)
  265.     {
  266.       // errorPrint(todo & 255);
  267.       if (retry == 0)
  268.       {
  269.         // clearStatus();
  270.         // printf(" Error quering[Response] DNS server.");
  271.         return 0;
  272.       }
  273.       retry--;
  274.       delayLong(200);
  275.       // printf(" Retry [%d]\r\n", retryInv - retry);
  276.     }
  277.     else
  278.     {
  279.       // printf("OS_WIZNETREAD_UDP: %u bytes read. \n\r", todo);
  280.       break;
  281.     }
  282.   } while (todo > 32767);
  283.  
  284.   netShutDown(socket, 0);
  285.  
  286.   if (!(netbuf[2] && 0x0f))
  287.   {
  288.     // clearStatus();
  289.     // printf(" Error quering[Parsing] DNS server.");
  290.     return 0;
  291.   }
  292.  
  293.   queryPos = 11;
  294.   queryLng = 0;
  295.   do
  296.   {
  297.     queryPos++;
  298.   } while (netbuf[queryPos] != 0);
  299.  
  300.   queryPos = queryPos + 7; // Skip to answer data
  301.   do
  302.   {
  303.     if (queryPos > sizeof(netbuf) - 11)
  304.     {
  305.       // clearStatus();
  306.       // printf(" Error quering DNS server[Buffer overrun]. ");
  307.       return 0;
  308.     }
  309.     queryType = netbuf[queryPos] * 256 + netbuf[queryPos + 1];
  310.     // printf("Query type (0x0001): %d\r\n", queryType);
  311.  
  312.     queryPos = queryPos + 8; // Skip to answer lenght
  313.  
  314.     queryLng = netbuf[queryPos] * 256 + netbuf[queryPos + 1];
  315.     // printf("Query data lenght: %d\r\n", queryLng);
  316.     queryPos = queryPos + queryLng + 4;
  317.   } while (queryType != 1);
  318.  
  319.   targetadr.b1 = netbuf[queryPos - 6];
  320.   targetadr.b2 = netbuf[queryPos - 5];
  321.   targetadr.b3 = netbuf[queryPos - 4];
  322.   targetadr.b4 = netbuf[queryPos - 3];
  323.  
  324.   // printf("\r\nAddress:%u.%u.%u.%u:%u\r\n", targetadr.b1, targetadr.b2, targetadr.b3, targetadr.b4, targetadr.porth * 256 + targetadr.portl);
  325.   return 1;
  326. }
  327.  
  328. void get_dns(void)
  329. {
  330.   unsigned char ipaddress[4];
  331.   OS_GETDNS(ipaddress);
  332.   dnsaddress.family = AF_INET;
  333.   dnsaddress.porth = 00;
  334.   dnsaddress.portl = 53;
  335.   dnsaddress.b1 = ipaddress[0];
  336.   dnsaddress.b2 = ipaddress[1];
  337.   dnsaddress.b3 = ipaddress[2];
  338.   dnsaddress.b4 = ipaddress[3];
  339. }