amiws  2.2.1
amiws.h
Go to the documentation of this file.
1 
29 #ifndef __AMIWS_H
30 #define __AMIWS_H
31 
32 #include <stdio.h>
33 #include <syslog.h>
34 #include <yaml.h>
35 
36 #include "amipack.h"
37 #include "config.h"
38 #include "frozen.h"
39 #include "mongoose.h"
40 
42 #define DEFAULT_CONF_FILE "/etc/amiws.yaml"
44 #define DEFAULT_WEB_ROOT "./web_root"
46 #define DEFAULT_LOG_LEVEL LOG_INFO
48 #define DEFAULT_LOG_FACILITY LOG_SYSLOG
50 #define DEFAULT_WEBSOCK_PORT 8000
52 #define POLL_SLEEP 1000
53 
55 #define intval(val) str2int(val, strlen(val))
56 
58 #define macro_init_conf(conf) conf = (struct amiws_config *) malloc(sizeof(struct amiws_config));\
59  memset(conf,0,sizeof(struct amiws_config)); \
60  conf->log_level = DEFAULT_LOG_LEVEL; \
61  conf->log_facility = DEFAULT_LOG_FACILITY; \
62  conf->ws_port = DEFAULT_WEBSOCK_PORT; \
63  conf->size = 0; \
64  conf->head = NULL; \
65  conf->tail = NULL; \
66  conf->web_root = NULL; \
67  conf->auth_domain = NULL; \
68  conf->parse_fail = 0; \
69  conf->syntax_error = 0; \
70  conf->auth_file = NULL;
71 
72 
74 #define macro_init_conn(conn) conn = (struct amiws_conn *) malloc(sizeof(struct amiws_conn)); \
75  memset(conn,0,sizeof(struct amiws_conn)); \
76  conn->port = 5038; \
77  conn->address = NULL; \
78  conn->name = NULL; \
79  conn->host = NULL; \
80  conn->username = NULL; \
81  conn->secret = NULL; \
82  conn->is_ssl = 0; \
83  conn->event_names = NULL; \
84 
85 
90 struct amiws_config {
91  struct amiws_conn *head;
92  struct amiws_conn *tail;
93  int size;
95  int log_level;
96  int ws_port;
97  char *web_root;
98  char *auth_domain;
99  char *auth_file;
102 #if MG_ENABLE_SSL
103  char *ssl_cert;
104  char *ssl_key;
105 #endif
106 
107  int parse_fail:1;
108  int syntax_error:1;
109 };
110 
114 struct amiws_conn {
115  int id;
116  char *name;
117  char *address;
118  unsigned int port;
119  char *host;
120  char *username;
121  char *secret;
122  char *event_names;
124 #if MG_ENABLE_SSL
125  char *ssl_cert;
126  char *ssl_key;
127 #endif
128  int is_ssl;
129  struct amiws_conn *next;
130 };
131 
132 int isExiting();
133 
134 void setExiting();
135 
142 void amiws_init(struct amiws_config *conf);
143 
148 void amiws_connect_ami_server(struct amiws_conn *conn);
149 
153 void amiws_destroy();
154 
158 void amiws_loop();
159 
166 void ami_ev_handler(struct mg_connection *nc, int ev, void *ev_data);
167 
174 void websock_ev_handler (struct mg_connection *nc, int ev, void *ev_data);
175 
181 void websock_send (struct mg_connection *nc, const char *json);
182 
188 void ami_login(struct mg_connection *nc, struct amiws_conn *conn);
189 
197 char *amipack_to_json(const char *buf, int len, struct amiws_conn *conn);
198 
204 struct amiws_config *read_conf(const char *filename);
205 
213 int scan_amipack(const char *pack, size_t len);
214 
219 void free_conf(struct amiws_config *conf);
220 
221 /* private methods */
222 
223 static void recv_callback(struct mbuf *io, struct amiws_conn *conn, struct mg_connection *nc);
224 
225 static void read_buffer(struct mbuf *io, struct mg_connection *nc);
226 
227 void set_conf_param(struct amiws_config *conf, char *key, char *value);
228 
229 void set_conn_param(struct amiws_conn *conn, char *key, char *value);
230 
231 static int str2int(const char *val, int len);
232 
233 static char* int2str(const char *val, int len);
234 
235 static int auth_fail(AMIPacket *amipack);
236 
237 static void send_ami_action(struct websocket_message *wm, struct mg_connection *nc);
238 
239 static void json_scan_cb(void *callback_data,
240  const char *name, size_t name_len,
241  const char *path,
242  const struct json_token *token);
243 
244 static struct amiws_config *valid_conf(struct amiws_config *conf);
245 
246 static int is_valid_auth_settings(struct amiws_config *conf);
247 
248 #if MG_ENABLE_SSL
249 static int is_valid_ssl_settings(struct amiws_config *conf);
250 
251 static int is_valid_ssl_conn_settings(struct amiws_conn *conn);
252 #endif
253 
254 #endif
AMI (Asterisk Management Interface) messages read/create functions interface. AMI packet is implement...
void websock_send(struct mg_connection *nc, const char *json)
Definition: amiws_lib.c:279
void ami_ev_handler(struct mg_connection *nc, int ev, void *ev_data)
Definition: amiws_lib.c:116
void amiws_init(struct amiws_config *conf)
Definition: amiws_lib.c:35
void amiws_destroy()
Definition: amiws_lib.c:103
void amiws_connect_ami_server(struct amiws_conn *conn)
Definition: amiws_lib.c:79
void ami_login(struct mg_connection *nc, struct amiws_conn *conn)
Definition: amiws_lib.c:327
char * amipack_to_json(const char *buf, int len, struct amiws_conn *conn)
Definition: amiws_lib.c:173
void amiws_loop()
Definition: amiws_lib.c:110
int scan_amipack(const char *pack, size_t len)
Definition: amiws_lib.c:377
struct amiws_config * read_conf(const char *filename)
Definition: config.c:32
void free_conf(struct amiws_config *conf)
Definition: config.c:236
void websock_ev_handler(struct mg_connection *nc, int ev, void *ev_data)
Definition: amiws_lib.c:293
Definition: amipack.h:109
Definition: amipack.h:65
Definition: amiws.h:90
char * auth_file
Definition: amiws.h:99
char * web_root
Definition: amiws.h:97
int ws_port
Definition: amiws.h:96
int log_facility
Definition: amiws.h:94
char * auth_domain
Definition: amiws.h:98
int log_level
Definition: amiws.h:95
struct amiws_conn * head
Definition: amiws.h:91
struct amiws_conn * tail
Definition: amiws.h:92
int size
Definition: amiws.h:93
Definition: amiws.h:114
char * name
Definition: amiws.h:116
int id
Definition: amiws.h:115
struct amiws_conn * next
Definition: amiws.h:129
int is_ssl
Definition: amiws.h:128
char * host
Definition: amiws.h:119
char * username
Definition: amiws.h:120
char * address
Definition: amiws.h:117
char * secret
Definition: amiws.h:121
unsigned int port
Definition: amiws.h:118
char * event_names
Definition: amiws.h:122
AMIVer ami_ver
Definition: amiws.h:123