Skip to content

Commit fb724dc

Browse files
author
gasmibal
committed
+ crash fix in signal handler (free of not allocated var !)
+ add some comments
1 parent af7a257 commit fb724dc

12 files changed

+971
-933
lines changed

src/esip.c

+62-64
Original file line numberDiff line numberDiff line change
@@ -36,95 +36,93 @@
3636
#include "esosip.h"
3737

3838
typedef struct app_s {
39-
struct event_config *cfg;
40-
struct event_base *base;
41-
struct event *evsig;
39+
struct event_config * cfg;
40+
struct event_base * base;
41+
struct event * evsig;
4242

43-
/* osip stack */
44-
es_osip_t *osipCtx;
43+
/* osip stack */
44+
es_osip_t * osipCtx;
4545
} app_t;
4646

47-
static void log_cb(
48-
int severity,
49-
const char *msg)
47+
static void libevent_log_cb(int severity, const char * msg)
5048
{
51-
ESIP_TRACE(ESIP_LOG_DEBUG, "%s", msg);
49+
ESIP_TRACE(ESIP_LOG_INFO, "[libevent]- %s", msg);
5250
}
5351

54-
static void signal_cb(
55-
evutil_socket_t fd,
56-
short event,
57-
void *arg)
52+
static void signal_cb(evutil_socket_t fd, short event, void * arg)
5853
{
59-
app_t *ctx = (app_t *)arg;
60-
ESIP_TRACE(ESIP_LOG_INFO, "Terminate %s", PACKAGE);
61-
(void)event_base_loopexit(ctx->base, NULL);
54+
app_t * ctx = (app_t *)arg;
55+
ESIP_TRACE(ESIP_LOG_INFO, "Terminate %s", PACKAGE);
56+
(void)event_base_loopexit(ctx->base, NULL);
57+
58+
/* TODO: free all allocated mem */
59+
//free(ctx);
6260
}
6361

64-
int main(const int argc, const char *argv[])
62+
int main(const int argc, const char * argv[])
6563
{
66-
app_t ctx;
67-
int ret = EXIT_SUCCESS;
64+
app_t ctx;
65+
int ret = EXIT_SUCCESS;
6866

69-
ESIP_TRACE(ESIP_LOG_INFO, "Starting %s v%s", PACKAGE, VERSION);
67+
ESIP_TRACE(ESIP_LOG_INFO, "Starting %s v%s", PACKAGE, VERSION);
7068

71-
event_set_log_callback(log_cb);
69+
event_set_log_callback(libevent_log_cb);
7270

73-
//event_enable_debug_logging(0xffffffffu);
71+
//event_enable_debug_logging(0xffffffffu);
7472

75-
ctx.cfg = event_config_new();
76-
if (event_config_avoid_method(ctx.cfg, "select") != 0) {
77-
ESIP_TRACE(ESIP_LOG_ERROR, "Can not avoid select methode");
78-
}
73+
ctx.cfg = event_config_new();
74+
if (event_config_avoid_method(ctx.cfg, "select") != 0) {
75+
ESIP_TRACE(ESIP_LOG_ERROR, "Can not avoid select methode");
76+
}
7977

80-
ctx.base = event_base_new_with_config(ctx.cfg);
81-
if (ctx.base == NULL) {
82-
ESIP_TRACE(ESIP_LOG_ERROR, "Can not create event Config");
83-
goto ERROR_EXIT;
84-
}
78+
ctx.base = event_base_new_with_config(ctx.cfg);
79+
if (ctx.base == NULL) {
80+
ESIP_TRACE(ESIP_LOG_ERROR, "Can not create event Config");
81+
goto ERROR_EXIT;
82+
}
8583

86-
/* free cfg */
87-
event_config_free(ctx.cfg);
88-
ctx.cfg = NULL;
84+
/* free cfg */
85+
event_config_free(ctx.cfg);
86+
ctx.cfg = NULL;
8987

90-
/* Set MAX priority */
91-
if (event_base_priority_init(ctx.base, 2) != 0) {
92-
ESIP_TRACE(ESIP_LOG_ERROR, "Can not set MAX priority in event loop");
93-
}
88+
/* Set MAX priority */
89+
if (event_base_priority_init(ctx.base, 2) != 0) {
90+
ESIP_TRACE(ESIP_LOG_ERROR, "Can not set MAX priority in event loop");
91+
}
9492

95-
ctx.evsig = evsignal_new(ctx.base, SIGINT, &signal_cb, (void *)&ctx);
96-
if (ctx.evsig == NULL) {
97-
ESIP_TRACE(ESIP_LOG_ERROR, "Can not create event for signal");
98-
goto ERROR_EXIT;
99-
}
93+
ctx.evsig = evsignal_new(ctx.base, SIGINT, &signal_cb, (void *)&ctx);
94+
if (ctx.evsig == NULL) {
95+
ESIP_TRACE(ESIP_LOG_ERROR, "Can not create event for signal");
96+
goto ERROR_EXIT;
97+
}
10098

101-
if (evsignal_add(ctx.evsig, NULL) != 0) {
102-
ESIP_TRACE(ESIP_LOG_ERROR, "Can not make Signal event pending");
103-
goto ERROR_EXIT;
104-
}
99+
if (evsignal_add(ctx.evsig, NULL) != 0) {
100+
ESIP_TRACE(ESIP_LOG_ERROR, "Can not make Signal event pending");
101+
goto ERROR_EXIT;
102+
}
105103

106-
/* Init OSip Stack */
107-
if (es_osip_init(&ctx.osipCtx, ctx.base) != ES_OK) {
108-
ESIP_TRACE(ESIP_LOG_ERROR, "Can not initialize OSip stack");
109-
goto ERROR_EXIT;
110-
}
104+
/* Init OSip Stack */
105+
if (es_osip_init(&ctx.osipCtx, ctx.base) != ES_OK) {
106+
ESIP_TRACE(ESIP_LOG_ERROR, "Can not initialize OSip stack");
107+
goto ERROR_EXIT;
108+
}
111109

112-
ESIP_TRACE(ESIP_LOG_DEBUG, "Starting %s v%s main loop", PACKAGE, VERSION);
113-
if (event_base_dispatch(ctx.base) != 0) {
114-
ESIP_TRACE(ESIP_LOG_EMERG, "Can not start main event lopp");
115-
goto ERROR_EXIT;
116-
}
110+
ESIP_TRACE(ESIP_LOG_DEBUG, "Starting %s v%s main loop", PACKAGE, VERSION);
111+
if (event_base_dispatch(ctx.base) != 0) {
112+
ESIP_TRACE(ESIP_LOG_EMERG, "Can not start main event lopp");
113+
goto ERROR_EXIT;
114+
}
117115

118-
goto EXIT;
116+
goto EXIT;
119117

120118
ERROR_EXIT:
121-
ESIP_TRACE(ESIP_LOG_EMERG, "%s fatal error: stoped.", PACKAGE);
122-
ret = EXIT_FAILURE;
119+
ESIP_TRACE(ESIP_LOG_EMERG, "%s fatal error: stoped.", PACKAGE);
120+
ret = EXIT_FAILURE;
123121

124122
EXIT:
125-
event_free(ctx.evsig);
126-
event_base_free(ctx.base);
123+
event_free(ctx.evsig);
124+
event_base_free(ctx.base);
127125

128-
//libevent_global_shutdown();
129-
return ret;
126+
//libevent_global_shutdown();
127+
return ret;
130128
}

