Packages
vintage_net
0.2.0
0.13.12
0.13.11
0.13.10
0.13.9
0.13.8
0.13.7
0.13.6
0.13.5
0.13.4
0.13.3
0.13.2
0.13.1
0.13.0
0.12.2
0.12.1
0.12.0
0.11.5
0.11.4
0.11.3
0.11.2
0.11.1
0.11.0
0.10.5
0.10.4
0.10.3
0.10.2
0.10.1
0.10.0
0.9.3
0.9.2
0.9.1
0.9.0
0.8.0
0.7.9
0.7.8
0.7.7
0.7.6
0.7.5
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.6
0.6.5
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
0.5.1
0.5.0
0.4.1
0.4.0
0.3.1
0.3.0
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.1.0
Network configuration and management for Nerves
Current section
Files
Jump to
Current section
Files
src/to_elixir.c
#include <err.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <ei.h>
#define SOCKET_PATH "/tmp/vintage_net/comms"
static void encode_string(ei_x_buff *buff, const char *str)
{
// Encode strings as binaries so that we get Elixir strings
// NOTE: the strings that we encounter here are expected to be ASCII to
// my knowledge
ei_x_encode_binary(buff, str, strlen(str));
}
int main(int argc, char *argv[])
{
if (argc != 2)
errx(EXIT_FAILURE, "Expecting a message");
int fd = socket(AF_UNIX, SOCK_DGRAM, 0);
if (fd < 0)
err(EXIT_FAILURE, "socket");
struct sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, SOCKET_PATH, sizeof(addr.sun_path) - 1);
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
err(EXIT_FAILURE, "connect");
ei_x_buff buff;
if (ei_x_new_with_version(&buff) < 0)
err(EXIT_FAILURE, "ei_x_new_with_version");
ei_x_encode_tuple_header(&buff, 2);
ei_x_encode_atom(&buff, "to_elixir");
encode_string(&buff, argv[1]);
ssize_t rc = write(fd, buff.buff, buff.index);
if (rc < 0)
err(EXIT_FAILURE, "write");
if (rc != buff.index)
errx(EXIT_FAILURE, "write wasn't able to send %d chars all at once!", buff.index);
close(fd);
return 0;
}