myco-modules-default.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* Copyright (C) 2016 Max Riechelmann <max.riechelmann@student.kit.edu>
  2. (Karlsruhe Institute of Technology)
  3. This library is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU Lesser General Public License as published by the
  5. Free Software Foundation; either version 2.1 of the License, or (at your
  6. option) any later version.
  7. This library is distributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  10. details.
  11. You should have received a copy of the GNU Lesser General Public License along
  12. with this library; if not, write to the Free Software Foundation, Inc., 51
  13. Franklin St, Fifth Floor, Boston, MA 02110, USA
  14. */
  15. #include <sys/types.h>
  16. #include <sys/socket.h>
  17. #include <netinet/in.h>
  18. #include <arpa/inet.h>
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #define BUF 1024
  24. int myco_network_fetch_resource(void *local_pointer, pid_t local_pid, int resource_size) {
  25. printf("\nTransferring data over network!\n");
  26. int port = 15001;
  27. int create_socket, new_socket;
  28. socklen_t addrlen;
  29. char *buffer = malloc (BUF);
  30. ssize_t size;
  31. struct sockaddr_in address;
  32. const int y = 1;
  33. char *command;
  34. char *agent;
  35. char *resource;
  36. create_socket=socket (AF_INET, SOCK_STREAM, 0);
  37. setsockopt( create_socket, SOL_SOCKET,
  38. SO_REUSEADDR, &y, sizeof(int));
  39. address.sin_family = AF_INET;
  40. address.sin_addr.s_addr = INADDR_ANY;
  41. address.sin_port = htons (port);
  42. if (bind ( create_socket,
  43. (struct sockaddr *) &address,
  44. sizeof (address)) != 0) {
  45. printf( "Port already taken!\n");
  46. }
  47. listen (create_socket, 5);
  48. addrlen = sizeof (struct sockaddr_in);
  49. while (1) {
  50. new_socket = accept (create_socket, (struct sockaddr *) &address, &addrlen);
  51. printf("DEBUG\n");
  52. if (new_socket > 0)
  53. inet_ntoa (address.sin_addr);
  54. do {
  55. size = recv (new_socket, buffer, BUF-1, 0);
  56. if( size > 0)
  57. buffer[size] = '\0';
  58. printf ("DEBUG: %s\n", buffer);
  59. } while (strcmp (buffer, "quit\n") != 0);
  60. close (new_socket);
  61. }
  62. close (create_socket);
  63. /*
  64. // Write memory to Agent
  65. if (myco_write_memory(local_pid, local_pointer, size, (void *)resource_pointer, size) == -1) {
  66. printf("ERROR: %s\n", strerror(errno));
  67. return -1;
  68. }
  69. */
  70. return 0;
  71. }