HomePort
echo.c
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Aalborg University. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are
5  * permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  * conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  * of conditions and the following disclaimer in the documentation and/or other materials
12  * provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY Aalborg University ''AS IS'' AND ANY EXPRESS OR IMPLIED
15  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Aalborg University OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * The views and conclusions contained in the software and documentation are those of the
25  * authors and should not be interpreted as representing official policies, either expressed
26  */
27 
28 #include "hpd_tcpd.h"
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <ev.h>
32 
33 // A tcpd instance
34 static hpd_tcpd_t *ws = NULL;
35 
36 // Receive messages
37 static int on_receive(hpd_tcpd_t *instance, hpd_tcpd_conn_t *conn, void *ctx, void **data,
38  const char *buf, size_t len)
39 {
40  hpd_tcpd_conn_sendf(conn, "%.*s", (int) len, buf);
41  return 0;
42 }
43 
44 // Handle correct exiting
45 static void exit_handler(int sig)
46 {
47  // Stop tcpd
48  if (hpd_ws != NULL) {
49  hpd_tcpd_stop(hpd_ws);
50  hpd_tcpd_destroy(hpd_ws);
51  }
52 
53  // Exit
54  printf("Exiting....\n");
55  exit(sig);
56 }
57 
58 // Main function
59 int main(int argc, char *argv[])
60 {
61  // The event loop for the tcpd to run on
62  hpd_ev_loop_t *loop = EV_DEFAULT;
63 
64  // Settings for the tcpd
66  settings.port = HPD_TCPD_P_HTTP_ALT;
67  settings.on_receive = on_receive;
68 
69  // Inform if we have been built with debug flag
70 #ifdef DEBUG
71  printf("Debugging is set\n");
72 #endif
73 
74  // Register signals for correctly exiting
75  signal(SIGINT, exit_handler);
76  signal(SIGTERM, exit_handler);
77 
78  // Create tcpd
79  hpd_ws = hpd_tcpd_create(NULL, &settings, NULL, NULL);
80  hpd_tcpd_start(hpd_ws);
81 
82  // Start the event loop and tcpd
83  ev_run(loop, 0);
84 
85  // Exit
86  exit_handler(0);
87  return 0;
88 }
Instance of a tcpd.
Definition: tcpd_intern.h:45
struct up * instance
hpd_error_t hpd_tcpd_create(hpd_tcpd_t **tcpd, hpd_tcpd_settings_t *settings, const hpd_module_t *context, hpd_ev_loop_t *loop)
Create new tcpd instance.
Definition: tcpd.c:269
hpd_tcpd_port_t port
Port number.
Definition: hpd_tcpd.h:95
struct hp_settings settings
hpd_error_t hpd_tcpd_stop(hpd_tcpd_t *tcpd)
Stop an already running tcpd.
Definition: tcpd.c:428
hpd_error_t hpd_tcpd_start(hpd_tcpd_t *tcpd)
Start the tcpd.
Definition: tcpd.c:318
All data to represent a connection.
Definition: tcpd_intern.h:56
hpd_error_t hpd_tcpd_conn_sendf(hpd_tcpd_conn_t *conn, const char *fmt,...)
Send message on connection.
Definition: tcpd.c:503
static struct ev_loop * loop
int main(int argc, char *argv[])
Definition: echo.c:59
hpd_tcpd_data_f on_receive
Definition: hpd_tcpd.h:100
static hpd_tcpd_t * ws
Definition: echo.c:34
static void exit_handler(int sig)
Definition: echo.c:45
struct ev_loop hpd_ev_loop_t
Definition: hpd_types.h:51
hpd_error_t hpd_tcpd_destroy(hpd_tcpd_t *tcpd)
Destroy tcpd and free used memory.
Definition: tcpd.c:297
Settings struct for tcpd.
Definition: hpd_tcpd.h:94
#define HPD_TCPD_SETTINGS_DEFAULT
Default settings for tcpd.
Definition: hpd_tcpd.h:113
static int on_receive(hpd_tcpd_t *instance, hpd_tcpd_conn_t *conn, void *ctx, void **data, const char *buf, size_t len)
Definition: echo.c:37