static Packet * vtRPC(VtSession *z, int op, Packet *p) { uint8_t *hdr, buf[2]; char *err; if(z == nil){ vtSetError(ENotConnected); return nil; } /* * single threaded for the momment */ vtLock(z->lk); if(z->cstate != VtStateConnected){ vtSetError(ENotConnected); goto Err; } hdr = packetHeader(p, 2); hdr[0] = op; /* op */ hdr[1] = 0; /* tid */ vtDebug(z, "client send: "); vtDebugMesg(z, p, "\n"); if(!vtSendPacket(z, p)) { p = nil; goto Err; } p = vtRecvPacket(z); if(p == nil) goto Err; vtDebug(z, "client recv: "); vtDebugMesg(z, p, "\n"); if(!packetConsume(p, buf, 2)) goto Err; if(buf[0] == VtRError) { if(!vtGetString(p, &err)) { vtSetError(EProtocolBotch); goto Err; } vtSetError(err); vtMemFree(err); packetFree(p); vtUnlock(z->lk); return nil; } if(buf[0] != op+1 || buf[1] != 0) { vtSetError(EProtocolBotch); goto Err; } vtUnlock(z->lk); return p; Err: vtDebug(z, "vtRPC failed: %s\n", vtGetError()); if(p != nil) packetFree(p); vtUnlock(z->lk); vtDisconnect(z, 1); return nil; }
NetworkServer::AutoPacket NetworkServer::receive() { assert(isConnected()); for (;;) { if (_queuedUser) { removeUser(_queuedUser); _queuedUser = 0; } Packet* raw = _peer->Receive(); if (!raw) { return AutoPacket(); } AutoDepacketer depacketer(*_peer, raw); AutoPacket packet(processPacket(*raw)); if (packet.get()) { User *user = findUser(raw->systemAddress); if (!user) { packetHeader(std::cout, *raw); std::cout << "Packet from unknown user: " << int(packet->kind()) << std::endl; continue; } packet->read(raw, user); return packet; } } }
void UdpReceiver::readPendingDatagrams() { //while data is available while (socket->hasPendingDatagrams()) { QByteArray datagram; datagram.resize(socket->pendingDatagramSize()); QHostAddress sender = RECEIVING_IP; quint16 senderPort = RECEIVING_PORT; socket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort); //Read Packet Header std::string packetHeader(datagram.constData(), 6); if(packetHeader.compare("EARNLS")==0) { emit ackRunLaser(); } else if(packetHeader.compare("EASTLS")==0) { emit ackStopLaser(); } else if(packetHeader.compare("EASFFS")==0) { emit ackFullScanMode(); } else if(packetHeader.compare("EASBES")==0) { emit ackBoundedElevation(); } else if(packetHeader.compare("EASRES")==0) { emit ackRegionScan(); } else if(packetHeader.compare("EACHLP")==0) { emit ackLaserSensorPosition(); } else { parseDatagram(datagram); } } }
void vtDisconnect(VtSession *z, int error) { Packet *p; uint8_t *b; vtDebug(z, "vtDisconnect\n"); vtLock(z->lk); if(z->cstate == VtStateConnected && !error && z->vtbl == nil) { /* clean shutdown */ p = packetAlloc(); b = packetHeader(p, 2); b[0] = VtQGoodbye; b[1] = 0; vtSendPacket(z, p); } if(z->fd >= 0) vtFdClose(z->fd); z->fd = -1; z->cstate = VtStateClosed; vtUnlock(z->lk); }
int vtWritePacket(VtSession *z, uint8_t score[VtScoreSize], int type, Packet *p) { int n = packetSize(p); uint8_t *hdr; if(n > VtMaxLumpSize || n < 0) { vtSetError(ELumpSize); goto Err; } if(n == 0) { memmove(score, vtZeroScore, VtScoreSize); return 1; } hdr = packetHeader(p, 4); hdr[0] = type; hdr[1] = 0; /* pad */ hdr[2] = 0; /* pad */ hdr[3] = 0; /* pad */ p = vtRPC(z, VtQWrite, p); if(p == nil) return 0; if(!packetConsume(p, score, VtScoreSize)) goto Err; if(packetSize(p) != 0) { vtSetError(EProtocolBotch); goto Err; } packetFree(p); return 1; Err: packetFree(p); return 0; }
// -------------------------------------------------------------------- // processPacket // -------------------------------------------------------------------- bool processPacket(int sock, File* outFile, bool display, bool &close) { LogPacketHeader packetHeader((LogType)0); struct tm *timeInfo=NULL; bool magicOK = false; int magic=0; static Byte data[LOG_DATA_MAX_LENGTH]; close=false; // look for magic while (!magicOK) { Byte byte=0; while(1) { if (peekFromSocket(sock, &byte, 1)==0) { if (byte==0xa0) break; else readFromSocket(sock, &byte, 1); } } if (peekFromSocket(sock, &magic, sizeof(int))==0) { if (magic != LOG_MAGIC_NBR) { printf ("*** Packet corrupted try to resynchronize (magic " ": 0x%x != 0x%x) !!!\n", magic, LOG_MAGIC_NBR); readFromSocket(sock, &magic, sizeof(int)); } else { break; } } else { return false; } } if (readFromSocket(sock, &packetHeader, sizeof(LogPacketHeader))==0) { if (display) { printf(" timeStamp = "); timeInfo = localtime(&packetHeader.timeStamp.tv_sec); printf("%02d/%02d/%02d %02d:%02d:%02d,%06d\n", timeInfo->tm_mday, timeInfo->tm_mon+1, timeInfo->tm_year+1900, timeInfo->tm_hour, timeInfo->tm_min, timeInfo->tm_sec, (int)packetHeader.timeStamp.tv_usec); } if (packetHeader.type == LOG_TYPE_CLOSE) { printf("Closing log\n"); close=true; return false; } //printf("read length=%d\n", packetHeader.length); if (readFromSocket(sock, data, packetHeader.length)==0) { if (display) { displayPacket(packetHeader, data); } } storePacket(outFile, packetHeader, data); return true; } else { printf("read error\n"); return false; } }
NetworkServer::AutoPacket NetworkServer::processPacket(const Packet& raw) { unsigned char kind = packetKind(raw); // User packets if (kind >= ID_NOT_INTERNAL) { AutoPacket packet(_manager.create(kind)); if (packet.get()) { return packet; } packetHeader(std::cout, raw); std::cout << "Unknown user packet detected: " << int(kind) << std::endl; return AutoPacket(new TamperPacket); } switch (kind) { // A client just connected for the first time case ID_NEW_INCOMING_CONNECTION: { // TODO: Timeout for no login return AutoPacket(); } // A client is asking to login/register case ID_ACCOUNT_LOGIN: { const User *user = createUser(raw); unsigned char sendKind = ID_ACCOUNT_FAILURE; AutoPacket packet; if (user) { sendKind = ID_ACCOUNT_SUCCESS; packet.reset(new ConnectionPacket()); } NetworkParams params(sendKind, HIGH_PRIORITY); sendSimplePacket(*_peer, params, raw.systemAddress); return packet; } // A client has disconnected case ID_DISCONNECTION_NOTIFICATION: case ID_CONNECTION_LOST: { User *user = findUser(raw.systemAddress); if (!user) { return AutoPacket(); } assert(!_queuedUser); _queuedUser = user; std::string reason; switch (kind) { case ID_DISCONNECTION_NOTIFICATION: reason = "The client closed the connection."; break; default: reason = "The connection to the client was lost."; } return AutoPacket(new DisconnectionPacket(reason)); } // A packet was tampered with in transit case ID_MODIFIED_PACKET: return AutoPacket(new TamperPacket()); // Some other packet we don't care about default: packetHeader(std::cout, raw); std::cout << "Unused system packet ignored: " << int(kind) << std::endl; return AutoPacket(); } }