src/inc/eserror.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ typedef int es_status;
3333
#define ES_ERROR_DESTRUCTED -6 /* operation attempted on a destructed object */
3434
#define ES_ERROR_NOTSUPPORTED -7 /* request not supported under current configuration */
3535
#define ES_ERROR_UNINITIALIZED -8 /* object uninitialized */
36-
#define ES_ERROR_TRY_AGAIN -9 /* incomplete operation, used by semaphore's try lock */
36+
#define ES_ERROR_TRY_AGAIN -9 /* incomplete operation, used by semaphore's try lock */
3737
#define ES_ERROR_ILLEGAL_ACTION -10 /* the requested action is illegal */
3838
#define ES_ERROR_NETWORK_PROBLEM -11 /* action failed due to network problems */
3939
#define ES_ERROR_INVALID_HANDLE -12 /* a handle passed to a function is illegal */

src/inc/esosip.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ extern "C" {
2828

2929
typedef struct es_osip_s es_osip_t;
3030

31-
es_status es_osip_init(OUT es_osip_t **ctx, struct event_base *base);
31+
es_status es_osip_init(OUT es_osip_t ** ctx, struct event_base * base);
3232

33-
es_status es_osip_parse_msg(IN es_osip_t *ctx, const char *buf, unsigned int size);
33+
es_status es_osip_parse_msg(IN es_osip_t * ctx, const char * buf, unsigned int size);
3434

3535
#if defined(__cplusplus)
3636
}

src/inc/estransport.h

+20-20
Original file line numberDiff line numberDiff line change
@@ -29,38 +29,38 @@ extern "C" {
2929
typedef struct es_transport_s es_transport_t;
3030

3131
typedef void (*es_transport_event_cb)(
32-
IN es_transport_t *transp,
33-
IN int ev,
34-
IN int error,
35-
OUT void *ctx);
32+
IN es_transport_t * transp,
33+
IN int ev,
34+
IN int error,
35+
OUT void * ctx);
3636

