inet.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  1. /*
  2. * Copyright STREAMIT BV, 2010.
  3. *
  4. * Project : SIR
  5. * Module : Inet
  6. * File name $Workfile: Inet.c $
  7. * Last Save $Date: 2006/02/24 13:46:16 $
  8. * $Revision: 0.1 $
  9. * Creation Date : 2006/02/24 13:46:16
  10. *
  11. * Description :
  12. *
  13. */
  14. #define LOG_MODULE LOG_INET_MODULE
  15. /*--------------------------------------------------------------------------*/
  16. /* Include files */
  17. /*--------------------------------------------------------------------------*/
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <ctype.h>
  22. #include <sys/timer.h>
  23. #include <sys/socket.h>
  24. #include <sys/heap.h>
  25. #include <netinet/tcp.h>
  26. #include <arpa/inet.h>
  27. #include <errno.h>
  28. //#pragma text:appcode
  29. #include "system.h"
  30. #include "version.h"
  31. #include "log.h"
  32. #include "settings.h"
  33. #include "util.h"
  34. #include "inet.h"
  35. /*--------------------------------------------------------------------------*/
  36. /* Constant definitions */
  37. /*--------------------------------------------------------------------------*/
  38. /*!\brief Allow some problems before giving up */
  39. //#define MAX_NODNS 5
  40. #define MAX_NODNS 2
  41. #define MAX_NOCONNECT 2
  42. #define MAX_BADRESPONSE 5
  43. #define MAX_REDIRECT 5 /* see RFC2616 */
  44. /*!\brief Default Receive timeout */
  45. #define TCP_RECVTO_DEFAULT 5000
  46. /*!\brief HTTP line buffer size. Allocates in chunks of this size. */
  47. #define HTTP_HEADER_LINE_SIZE 512
  48. #ifdef DEBUG
  49. //#define INET_DEBUG
  50. #endif /* #ifdef DEBUG */
  51. /*--------------------------------------------------------------------------*/
  52. /* Type declarations */
  53. /*--------------------------------------------------------------------------*/
  54. /*--------------------------------------------------------------------------*/
  55. /* Local variables */
  56. /*--------------------------------------------------------------------------*/
  57. /*--------------------------------------------------------------------------*/
  58. /* Global variables */
  59. /*--------------------------------------------------------------------------*/
  60. /*--------------------------------------------------------------------------*/
  61. /* Local functions */
  62. /*--------------------------------------------------------------------------*/
  63. static TError Connect(HINET hInet);
  64. static void GetHeaders(HINET hInet);
  65. static int CreateRequest(HINET hInet, CONST char *pszMethod, CONST char *pszPath, CONST char *pszAccept);
  66. static void CloseDescriptors(HINET hInet);
  67. #ifdef INET_DEBUG
  68. static void ShowDebug(void)
  69. {
  70. LogMsg_P(LOG_DEBUG, PSTR("free %d"), NutHeapAvailable());
  71. }
  72. #else
  73. #define ShowDebug()
  74. #endif
  75. /*!
  76. * \brief Opens an Internet session.
  77. *
  78. * \param hInet [in] Handle returned by a previous call to InternetOpen.
  79. *
  80. * \return 0 if the connection is successful
  81. * TError otherwise.
  82. */
  83. static TError Connect(HINET hInet)
  84. {
  85. TError tError = OK;
  86. unsigned char byDone = 0;
  87. char ModeString[5];
  88. /*
  89. * Connect to the server. Retry in case of problems
  90. */
  91. while (byDone == 0)
  92. {
  93. if (hInet->tState == INET_STATE_CLOSING)
  94. {
  95. tError = USER_ABORT;
  96. }
  97. if (tError == OK)
  98. {
  99. hInet->tState = INET_STATE_BUSY;
  100. }
  101. /*
  102. * Translate to an IP number and port
  103. */
  104. if (tError == OK)
  105. {
  106. if ((hInet->wPort = atoi(hInet->tUrlParts.pszPort)) == 0)
  107. {
  108. // Use defaults if not specified
  109. hInet->wPort = 80;
  110. }
  111. LogMsg_P(LOG_DEBUG, PSTR("Looking up [%s]"), hInet->tUrlParts.pszHost);
  112. if ((hInet->ulIpAddress = GetHostByName(hInet->tUrlParts.pszHost)) == 0)
  113. {
  114. tError = INET_HOSTNOTFOUND;
  115. /*
  116. * Check if we can retry
  117. */
  118. if (++hInet->tRetries.byNoDnsCount >= MAX_NODNS)
  119. {
  120. /* Too many failures, stop */
  121. tError = INET_HOST_NONEXISTANT;
  122. }
  123. }
  124. }
  125. /*
  126. * We could have been asleep; Check if we have received a close request
  127. */
  128. if (hInet->tState == INET_STATE_CLOSING)
  129. {
  130. tError = USER_ABORT;
  131. }
  132. /*
  133. * Create a socket.
  134. */
  135. ShowDebug();
  136. if (tError == OK)
  137. {
  138. if ((hInet->ptSocket = NutTcpCreateSocket()) == 0)
  139. {
  140. tError = INET_CREATE_SOCKET;
  141. }
  142. }
  143. #ifdef INET_DEBUG
  144. LogMsg_P(LOG_DEBUG, PSTR("ptSocket @%X"), hInet->ptSocket);
  145. #endif /* #ifdef INET_DEBUG */
  146. ShowDebug();
  147. /*
  148. * Set socket options
  149. */
  150. if (tError == OK)
  151. {
  152. /* We use our own default if not specified */
  153. if (hInet->ulRecvTimeout == 0)
  154. {
  155. hInet->ulRecvTimeout = TCP_RECVTO_DEFAULT;
  156. }
  157. /* Nut/OS defaults to infinite receive timeout. So always set our default timeout */
  158. if (NutTcpSetSockOpt(hInet->ptSocket, SO_RCVTIMEO, &hInet->ulRecvTimeout, sizeof(hInet->ulRecvTimeout)))
  159. {
  160. tError = INET_SOCK_RCVTO;
  161. }
  162. }
  163. if (tError == OK)
  164. {
  165. /* Use NutOS's default if not specified */
  166. if ((hInet->unMss != 0) &&
  167. (NutTcpSetSockOpt(hInet->ptSocket, TCP_MAXSEG, &hInet->unMss, sizeof(hInet->unMss))))
  168. {
  169. tError = INET_SOCK_MSS;
  170. }
  171. }
  172. if (tError == OK)
  173. {
  174. /* Use NutOS's default if not specified */
  175. if ((hInet->unTcpRecvBufSize != 0) &&
  176. (NutTcpSetSockOpt(hInet->ptSocket, SO_RCVBUF, &hInet->unTcpRecvBufSize, sizeof(hInet->unTcpRecvBufSize))))
  177. {
  178. tError = INET_SOCK_RXBUF;
  179. }
  180. }
  181. /*
  182. * Connect to destination
  183. */
  184. if (tError == OK)
  185. {
  186. LogMsg_P(LOG_DEBUG, PSTR("Connecting to %s:%d"), inet_ntoa(hInet->ulIpAddress), hInet->wPort);
  187. if (NutTcpConnect(hInet->ptSocket, hInet->ulIpAddress, hInet->wPort) != 0)
  188. {
  189. tError = INET_NOCONNECT;
  190. LogMsg_P(LOG_ERR, PSTR("No connect"));
  191. if (++hInet->tRetries.byNoConnectCount >= MAX_NOCONNECT)
  192. {
  193. tError = INET_TOO_MANY_NOCONNECTS;
  194. }
  195. }
  196. else
  197. {
  198. /* Connected, stop */
  199. byDone = 1;
  200. LogMsg_P(LOG_DEBUG, PSTR("TCP Connected"));
  201. /* Let the TCP/IP stack settle down first */
  202. NutSleep(500);
  203. }
  204. }
  205. /*
  206. * We could have been asleep; Check if we have receive a close request
  207. */
  208. if (hInet->tState == INET_STATE_CLOSING)
  209. {
  210. tError = USER_ABORT;
  211. }
  212. /*
  213. * Create a stream from the socket.
  214. */
  215. if (tError == OK)
  216. {
  217. strcpy_P(ModeString, PSTR("r+b"));
  218. if ((hInet->ptStream = _fdopen((int)hInet->ptSocket, ModeString)) == 0)
  219. {
  220. LogMsg_P(LOG_ERR, PSTR("No stream %d"), errno);
  221. tError = INET_CREATE_STREAM;
  222. }
  223. }
  224. #ifdef INET_DEBUG
  225. LogMsg_P(LOG_DEBUG, PSTR("ptStream @%X"), hInet->ptStream);
  226. #endif /* #ifdef INET_DEBUG */
  227. if (tError != OK)
  228. {
  229. LogMsg_P(LOG_ERR, PSTR("Error [%d]"), tError);
  230. /*
  231. * Check if we need to try again
  232. */
  233. if ((tError > PLAYER_WARNINGS) && (tError < PLAYER_ERRORS))
  234. {
  235. LogMsg_P(LOG_INFO, PSTR("Retry"));
  236. CloseDescriptors(hInet);
  237. /*
  238. * Try again
  239. */
  240. tError = OK;
  241. /*
  242. * Give other threads some time before
  243. * we try again
  244. */
  245. NutSleep(300);
  246. }
  247. else
  248. {
  249. /* Errors, stop */
  250. byDone = 1;
  251. }
  252. }
  253. } /* end while */
  254. ShowDebug();
  255. hInet->tState = INET_STATE_IDLE;
  256. return (tError);
  257. }
  258. /*!
  259. * \brief Create a new request to be sent to an Internet server.
  260. *
  261. * hInet->hRequest should already have allocated memory behind it, as this
  262. * routine does not allocate (or free) memory.
  263. *
  264. * \param hInet [in] Handle returned by a previous call to InternetOpen.
  265. * \param pszMethod [in] A pointer to a null-terminated string that contains
  266. * the method to use in the request. If this parameter is NULL, the
  267. * function uses GET.
  268. * \param pszPath [in] A pointer to a null-terminated string that contains
  269. * the path to act upon.
  270. * \param pszAccept[in] A pointer to a null-terminated string that indicates
  271. * the media types accepted by the client. If this parameter is NULL,
  272. * a string that indicates that all types are accepted is sent to the
  273. * server.
  274. *
  275. * \return 0 when the request was successfully created
  276. * -1 on errors
  277. */
  278. static int CreateRequest(HINET hInet, CONST char *pszMethod, CONST char *pszPath, CONST char *pszAccept)
  279. {
  280. static prog_char cszAction_P[] = "%s /%s%s HTTP/1.0\r\n";
  281. static prog_char cszUserAgent_P[] = "User-Agent: %s/%s s/n:%s\r\n";
  282. // create buffers for RAM-strings...
  283. static char szEmptyString[1];
  284. static char szGet[4];
  285. static char szSerialNum[9];
  286. static char szAcceptDefault[20];
  287. int nResult = 0;
  288. char *pszSerialNr;
  289. // apply defaults for RAM-strings....
  290. szEmptyString[0] = '\0';
  291. szSerialNum[0] = '\0';
  292. strcpy_P(szGet, PSTR("GET"));
  293. strcpy_P(szAcceptDefault, PSTR("Accept: */*\r\n"));
  294. pszSerialNr = szEmptyString;
  295. #ifdef INET_DEBUG
  296. LogMsg_P(LOG_DEBUG, PSTR("Create request"));
  297. #endif /* #ifdef INET_DEBUG */
  298. if ((hInet == NULL) || (hInet->hRequest == NULL))
  299. {
  300. /* Bad argument */
  301. nResult = -1;
  302. }
  303. if (nResult >= 0)
  304. {
  305. /*
  306. * Erase any previous requests
  307. */
  308. hInet->hRequest->unRequestInUse = 0;
  309. hInet->hRequest->pszRequest[0] = '\0';
  310. /*
  311. * Use defaults for the method and Uri if not further specified
  312. */
  313. if (pszMethod == NULL)
  314. {
  315. pszMethod = szGet;
  316. }
  317. if (pszPath == NULL)
  318. {
  319. pszPath = hInet->tUrlParts.pszPath;
  320. }
  321. //sprintf_P(szSerialNum, PSTR("%5.5lX"), SettingsGetSerialnumber());
  322. }
  323. /*
  324. * Check if we need to add our serial number to the end of the URL
  325. */
  326. if (nResult >= 0)
  327. {
  328. if ((hInet->hRequest->wOptions & INET_FLAG_ADD_SERIAL) == INET_FLAG_ADD_SERIAL)
  329. {
  330. if (strlen(pszPath) > 0)
  331. {
  332. if (pszPath[strlen(pszPath)-1] == '=')
  333. {
  334. pszSerialNr = szSerialNum;
  335. }
  336. }
  337. }
  338. }
  339. /*
  340. * Create the request
  341. */
  342. if (nResult >= 0)
  343. {
  344. nResult = sprintf_P(&hInet->hRequest->pszRequest[hInet->hRequest->unRequestInUse],
  345. cszAction_P,
  346. pszMethod,
  347. pszPath,
  348. pszSerialNr);
  349. if (nResult >= 0)
  350. {
  351. #ifdef INET_DEBUG
  352. /* We already print out the request at the end of this routine */
  353. #else /* #ifdef INET_DEBUG */
  354. // LogMsg_P(LOG_DEBUG, PSTR("Request [%s]"), &hInet->hRequest->pszRequest[hInet->hRequest->unRequestInUse]);
  355. #endif /* #ifdef INET_DEBUG */
  356. hInet->hRequest->unRequestInUse += nResult;
  357. if (hInet->hRequest->unRequestInUse > hInet->hRequest->unRequestBufSize)
  358. {
  359. nResult = -1;
  360. }
  361. }
  362. }
  363. /*
  364. * Add the User-Agent
  365. */
  366. if (nResult >= 0)
  367. {
  368. //nResult = sprintf_P(&hInet->hRequest->pszRequest[hInet->hRequest->unRequestInUse],
  369. // cszUserAgent_P,
  370. //VersionGetAppProductName(),
  371. //VersionGetAppString(),
  372. // szSerialNum);
  373. if (nResult >= 0)
  374. {
  375. hInet->hRequest->unRequestInUse += nResult;
  376. if (hInet->hRequest->unRequestInUse > hInet->hRequest->unRequestBufSize)
  377. {
  378. nResult = -1;
  379. }
  380. }
  381. }
  382. /*
  383. * Add the host header if needed
  384. */
  385. if (nResult >= 0)
  386. {
  387. if ((hInet->tUrlParts.pszHost != NULL) && (strlen(hInet->tUrlParts.pszHost) > 0))
  388. {
  389. if ((hInet->tUrlParts.pszPort != NULL) && (strlen(hInet->tUrlParts.pszPort) > 0))
  390. {
  391. nResult = sprintf_P(&hInet->hRequest->pszRequest[hInet->hRequest->unRequestInUse],
  392. PSTR("Host: %s:%s\r\n"),
  393. hInet->tUrlParts.pszHost,
  394. hInet->tUrlParts.pszPort);
  395. }
  396. else
  397. {
  398. nResult = sprintf_P(&hInet->hRequest->pszRequest[hInet->hRequest->unRequestInUse],
  399. PSTR("Host: %s\r\n"),
  400. hInet->tUrlParts.pszHost);
  401. }
  402. if (nResult >= 0)
  403. {
  404. hInet->hRequest->unRequestInUse += nResult;
  405. if (hInet->hRequest->unRequestInUse > hInet->hRequest->unRequestBufSize)
  406. {
  407. nResult = -1;
  408. }
  409. }
  410. }
  411. }
  412. /*
  413. * Add the accept header
  414. */
  415. if (nResult >= 0)
  416. {
  417. if (pszAccept == NULL)
  418. {
  419. pszAccept = szAcceptDefault;
  420. }
  421. nResult = sprintf(&hInet->hRequest->pszRequest[hInet->hRequest->unRequestInUse],
  422. szAcceptDefault,
  423. pszAccept);
  424. if (nResult >= 0)
  425. {
  426. hInet->hRequest->unRequestInUse += nResult;
  427. if (hInet->hRequest->unRequestInUse > hInet->hRequest->unRequestBufSize)
  428. {
  429. nResult = -1;
  430. }
  431. }
  432. }
  433. /*
  434. * Check if we need to do a request for ICY meta data
  435. */
  436. if (nResult >= 0)
  437. {
  438. if ((hInet->hRequest->wOptions & INET_FLAG_ICY_META_REQ) == INET_FLAG_ICY_META_REQ)
  439. {
  440. nResult = sprintf_P(&hInet->hRequest->pszRequest[hInet->hRequest->unRequestInUse],
  441. PSTR("Icy-MetaData:1\r\n"));
  442. if (nResult >= 0)
  443. {
  444. hInet->hRequest->unRequestInUse += nResult;
  445. if (hInet->hRequest->unRequestInUse > hInet->hRequest->unRequestBufSize)
  446. {
  447. nResult = -1;
  448. }
  449. }
  450. }
  451. }
  452. /*
  453. * Check if we need to close the connection
  454. */
  455. if (nResult >= 0)
  456. {
  457. if ((hInet->hRequest->wOptions & INET_FLAG_CLOSE) == INET_FLAG_CLOSE)
  458. {
  459. nResult = sprintf_P(&hInet->hRequest->pszRequest[hInet->hRequest->unRequestInUse],
  460. PSTR("Connection: close\r\n"));
  461. if (nResult >= 0)
  462. {
  463. hInet->hRequest->unRequestInUse += nResult;
  464. if (hInet->hRequest->unRequestInUse > hInet->hRequest->unRequestBufSize)
  465. {
  466. nResult = -1;
  467. }
  468. }
  469. }
  470. }
  471. /* Correct lenght in use for last \0 */
  472. if (hInet->hRequest->unRequestInUse > 0)
  473. {
  474. hInet->hRequest->unRequestInUse += 1;
  475. }
  476. /*
  477. * Log the request
  478. */
  479. if (nResult >= 0)
  480. {
  481. //#ifdef INET_DEBUG
  482. LogMsg_P(LOG_DEBUG, PSTR("Request %u [%s]"), hInet->hRequest->unRequestInUse, hInet->hRequest->pszRequest);
  483. //#endif /* #ifdef INET_DEBUG */
  484. }
  485. return (nResult);
  486. }
  487. /*!
  488. * \brief Get the HTTP response headers.
  489. *
  490. * This function returns after all response headers have been
  491. * received.
  492. *
  493. * \param hInet [in] Handle returned by a previous call to InternetOpen.
  494. *
  495. * \return -
  496. */
  497. static void GetHeaders(HINET hInet)
  498. {
  499. unsigned char byDone = 0;
  500. /*
  501. * Create room for buffers
  502. */
  503. char *pszRespLine = MyMalloc(HTTP_HEADER_LINE_SIZE);
  504. if (pszRespLine == NULL)
  505. {
  506. /* No memory */
  507. byDone = 1;
  508. }
  509. ShowDebug();
  510. if (hInet->hRequest == NULL)
  511. {
  512. /* Bad argument */
  513. byDone = 1;
  514. }
  515. else
  516. {
  517. /* Reset received counter */
  518. hInet->hRequest->unResponseInUse = 0;
  519. }
  520. /*
  521. * Process all header lines
  522. */
  523. while ((byDone == 0) &&
  524. (fgets(pszRespLine, HTTP_HEADER_LINE_SIZE, hInet->ptStream) != NULL))
  525. {
  526. /*
  527. * We could have been asleep; Check if we have received a close request
  528. */
  529. if (hInet->tState == INET_STATE_CLOSING)
  530. {
  531. byDone = 1;
  532. }
  533. else if ((pszRespLine[0] == '\r') && (pszRespLine[1] == '\n'))
  534. {
  535. /*
  536. * An empty line indicates the end of the headers
  537. */
  538. byDone = 1;
  539. }
  540. else
  541. {
  542. /*
  543. * Log the line (without the end of line stuff)
  544. */
  545. unsigned int unLength = 0;
  546. char *pszLogEol = strchr(pszRespLine, '\r');
  547. if (pszLogEol != NULL)
  548. {
  549. unLength = pszLogEol - pszRespLine;
  550. }
  551. LogMsg_P(LOG_DEBUG, PSTR("Read [%.*s]"), unLength, pszRespLine);
  552. if (BufferAddString(&hInet->hRequest->pszResponse,
  553. &hInet->hRequest->unResponseBufSize,
  554. &hInet->hRequest->unResponseInUse,
  555. pszRespLine) != 0)
  556. {
  557. byDone = 1;
  558. }
  559. }
  560. }
  561. ShowDebug();
  562. #ifdef INET_DEBUG
  563. LogMsg_P(LOG_DEBUG, PSTR("%d Read"), hInet->hRequest->unResponseInUse);
  564. #endif /* #ifdef INET_DEBUG */
  565. /*
  566. * Cleanup
  567. */
  568. MyFree(pszRespLine);
  569. }
  570. /*!
  571. * \brief Close the socket and file descriptors of an INET handle.
  572. *
  573. * \param hInet [in] Handle returned by a previous call to InternetOpen.
  574. *
  575. * \return -
  576. */
  577. static void CloseDescriptors(HINET hInet)
  578. {
  579. if (hInet != NULL)
  580. {
  581. /*
  582. * Close the stream and the connection
  583. */
  584. if (hInet->ptStream != NULL)
  585. {
  586. (void)fclose(hInet->ptStream);
  587. hInet->ptStream = NULL;
  588. }
  589. if (hInet->ptSocket != NULL)
  590. {
  591. (void)NutTcpCloseSocket(hInet->ptSocket);
  592. hInet->ptSocket = NULL;
  593. }
  594. }
  595. }
  596. /*--------------------------------------------------------------------------*/
  597. /* Global functions */
  598. /*--------------------------------------------------------------------------*/
  599. HINET InetOpen(void)
  600. {
  601. HINET hInet = NULL;
  602. ShowDebug();
  603. hInet = (HINET)MyMalloc(sizeof(INET));
  604. if (hInet != NULL)
  605. {
  606. // LogMsg_P(LOG_DEBUG, PSTR("Open %X"), hInet);
  607. memset(hInet, 0, sizeof(INET));
  608. hInet->tState = INET_STATE_IDLE;
  609. }
  610. return (hInet);
  611. }
  612. TError InetConnect(HINET hInet, CONST char *pszUrl, unsigned long ulRecvTimeout, unsigned int unMss, unsigned int unTcpRecvBufSize)
  613. {
  614. TError tError = OK;
  615. ShowDebug();
  616. /*
  617. * Parse the Url
  618. */
  619. if (tError == OK)
  620. {
  621. hInet->pszUrl = strdup(pszUrl);
  622. if (hInet->pszUrl != NULL)
  623. {
  624. HttpParseUrl(hInet->pszUrl, &hInet->tUrlParts);
  625. }
  626. else
  627. {
  628. tError = INET_NOMEM;
  629. }
  630. }
  631. if (tError == OK)
  632. {
  633. /* Store the connect parameters */
  634. hInet->ulRecvTimeout = ulRecvTimeout;
  635. hInet->unMss = unMss;
  636. hInet->unTcpRecvBufSize = unTcpRecvBufSize;
  637. /* Reset the problem counters */
  638. memset(&hInet->tRetries, 0, sizeof(hInet->tRetries));
  639. tError = Connect(hInet);
  640. }
  641. return (tError);
  642. }
  643. TError InetHttpOpenRequest(HINET hInet, CONST char *pszMethod, CONST char *pszPath, CONST char *pszAccept, unsigned short wOptions)
  644. {
  645. const unsigned int cunReqBufSize = 256;
  646. TError tError = OK;
  647. ShowDebug();
  648. if (hInet == NULL)
  649. {
  650. /* Bad argument */
  651. tError = INET_NOMEM;
  652. }
  653. /*
  654. * Create the request
  655. */
  656. if (tError == OK)
  657. {
  658. if (hInet->hRequest == NULL)
  659. {
  660. hInet->hRequest = (HINETREQ)MyMalloc(sizeof(INETREQ));
  661. if (hInet->hRequest == NULL)
  662. {
  663. /* No memory */
  664. tError = INET_NOMEM;
  665. }
  666. else
  667. {
  668. memset(hInet->hRequest, 0, sizeof(INETREQ));
  669. }
  670. }
  671. }
  672. /*
  673. * Create the request buffer
  674. */
  675. if (tError == OK)
  676. {
  677. if (hInet->hRequest->pszRequest == NULL)
  678. {
  679. /* Allocate the request buffer */
  680. hInet->hRequest->pszRequest = MyMalloc(cunReqBufSize);
  681. if (hInet->hRequest->pszRequest == NULL)
  682. {
  683. /* No memory */
  684. tError = INET_NOMEM;
  685. }
  686. else
  687. {
  688. hInet->hRequest->unRequestBufSize = cunReqBufSize;
  689. }
  690. }
  691. }
  692. #ifdef INET_DEBUG
  693. LogMsg_P(LOG_DEBUG, PSTR("hInet @%X"), hInet);
  694. LogMsg_P(LOG_DEBUG, PSTR("hRequest @%X"), hInet->hRequest);
  695. #endif /* #ifdef INET_DEBUG */
  696. /*
  697. * Create the actual request
  698. */
  699. if (tError == OK)
  700. {
  701. int nResult = 0;
  702. /* Store the requested options */
  703. hInet->hRequest->wOptions = wOptions;
  704. /* Create the request */
  705. nResult = CreateRequest(hInet, pszMethod, pszPath, pszAccept);
  706. if (nResult < 0)
  707. {
  708. tError = INET_NOMEM;
  709. }
  710. }
  711. ShowDebug();
  712. return (tError);
  713. }
  714. int InetHttpAddRequestHeaders(HINET hInet, CONST char *pszNewHeaders)
  715. {
  716. int nResult = 0;
  717. ShowDebug();
  718. if ((hInet == NULL) || (hInet->hRequest == NULL))
  719. {
  720. /* Bad argument */
  721. nResult = -1;
  722. }
  723. if (nResult >= 0)
  724. {
  725. nResult = BufferAddString(&hInet->hRequest->pszRequest,
  726. &hInet->hRequest->unRequestBufSize,
  727. &hInet->hRequest->unRequestInUse,
  728. pszNewHeaders);
  729. }
  730. #ifdef INET_DEBUG
  731. LogMsg_P(LOG_DEBUG, PSTR("Request %u [%s]"), hInet->hRequest->unRequestInUse, hInet->hRequest->pszRequest);
  732. #endif /* #ifdef INET_DEBUG */
  733. ShowDebug();
  734. return (nResult);
  735. }
  736. TError InetHttpSendRequest(HINET hInet)
  737. {
  738. TError tError = OK;
  739. unsigned char byDone = 0;
  740. unsigned char byRedirectCount = 0;
  741. int nHeaderNumber;
  742. long lResponseCode;
  743. void *plResponseCode;
  744. unsigned int unInfoSize;
  745. /*
  746. * Talk to the server and parse its reponse
  747. */
  748. while (byDone == 0)
  749. {
  750. int nResult = +1;
  751. int nResponse = -1;
  752. ShowDebug();
  753. if (hInet->tState == INET_STATE_CLOSING)
  754. {
  755. tError = USER_ABORT;
  756. }
  757. if (tError == OK)
  758. {
  759. hInet->tState = INET_STATE_BUSY;
  760. }
  761. /*
  762. * Send the request
  763. */
  764. if (tError == OK)
  765. {
  766. nResult = HttpSendRequest(hInet->ptStream, hInet->hRequest->pszRequest, hInet->hRequest->wHttpMode);
  767. if (nResult < 0)
  768. {
  769. tError = INET_SEND_FAIL;
  770. }
  771. LogMsg_P(LOG_DEBUG, PSTR("Sent %d"), nResult);
  772. }
  773. /*
  774. * We could have been asleep; Check if we still need to play
  775. */
  776. if (hInet->tState == INET_STATE_CLOSING)
  777. {
  778. tError = USER_ABORT;
  779. }
  780. /*
  781. * Get the response
  782. */
  783. if (tError == OK)
  784. {
  785. ShowDebug();
  786. GetHeaders(hInet);
  787. nHeaderNumber = 0;
  788. lResponseCode = 0;
  789. plResponseCode = &lResponseCode;
  790. unInfoSize = sizeof(lResponseCode);
  791. nResult = InetHttpQueryInfo(hInet,
  792. INET_HTTP_QUERY_STATUS_CODE | INET_HTTP_QUERY_MOD_NUMERIC,
  793. &plResponseCode,
  794. &unInfoSize,
  795. &nHeaderNumber);
  796. if (nResult > 0)
  797. {
  798. nResponse = lResponseCode;
  799. }
  800. else
  801. {
  802. /* Unable to process the response */
  803. nResponse = -1;
  804. }
  805. ShowDebug();
  806. /*
  807. * We could have been asleep; Check if we still need to play
  808. */
  809. if (hInet->tState == INET_STATE_CLOSING)
  810. {
  811. tError = USER_ABORT;
  812. }
  813. }
  814. if (tError == OK)
  815. {
  816. /*
  817. * Check the response code
  818. */
  819. if (nResponse >= 100 && nResponse < 300)
  820. {
  821. /* Connected: we're done */
  822. LogMsg_P(LOG_INFO, PSTR("Connect [%d]"), nResponse);
  823. byDone = 1;
  824. }
  825. else if (nResponse >= 300 && nResponse < 400)
  826. {
  827. /* Redirect */
  828. LogMsg_P(LOG_INFO, PSTR("Redirect [%d]"), nResponse);
  829. tError = INET_REDIRECT;
  830. /*
  831. * Check if we can retry
  832. */
  833. if (++byRedirectCount >= MAX_REDIRECT)
  834. {
  835. /* Too many redirects, stop */
  836. tError = INET_TOO_MANY_REDIRECTS;
  837. }
  838. else
  839. {
  840. int nHeaderNumber = 0;
  841. char *pszLocation = NULL;
  842. void *ppszLocation = &pszLocation;
  843. unsigned int unSize = 0;
  844. /* Get size of the new location string */
  845. nResult = InetHttpQueryInfo(hInet,
  846. INET_HTTP_QUERY_LOCATION,
  847. ppszLocation,
  848. &unSize,
  849. &nHeaderNumber);
  850. /*
  851. * (Re)parse the newly received URL
  852. */
  853. if (nResult > 0)
  854. {
  855. MyFree(hInet->pszUrl);
  856. hInet->pszUrl = pszLocation;
  857. LogMsg_P(LOG_INFO, PSTR("To [%s]"), hInet->pszUrl);
  858. HttpParseUrl(hInet->pszUrl, &hInet->tUrlParts);
  859. nResult = CreateRequest(hInet, NULL, NULL, NULL);
  860. }
  861. if (nResult >= 0)
  862. {
  863. /*
  864. * Allow each server a maximum of problems :-)
  865. */
  866. memset(&hInet->tRetries, 0, sizeof(hInet->tRetries));
  867. }
  868. else
  869. {
  870. tError = INET_NOMEM;
  871. }
  872. }
  873. }
  874. else if (nResponse == 401)
  875. {
  876. /*
  877. * If we did not try authentication, try again with authentication.
  878. * If we already tried using authentication or shouldn't use it, give up
  879. */
  880. if (((hInet->hRequest->wHttpMode & HTTP_AUTH) != HTTP_AUTH) &&
  881. ((hInet->hRequest->wOptions & INET_FLAG_NO_AUTH) != INET_FLAG_NO_AUTH))
  882. {
  883. hInet->hRequest->wHttpMode |= HTTP_AUTH;
  884. tError = INET_ACCESS_RESTRICTED;
  885. }
  886. else
  887. {
  888. tError = INET_ACCESS_DENIED;
  889. LogMsg_P(LOG_CRIT, PSTR("Access denied"));
  890. }
  891. }
  892. else
  893. {
  894. /* Bad response or timeout */
  895. LogMsg_P(LOG_ERR, PSTR("Bad response [%d]"), nResponse);
  896. tError = INET_BADRESPONSE;
  897. if (++hInet->tRetries.byBadResponseCount >= MAX_BADRESPONSE)
  898. {
  899. /* Too many badresponses, stop */
  900. tError = INET_TOO_MANY_BADRESPONSES;
  901. }
  902. }
  903. }
  904. if (tError != OK)
  905. {
  906. LogMsg_P(LOG_ERR, PSTR("Error [%d]"), tError);
  907. /*
  908. * Check if we need to try again
  909. */
  910. if ((tError > PLAYER_WARNINGS) && (tError < PLAYER_ERRORS))
  911. {
  912. LogMsg_P(LOG_INFO, PSTR("Retry"));
  913. CloseDescriptors(hInet);
  914. /*
  915. * Try again
  916. */
  917. tError = Connect(hInet);
  918. /*
  919. * Give other threads some time before
  920. * we try again
  921. */
  922. NutSleep(300);
  923. }
  924. else
  925. {
  926. /* Errors, stop */
  927. byDone = 1;
  928. }
  929. }
  930. } /* end while */
  931. ShowDebug();
  932. hInet->tState = INET_STATE_IDLE;
  933. return (tError);
  934. }
  935. int InetHttpQueryInfo(HINET hInet, unsigned short wInfoLevel, void **pInfo, unsigned int *punInfoSize, int *pnIndex)
  936. {
  937. /* Status prefixes: */
  938. static prog_char cszHttpVer_P[] = "HTTP/";
  939. static prog_char cszIcy_P[] = "ICY";
  940. /* HTTP headers: */
  941. static prog_char cszLocation_P[] = "Location:";
  942. static prog_char cszContentLength_P[] = "Content-Length:";
  943. static prog_char cszContentType_P[] = "Content-Type:";
  944. /* ICY headers: */
  945. static prog_char cszIcyMetaData_P[] = "icy-metaint:";
  946. int nResult = 0;
  947. char *pszRespLine = hInet->hRequest->pszResponse;
  948. char *pszStart = pszRespLine;
  949. char *pszEnd = pszRespLine;
  950. unsigned int unResultSize = 0;
  951. if ((pInfo == NULL) || (punInfoSize == NULL) || (pszRespLine == NULL))
  952. {
  953. /* Bad argument */
  954. nResult = -1;
  955. }
  956. while ((nResult == 0) && (pszEnd != NULL))
  957. {
  958. /* Skip empty lines and whitepace */
  959. while (isspace(*pszRespLine))
  960. {
  961. pszRespLine++;
  962. }
  963. pszEnd = strchr(pszRespLine, '\r');
  964. if (pszEnd != NULL)
  965. {
  966. if ((wInfoLevel & INET_HTTP_QUERY_STATUS_CODE) == INET_HTTP_QUERY_STATUS_CODE)
  967. {
  968. int iProcessed = 0;
  969. /*
  970. * Check the response code in the first line
  971. */
  972. if (strncasecmp_P(pszRespLine, cszHttpVer_P, sizeof(cszHttpVer_P)-1) == 0)
  973. {
  974. hInet->hRequest->byProto = INET_PROTO_HTTP;
  975. iProcessed = sizeof(cszHttpVer_P)-1;
  976. }
  977. else if (strncasecmp_P(pszRespLine, cszIcy_P, sizeof(cszIcy_P)-1) == 0)
  978. {
  979. hInet->hRequest->byProto = INET_PROTO_ICY;
  980. iProcessed = sizeof(cszIcy_P)-1;
  981. }
  982. if (iProcessed)
  983. {
  984. nResult = 1;
  985. /* Skip version number */
  986. for (pszStart = pszRespLine + iProcessed; *pszStart != '\r' && *pszStart != ' '; pszStart++)
  987. {
  988. ;
  989. }
  990. if (*pszStart == '\r')
  991. {
  992. /* Could not find whitespace after the version number */
  993. nResult = -1;
  994. }
  995. else
  996. {
  997. /* Skip leading whitespace */
  998. for (; *pszStart == ' '; pszStart++)
  999. {
  1000. ;
  1001. }
  1002. /* Strip trailing whitespace */
  1003. for (; ((pszEnd > pszStart) && (*(pszEnd-1) == ' ')); pszEnd--)
  1004. {
  1005. ;
  1006. }
  1007. }
  1008. }
  1009. }
  1010. else if ((wInfoLevel & INET_HTTP_QUERY_LOCATION) == INET_HTTP_QUERY_LOCATION)
  1011. {
  1012. /*
  1013. * Get the new location (redirect URL)
  1014. */
  1015. if (strncasecmp_P(pszRespLine, cszLocation_P, sizeof(cszLocation_P)-1) == 0)
  1016. {
  1017. nResult = 1;
  1018. /* Skip leading whitespace */
  1019. for (pszStart = pszRespLine + sizeof(cszLocation_P)-1; (*pszStart == ' '); pszStart++)
  1020. {
  1021. ;
  1022. }
  1023. /* Strip trailing whitespace */
  1024. for (; ((pszEnd > pszStart) && (*(pszEnd-1) == ' ')); pszEnd--)
  1025. {
  1026. ;
  1027. }
  1028. }
  1029. }
  1030. else if ((wInfoLevel & INET_HTTP_QUERY_CONTENT_LENGTH) == INET_HTTP_QUERY_CONTENT_LENGTH)
  1031. {
  1032. /*
  1033. * Get the content-length
  1034. */
  1035. if (strncasecmp_P(pszRespLine, cszContentLength_P, sizeof(cszContentLength_P)-1) == 0)
  1036. {
  1037. nResult = 1;
  1038. /* Skip leading whitespace */
  1039. for (pszStart = pszRespLine + sizeof(cszContentLength_P)-1; (*pszStart == ' '); pszStart++)
  1040. {
  1041. ;
  1042. }
  1043. }
  1044. }
  1045. else if ((wInfoLevel & INET_HTTP_QUERY_CONTENT_TYPE) == INET_HTTP_QUERY_CONTENT_TYPE)
  1046. {
  1047. /*
  1048. * Get the content-type
  1049. */
  1050. if (strncasecmp_P(pszRespLine, cszContentType_P, sizeof(cszContentType_P)-1) == 0)
  1051. {
  1052. nResult = 1;
  1053. /* Skip leading whitespace */
  1054. for (pszStart = pszRespLine + sizeof(cszContentType_P)-1; (*pszStart == ' '); pszStart++)
  1055. {
  1056. ;
  1057. }
  1058. }
  1059. }
  1060. else if ((wInfoLevel & INET_HTTP_QUERY_ICY_METADATA) == INET_HTTP_QUERY_ICY_METADATA)
  1061. {
  1062. /*
  1063. * Get the content-type
  1064. */
  1065. if (strncasecmp_P(pszRespLine, cszIcyMetaData_P, sizeof(cszIcyMetaData_P)-1) == 0)
  1066. {
  1067. nResult = 1;
  1068. /* Skip leading whitespace */
  1069. for (pszStart = pszRespLine + sizeof(cszIcyMetaData_P)-1; (*pszStart == ' '); pszStart++)
  1070. {
  1071. ;
  1072. }
  1073. }
  1074. }
  1075. }
  1076. /* Find next line */
  1077. pszRespLine = pszEnd;
  1078. } /* end while */
  1079. /*
  1080. * If we found what we are looking for, pass and optionally convert the resulting value to the caller
  1081. */
  1082. if (nResult > 0)
  1083. {
  1084. long lNumericValue = -1;
  1085. if ((wInfoLevel & INET_HTTP_QUERY_MOD_NUMERIC) == INET_HTTP_QUERY_MOD_NUMERIC)
  1086. {
  1087. unResultSize = sizeof(lNumericValue);
  1088. }
  1089. else
  1090. {
  1091. unResultSize = pszEnd - pszStart;
  1092. if (unResultSize > 0)
  1093. {
  1094. /* Correction so we can store the \0 */
  1095. unResultSize += 1;
  1096. }
  1097. }
  1098. /* Allocate a buffer if the caller did not */
  1099. if (*punInfoSize == 0)
  1100. {
  1101. *pInfo = MyMalloc(unResultSize);
  1102. *punInfoSize = unResultSize;
  1103. }
  1104. /* Copy if there is room */
  1105. if ((*punInfoSize >= unResultSize) &&
  1106. (*pInfo != NULL))
  1107. {
  1108. if ((wInfoLevel & INET_HTTP_QUERY_MOD_NUMERIC) == INET_HTTP_QUERY_MOD_NUMERIC)
  1109. {
  1110. long *plDest = *pInfo;
  1111. lNumericValue = strtol(pszStart, (char **) NULL, 0);
  1112. *plDest = lNumericValue;
  1113. }
  1114. else
  1115. {
  1116. if (unResultSize > 0)
  1117. {
  1118. char *pszDest = *pInfo;
  1119. memcpy(pszDest, pszStart, unResultSize-1);
  1120. pszDest[unResultSize-1] = '\0';
  1121. }
  1122. }
  1123. }
  1124. else
  1125. {
  1126. /* No room to store the result */
  1127. nResult = -1;
  1128. }
  1129. *punInfoSize = unResultSize;
  1130. }
  1131. return (nResult);
  1132. }
  1133. /*\brief Mime types */
  1134. static prog_char cszTypeAudio_P[] = "audio/";
  1135. static prog_char cszTypeText_P[] = "text/";
  1136. /*\brief Audio subtypes */
  1137. static prog_char cszTypeM3u_P[] = "x-mpegurl";
  1138. static prog_char cszTypePls_P[] = "x-scpls";
  1139. //static prog_char cszSubtypeWma[] = "x-ms-wma";
  1140. //static prog_char cszSubtypeMp3[] = "mpeg";
  1141. //static prog_char cszSubtypeAac[] = "aacp";
  1142. /*\brief File extensions */
  1143. static prog_char cszPlsExtension_P[] = "pls";
  1144. static prog_char cszM3uExtension_P[] = "m3u";
  1145. int InetGetMimeType(HINET hInet)
  1146. {
  1147. int nResult = +1;
  1148. int nType = MIME_TYPE_UNKNOWN;
  1149. char *pszContentType = NULL;
  1150. /*
  1151. * Try to determine the filetype based on the content type header
  1152. */
  1153. int nHeaderNumber = 0;
  1154. void *ppszContentType = &pszContentType;
  1155. unsigned int unInfoSize = 0;
  1156. nResult = InetHttpQueryInfo(hInet,
  1157. INET_HTTP_QUERY_CONTENT_TYPE,
  1158. ppszContentType,
  1159. &unInfoSize,
  1160. &nHeaderNumber);
  1161. if (nResult > 0)
  1162. {
  1163. if (strncasecmp_P(pszContentType, cszTypeAudio_P, sizeof(cszTypeAudio_P)-1) == 0)
  1164. {
  1165. nType = MIME_TYPE_MP3;
  1166. char *pszSubType = pszContentType + sizeof(cszTypeAudio_P)-1;
  1167. if (strncasecmp_P(pszSubType, cszTypePls_P, sizeof(cszTypePls_P)-1) == 0)
  1168. {
  1169. nType = MIME_TYPE_PLS;
  1170. }
  1171. else if (strncasecmp_P(pszSubType, cszTypeM3u_P, sizeof(cszTypeM3u_P)-1) == 0)
  1172. {
  1173. nType = MIME_TYPE_M3U;
  1174. }
  1175. }
  1176. else if (strncasecmp_P(pszContentType, cszTypeText_P, sizeof(cszTypeText_P)-1) == 0)
  1177. {
  1178. /* Assume the generic text type */
  1179. nType = MIME_TYPE_TEXT;
  1180. }
  1181. }
  1182. else
  1183. {
  1184. nType = MIME_TYPE_MP3;
  1185. }
  1186. /*
  1187. * Icecast servers serve only audio
  1188. */
  1189. if ((nType == MIME_TYPE_UNKNOWN) && (hInet->hRequest->byProto == INET_PROTO_ICY))
  1190. {
  1191. nType = MIME_TYPE_MP3;
  1192. }
  1193. /*
  1194. * If all else fails, use the extension of the path
  1195. */
  1196. if ((nType == MIME_TYPE_UNKNOWN) || (nType == MIME_TYPE_TEXT))
  1197. {
  1198. /* Assume the extension starts at the last '.' we can find */
  1199. char *szExtension = strrchr(hInet->tUrlParts.pszPath, '.');
  1200. if (szExtension != NULL)
  1201. {
  1202. szExtension++; /* Skip the . */
  1203. if (strncasecmp_P(szExtension, cszPlsExtension_P, sizeof(cszPlsExtension_P)-1) == 0)
  1204. {
  1205. nType = MIME_TYPE_PLS;
  1206. }
  1207. else if (strncasecmp_P(szExtension, cszM3uExtension_P, sizeof(cszM3uExtension_P)-1) == 0)
  1208. {
  1209. nType = MIME_TYPE_M3U;
  1210. }
  1211. }
  1212. }
  1213. LogMsg_P(LOG_INFO, PSTR("File type %d"), nType);
  1214. MyFree(pszContentType);
  1215. return (nType);
  1216. }
  1217. int InetRead(HINET hInet, char *pcBuf, unsigned int unBufSize)
  1218. {
  1219. int nResult = -1;
  1220. if (hInet != NULL)
  1221. {
  1222. if (hInet->tState != INET_STATE_CLOSING)
  1223. {
  1224. hInet->tState = INET_STATE_BUSY;
  1225. //nResult = _read((int)hInet->ptSocket, pcBuf, unBufSize);
  1226. nResult = NutTcpReceive(hInet->ptSocket, pcBuf, unBufSize);
  1227. if (nResult < 0)
  1228. {
  1229. /*
  1230. * Either an error occurred or the other side closed the connection (= EOF).
  1231. */
  1232. LogMsg_P(LOG_INFO, PSTR("EOF %d"), NutTcpError(hInet->ptSocket));
  1233. }
  1234. else if (nResult == 0)
  1235. {
  1236. LogMsg_P(LOG_INFO, PSTR("Read Timeout"));
  1237. }
  1238. }
  1239. hInet->tState = INET_STATE_IDLE;
  1240. }
  1241. return (nResult);
  1242. }
  1243. int InetReadExact(HINET hInet, unsigned char *pbyBuf, unsigned int unBufSize)
  1244. {
  1245. int nResult = +1;
  1246. unsigned int unBufInUse = 0;
  1247. /* Sanity check */
  1248. if ((hInet == NULL) || (pbyBuf == NULL))
  1249. {
  1250. return (-1);
  1251. }
  1252. /* Keep reading until we are done */
  1253. while ((nResult > 0) && (unBufSize-unBufInUse) > 0)
  1254. {
  1255. nResult = InetRead(hInet, (char*)&pbyBuf[unBufInUse], unBufSize-unBufInUse);
  1256. //LogMsg_P(LOG_DEBUG, PSTR("r %d,max %u"), nResult, unBufSize-unBufInUse);
  1257. if (nResult > 0)
  1258. {
  1259. unBufInUse += nResult;
  1260. }
  1261. }
  1262. return (unBufInUse);
  1263. }
  1264. int InetReadFile(HINET hInet, char **ppcBuf, unsigned int *punBufSize)
  1265. {
  1266. int nResult = +1;
  1267. unsigned int unBufInUse = 0;
  1268. unsigned char byDoResize = 0;
  1269. /* Sanity check */
  1270. if ((ppcBuf == NULL) || (punBufSize == NULL))
  1271. {
  1272. nResult = -1;
  1273. }
  1274. /* If you did not provide a buffer, one will be provided for you */
  1275. if (nResult > 0)
  1276. {
  1277. if (*ppcBuf == NULL)
  1278. {
  1279. byDoResize = 1;
  1280. *punBufSize = 0;
  1281. }
  1282. }
  1283. /* Keep reading until we are done */
  1284. while (nResult > 0)
  1285. {
  1286. if (byDoResize)
  1287. {
  1288. nResult = BufferMakeRoom((char**)ppcBuf, punBufSize, unBufInUse, 100);
  1289. //LogMsg_P(LOG_DEBUG, PSTR("r %d,size %u, %x"), nResult, *punBufSize, &(*ppcBuf)[unBufInUse]);
  1290. }
  1291. if (nResult >= 0)
  1292. {
  1293. nResult = InetRead(hInet, (char*)&(*ppcBuf)[unBufInUse], *punBufSize-unBufInUse);
  1294. //LogMsg_P(LOG_DEBUG, PSTR("r %d,max %u"), nResult, *punBufSize-unBufInUse);
  1295. }
  1296. if (nResult > 0)
  1297. {
  1298. unBufInUse += nResult;
  1299. }
  1300. else
  1301. {
  1302. /*
  1303. * We have a eof/disconnect (-1), or a read timeout (0)
  1304. */
  1305. }
  1306. }
  1307. if (unBufInUse == 0)
  1308. {
  1309. LogMsg_P(LOG_WARNING, PSTR("No data"));
  1310. }
  1311. return (unBufInUse);
  1312. }
  1313. HINET InetClose(HINET hInet)
  1314. {
  1315. // LogMsg_P(LOG_DEBUG, PSTR("Close %X %d"), hInet, hInet->tState);
  1316. if (hInet != NULL)
  1317. {
  1318. if (hInet->tState != INET_STATE_IDLE)
  1319. {
  1320. hInet->tState = INET_STATE_CLOSING;
  1321. /*
  1322. * Wait for the close to be handled
  1323. */
  1324. while (hInet->tState != INET_STATE_IDLE)
  1325. {
  1326. unsigned int nCount = 0;
  1327. NutSleep(100);
  1328. /* After 10 seconds */
  1329. if (++nCount == 100)
  1330. {
  1331. LogMsg_P(LOG_EMERG, PSTR("Close failed"));
  1332. break;
  1333. }
  1334. }
  1335. }
  1336. CloseDescriptors(hInet);
  1337. MyFree(hInet->pszUrl);
  1338. ShowDebug();
  1339. /*
  1340. * Deallocate the request struct
  1341. */
  1342. if (hInet->hRequest != NULL)
  1343. {
  1344. MyFree(hInet->hRequest->pszRequest);
  1345. ShowDebug();
  1346. MyFree(hInet->hRequest->pszResponse);
  1347. ShowDebug();
  1348. MyFree(hInet->hRequest);
  1349. ShowDebug();
  1350. }
  1351. MyFree(hInet);
  1352. }
  1353. ShowDebug();
  1354. return (hInet);
  1355. }