network.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // Created by janco on 25-2-16.
  3. //
  4. #include <dev/board.h>
  5. #include <dev/debug.h>
  6. #include <sys/timer.h>
  7. #include <sys/confnet.h>
  8. #include <sys/socket.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <time.h>
  12. #include <net/route.h>
  13. #include <dev/nicrtl.h>
  14. #include <stdio.h>
  15. #include <io.h>
  16. #include <arpa/inet.h>
  17. #include <pro/dhcp.h>
  18. #include <pro/sntp.h>
  19. #include "network.h"
  20. #include "ntp.h"
  21. void NetworkInit() {
  22. /* Register de internet controller. */
  23. if (NutRegisterDevice(&DEV_ETHER, 0, 0)) {
  24. printf("Registering failed.");
  25. }/* Netwerk configureren op dhcp */
  26. else if (NutDhcpIfConfig(DEV_ETHER_NAME, NULL, 0)) {
  27. /* Done. */
  28. }else {
  29. printf("Ik heb een internet connectie. Ip is: %s", inet_ntoa(confnet.cdn_ip_addr));
  30. }
  31. }
  32. char* httpGet(char address[]){
  33. printf("komt in httpget()");
  34. NutDelay(1000);
  35. TCPSOCKET* sock = NutTcpCreateSocket();
  36. char http[150];
  37. sprintf(http, "GET %s HTTP/1.1\r\nHost: jancokock.me \r\n\r\n", address);
  38. printf("%s", http);
  39. NutDelay(100);
  40. char buffer[200];
  41. int len = sizeof(http);
  42. if (NutTcpConnect(sock, inet_addr("62.195.226.247"), 80)) {
  43. printf("Can't connect to server\n");
  44. }else{
  45. FILE *stream;
  46. stream = _fdopen((int) sock, "r b");
  47. if(NutTcpSend(sock, http, len) != len){
  48. printf("Writing headers failed.");
  49. NutDelay(1000);
  50. }else{
  51. printf("Headers writed. Now reading.");
  52. NutDelay(1000);
  53. NutTcpReceive(sock, buffer, sizeof(buffer));
  54. //fread(buffer, 1, sizeof(buffer), stream);
  55. NutDelay(1000);
  56. printf(buffer);
  57. };
  58. fclose(stream);
  59. }
  60. NutTcpCloseSocket(sock);
  61. int i;
  62. int enters = 0;
  63. int t = 0;
  64. char* content = (char*) calloc(1 , sizeof(buffer));
  65. for(i = 0; i < strlen(buffer); i++)
  66. {
  67. if(enters > 3) {
  68. enters = 10;
  69. content[t] = buffer[i];
  70. t++;
  71. }
  72. if(buffer[i] == '\n' || buffer[i] == '\r')
  73. {
  74. enters++;
  75. }
  76. else if(enters < 10)
  77. {
  78. enters = 0;
  79. }
  80. }
  81. content[t] = '\0';
  82. printf("Content size %d\n", t);
  83. return content;
  84. }