3737
typedef void (*es_transport_msg_recv_cb)(
38-
IN es_transport_t* transp,
39-
IN const char const *msg,
40-
IN const unsigned int size,
41-
OUT void *ctx);
38+
IN es_transport_t * transp,
39+
IN const char const * msg,
40+
IN const unsigned int size,
41+
OUT void * ctx);
4242

4343

4444
struct es_transport_callbacks_s {
45-
es_transport_event_cb event_cb;
46-
es_transport_msg_recv_cb msg_recv_cb;
47-
void *user_data;
45+
es_transport_event_cb event_cb;
46+
es_transport_msg_recv_cb msg_recv_cb;
47+
void * user_data;
4848
};
4949

50-
es_status es_transport_init(OUT es_transport_t **ctx, IN struct event_base *base);
50+
es_status es_transport_init(OUT es_transport_t ** ctx, IN struct event_base * base);
5151

52-
es_status es_transport_set_callbacks(IN es_transport_t *ctx, IN struct es_transport_callbacks_s *cs);
52+
es_status es_transport_set_callbacks(IN es_transport_t * ctx, IN struct es_transport_callbacks_s * cs);
5353

54-
es_status es_transport_start(es_transport_t *ctx);
54+
es_status es_transport_start(es_transport_t * ctx);
5555

56-
es_status es_transport_get_udp_socket(IN es_transport_t *ctx, OUT int *fd);
56+
es_status es_transport_get_udp_socket(IN es_transport_t * ctx, OUT int * fd);
5757

5858
es_status es_transport_send(
59-
IN es_transport_t *ctx,
60-
IN char *ip,
61-
IN int port,
62-
IN const char *msg,
63-
IN size_t size);
59+
IN es_transport_t * ctx,
60+
IN char * ip,
61+
IN int port,
62+
IN const char * msg,
63+
IN size_t size);
6464

6565
#if defined(__cplusplus)
6666
}

src/inc/estypes.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include <inttypes.h>
3131
#else
3232
#error Define HAVE_STDINT_H as 1 if you have <stdint.h>, \
33-
or HAVE_INTTYPES_H if you have <inttypes.h>
33+
or HAVE_INTTYPES_H if you have <inttypes.h>
3434
#endif
3535

3636
#if defined(__cplusplus)
@@ -46,8 +46,8 @@ extern "C" {
4646
#define OUT
4747
#define INOUT
4848

49-
#define VALUE2STRING(_m) #_m
50-
#define MACRO2STRING(_m) VALUE2STRING(_m)
49+
#define VALUE2STRING(_m) #_m
50+
#define MACRO2STRING(_m) VALUE2STRING(_m)
5151

5252

5353
#if defined(__cplusplus)

src/inc/log.h

+27-12
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,34 @@
1919
#define _ESIP_LOG_H_
2020

2121
/** LOGGING LEVELS */
22-
#define ESIP_LOG_EMERG 0
23-
#define ESIP_LOG_ALERT 1
24-
#define ESIP_LOG_CRIT 2
25-
#define ESIP_LOG_ERROR 3
26-
#define ESIP_LOG_WARNING 4
27-
#define ESIP_LOG_NOTICE 5
28-
#define ESIP_LOG_INFO 6
29-
#define ESIP_LOG_DEBUG 7
22+
#define ESIP_LOG_EMERG 0
23+
#define ESIP_LOG_ALERT 1
24+
#define ESIP_LOG_CRIT 2
25+
#define ESIP_LOG_ERROR 3
26+
#define ESIP_LOG_WARNING 4
27+
#define ESIP_LOG_NOTICE 5
28+
#define ESIP_LOG_INFO 6
29+
#define ESIP_LOG_DEBUG 7
30+
31+
32+
#define ES_EMERG(msg, ...) \
33+
do { \
34+
printf("[%s][EMERGENCY]- " msg "\r\n", PACKAGE, ##__VA_ARGS__); \
35+
} while(0)
36+
37+
#define ES_ALERT(msg, ...) \
38+
do { \
39+
printf("[%s][ALERT]- " msg "\r\n", PACKAGE, ##__VA_ARGS__); \
40+
} while(0)
3041

3142
/** */
32-
#define ESIP_TRACE(level, msg, ...) \
33-
do { \
34-
printf("[%s] %s(%d) - " msg "\r\n", PACKAGE, __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); \
35-
} while(0)
43+
#define ESIP_TRACE(level, msg, ...) \
44+
do { \
45+
if (level > ESIP_LOG_INFO) { \
46+
printf("[%s]- [%s(%d)]- " msg "\r\n", PACKAGE, __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); \
47+
} else { \
48+
printf("[%s]- " msg "\r\n", PACKAGE, ##__VA_ARGS__); \
49+
} \
50+
} while(0)
3651

3752
#endif /* _ESIP_LOG_H_ */

0 commit comments

Comments
 (0)