static void filter_wrapper(struct thread_state *state, struct packet *pkt) { int h; LUA_STACK_MARK(state->lua->L); packet_addref(pkt); lua_pushcfunction(state->lua->L, lua_state_error_formater); h = lua_gettop(state->lua->L); lua_getglobal(state->lua->L, "haka"); lua_getfield(state->lua->L, -1, "filter"); if (!lua_isnil(state->lua->L, -1)) { if (!lua_pushppacket(state->lua->L, pkt)) { LOG_ERROR(core, "packet internal error"); packet_drop(pkt); } else { if (lua_pcall(state->lua->L, 1, 0, h)) { lua_state_print_error(state->lua->L, "filter"); packet_drop(pkt); } } } else { lua_pop(state->lua->L, 1); packet_drop(pkt); } lua_pop(state->lua->L, 2); LUA_STACK_CHECK(state->lua->L, 0); packet_release(pkt); }
/** Kinda deprecated but well, you can't have everything */ char*message_receive(lsocket*sck){ lpacket*pck=packet_receive(sck); char*msg=malloc(sizeof(char)*strlen(pck->message)); strcpy(pck->message,msg); packet_drop(pck); return msg; }
/** Have a handshake with the server * @param main_socket The server socket, used to initiate the handshake * @param sockets The newly created sockets * @return -1 if the communication cannot be established, 1 otherwise */ int handshake(lsocket*main_socket,lsocket**sockets){ char message[SIZE_BUFFER*2]; lpacket* feedback; int ret_code; /* Forging message */ sprintf(message,"%s:%s\n",sockets[0]->addr,sockets[1]->addr); /* Sending to server */ if (verbose) printf("The following message will be send to the server:\n %s",message); socket_message_send(main_socket,msg_text,message); if (verbose) {printf("Waiting for server response...");fflush(stdout);} /* Handshake received */ feedback=packet_receive(sockets[0]); ret_code=feedback->type; if (verbose) printf("Received !\n"); /* Clean */ packet_drop(feedback); close_socket(main_socket,0); /* Return */ if (ret_code==msg_recv) return 1; else return -1; }
/** [Deprecated] Initiate an exchange * Send a message, wait for a correct answer * @param sck_send Socket used for sending * @param type_message_send Type of the sended message * @param msg The sended message * @param sck_recv Socket used for reception * @param type_message_recv Type expected for reception */ char*message_exchange(lsocket*sck_send,msg_type type_message_send,char*msg,lsocket*sck_recv,msg_type type_message_recv){ int count=KEEP_ALIVE; /** <Time to keep-alive connection */ lpacket*pck; char*answer; /* Force good formated answer */ if (verbose>1) {printf("Awaiting answer...");fflush(stdout);} do{ socket_message_send(sck_send,type_message_send,msg); if (KEEP_ALIVE-count) sleep(LATENCY); /* [ILL IMPLEMENTED] Find a damn smaller function */ pck=packet_receive(sck_recv); if (verbose>1) {printf("%c",pck->type==msg_recv?'*':'.');fflush(stdout);} if (!count--) {printf("\n");OUT("Server didn't sent expected message")}; } while (pck->type!=type_message_recv); if (verbose>1) printf("\nServer replied !\n"); /* Copy answer */ answer=malloc(sizeof(char)*strlen(pck->message)); strcpy(answer,pck->message); /* Free it and return */ packet_drop(pck); return answer; }
/** Sending a message, with less information to provide */ int socket_message_send(lsocket*sck,msg_type type_message,char *message){ lpacket*pck=packet_forge(type_message,message); packet_send(sck,pck); packet_drop(pck); return packet_snd_bytes; }