Advertisement
cheako

Gnunet example server 001.

Feb 24th, 2017
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.54 KB | None | 0 0
  1. #define HAVE_SYS_TYPES_H 1
  2. #define HAVE_NETINET_IN_H 1
  3. #define HAVE_NETINET_IN_SYSTM_H 1
  4. #define HAVE_NETINET_IP_H 1
  5. #define HAVE_MALLOC_H 1
  6. #define HAVE_SYS_PARAM_H 1
  7. #define HAVE_SYS_TIME_H 1
  8. #define HAVE_UCRED_H 0
  9. #define HAVE_SYS_UCRED_H 0
  10. #define HAVE_IFADDRS_H 1
  11. #define HAVE_VFORK_H 0
  12. #define HAVE_SYS_RESOURCE_H 1
  13. #define HAVE_ENDIAN_H 1
  14. #define HAVE_SYS_ENDIAN_H 0
  15. #include <gnunet/platform.h>
  16. #include <stdbool.h>
  17. #include <gnunet/gnunet_util_lib.h>
  18.  
  19. static int ret;
  20.  
  21. struct ConnectionNode {
  22.     struct GNUNET_NETWORK_Handle *nhandle;
  23.     struct ConnectionNodeHandler {
  24.         void (*func)(struct ConnectionNode *, struct ConnectionNodeHandler *,
  25.                 char *, size_t);
  26.         void (*free)(struct ConnectionNode *, struct ConnectionNodeHandler *);
  27.         struct ConnectionNodeHandler *prev;
  28.     }*handler;
  29. };
  30.  
  31. typedef int (*CommandFunc)(struct ConnectionNode *, int, char **);
  32.  
  33. struct client_function {
  34.     char * keyword;
  35.     CommandFunc function;
  36. };
  37.  
  38. int echo_func(struct ConnectionNode *conn, int argc, char **argv) {
  39.     int i;
  40.     size_t n;
  41.     char *t = NULL;
  42.  
  43.     while (t == NULL )
  44.         t = strdup("211 pong:");
  45.     for (i = 1; i < argc; i++) {
  46.         char *r = NULL;
  47.         n = strlen(t) + strlen(argv[i]) + 4;
  48.         while (r == NULL )
  49.             r = realloc(t, n);
  50.         t = r;
  51.         strncat(t, " ", n);
  52.         strncat(t, argv[i], n);
  53.     }
  54.     strncat(t, "\r\n", n);
  55.     GNUNET_NETWORK_socket_send(conn->nhandle, t, strlen(t));
  56.     free(t);
  57.     return 0;
  58. }
  59. static struct client_function commands[] = {
  60.     { "ping", echo_func },
  61.     { NULL, NULL } };
  62.  
  63. CommandFunc get_command_function(char *cmd) {
  64.     int i;
  65.  
  66.     if (cmd == NULL )
  67.         return NULL ;
  68.  
  69.     for (i = 0; commands[i].keyword != NULL ; i++)
  70.         if (0 == strcmp(cmd, commands[i].keyword))
  71.             return commands[i].function;
  72.  
  73.     return NULL ;
  74. }
  75.  
  76. struct ProccessInputHandler {
  77.     void (*func)(struct ConnectionNode *, struct ProccessInputHandler *, char *, size_t);
  78.     void (*free)(struct ConnectionNode *, struct ProccessInputHandler *);
  79.     struct ConnectionNodeHandler *prev;
  80.     char * buf;
  81.     size_t nbytes;
  82. };
  83.  
  84. void ProccessInputFree(struct ConnectionNode *i, struct ProccessInputHandler *h) {
  85.     if (h->prev != NULL)
  86.         h->prev->free(i, h->prev);
  87.     if (h->buf != NULL )
  88.         free(h->buf);
  89.     free(h);
  90. }
  91.  
  92. void LineLocator(struct ConnectionNode *conn, struct ProccessInputHandler *h) {
  93.     /* look for the end of the line */
  94.     char *str1, *saveptr1, *ntoken, *token;
  95.     bool endswell = false;
  96.     switch (h->buf[h->nbytes - 1]) {
  97.     case '\r':
  98.     case '\n':
  99.         endswell = true;
  100.         break;
  101.     };
  102.  
  103.     for (str1 = h->buf;; str1 = NULL ) {
  104.         ntoken = strtok_r(str1, "\r\n", &saveptr1);
  105.         /* Reverse this so we know the next result. */
  106.         if (str1 == NULL ) {
  107.             if (ntoken != NULL || endswell) {
  108.                 char *str2, *saveptr2, *subtoken;
  109.                 char **argv = NULL;
  110.                 while (argv == NULL )
  111.                     argv = malloc(0);
  112.                 int argc = 0;
  113.  
  114.                 for (str2 = token;; str2 = NULL ) {
  115.                     subtoken = strtok_r(str2, " \t", &saveptr2);
  116.                     if (subtoken == NULL )
  117.                         break;
  118.                     void *i = NULL;
  119.                     while (i == NULL )
  120.                         i = realloc(argv, sizeof(void*) * (argc + 1));
  121.                     argv = i;
  122.                     argv[argc++] = subtoken;
  123.                 }
  124.                 CommandFunc f;
  125.                 f = get_command_function(argv[0]);
  126.                 if (!f) {
  127.     GNUNET_NETWORK_socket_send(conn->nhandle,
  128.             "502 Bad command or it is not implemented here.\r\n",
  129.             48);
  130.                 } else
  131.                     f(conn, argc, argv);
  132.                 free(argv);
  133.                 if (ntoken == NULL ) {
  134.                     free(h->buf);
  135.                     h->buf = NULL;
  136.                     h->nbytes = 0;
  137.                     break;
  138.                 }
  139.             } else {
  140.                 h->nbytes = strlen(token);
  141.                 str1 = strdup(token);
  142.                 free(h->buf);
  143.                 h->buf = str1;
  144.                 break;
  145.             }
  146.         }
  147.         token = ntoken;
  148.     }
  149. }
  150.  
  151. void ProccessInput(struct ConnectionNode *conn,
  152.         struct ProccessInputHandler *h, char *buf, size_t nbytes) {
  153.     /* we got some data from a client */
  154.     h->nbytes += nbytes;
  155.     if (h->buf != NULL ) {
  156.         char *t = NULL;
  157.         while (t == NULL )
  158.             t = GNUNET_realloc(h->buf, h->nbytes + 1);
  159.         strncat(t, buf, h->nbytes);
  160.         h->buf = t;
  161.     } else
  162.         h->buf = strdup(buf);
  163.     LineLocator(conn, h);
  164. }
  165.  
  166. static void run_recv(void *cst, const struct GNUNET_SCHEDULER_TaskContext *ctx) {
  167.     struct ConnectionNode *desc = cst;
  168.     char buf[1024];
  169.     int nbytes;
  170.     if ((nbytes = GNUNET_NETWORK_socket_recv(desc->nhandle, buf, sizeof(buf) - 1)) <= 0) {
  171.         /* got error or connection closed by client */
  172.         if (nbytes == 0) {
  173.             /* connection closed */
  174.             GNUNET_NETWORK_socket_close(desc->nhandle);
  175.             GNUNET_free(desc);     
  176.         } else {
  177.             /* Ensure this is an ansi string */
  178.             buf[nbytes] = '\0';
  179.             desc->handler->func(desc, desc->handler, buf, nbytes);
  180.             GNUNET_SCHEDULER_add_read_net(
  181.                     GNUNET_TIME_UNIT_FOREVER_REL,
  182.                     desc->nhandle, &run_recv, desc);
  183.         }
  184.     }
  185. }
  186.  
  187. static void run_accept(void *cst, const struct GNUNET_SCHEDULER_TaskContext *ctx) {
  188.     struct GNUNET_NETWORK_Handle *desc = cst;
  189.     GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
  190.             desc, &run_accept, desc);
  191.     struct GNUNET_NETWORK_Handle *new =
  192.     GNUNET_NETWORK_socket_accept (desc, NULL, NULL);
  193.     if(new == NULL) return;
  194.     struct ConnectionNode *TempNode = NULL;
  195.     while(TempNode == NULL)
  196.         TempNode = GNUNET_malloc(sizeof(struct ConnectionNode));
  197.     struct ProccessInputHandler *handler = NULL;
  198.     while(handler == NULL)
  199.         handler = GNUNET_malloc(sizeof(struct ProccessInputHandler));
  200.     handler->func = &ProccessInput;
  201.     handler->free = &ProccessInputFree;
  202.     handler->prev = NULL;
  203.     handler->nbytes = 0;
  204.     handler->buf = NULL;
  205.  
  206.     TempNode->handler = (struct ConnectionNodeHandler *)handler;
  207.     TempNode->nhandle = new;
  208.     GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
  209.             new, &run_recv, TempNode);
  210. }
  211.  
  212. static void run (void *cls, char *const *args, const char *cfgfile,
  213.         const struct GNUNET_CONFIGURATION_Handle *cfg) {
  214.  
  215.     struct sockaddr_in sa;
  216.     struct GNUNET_NETWORK_Handle *desc;
  217.  
  218.     memset (&sa, 0, sizeof (sa));
  219. #if HAVE_SOCKADDR_IN_SIN_LEN
  220.     sa.sin_len = sizeof (sa);
  221. #endif
  222.     sa.sin_family = AF_INET;
  223.     sa.sin_port = htons (6677);
  224.     sa.sin_addr.s_addr = inet_addr("127.0.0.1");
  225.     desc = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0);
  226.     GNUNET_assert (desc != NULL);
  227.     GNUNET_assert (GNUNET_OK == GNUNET_NETWORK_socket_bind
  228.             (desc, (struct sockaddr *) &sa, sizeof (sa)));
  229.     GNUNET_NETWORK_socket_listen (desc, 5);
  230.     GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
  231.             desc, &run_accept, desc);
  232.     ret = 0;
  233. }
  234.  
  235. int main (int argc, char *const *argv) {
  236.     static const struct GNUNET_GETOPT_CommandLineOption options[] = {
  237.         GNUNET_GETOPT_OPTION_END
  238.     };
  239.     return (GNUNET_OK == GNUNET_PROGRAM_run (
  240.         argc, argv, "gnunetircd",
  241.         gettext_noop ("ircd over gnunet"),
  242.         options, &run, NULL)) ? ret : 1;
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement