int cliser_server_broadcast(lua_State *L) { double t0 = _ipc_seconds(); server_t *server = (server_t *)lua_touserdata(L, 1); const char *tag = luaL_optstring(L, 3, NULL); client_t **clients = alloca(server->num_clients * sizeof(client_t*)); client_t *client = server->clients; int i = 0; while (client) { if (!tag || (client->tag && strcmp(tag, client->tag) == 0)) { clients[i] = client; i++; } client = client->next; } qsort(clients, i, sizeof(client_t*), compare_clients); int ret = 0; for (int j = 0; j < i; j++) { client = clients[j]; if (lua_type(L, 2) == LUA_TUSERDATA) { ret = sock_send_userdata(L, 2, client->sock, &server->copy_context); } else { ret = sock_send_msg(L, 2, client->sock, client->send_rb, &server->copy_context); } if (ret) break; } server->copy_context.tx.total_seconds += (_ipc_seconds() - t0); server->copy_context.tx.num_calls++; return ret; }
int sock_read_query(int socket_serveur, msg *message) { int sock_client = -1; socklen_t len; struct sockaddr_un sockad; len = sizeof(sockad); while (!end_process) { if ((sock_client = accept(socket_serveur, (struct sockaddr *)&sockad, &len)) == -1) { log(LOG_WARNING, "accept error : %m"); return (-1); } in_action = true; if (sock_read_msg(sock_client, message, NULL)) { in_action = false; return(-1); } else { if (message->op == OP_PING) { message->op = OP_PONG; sock_send_msg(sock_client, message, NULL); } else { in_action = false; return(sock_client); } } in_action = false; } close(sock_client); return(-1); }
int sock_send_ping(int socket, msg *query, char **err_msg) { memset(query, 0, sizeof(msg)); query->op = OP_PING; query->version = SAUTHPF_PROTO_VERSION; if (sock_send_msg(socket, query, err_msg)) return (1); return (0); }
int sock_send_list_user(int socket, msg *query, char **err_msg) { memset(query, 0, sizeof(msg)); query->version = SAUTHPF_PROTO_VERSION; query->op = OP_LIST; if (sock_send_msg(socket, query, err_msg)) return (1); return 0; }
int sock_send_log_histo(int socket, msg *query, time_t date,char **err_msg) { memset(query, 0, sizeof(msg)); query->version = SAUTHPF_PROTO_VERSION; query->op = OP_HISTO; query->data.log.start_time = date; if (sock_send_msg(socket, query, err_msg)) return (1); return 0; }
int sock_send_isauth(int socket, msg *query, char *ip, char **err_msg) { memset(query, 0, sizeof(msg)); query->version = SAUTHPF_PROTO_VERSION; query->op = OP_ISAUTH; strlcpy(query->data.auth.ip, ip, sizeof(query->data.auth.ip)); if (sock_send_msg(socket, query, err_msg)) return (1); return 0; }
int sock_send_unauth(int socket, msg *query, flag_unauth flag, char *user_or_ip, char **err_msg) { memset(query, 0, sizeof(msg)); query->version = SAUTHPF_PROTO_VERSION; query->op = OP_UNAUTH; strlcpy(query->data.unauth.user_or_ip, user_or_ip, sizeof(query->data.unauth.user_or_ip)); query->data.unauth.flag = flag; if (sock_send_msg(socket, query, err_msg)) return (1); return (0); }
int cliser_client_send(lua_State *L) { double t0 = _ipc_seconds(); client_t *client = *(client_t **)lua_touserdata(L, 1); int ret; if (lua_type(L, 2) == LUA_TUSERDATA) { ret = sock_send_userdata(L, 2, client->sock, &client->copy_context); } else { ret = sock_send_msg(L, 2, client->sock, client->send_rb, &client->copy_context); } client->copy_context.tx.total_seconds += (_ipc_seconds() - t0); client->copy_context.tx.num_calls++; return ret; }
int sock_send_auth(int socket, msg *query, char *user, char *ip, char *password, char **err_msg) { memset(query, 0, sizeof(msg)); query->version = SAUTHPF_PROTO_VERSION; query->op = OP_AUTH; strlcpy(query->data.auth.user, user, sizeof(query->data.auth.user)); strlcpy(query->data.auth.ip, ip, sizeof(query->data.auth.ip)); if (password) strlcpy(query->data.auth.password, password, sizeof(query->data.auth.password)); if (sock_send_msg(socket, query, err_msg)) return (1); return 0; }
int cliser_server_send(lua_State *L) { double t0 = _ipc_seconds(); server_client_t *server_client = (server_client_t *)lua_touserdata(L, 1); if (server_client->client == NULL) return LUA_HANDLE_ERROR_STR(L, "server client is invalid, either closed or used outside of server function scope"); int ret; if (lua_type(L, 2) == LUA_TUSERDATA) { server_client->server->copy_context.use_fastpath = server_client->client->copy_context.use_fastpath; ret = sock_send_userdata(L, 2, server_client->client->sock, &server_client->server->copy_context); } else { ret = sock_send_msg(L, 2, server_client->client->sock, server_client->client->send_rb, &server_client->server->copy_context); } server_client->server->copy_context.tx.total_seconds += (_ipc_seconds() - t0); server_client->server->copy_context.tx.num_calls++; return ret; }
int sock_send_msg_auth(int socket, session *session_reply) { msg reply; memset(&reply, 0, sizeof(msg)); reply.op = OP_MSG_AUTH; reply.version = SAUTHPF_PROTO_VERSION; reply.data.auth.start_time = session_reply->start_time; reply.data.auth.expire_date = session_reply->expire_date; strlcpy(reply.data.auth.ip, session_reply->ip, sizeof(reply.data.auth.ip)); strlcpy(reply.data.auth.user, session_reply->user_name, sizeof(reply.data.auth.user)); if (sock_send_msg(socket, &reply, NULL)) return (1); return (0); }
int sock_send_msg_log(int socket, session *session_reply) { msg reply; memset(&reply, 0, sizeof(msg)); reply.op = OP_MSG_LOG; reply.version = SAUTHPF_PROTO_VERSION; reply.data.log.start_time = session_reply->start_time; reply.data.log.event_time = session_reply->event_time; reply.data.log.type = session_reply->type; strlcpy(reply.data.log.ip, session_reply->ip, sizeof(reply.data.log.ip)); strlcpy(reply.data.log.user, session_reply->user_name, sizeof(reply.data.log.user)); if (sock_send_msg(socket, &reply, NULL)) return (1); return (0); }
int sock_send_reply(int socket, int reply_code, char *reply, ...) { msg mess; char *ans; memset(&mess, 0, sizeof(msg)); va_list ap; va_start(ap, reply); if (vasprintf(&ans, reply, ap) < 0) { log(LOG_WARNING, "vasprintf error : %m"); va_end(ap); return (1); } va_end(ap); mess.op = OP_ANSWER; mess.version = SAUTHPF_PROTO_VERSION; mess.data.answer.answer_code = reply_code; strlcpy(mess.data.answer.answer_msg, ans, sizeof(mess.data.answer.answer_msg)); free(ans); if (sock_send_msg(socket, &mess, NULL)) return (1); return (0); }