Subversion Repositories NedoOS

Rev

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