starter: Use stream abstraction to communicate with stroke plugin

This commit is contained in:
Tobias Brunner
2014-06-19 13:56:37 +02:00
parent 906a409608
commit 02de66e1bf
+16 -33
View File
@@ -13,16 +13,9 @@
* for more details. * for more details.
*/ */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stddef.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <credentials/auth_cfg.h> #include <credentials/auth_cfg.h>
@@ -56,48 +49,38 @@ static char* push_string(stroke_msg_t *msg, char *string)
static int send_stroke_msg (stroke_msg_t *msg) static int send_stroke_msg (stroke_msg_t *msg)
{ {
struct sockaddr_un ctl_addr; stream_t *stream;
int byte_count; char *uri, buffer[64];
char buffer[64]; int count;
ctl_addr.sun_family = AF_UNIX;
strcpy(ctl_addr.sun_path, CHARON_CTL_FILE);
/* starter is not called from commandline, and therefore absolutely silent */ /* starter is not called from commandline, and therefore absolutely silent */
msg->output_verbosity = -1; msg->output_verbosity = -1;
int sock = socket(AF_UNIX, SOCK_STREAM, 0); uri = lib->settings->get_str(lib->settings, "%s.plugins.stroke.socket",
"unix://" CHARON_CTL_FILE, daemon_name);
if (sock < 0) stream = lib->streams->connect(lib->streams, uri);
if (!stream)
{ {
DBG1(DBG_APP, "socket() failed: %s", strerror(errno)); DBG1(DBG_APP, "failed to connect to stroke socket '%s'", uri);
return -1;
}
if (connect(sock, (struct sockaddr *)&ctl_addr, offsetof(struct sockaddr_un, sun_path) + strlen(ctl_addr.sun_path)) < 0)
{
DBG1(DBG_APP, "connect(charon_ctl) failed: %s", strerror(errno));
close(sock);
return -1; return -1;
} }
/* send message */ if (!stream->write_all(stream, msg, msg->length))
if (write(sock, msg, msg->length) != msg->length)
{ {
DBG1(DBG_APP, "write(charon_ctl) failed: %s", strerror(errno)); DBG1(DBG_APP, "sending stroke message failed");
close(sock); stream->destroy(stream);
return -1; return -1;
} }
while ((byte_count = read(sock, buffer, sizeof(buffer)-1)) > 0) while ((count = stream->read(stream, buffer, sizeof(buffer)-1, TRUE)) > 0)
{ {
buffer[byte_count] = '\0'; buffer[count] = '\0';
DBG1(DBG_APP, "%s", buffer); DBG1(DBG_APP, "%s", buffer);
} }
if (byte_count < 0) if (count < 0)
{ {
DBG1(DBG_APP, "read() failed: %s", strerror(errno)); DBG1(DBG_APP, "reading stroke response failed");
} }
stream->destroy(stream);
close(sock);
return 0; return 0;
} }