Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define HAVE_SYS_TYPES_H 1
- #define HAVE_NETINET_IN_H 1
- #define HAVE_NETINET_IN_SYSTM_H 1
- #define HAVE_NETINET_IP_H 1
- #define HAVE_MALLOC_H 1
- #define HAVE_SYS_PARAM_H 1
- #define HAVE_SYS_TIME_H 1
- #define HAVE_UCRED_H 0
- #define HAVE_SYS_UCRED_H 0
- #define HAVE_IFADDRS_H 1
- #define HAVE_VFORK_H 0
- #define HAVE_SYS_RESOURCE_H 1
- #define HAVE_ENDIAN_H 1
- #define HAVE_SYS_ENDIAN_H 0
- #include <gnunet/platform.h>
- #include <stdbool.h>
- #include <gnunet/gnunet_util_lib.h>
- static int ret;
- struct ConnectionNode {
- struct GNUNET_NETWORK_Handle *nhandle;
- struct ConnectionNodeHandler {
- void (*func)(struct ConnectionNode *, struct ConnectionNodeHandler *,
- char *, size_t);
- void (*free)(struct ConnectionNode *, struct ConnectionNodeHandler *);
- struct ConnectionNodeHandler *prev;
- }*handler;
- };
- typedef int (*CommandFunc)(struct ConnectionNode *, int, char **);
- struct client_function {
- char * keyword;
- CommandFunc function;
- };
- int echo_func(struct ConnectionNode *conn, int argc, char **argv) {
- int i;
- size_t n;
- char *t = NULL;
- while (t == NULL )
- t = strdup("211 pong:");
- for (i = 1; i < argc; i++) {
- char *r = NULL;
- n = strlen(t) + strlen(argv[i]) + 4;
- while (r == NULL )
- r = realloc(t, n);
- t = r;
- strncat(t, " ", n);
- strncat(t, argv[i], n);
- }
- strncat(t, "\r\n", n);
- GNUNET_NETWORK_socket_send(conn->nhandle, t, strlen(t));
- free(t);
- return 0;
- }
- static struct client_function commands[] = {
- { "ping", echo_func },
- { NULL, NULL } };
- CommandFunc get_command_function(char *cmd) {
- int i;
- if (cmd == NULL )
- return NULL ;
- for (i = 0; commands[i].keyword != NULL ; i++)
- if (0 == strcmp(cmd, commands[i].keyword))
- return commands[i].function;
- return NULL ;
- }
- struct ProccessInputHandler {
- void (*func)(struct ConnectionNode *, struct ProccessInputHandler *, char *, size_t);
- void (*free)(struct ConnectionNode *, struct ProccessInputHandler *);
- struct ConnectionNodeHandler *prev;
- char * buf;
- size_t nbytes;
- };
- void ProccessInputFree(struct ConnectionNode *i, struct ProccessInputHandler *h) {
- if (h->prev != NULL)
- h->prev->free(i, h->prev);
- if (h->buf != NULL )
- free(h->buf);
- free(h);
- }
- void LineLocator(struct ConnectionNode *conn, struct ProccessInputHandler *h) {
- /* look for the end of the line */
- char *str1, *saveptr1, *ntoken, *token;
- bool endswell = false;
- switch (h->buf[h->nbytes - 1]) {
- case '\r':
- case '\n':
- endswell = true;
- break;
- };
- for (str1 = h->buf;; str1 = NULL ) {
- ntoken = strtok_r(str1, "\r\n", &saveptr1);
- /* Reverse this so we know the next result. */
- if (str1 == NULL ) {
- if (ntoken != NULL || endswell) {
- char *str2, *saveptr2, *subtoken;
- char **argv = NULL;
- while (argv == NULL )
- argv = malloc(0);
- int argc = 0;
- for (str2 = token;; str2 = NULL ) {
- subtoken = strtok_r(str2, " \t", &saveptr2);
- if (subtoken == NULL )
- break;
- void *i = NULL;
- while (i == NULL )
- i = realloc(argv, sizeof(void*) * (argc + 1));
- argv = i;
- argv[argc++] = subtoken;
- }
- CommandFunc f;
- f = get_command_function(argv[0]);
- if (!f) {
- GNUNET_NETWORK_socket_send(conn->nhandle,
- "502 Bad command or it is not implemented here.\r\n",
- 48);
- } else
- f(conn, argc, argv);
- free(argv);
- if (ntoken == NULL ) {
- free(h->buf);
- h->buf = NULL;
- h->nbytes = 0;
- break;
- }
- } else {
- h->nbytes = strlen(token);
- str1 = strdup(token);
- free(h->buf);
- h->buf = str1;
- break;
- }
- }
- token = ntoken;
- }
- }
- void ProccessInput(struct ConnectionNode *conn,
- struct ProccessInputHandler *h, char *buf, size_t nbytes) {
- /* we got some data from a client */
- h->nbytes += nbytes;
- if (h->buf != NULL ) {
- char *t = NULL;
- while (t == NULL )
- t = GNUNET_realloc(h->buf, h->nbytes + 1);
- strncat(t, buf, h->nbytes);
- h->buf = t;
- } else
- h->buf = strdup(buf);
- LineLocator(conn, h);
- }
- static void run_recv(void *cst, const struct GNUNET_SCHEDULER_TaskContext *ctx) {
- struct ConnectionNode *desc = cst;
- char buf[1024];
- int nbytes;
- if ((nbytes = GNUNET_NETWORK_socket_recv(desc->nhandle, buf, sizeof(buf) - 1)) <= 0) {
- /* got error or connection closed by client */
- if (nbytes == 0) {
- /* connection closed */
- GNUNET_NETWORK_socket_close(desc->nhandle);
- GNUNET_free(desc);
- } else {
- /* Ensure this is an ansi string */
- buf[nbytes] = '\0';
- desc->handler->func(desc, desc->handler, buf, nbytes);
- GNUNET_SCHEDULER_add_read_net(
- GNUNET_TIME_UNIT_FOREVER_REL,
- desc->nhandle, &run_recv, desc);
- }
- }
- }
- static void run_accept(void *cst, const struct GNUNET_SCHEDULER_TaskContext *ctx) {
- struct GNUNET_NETWORK_Handle *desc = cst;
- GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
- desc, &run_accept, desc);
- struct GNUNET_NETWORK_Handle *new =
- GNUNET_NETWORK_socket_accept (desc, NULL, NULL);
- if(new == NULL) return;
- struct ConnectionNode *TempNode = NULL;
- while(TempNode == NULL)
- TempNode = GNUNET_malloc(sizeof(struct ConnectionNode));
- struct ProccessInputHandler *handler = NULL;
- while(handler == NULL)
- handler = GNUNET_malloc(sizeof(struct ProccessInputHandler));
- handler->func = &ProccessInput;
- handler->free = &ProccessInputFree;
- handler->prev = NULL;
- handler->nbytes = 0;
- handler->buf = NULL;
- TempNode->handler = (struct ConnectionNodeHandler *)handler;
- TempNode->nhandle = new;
- GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
- new, &run_recv, TempNode);
- }
- static void run (void *cls, char *const *args, const char *cfgfile,
- const struct GNUNET_CONFIGURATION_Handle *cfg) {
- struct sockaddr_in sa;
- struct GNUNET_NETWORK_Handle *desc;
- memset (&sa, 0, sizeof (sa));
- #if HAVE_SOCKADDR_IN_SIN_LEN
- sa.sin_len = sizeof (sa);
- #endif
- sa.sin_family = AF_INET;
- sa.sin_port = htons (6677);
- sa.sin_addr.s_addr = inet_addr("127.0.0.1");
- desc = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0);
- GNUNET_assert (desc != NULL);
- GNUNET_assert (GNUNET_OK == GNUNET_NETWORK_socket_bind
- (desc, (struct sockaddr *) &sa, sizeof (sa)));
- GNUNET_NETWORK_socket_listen (desc, 5);
- GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
- desc, &run_accept, desc);
- ret = 0;
- }
- int main (int argc, char *const *argv) {
- static const struct GNUNET_GETOPT_CommandLineOption options[] = {
- GNUNET_GETOPT_OPTION_END
- };
- return (GNUNET_OK == GNUNET_PROGRAM_run (
- argc, argv, "gnunetircd",
- gettext_noop ("ircd over gnunet"),
- options, &run, NULL)) ? ret : 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement