Subversion Repositories NedoOS

Rev

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