View difference between Paste ID: Zvzurj2N and qUt205T5
SHOW: | | - or go back to the newest paste.
1
#include <SystemConfiguration/SystemConfiguration.h>
2
#include <CoreFoundation/CoreFoundation.h>
3
#include <sys/types.h>
4
#include <sys/stat.h>
5
#include <sys/socket.h>
6
#include <netinet/in.h>
7
#include <netinet/in_systm.h>
8
#include <netinet/ip.h>
9
#include <netinet/tcp.h>
10
#include <arpa/inet.h>
11
#include <ev.h>
12
#include <getopt.h>
13
#include <netdb.h>
14
#include <signal.h>
15
#include <ctype.h>
16
#include <stdarg.h>
17
#include <assert.h>
18
#include <stdio.h>
19
#include <stdlib.h>
20
#include <string.h>
21
#include <stddef.h>
22
#include <fcntl.h>
23
#include <sys/un.h>
24
#include <unistd.h>
25
#include <errno.h>
26
#include <pthread.h>
27
28
#define DEFAULT_CONNECT_TIMER 7.0
29-
#define SERVER_PORTNO  80
29+
#define SERVER_PORTNO  81
30
#define SERVER_ADDRESS "google.com"
31
32
#define LOG_ERR   0
33
#define LOG_WARN  1
34
#define LOG_DEBUG 2
35
36
37
#define container_of(ptr, type, member) ({ \
38
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
39
(type *)( (char *)__mptr - offsetof(type,member) );})
40
41
bool bps_policy_server_status = TRUE;
42
43
struct policy_server_heartbeat_ {
44
    ev_io io;
45
    ev_timer timer;
46
    int sd;
47
    struct sockaddr_in serveraddr;
48
};
49
50
typedef struct policy_server_heartbeat_ policy_server_heartbeat_t;
51
52
void
53
bps_log(int log_level, char *err_str, int err) {
54
    if (err < 0) {
55
        printf("%s: Errno:%d\n", err_str, err);
56
    } else {
57
        perror(err_str);
58
    }
59
}
60
61
int
62
set_socket_nonblock(int sd) {
63
    
64
    fcntl(sd, F_SETFL, fcntl(sd, F_GETFL) | O_NONBLOCK);
65
    if (fcntl(sd, F_GETFL) & O_NONBLOCK) {
66
        /* Ah, non-block set */
67
    } else {
68
        return -1;
69
    }
70
    return 0;
71
}
72
73
74
static void hb_timeout_handler(struct ev_loop *loop, struct ev_timer *w, int events) {
75
    
76
    policy_server_heartbeat_t *ps_hb = container_of(w, policy_server_heartbeat_t, timer);
77
    printf("Checking HB timeout\n");
78
    if (events & EV_TIMEOUT) {
79
    ev_io_stop(loop, &ps_hb->io);
80
    ev_timer_stop(loop, &ps_hb->timer);
81
  
82
    printf("False\n");
83
    
84
    close(ps_hb->sd);
85
    sleep(10);
86
    //ev_clear_pending(loop, &ps_hb->timer);
87
    if ((ps_hb->sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
88
        bps_log(LOG_ERR, "Socket cannot be created to connect to server", errno);
89
        return;
90
    }
91
    
92
    if (set_socket_nonblock(ps_hb->sd)) {
93
        bps_log(LOG_ERR, "Could not set the socket to non-block", -1);
94
        return;
95
    }
96
    ev_io_set(&ps_hb->io, ps_hb->sd, EV_WRITE);
97-
    ev_timer_set(&ps_hb->timer, DEFAULT_CONNECT_TIMER + ev_now (loop) - ev_time (), 0);
97+
    //ev_timer_set(&ps_hb->timer, DEFAULT_CONNECT_TIMER + ev_now (loop) - ev_time (), 0);
98
    ev_io_start(loop, &ps_hb->io);
99
    ev_now_update(loop);
100
    ev_timer_set(&ps_hb->timer, DEFAULT_CONNECT_TIMER, 0);
101
    ev_timer_start(loop, &ps_hb->timer);
102
    
103
try_again:
104
    if (connect(ps_hb->sd, (struct sockaddr *)&ps_hb->serveraddr, sizeof(struct sockaddr_in)) < 0) {
105
        if (errno != EINPROGRESS) {
106
            bps_log(LOG_ERR, "Could not connect to server", errno);
107
            sleep(10);
108
            goto try_again;
109
        }
110
    }
111
    }
112
}
113
114
115
static void hb_conn_cb(struct ev_loop *loop, struct ev_io *w, int events) {
116
    
117
    policy_server_heartbeat_t *ps_hb = (policy_server_heartbeat_t *)w;
118
    
119
    if(EV_ERROR & events)
120
    {
121
        bps_log(LOG_ERR, "TCP socket call back error", errno);
122
        return;
123
    }
124
    printf("Checking HB\n");
125
    if (EV_WRITE & events) {
126
        ev_io_stop(loop, &ps_hb->io);
127
        ev_timer_stop(loop, &ps_hb->timer);
128
       
129
        printf("TRUE\n");
130
        close(ps_hb->sd);
131
        sleep(10);
132
        //ev_clear_pending(loop, &ps_hb->timer);
133
        if ((ps_hb->sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
134
            bps_log(LOG_ERR, "Socket cannot be created to connect to server", errno);
135
            return;
136
        }
137
        
138
        if (set_socket_nonblock(ps_hb->sd)) {
139
            bps_log(LOG_ERR, "Could not set the socket to non-block", -1);
140
            return;
141-
        ev_timer_set(&ps_hb->timer, DEFAULT_CONNECT_TIMER + ev_now (loop) - ev_time (), 0);
141+
142
        ev_io_set(&ps_hb->io, ps_hb->sd, EV_WRITE);
143
        //ev_timer_set(&ps_hb->timer, DEFAULT_CONNECT_TIMER + ev_now (loop) - ev_time (), 0);
144
        ev_io_start(loop, &ps_hb->io);
145
        ev_now_update(loop);
146
        ev_timer_set(&ps_hb->timer, DEFAULT_CONNECT_TIMER, 0);
147
        ev_timer_start(loop, &ps_hb->timer);
148
        
149
    try_again:
150
        if (connect(ps_hb->sd, (struct sockaddr *)&ps_hb->serveraddr, sizeof(struct sockaddr_in)) < 0) {
151
            if (errno != EINPROGRESS) {
152
                bps_log(LOG_ERR, "Could not connect to server", errno);
153
                sleep(10);
154
                goto try_again;
155
            }
156
        }
157
    }
158
    return;
159
}
160
161
int
162
init_ps_heartbeat(policy_server_heartbeat_t *ps_hb, struct ev_loop *loop) {
163
    ev_io_init(&ps_hb->io, hb_conn_cb, ps_hb->sd, EV_WRITE);
164
    ev_timer_init(&ps_hb->timer, hb_timeout_handler, DEFAULT_CONNECT_TIMER, 0);
165
    ev_io_start(loop, &ps_hb->io);
166
    ev_timer_start(loop, &ps_hb->timer);
167
    return 0;
168
}
169
170
int main(int argc, const char * argv[])
171
{
172
173
    // insert code here...
174
    int sd = 0;
175
    struct hostent *hostp = NULL;
176
    struct sockaddr_in serveraddr;
177
    struct ev_loop *loop = ev_default_loop(0);
178
    policy_server_heartbeat_t ps_hb;
179
    
180
    memset(&serveraddr, 0x00, sizeof(struct sockaddr_in));
181
    serveraddr.sin_family = AF_INET;
182
    serveraddr.sin_port = htons(SERVER_PORTNO);
183
    
184
    hostp = gethostbyname(SERVER_ADDRESS);
185
    if (hostp) {
186
        memcpy(&serveraddr.sin_addr, hostp->h_addr, sizeof(serveraddr.sin_addr));
187
    } else {
188
        bps_log(LOG_ERR, "Server address could not be resolved", errno);
189
        return -1;
190
    }
191
    
192
    if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
193
        bps_log(LOG_ERR, "Socket cannot be created to connect to server", errno);
194
        return -1;
195
    }
196
    
197
    if (set_socket_nonblock(sd)) {
198
        bps_log(LOG_ERR, "Could not set the socket to non-block", -1);
199
        return -1;
200
    }
201
    ps_hb.sd = sd;
202
    memcpy(&ps_hb.serveraddr, &serveraddr, sizeof(struct sockaddr_in));
203
    init_ps_heartbeat(&ps_hb, loop);
204
    
205
try_again:
206
    if (connect(ps_hb.sd, (struct sockaddr *)&ps_hb.serveraddr, sizeof(struct sockaddr_in)) < 0) {
207
        if (errno != EINPROGRESS) {
208
            bps_log(LOG_ERR, "Could not connect to server", errno);
209
            sleep(10);
210
            goto try_again;
211
        }
212
    }
213
    
214
    ev_loop(loop, 0);
215
216
    return 0;
217
}