?login_element?

Subversion Repositories NedoOS

Rev

Rev 2210 | Blame | Compare with Previous | Last modification | View Log | Download

  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.   const 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(const char *process, int socket)
  84. {
  85.   if (socket < 0)
  86.   {
  87.     printf("%s: [ERROR:", process);
  88.     errorPrint(-socket);
  89.     printf("]\r\n");
  90.     YIELD();
  91.     exit(0);
  92.   }
  93. }
  94.  
  95. char OpenSock(unsigned char family, unsigned char protocol)
  96. {
  97.   signed char socket;
  98.   unsigned int todo;
  99.   todo = OS_NETSOCKET((family << 8) + protocol);
  100.   if (todo > 32767)
  101.   {
  102.     return 0 - (todo & 255);
  103.   }
  104.   else
  105.   {
  106.     socket = ((todo & 65280) >> 8);
  107.     // printf("OS_NETSOCKET: Socket #%d created\n\r", socket);
  108.   }
  109.   return socket;
  110. }
  111.  
  112. char netShutDown(signed char socket, unsigned char type)
  113. {
  114.   unsigned int todo;
  115.   todo = OS_NETSHUTDOWN(socket, type);
  116.   if (todo > 32767)
  117.   {
  118.     return 0 - (todo & 255);
  119.   }
  120.   else
  121.   {
  122.     // printf("Socket #%d closed.\n\r", socket);
  123.   }
  124.   return socket;
  125. }
  126.  
  127. char netConnect(signed char socket, unsigned char retry)
  128. {
  129.   unsigned int todo = 0;
  130.  
  131.   while (retry != 0)
  132.   {
  133.     todo = OS_NETCONNECT(socket, &targetadr);
  134.  
  135.     if (todo > 32767)
  136.     {
  137.       retry--;
  138.       delayLong(500);
  139.       netShutDown(socket, 0);
  140.       socket = OpenSock(AF_INET, SOCK_STREAM);
  141.       testOperation("OS_NETSOCKET", socket);
  142.     }
  143.     else
  144.     {
  145.       // printf("OS_NETCONNECT: connection successful, %u\n\r", (todo & 255));
  146.       return socket;
  147.     }
  148.   }
  149.   return 0 - (todo & 255);
  150. }
  151.  
  152. int tcpSend(signed char socket, unsigned int messageadr, unsigned int size, unsigned char retry)
  153. {
  154.   unsigned int todo = 0;
  155.   readStruct.socket = socket;
  156.   readStruct.BufAdr = messageadr;
  157.   readStruct.bufsize = size;
  158.   readStruct.protocol = SOCK_STREAM;
  159.   while (retry != 0)
  160.   {
  161.     todo = OS_WIZNETWRITE(&readStruct);
  162.     if (todo > 32767)
  163.     {
  164.       retry--;
  165.       delayLong(500);
  166.     }
  167.     else
  168.     {
  169.       // printf("OS_WIZNETWRITE: %u bytes written. \n\r", todo);
  170.       return todo;
  171.     }
  172.   }
  173.   return 0 - (todo & 255);
  174. }
  175.  
  176. int tcpRead(signed char socket, unsigned char retry)
  177. {
  178.   unsigned int todo = 0;
  179.  
  180.   readStruct.socket = socket;
  181.   readStruct.BufAdr = (unsigned int)&netbuf;
  182.   readStruct.bufsize = sizeof(netbuf);
  183.   readStruct.protocol = SOCK_STREAM;
  184.  
  185.   while (retry != 0)
  186.   {
  187.     todo = OS_WIZNETREAD(&readStruct);
  188.  
  189.     if (todo > 32767)
  190.     {
  191.       if ((todo & 255) != ERR_EAGAIN) // nodata
  192.       {
  193.         retry--;
  194.         delayLong(500);
  195.       }
  196.     }
  197.     else
  198.     {
  199.       // printf("OS_WIZNETREAD: %u bytes read. \n\r", todo);
  200.       return todo; // succes
  201.     }
  202.   }
  203.   return 0 - (todo & 255); // timeout
  204. }
  205.  
  206. unsigned char dnsResolve(const char *domainName)
  207. {
  208.   unsigned char socket, retry;
  209.   unsigned int todo, queryPos, queryType, domainLng, comaCount, reqSize;
  210.   unsigned int loop;
  211.  
  212.   unsigned char dnsQuery1[] = {0x11, 0x22, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  213.   unsigned char dnsQuery2[] = {0x00, 0x00, 0x01, 0x00, 0x01};
  214.  
  215.   domainLng = strlen(domainName);
  216.   comaCount = 0;
  217.   loop = domainLng;
  218.   cmd[loop + 1] = 0;
  219.  
  220.   do
  221.   {
  222.     if (domainName[loop - 1] == '.')
  223.     {
  224.       cmd[loop] = comaCount;
  225.       comaCount = 0;
  226.     }
  227.     else
  228.     {
  229.       cmd[loop] = domainName[loop - 1];
  230.       comaCount++;
  231.     }
  232.     loop--;
  233.   } while (loop != 0);
  234.   cmd[0] = comaCount;
  235.  
  236.   memcpy(netbuf, dnsQuery1, sizeof(dnsQuery1));
  237.   memcpy(netbuf + sizeof(dnsQuery1), cmd, domainLng + 1);
  238.   memcpy(netbuf + domainLng + sizeof(dnsQuery1) + 1, dnsQuery2, sizeof(dnsQuery2));
  239.   reqSize = sizeof(dnsQuery1) + sizeof(dnsQuery2) + domainLng + 1;
  240.  
  241.   socket = OpenSock(AF_INET, SOCK_DGRAM);
  242.   readStruct.socket = socket;
  243.   readStruct.BufAdr = (unsigned int)&netbuf;
  244.   readStruct.bufsize = (unsigned int)reqSize;
  245.   readStruct.protocol = SOCK_DGRAM;
  246.  
  247.   todo = OS_WIZNETWRITE_UDP(&readStruct, &dnsaddress);
  248.   if (todo > 32767)
  249.   {
  250.     errorPrint(todo & 255);
  251.     return 0;
  252.   }
  253.   else
  254.   {
  255.     // printf("OS_WIZNETWRITE_UDP: %u bytes written. \n\r", todo);
  256.   }
  257.  
  258.   readStruct.BufAdr = (unsigned int)&netbuf;
  259.   readStruct.bufsize = (unsigned int)sizeof(netbuf);
  260.   retry = 10;
  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.         netShutDown(socket, 0);
  272.         return 0;
  273.       }
  274.       retry--;
  275.       delayLong(200);
  276.       // printf(" Retry [%d]\r\n", retryInv - retry);
  277.     }
  278.   } while (todo > 32767);
  279.  
  280.   netShutDown(socket, 0);
  281.  
  282.   if (!(netbuf[2] && 0x0f))
  283.   {
  284.     // clearStatus();
  285.     // printf(" Error quering[Parsing] DNS server.");
  286.     return 0;
  287.   }
  288.  
  289.   queryPos = 11;
  290.   do
  291.   {
  292.     queryPos++;
  293.   } while (netbuf[queryPos] != 0);
  294.  
  295.   queryPos = queryPos + 7; // Skip to answer data
  296.   do
  297.   {
  298.   unsigned int queryLng;
  299.     if (queryPos > sizeof(netbuf) - 11)
  300.     {
  301.       // clearStatus();
  302.       // printf(" Error quering DNS server[Buffer overrun]. ");
  303.       return 0;
  304.     }
  305.     queryType = netbuf[queryPos] * 256 + netbuf[queryPos + 1];
  306.     // printf("Query type (0x0001): %d\r\n", queryType);
  307.  
  308.     queryPos = queryPos + 8; // Skip to answer lenght
  309.  
  310.     queryLng = netbuf[queryPos] * 256 + netbuf[queryPos + 1];
  311.     // printf("Query data lenght: %d\r\n", queryLng);
  312.     queryPos = queryPos + queryLng + 4;
  313.   } while (queryType != 1);
  314.  
  315.   targetadr.b1 = netbuf[queryPos - 6];
  316.   targetadr.b2 = netbuf[queryPos - 5];
  317.   targetadr.b3 = netbuf[queryPos - 4];
  318.   targetadr.b4 = netbuf[queryPos - 3];
  319.  
  320.   // printf("\r\nAddress:%u.%u.%u.%u:%u\r\n", targetadr.b1, targetadr.b2, targetadr.b3, targetadr.b4, targetadr.porth * 256 + targetadr.portl);
  321.   return 1;
  322. }
  323.  
  324. void get_dns(void)
  325. {
  326.   unsigned char ipaddress[4];
  327.   OS_GETDNS(ipaddress);
  328.   dnsaddress.family = AF_INET;
  329.   dnsaddress.porth = 00;
  330.   dnsaddress.portl = 53;
  331.   dnsaddress.b1 = ipaddress[0];
  332.   dnsaddress.b2 = ipaddress[1];
  333.   dnsaddress.b3 = ipaddress[2];
  334.   dnsaddress.b4 = ipaddress[3];
  335. }