/* *Main thread for each client. Receives all messages *and passes the data off to the correct function. Receives *a pointer to the file descriptor for the socket the thread *should listen on */ void *client_receive(void *ptr) { int client = *(int *) ptr; int received; int logged_in = 0; packet in_pkt, *client_message_ptr = &in_pkt; while (1) { received = recv(client, &in_pkt, sizeof(packet), 0); if (received) { debugPacket(client_message_ptr); // Responses to not logged in clients if (!logged_in) { if(in_pkt.options == REGISTER) { logged_in = register_user(&in_pkt, client); } else if(in_pkt.options == LOGIN) { logged_in = login(&in_pkt, client); } else if(in_pkt.options == EXIT) { close(client); return NULL; } else { sendError("Not logged in.", client); } } // Responses to logged in clients else if (logged_in) { // Handle option messages for logged in client if (in_pkt.options < 1000) { if(in_pkt.options == REGISTER) { sendError("You may not register while logged in.", client); } else if(in_pkt.options == SETPASS) { set_pass(&in_pkt, client); } else if(in_pkt.options == SETNAME) { set_name(&in_pkt, client); } else if(in_pkt.options == LOGIN) { sendError("Already logged in.", client); } else if(in_pkt.options == EXIT) { exit_client(&in_pkt, client); return NULL; } else if(in_pkt.options == INVITE) { invite(&in_pkt, client); } else if(in_pkt.options == JOIN) { join(&in_pkt, client); } else if(in_pkt.options == LEAVE) { leave(&in_pkt, client); } else if(in_pkt.options == GETALLUSERS) { get_active_users(client); } else if(in_pkt.options == GETUSERS) { get_room_users(&in_pkt, client); } else if(in_pkt.options == GETUSER) { user_lookup(&in_pkt, client); } else if(in_pkt.options == GETROOMS) { get_room_list(client); } else if(in_pkt.options == GETMOTD) { sendMOTD(client); } else if(in_pkt.options == 0) { printf("%s --- Error:%s Abrupt disconnect on logged in client.\n", RED, NORMAL); exit_client(&in_pkt, client); return NULL; } else { printf("%s --- Error:%s Unknown message received from client.\n", RED, NORMAL); } } // Handle conversation message for logged in client else { // Will be treated as a message packet, safe to santize entire buffer sanitizeInput((void *)&in_pkt.buf, 0); send_message(&in_pkt, client); } } memset(&in_pkt, 0, sizeof(packet)); } } return NULL; }
NTSTATUS ReinjectPendedPacket( IN PENDED_PACKET *packet, IN FLOW_DATA *flowData) { NTSTATUS status; UINT32 flags; NET_BUFFER_LIST* netBufferList = NULL; FLOW_DATA *flowCtx; ULONG dataLength; if(packet->dataLength == 0 || packet->data == NULL) { return STATUS_UNSUCCESSFUL; } packet->mdl = IoAllocateMdl( packet->data, packet->dataLength, FALSE, FALSE, NULL); if (packet->mdl == NULL) { status = STATUS_NO_MEMORY; goto Exit; } MmBuildMdlForNonPagedPool(packet->mdl); status = FwpsAllocateNetBufferAndNetBufferList( gNetBufferListPool, 0, 0, packet->mdl, 0, packet->dataLength, &netBufferList); if(!NT_SUCCESS(status)) { goto Exit; } flags = packet->flags; dataLength = packet->dataLength; flowCtx = packet->flowContext; #ifdef DEBUG debugPacket(packet); DbgPrintEx( DPFLTR_IHVNETWORK_ID, DPFLTR_ERROR_LEVEL, "\n localCtr=%d, remoteCtr=%d\n", flowCtx->localCounter, flowCtx->remoteCounter); #endif // Keep correct sequence numbers // (Assume every reinjection is successful, otherwise synchronous injection is // needed for consistent sequence numbers implementation) if(flags & FWPS_STREAM_FLAG_SEND) { flowCtx->localCounter += dataLength; } else if(flags & FWPS_STREAM_FLAG_RECEIVE) { flowCtx->remoteCounter += dataLength; } else { #ifdef DEBUG DbgBreakPoint(); #endif } status = FwpsStreamInjectAsync( gInjectionHandle, NULL, 0, flowData->flowHandle, gStreamCalloutIdV4, FWPS_LAYER_STREAM_V4, flags, netBufferList, packet->dataLength, StreamInjectCompletionFn, packet); if (!NT_SUCCESS(status)) { goto Exit; } // Ownership transferred netBufferList = NULL; packet = NULL; Exit: if (netBufferList != NULL) { FwpsFreeNetBufferList(netBufferList); } if (packet != NULL) { FreePendedPacket(packet); } return status; }
void answerRequest(packet_t* packet) { printf("I must answer to this: \n"); debugPacket(packet); };