public
Last active

My attempt at making an erlang node

  • Download Gist
erl_node.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
//To make gethostname be defined
#define _BSD_SOURCE
 
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
 
#include "erl-commands.h"
 
#define BUFSIZE 1000
 
int my_listen(int port);
void handle_msg(ErlMessage emsg);
 
extern int fd; //erlang connection
extern char * rem_name;
 
int main(int argc, char **argv) {
 
if (argc <= 4 || !strcmp(argv[1],"-h") || !strcmp(argv[1],"--help")) {
printf("\nUsage: ./redis-proxy <sname> <lport> <rem-sname> <cookie> <redis-host> <redis-port>\n\n");
printf(" sname the short name you want this c-node to connect as\n");
printf(" lport the port you want this node to listen for new erlang connections on\n");
printf(" rem-name the long name of the node you want to connect to\n");
printf(" cookie the authentication cookie\n");
return 0;
}
 
char * sname = argv[1];
int port = atoi(argv[2]);
rem_name = argv[3];
char * cookie = argv[4];
 
char hostname[50];
gethostname(hostname,50);
 
char full_name[50];
full_name[0] = '\0';
strncat(full_name,sname,50);
strncat(full_name,"@",50);
strncat(full_name,"127.0.0.1",50);
 
struct in_addr addr; /* 32-bit IP number of host */
int listen; /* Listen socket */
int fd; /* fd to Erlang node */
ErlConnect conn; /* Connection data */
 
erl_init(NULL, 0);
 
addr.s_addr = inet_addr("127.0.0.1");
 
fprintf(stderr, "running erl_connect_xinit\n");
if (erl_connect_xinit(hostname, sname, full_name, &addr, cookie, 0) == -1)
erl_err_quit("erl_connect_xinit");
 
/* Make a listen socket */
fprintf(stderr, "setting up listen\n");
if ((listen = my_listen(port)) <= 0)
erl_err_quit("my_listen");
 
fprintf(stderr, "publishing port\n");
if (erl_publish(port) == -1)
erl_err_quit("erl_publish");
 
fprintf(stderr, "accepting\n");
if ((fd = erl_accept(listen, &conn)) == ERL_ERROR)
erl_err_quit("erl_accept");
 
fprintf(stderr, "Connected to %s\n\r", conn.nodename);
 
int loop = 1;
while (loop) {
 
unsigned char buf[BUFSIZE];
ErlMessage emsg;
int got = erl_receive_msg(fd, buf, BUFSIZE, &emsg);
 
if (got == ERL_TICK) {
/* ignore */
}
 
else if (got == ERL_ERROR) {
loop = 0;
}
 
else if (emsg.type == ERL_REG_SEND) {
 
handle_msg(emsg);
erl_free_term(emsg.from);
erl_free_term(emsg.msg);
erl_free_term(emsg.to);
 
}
}
 
return 0;
}
 
int my_listen(int port) {
int listen_fd;
struct sockaddr_in addr;
int on = 1;
 
if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
return (-1);
 
setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
 
memset((void*) &addr, 0, (size_t) sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
 
if (bind(listen_fd, (struct sockaddr*) &addr, sizeof(addr)) < 0)
return (-1);
 
listen(listen_fd, 5);
return listen_fd;
}
 
void handle_msg(ErlMessage emsg) {
 
if (!ERL_IS_TUPLE(emsg.msg)) return;
 
ETERM * _command = erl_element(1,emsg.msg);
erl_send(fd, emsg.from, _command);
erl_free_term(_command);
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.