static int init(int *sd, int *portnr, int *epmd_fd) { char host[HOSTNAMESZ]; char servernode[NODENAMESZ]; struct hostent *h; int error = 0; #ifdef __WIN32__ WORD wVersionRequested; WSADATA wsaData; wVersionRequested = MAKEWORD(1, 1); if ((error = WSAStartup(wVersionRequested, &wsaData))) { fprintf(stderr,"Can't initialize windows sockets: %d",error); } #endif /* get the host name */ error = gethostname(host,HOSTNAMESZ); if (error) { #ifdef __WIN32__ fprintf(stderr,"can't find own hostname (error = %ld) !\n",WSAGetLastError()); #else /* not __WIN32__ */ fprintf(stderr,"can't find own hostname !\n"); #endif } else { /* identify host */ if (!(h = erl_gethostbyname(host))) fprintf(stdout,"can't find own ip address\n"); else { /* get a listen port. 0 means let system choose port number */ *sd = getlisten(0); /* what port did we get? */ /* this call not necessary if we specified port in call to getlisten() */ *portnr = getport(*sd); /* make the nodename server@host */ sprintf(servernode,"%s@%s",SERVER,host); /* initiate */ erl_init(NULL,0); /* host, alive, alive@host, addr, cookie, creation */ erl_connect_xinit(host,SERVER,servernode,(Erl_IpAddr)(h->h_addr_list[0]),COOKIE,0); /* let epmd know we are here */ *epmd_fd = erl_publish(*portnr); return 0; } } return -1; }
int main(int argc, char **argv) { int port; // Listen port number int fd; // Socket descriptor int listen; // Listen socket ErlConnect conn; // Connection data char *cookie; // Erlang magic cookie pid_t pid; int x; pthread_t thread; pthread_attr_t thread_attrs; thread_data_t *data; // Per thread data: thread ID, // connection node name static unsigned int tidx = 0; pid = fork(); if (pid < 0) exit(EXIT_FAILURE); if (pid > 0) exit(EXIT_SUCCESS); if (setsid() < 0) exit(EXIT_FAILURE); umask(0); chdir("/"); signal(SIGCHLD, SIG_DFL); signal(SIGHUP, SIG_DFL); if (argc == 3) { port = atoi(argv[1]); cookie = argv[2]; } else { fprintf(stderr, "Usage: %s <port number> <erlang secret cookie>\n\r",argv[0]); exit(EXIT_FAILURE); } for (x = sysconf(_SC_OPEN_MAX); x>0; x--) close (x); setlogmask (LOG_UPTO (LOG_NOTICE)); openlog("nfq_node", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL0); erl_init(NULL, 0); if (erl_connect_init(1, cookie, 0) == -1) erl_err_quit("erl_connect_init"); if ((listen = my_listen(port)) <= 0) erl_err_quit("my_listen"); if (erl_publish(port) == -1) erl_err_quit("erl_publish"); if (pthread_attr_init(&thread_attrs)) { syslog(LOG_ERR, "error while init pthread attr struct\n\r"); exit(EXIT_FAILURE); } if ((pthread_attr_setdetachstate(&thread_attrs, PTHREAD_CREATE_DETACHED))) { syslog(LOG_ERR, "error while set pthread attributes\n\r"); exit(EXIT_FAILURE); } syslog(LOG_NOTICE,"Waiting for connections...\n\r"); for(;;) { while((fd = erl_accept(listen, &conn)) == ERL_ERROR) syslog(LOG_ERR,"%s Connection error\n\r", argv[0]); if((data = (thread_data_t *)malloc(sizeof(thread_data_t))) == NULL) { syslog(LOG_ERR, "Memory allocation error\n\r"); exit(EXIT_FAILURE); } if((data->node = (char *)malloc(strlen(conn.nodename)+1)) == NULL) { syslog(LOG_ERR, "Memory allocation error\n\r"); exit(EXIT_FAILURE); } data->fd = fd; data->idx = tidx; strcpy(data->node, conn.nodename); if (pthread_create(&thread, &thread_attrs, erl_message_read_loop, data)) { syslog(LOG_ERR, "Thread create failure\n\r"); exit(EXIT_FAILURE); } tidx++; } closelog(); return 0; }
int main(void) #endif { ei_x_buff eix; int index = 0; ETERM **etermpp = NULL, *etermp = NULL; char *charp = NULL; unsigned char uchar, **ucharpp = NULL, *ucharp = NULL; void *voidp = NULL; Erl_Heap *erl_heapp = NULL; int intx = 0; int *intp = NULL; unsigned int uintx, *uintp; unsigned long *ulongp = NULL; long longx = 0; double doublex = 0.0; short shortx = 42; FILE *filep = NULL; Erl_IpAddr erl_ipaddr = NULL; ErlMessage *erlmessagep = NULL; ErlConnect *erlconnectp = NULL; struct hostent *hostp = NULL; struct in_addr *inaddrp = NULL; /* Converion to erl_interface format is in liberl_interface */ intx = erl_errno; ei_encode_term(charp, &index, voidp); ei_x_encode_term(&eix, voidp); ei_decode_term(charp, &index, voidp); erl_init(voidp, longx); erl_connect_init(intx, charp,shortx); erl_connect_xinit(charp,charp,charp,erl_ipaddr,charp,shortx); erl_connect(charp); erl_xconnect(erl_ipaddr,charp); erl_close_connection(intx); erl_receive(intx, ucharp, intx); erl_receive_msg(intx, ucharp, intx, erlmessagep); erl_xreceive_msg(intx, ucharpp, intp, erlmessagep); erl_send(intx, etermp, etermp); erl_reg_send(intx, charp, etermp); erl_rpc(intx,charp,charp,etermp); erl_rpc_to(intx,charp,charp,etermp); erl_rpc_from(intx,intx,erlmessagep); erl_publish(intx); erl_accept(intx,erlconnectp); erl_thiscookie(); erl_thisnodename(); erl_thishostname(); erl_thisalivename(); erl_thiscreation(); erl_unpublish(charp); erl_err_msg(charp); erl_err_quit(charp); erl_err_ret(charp); erl_err_sys(charp); erl_cons(etermp,etermp); erl_copy_term(etermp); erl_element(intx,etermp); erl_hd(etermp); erl_iolist_to_binary(etermp); erl_iolist_to_string(etermp); erl_iolist_length(etermp); erl_length(etermp); erl_mk_atom(charp); erl_mk_binary(charp,intx); erl_mk_empty_list(); erl_mk_estring(charp, intx); erl_mk_float(doublex); erl_mk_int(intx); erl_mk_list(etermpp,intx); erl_mk_pid(charp,uintx,uintx,uchar); erl_mk_port(charp,uintx,uchar); erl_mk_ref(charp,uintx,uchar); erl_mk_long_ref(charp,uintx,uintx,uintx,uchar); erl_mk_string(charp); erl_mk_tuple(etermpp,intx); erl_mk_uint(uintx); erl_mk_var(charp); erl_print_term(filep,etermp); /* erl_sprint_term(charp,etermp); */ erl_size(etermp); erl_tl(etermp); erl_var_content(etermp, charp); erl_format(charp); erl_match(etermp, etermp); erl_global_names(intx, intp); erl_global_register(intx, charp, etermp); erl_global_unregister(intx, charp); erl_global_whereis(intx, charp, charp); erl_init_malloc(erl_heapp,longx); erl_alloc_eterm(uchar); erl_eterm_release(); erl_eterm_statistics(ulongp,ulongp); erl_free_array(etermpp,intx); erl_free_term(etermp); erl_free_compound(etermp); erl_malloc(longx); erl_free(voidp); erl_compare_ext(ucharp, ucharp); erl_decode(ucharp); erl_decode_buf(ucharpp); erl_encode(etermp,ucharp); erl_encode_buf(etermp,ucharpp); erl_ext_size(ucharp); erl_ext_type(ucharp); erl_peek_ext(ucharp,intx); erl_term_len(etermp); erl_gethostbyname(charp); erl_gethostbyaddr(charp, intx, intx); erl_gethostbyname_r(charp, hostp, charp, intx, intp); erl_gethostbyaddr_r(charp, intx, intx, hostp, charp, intx, intp); erl_init_resolve(); erl_distversion(intx); erl_epmd_connect(inaddrp); erl_epmd_port(inaddrp, charp, intp); charp = ERL_ATOM_PTR(etermp); intx = ERL_ATOM_SIZE(etermp); ucharp = ERL_BIN_PTR(etermp); intx = ERL_BIN_SIZE(etermp); etermp = ERL_CONS_HEAD(etermp); etermp = ERL_CONS_TAIL(etermp); intx = ERL_COUNT(etermp); doublex= ERL_FLOAT_VALUE(etermp); uintx = ERL_INT_UVALUE(etermp); intx = ERL_INT_VALUE(etermp); intx = ERL_IS_ATOM(etermp); intx = ERL_IS_BINARY(etermp); intx = ERL_IS_CONS(etermp); intx = ERL_IS_EMPTY_LIST(etermp); intx = ERL_IS_FLOAT(etermp); intx = ERL_IS_INTEGER(etermp); intx = ERL_IS_LIST(etermp); intx = ERL_IS_PID(etermp); intx = ERL_IS_PORT(etermp); intx = ERL_IS_REF(etermp); intx = ERL_IS_TUPLE(etermp); intx = ERL_IS_UNSIGNED_INTEGER(etermp); uchar = ERL_PID_CREATION(etermp); charp = ERL_PID_NODE(etermp); uintx = ERL_PID_NUMBER(etermp); uintx = ERL_PID_SERIAL(etermp); uchar = ERL_PORT_CREATION(etermp); charp = ERL_PORT_NODE(etermp); uintx = ERL_PORT_NUMBER(etermp); uchar = ERL_REF_CREATION(etermp); intx = ERL_REF_LEN(etermp); charp = ERL_REF_NODE(etermp); uintx = ERL_REF_NUMBER(etermp); uintp = ERL_REF_NUMBERS(etermp); etermp = ERL_TUPLE_ELEMENT(etermp,intx); intx = ERL_TUPLE_SIZE(etermp); return BUFSIZ + EAGAIN + EHOSTUNREACH + EINVAL + EIO + EMSGSIZE + ENOMEM + ERL_ATOM + ERL_BINARY + ERL_ERROR + ERL_EXIT + ERL_FLOAT + ERL_INTEGER + ERL_LINK + ERL_LIST + ERL_MSG + ERL_NO_TIMEOUT + ERL_PID + ERL_PORT + ERL_REF + ERL_REG_SEND + ERL_SEND + ERL_SMALL_BIG + ERL_TICK + ERL_TIMEOUT + ERL_TUPLE + ERL_UNLINK + ERL_U_INTEGER + ERL_U_SMALL_BIG + ERL_VARIABLE + ETIMEDOUT + MAXNODELEN + MAXREGLEN; }
int main(int argc, char **argv) { struct in_addr addr; /* 32-bit IP number of host */ int port; /* Listen port number */ int listen; /* Listen socket */ int fd; /* fd to Erlang node */ ErlConnect conn; /* Connection data */ int loop = 1; /* Lopp flag */ int got; /* Result of receive */ unsigned char buf[BUFSIZE]; /* Buffer for incoming message */ ErlMessage emsg; /* Incoming message */ ETERM *fromp, *tuplep, *fnp, *argp, *resp; int res; port = atoi(argv[1]); erl_init(NULL, 0); addr.s_addr = inet_addr("127.0.0.1"); if (erl_connect_xinit("alpha", "cnode", "*****@*****.**", &addr, "secretcookie", 0) == -1) erl_err_quit("erl_connect_xinit"); /* Make a listen socket */ if ((listen = my_listen(port)) <= 0) erl_err_quit("my_listen"); if (erl_publish(port) == -1) erl_err_quit("erl_publish"); if ((fd = erl_accept(listen, &conn)) == ERL_ERROR) erl_err_quit("erl_accept"); fprintf(stderr, "Connected to %s\n\r", conn.nodename); while (loop) { 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) { fromp = erl_element(2, emsg.msg); tuplep = erl_element(3, emsg.msg); fnp = erl_element(1, tuplep); argp = erl_element(2, tuplep); if (strncmp(ERL_ATOM_PTR(fnp), "foo", 3) == 0) { res = foo(ERL_INT_VALUE(argp)); } else if (strncmp(ERL_ATOM_PTR(fnp), "bar", 3) == 0) { res = bar(ERL_INT_VALUE(argp)); } resp = erl_format("{cnode, ~i}", res); erl_send(fd, fromp, resp); erl_free_term(emsg.from); erl_free_term(emsg.msg); erl_free_term(fromp); erl_free_term(tuplep); erl_free_term(fnp); erl_free_term(argp); erl_free_term(resp); } } } /* while */ }
int init_erlang(char *nodename, char *cookie) { struct sockaddr_in e_addr; char *ip; char *node; int efd; int rlen; char *cookiefile; char *cookiehome; char cookievalue[128]; int cookiefd; PyMethodDef *uwsgi_function; ip = strchr(nodename, '@'); if (ip == NULL) { fprintf(stderr, "*** invalid erlang node name ***\n"); return -1; } if (cookie == NULL) { // get the cookie from the home cookiehome = getenv("HOME"); if (!cookiehome) { fprintf(stderr, "unable to get erlang cookie from your home.\n"); return -1; } cookiefile = malloc(strlen(cookiehome) + 1 + strlen(".erlang.cookie") + 1); if (!cookiefile) { uwsgi_error("malloc()"); } cookiefile[0] = 0; strcat(cookiefile, cookiehome); strcat(cookiefile, "/.erlang.cookie"); cookiefd = open(cookiefile, O_RDONLY); if (cookiefd < 0) { uwsgi_error("open()"); free(cookiefile); return -1; } memset(cookievalue, 0, 128); if (read(cookiefd, cookievalue, 127) < 1) { fprintf(stderr, "invalid cookie found in %s\n", cookiefile); close(cookiefd); free(cookiefile); return -1; } cookie = cookievalue; close(cookiefd); free(cookiefile); } node = malloc((ip - nodename) + 1); if (node == NULL) { uwsgi_error("malloc()"); return -1; } memset(node, 0, (ip - nodename) + 1); memcpy(node, nodename, ip - nodename); erl_init(NULL, 0); if (erl_connect_xinit(ip + 1, node, nodename, NULL, cookie, 0) == -1) { fprintf(stderr, "*** unable to initialize erlang c-node ***\n"); return -1; } efd = socket(AF_INET, SOCK_STREAM, 0); if (efd < 0) { uwsgi_error("socket()"); return -1; } memset(&e_addr, 0, sizeof(struct sockaddr_in)); e_addr.sin_family = AF_INET; e_addr.sin_addr.s_addr = inet_addr(ip + 1); rlen = 1; if (setsockopt(efd, SOL_SOCKET, SO_REUSEADDR, &rlen, sizeof(rlen))) { uwsgi_error("setsockopt()"); close(efd); return -1; } if (bind(efd, (struct sockaddr *) &e_addr, sizeof(struct sockaddr_in)) < 0) { uwsgi_error("bind()"); close(efd); return -1; } rlen = sizeof(struct sockaddr_in); if (getsockname(efd, (struct sockaddr *) &e_addr, (socklen_t *) & rlen)) { uwsgi_error("getsockname()"); close(efd); return -1; } if (listen(efd, uwsgi.listen_queue)) { uwsgi_error("listen()"); close(efd); return -1; } if (erl_publish(ntohs(e_addr.sin_port)) < 0) { fprintf(stderr, "*** unable to subscribe with EPMD ***\n"); close(efd); return -1; } fprintf(stderr, "Erlang C-Node initialized on port %d you can access it with name %s\n", ntohs(e_addr.sin_port), nodename); for (uwsgi_function = uwsgi_erlang_methods; uwsgi_function->ml_name != NULL; uwsgi_function++) { PyObject *func = PyCFunction_New(uwsgi_function, NULL); PyDict_SetItemString(uwsgi.embedded_dict, uwsgi_function->ml_name, func); Py_DECREF(func); } return efd; }