Pleas ignore the exit(0) in the fail connect() condition. I have removed it since non blocking connect will return <0 anyways. still the output I get is connecting failed DISPATCHING IN CB CONNECT IN CB CHECKSTATUS ADDED READ EVENT
On Wed, Feb 12, 2020 at 10:34 PM Salman Ahmed <[email protected]> wrote: > Hello, > > I am trying to connect to the local openvpn client over its managment > socket and query its state/status. > > I have implemented the follow program with libevent support. > > > #include <stdio.h>#include <stdlib.h>#include <sys/socket.h>#include > <arpa/inet.h>#include <event2/event.h>#include <fcntl.h>#include <string.h> > char message[1000], server_reply[2000];struct event *ev, *ev1, *ev2; > void cb_func_hold_release(evutil_socket_t fd, short what, void *arg){ > int sock = *(int *)arg; > struct timeval one_seconds = {1,0}; > printf("IN CB CONNECT\n"); > sprintf (message, "hold release\n"); > if (send (sock, message, strlen (message), 0) < 0) { > printf("send failed\n"); > } > event_add(ev1, NULL);} > void cb_func_write(evutil_socket_t fd, short what, void *arg){ > int sock = *(int *)arg; > printf("IN CB CHECKSTATUS\n"); > sprintf (message, "status\n"); > if (send (sock, message, strlen (message), 0) < 0) { > printf("send failed\n"); > } > /* > int reclen = recv (sock, server_reply, 2000, 0); > if (reclen < 0) { > printf("recving failed"); > } else { > if (reclen > 1) { > server_reply[reclen-1] = '\0'; > } > printf("SERVER REPLT< %s\n", server_reply); > } > if (strstr(message, "CONNECTED")) { > printf("VPN CONNECTED"); > } > */ > event_add(ev2, NULL); > if (event_pending(ev2,EV_READ|EV_PERSIST, NULL)) { > printf("ADDED READ EVENT\n"); > > }} > void cb_func_read(evutil_socket_t fd, short what, void *arg){ > int sock = *(int *)arg; > printf("IN CB READ\n"); > int reclen = recv (sock, server_reply, 2000, 0); > if (reclen < 0) { > printf("recving failed"); > } else { > if (reclen > 1) { > server_reply[reclen-1] = '\0'; > } > printf("SERVER REPLY %s\n", server_reply); > } > if (strstr(message, "CONNECTED")) { > printf("VPN CONNECTED"); > } > } > int main(void){ > int flags; > int mgmretry = 0; > int sock = 0; > struct sockaddr_in server; > > struct timeval five_seconds = {1,0}; > struct event_base *base = event_base_new(); > > > ev = event_new(base, sock, EV_TIMEOUT, cb_func_hold_release, &sock); > ev1 = event_new(base, sock, EV_WRITE, cb_func_write, &sock); > ev2 = event_new(base, sock, EV_READ, cb_func_read, &sock); > > sock = socket (AF_INET, SOCK_STREAM, 0); > if (sock == -1) { > printf("COUILD NOT CREATE SOCKET\n"); > exit(0); > } > evutil_make_socket_nonblocking(sock); > > server.sin_addr.s_addr = inet_addr("127.0.0.1"); > server.sin_family = AF_INET; > server.sin_port = htons (12345); > > if (connect (sock, (struct sockaddr *)&server, sizeof (server)) < 0) { > printf("connecting failed\n"); > exit(0); > } > > > > event_add(ev, &five_seconds); > printf("DISPATCHING\n"); > event_base_dispatch(base); > } > > The issue is the the read callback "cb_func_read" is not being triggered. > My expectation is that after I send the "status" message to the vpn from > cb_func_write(), there is some data sent back which should trigger the read > callback but that's not happening. > > If I recv() directly after send() in the write callback(the commented > portion) I can print the response sent from openvpn so the communication > between these two are working. >
