Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SystemConfiguration/SystemConfiguration.h>
- #include <CoreFoundation/CoreFoundation.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netinet/in_systm.h>
- #include <netinet/ip.h>
- #include <netinet/tcp.h>
- #include <arpa/inet.h>
- #include <ev.h>
- #include <getopt.h>
- #include <netdb.h>
- #include <signal.h>
- #include <ctype.h>
- #include <stdarg.h>
- #include <assert.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stddef.h>
- #include <fcntl.h>
- #include <sys/un.h>
- #include <unistd.h>
- #include <errno.h>
- #include <pthread.h>
- #define DEFAULT_CONNECT_TIMER 7.0
- #define SERVER_PORTNO 81
- #define SERVER_ADDRESS "google.com"
- #define LOG_ERR 0
- #define LOG_WARN 1
- #define LOG_DEBUG 2
- #define container_of(ptr, type, member) ({ \
- const typeof( ((type *)0)->member ) *__mptr = (ptr); \
- (type *)( (char *)__mptr - offsetof(type,member) );})
- bool bps_policy_server_status = TRUE;
- struct policy_server_heartbeat_ {
- ev_io io;
- ev_timer timer;
- int sd;
- struct sockaddr_in serveraddr;
- };
- typedef struct policy_server_heartbeat_ policy_server_heartbeat_t;
- void
- bps_log(int log_level, char *err_str, int err) {
- if (err < 0) {
- printf("%s: Errno:%d\n", err_str, err);
- } else {
- perror(err_str);
- }
- }
- int
- set_socket_nonblock(int sd) {
- fcntl(sd, F_SETFL, fcntl(sd, F_GETFL) | O_NONBLOCK);
- if (fcntl(sd, F_GETFL) & O_NONBLOCK) {
- /* Ah, non-block set */
- } else {
- return -1;
- }
- return 0;
- }
- static void hb_timeout_handler(struct ev_loop *loop, struct ev_timer *w, int events) {
- policy_server_heartbeat_t *ps_hb = container_of(w, policy_server_heartbeat_t, timer);
- printf("Checking HB timeout\n");
- if (events & EV_TIMEOUT) {
- ev_io_stop(loop, &ps_hb->io);
- ev_timer_stop(loop, &ps_hb->timer);
- printf("False\n");
- close(ps_hb->sd);
- sleep(10);
- //ev_clear_pending(loop, &ps_hb->timer);
- if ((ps_hb->sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
- bps_log(LOG_ERR, "Socket cannot be created to connect to server", errno);
- return;
- }
- if (set_socket_nonblock(ps_hb->sd)) {
- bps_log(LOG_ERR, "Could not set the socket to non-block", -1);
- return;
- }
- ev_io_set(&ps_hb->io, ps_hb->sd, EV_WRITE);
- //ev_timer_set(&ps_hb->timer, DEFAULT_CONNECT_TIMER + ev_now (loop) - ev_time (), 0);
- ev_io_start(loop, &ps_hb->io);
- ev_now_update(loop);
- ev_timer_set(&ps_hb->timer, DEFAULT_CONNECT_TIMER, 0);
- ev_timer_start(loop, &ps_hb->timer);
- try_again:
- if (connect(ps_hb->sd, (struct sockaddr *)&ps_hb->serveraddr, sizeof(struct sockaddr_in)) < 0) {
- if (errno != EINPROGRESS) {
- bps_log(LOG_ERR, "Could not connect to server", errno);
- sleep(10);
- goto try_again;
- }
- }
- }
- }
- static void hb_conn_cb(struct ev_loop *loop, struct ev_io *w, int events) {
- policy_server_heartbeat_t *ps_hb = (policy_server_heartbeat_t *)w;
- if(EV_ERROR & events)
- {
- bps_log(LOG_ERR, "TCP socket call back error", errno);
- return;
- }
- printf("Checking HB\n");
- if (EV_WRITE & events) {
- ev_io_stop(loop, &ps_hb->io);
- ev_timer_stop(loop, &ps_hb->timer);
- printf("TRUE\n");
- close(ps_hb->sd);
- sleep(10);
- //ev_clear_pending(loop, &ps_hb->timer);
- if ((ps_hb->sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
- bps_log(LOG_ERR, "Socket cannot be created to connect to server", errno);
- return;
- }
- if (set_socket_nonblock(ps_hb->sd)) {
- bps_log(LOG_ERR, "Could not set the socket to non-block", -1);
- return;
- }
- ev_io_set(&ps_hb->io, ps_hb->sd, EV_WRITE);
- //ev_timer_set(&ps_hb->timer, DEFAULT_CONNECT_TIMER + ev_now (loop) - ev_time (), 0);
- ev_io_start(loop, &ps_hb->io);
- ev_now_update(loop);
- ev_timer_set(&ps_hb->timer, DEFAULT_CONNECT_TIMER, 0);
- ev_timer_start(loop, &ps_hb->timer);
- try_again:
- if (connect(ps_hb->sd, (struct sockaddr *)&ps_hb->serveraddr, sizeof(struct sockaddr_in)) < 0) {
- if (errno != EINPROGRESS) {
- bps_log(LOG_ERR, "Could not connect to server", errno);
- sleep(10);
- goto try_again;
- }
- }
- }
- return;
- }
- int
- init_ps_heartbeat(policy_server_heartbeat_t *ps_hb, struct ev_loop *loop) {
- ev_io_init(&ps_hb->io, hb_conn_cb, ps_hb->sd, EV_WRITE);
- ev_timer_init(&ps_hb->timer, hb_timeout_handler, DEFAULT_CONNECT_TIMER, 0);
- ev_io_start(loop, &ps_hb->io);
- ev_timer_start(loop, &ps_hb->timer);
- return 0;
- }
- int main(int argc, const char * argv[])
- {
- // insert code here...
- int sd = 0;
- struct hostent *hostp = NULL;
- struct sockaddr_in serveraddr;
- struct ev_loop *loop = ev_default_loop(0);
- policy_server_heartbeat_t ps_hb;
- memset(&serveraddr, 0x00, sizeof(struct sockaddr_in));
- serveraddr.sin_family = AF_INET;
- serveraddr.sin_port = htons(SERVER_PORTNO);
- hostp = gethostbyname(SERVER_ADDRESS);
- if (hostp) {
- memcpy(&serveraddr.sin_addr, hostp->h_addr, sizeof(serveraddr.sin_addr));
- } else {
- bps_log(LOG_ERR, "Server address could not be resolved", errno);
- return -1;
- }
- if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
- bps_log(LOG_ERR, "Socket cannot be created to connect to server", errno);
- return -1;
- }
- if (set_socket_nonblock(sd)) {
- bps_log(LOG_ERR, "Could not set the socket to non-block", -1);
- return -1;
- }
- ps_hb.sd = sd;
- memcpy(&ps_hb.serveraddr, &serveraddr, sizeof(struct sockaddr_in));
- init_ps_heartbeat(&ps_hb, loop);
- try_again:
- if (connect(ps_hb.sd, (struct sockaddr *)&ps_hb.serveraddr, sizeof(struct sockaddr_in)) < 0) {
- if (errno != EINPROGRESS) {
- bps_log(LOG_ERR, "Could not connect to server", errno);
- sleep(10);
- goto try_again;
- }
- }
- ev_loop(loop, 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement