void HandleGroupListRequest(CTCPRequestPacket* PTCPRequest) { uint8* data = (uint8*)PTCPRequest->GetData(); uint32 partyid = RBUFL(data,(0x10)); uint32 linkshellid = RBUFL(data,(0x18)); ShowMessage("SEARCH::PartyID = %u\n", partyid); ShowMessage("SEARCH::LinkshlellID = %u\n", linkshellid); CDataLoader* PDataLoader = new CDataLoader(); if (partyid != 0) { std::list<SearchEntity*> PartyList = PDataLoader->GetPartyList(partyid); CPartyListPacket* PPartyPacket = new CPartyListPacket(partyid,PartyList.size()); for (std::list<SearchEntity*>::iterator it = PartyList.begin(); it != PartyList.end(); ++it) { PPartyPacket->AddPlayer(*it); } PrintPacket((int8*)PPartyPacket->GetData(), PPartyPacket->GetSize()); PTCPRequest->SendToSocket(PPartyPacket->GetData(), PPartyPacket->GetSize()); delete PPartyPacket; } else if (linkshellid != 0) { std::list<SearchEntity*> LinkshellList = PDataLoader->GetLinkshellList(linkshellid); CLinkshellListPacket* PLinkshellPacket = new CLinkshellListPacket(linkshellid,LinkshellList.size()); for (std::list<SearchEntity*>::iterator it = LinkshellList.begin(); it != LinkshellList.end(); ++it) { PLinkshellPacket->AddPlayer(*it); } PrintPacket((int8*)PLinkshellPacket->GetData(), PLinkshellPacket->GetSize()); PTCPRequest->SendToSocket(PLinkshellPacket->GetData(), PLinkshellPacket->GetSize()); delete PLinkshellPacket; } delete PDataLoader; }
int anonprint_process(struct anonflow *flow, void *internal_data, unsigned char *dev_pkt, anon_pkthdr_t * pkt_head) { struct pcap_pkthdr pkthdr; anonpacket decoded_pkt; pkthdr.caplen = pkt_head->caplen; pkthdr.len = pkt_head->wlen; pkthdr.ts.tv_sec = pkt_head->ts.tv_sec; pkthdr.ts.tv_usec = pkt_head->ts.tv_usec; decode_packet(flow->link_type, flow->cap_length, &pkthdr, (unsigned char *)dev_pkt, &decoded_pkt); PrintPacket(stdout, &decoded_pkt, flow->link_type); return 1; }
void test(const char* exp, const char* dev) { int sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP)); if (sock<=0) { P("socket error"); return; } //设置缓冲区 #define PER_PACKET_SIZE 2048 const int BUFFER_SIZE = 1024*1024*16; //16MB的缓冲区 struct tpacket_req req; req.tp_block_size = 4096; req.tp_block_nr = BUFFER_SIZE/req.tp_block_size; req.tp_frame_size = PER_PACKET_SIZE; req.tp_frame_nr = BUFFER_SIZE/req.tp_frame_size; if (-1==setsockopt(sock, SOL_PACKET, PACKET_RX_RING, &req, sizeof(struct tpacket_req))) { perror("setsockopt"); P("set PACKET_RX_RING error"); close(sock); return; } //映射缓冲区 char* pBuffer = (char*)mmap(0, BUFFER_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, sock, 0); if (MAP_FAILED==pBuffer) { P("mmap error"); close(sock); return; } //注意:一定要先映射后再绑定,否则会有问题 // 问题的详细描述可参考:https://lists.linux-foundation.org/pipermail/bugme-new/2003-October/009110.html if (!SetPromisc(sock, dev)) { P("SetPromisc [%s] error", dev); memset(&req, 0, sizeof(req)); if (-1==setsockopt(sock, SOL_PACKET, PACKET_RX_RING, &req, sizeof(req))) { P("set map buffer to 0 error!"); } close(sock); return; } if (!BindDevice(sock, dev)) { P("bind [%s] error", dev); memset(&req, 0, sizeof(req)); if (-1==setsockopt(sock, SOL_PACKET, PACKET_RX_RING, &req, sizeof(req))) { P("set map buffer to 0 error!"); } close(sock); return; } //设置过滤器 int nExpLen = strlen(exp); struct sock_fprog Filter; memset(&Filter, 0, sizeof(struct sock_fprog)); if (nExpLen>0) { if (ExpressionToFilter(exp, &Filter)) { if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &Filter, sizeof(Filter))<0) { perror("setsockopt"); } } } // struct pollfd pfd; int nIndex = 0; for (; ; ) { for (; ; ) { struct tpacket_hdr* pHead = (struct tpacket_hdr*)(pBuffer + nIndex*PER_PACKET_SIZE); if (pHead->tp_len<34 || pHead->tp_len>1614) { break; } PrintPacket((char*)pHead+pHead->tp_net); pHead->tp_len = 0; pHead->tp_status = TP_STATUS_KERNEL; //注意:pHead->tp_status这个变量并不能真正反应出包的处理状态, //在有的服务器上 TP_STATUS_USER(1)代表包可用, TP_STATUS_KERNEL(0)代表包不可用 //但是在有的服务器上,这个标志变量无效 //对于这个问题我还没找到原因 nIndex++; if (nIndex>=BUFFER_SIZE/PER_PACKET_SIZE) { nIndex = 0; } } // pfd.fd = sock; pfd.events = POLLIN | POLLERR; pfd.revents = 0; switch (poll(&pfd, 1, 1000)) { case -1: perror("poll"); P("poll error"); goto EndWhile; break; case 0: P("time out"); continue; break; } } EndWhile: if (-1==munmap(pBuffer, BUFFER_SIZE)) { P("unmap error!"); } memset(&req, 0, sizeof(req)); if (-1==setsockopt(sock, SOL_PACKET, PACKET_RX_RING, &req, sizeof(req))) { P("set map buffer to 0 error!"); } close(sock); sock = -1; // if (nExpLen>0) { FreeFilter(&Filter); } }
static void DoAliasing (int fd, int direction) { int bytes; int origBytes; int status; int addrSize; struct ip* ip; if (assignAliasAddr) { SetAliasAddressFromIfName (ifName); assignAliasAddr = 0; } /* * Get packet from socket. */ addrSize = sizeof packetAddr; origBytes = recvfrom (fd, packetBuf, sizeof packetBuf, 0, (struct sockaddr*) &packetAddr, &addrSize); if (origBytes == -1) { if (errno != EINTR) Warn ("read from divert socket failed"); return; } /* * This is a IP packet. */ ip = (struct ip*) packetBuf; if (direction == DONT_KNOW) { if (packetAddr.sin_addr.s_addr == INADDR_ANY) direction = OUTPUT; else direction = INPUT; } if (verbose) { /* * Print packet direction and protocol type. */ printf (direction == OUTPUT ? "Out " : "In "); switch (ip->ip_p) { case IPPROTO_TCP: printf ("[TCP] "); break; case IPPROTO_UDP: printf ("[UDP] "); break; case IPPROTO_ICMP: printf ("[ICMP] "); break; default: printf ("[%d] ", ip->ip_p); break; } /* * Print addresses. */ PrintPacket (ip); } if (direction == OUTPUT) { /* * Outgoing packets. Do aliasing. */ PacketAliasOut (packetBuf, IP_MAXPACKET); } else { /* * Do aliasing. */ status = PacketAliasIn (packetBuf, IP_MAXPACKET); if (status == PKT_ALIAS_IGNORED && dropIgnoredIncoming) { if (verbose) printf (" dropped.\n"); if (logDropped) SyslogPacket (ip, LOG_WARNING, "denied"); return; } } /* * Length might have changed during aliasing. */ bytes = ntohs (ip->ip_len); /* * Update alias overhead size for outgoing packets. */ if (direction == OUTPUT && bytes - origBytes > aliasOverhead) aliasOverhead = bytes - origBytes; if (verbose) { /* * Print addresses after aliasing. */ printf (" aliased to\n"); printf (" "); PrintPacket (ip); printf ("\n"); } packetLen = bytes; packetSock = fd; packetDirection = direction; FlushPacketBuffer (fd); }
Boolean OSCInvokeMessagesThatAreReady(OSCTimeTag now) { queuedData *x; OSCTimeTag thisTimeTag; globals.lastTimeTag = now; globals.timePassed = TRUE; thisTimeTag = OSCQueueEarliestTimeTag(globals.TheQueue); if (OSCTT_Compare(thisTimeTag, now) > 0) { /* No messages ready yet. */ return FALSE; } #ifdef DEBUG printf("OSCInvokeMessagesThatAreReady(%llx) - yes, some are ready; earliest %llx\n", now, thisTimeTag); #endif while (OSCTT_Compare(thisTimeTag, OSCQueueEarliestTimeTag(globals.TheQueue)) == 0) { x = (queuedData *) OSCQueueRemoveEarliest(globals.TheQueue); #ifdef DEBUG printf("...Just removed earliest entry from queue: %p, TT %llx, %s\n", x, x->timetag, x->type == MESSAGE ? "message" : "bundle"); if (x->type == MESSAGE) { printf("...message %s, len %d, args %p, arglen %d, callbacks %p\n", x->data.message.messageName, x->data.message.length, x->data.message.args, x->data.message.argLength, x->data.message.callbacks); } else { if (x->data.bundle.length == 0) { printf("...bundle is empty.\n"); } else { printf("...bundle len %d, first count %d, first msg %s\n", x->data.bundle.length, *((int *) x->data.bundle.bytes), x->data.bundle.bytes+4); } } PrintPacket(x->myPacket); #endif if (x->type == BUNDLE) { ParseBundle(x); } else { if (x->data.message.callbacks == NOT_DISPATCHED_YET) { if (ParseMessage(x) == FALSE) { /* Problem with this message - flush it. */ PacketRemoveRef(x->myPacket); FreeQD(x); continue; } } CallWholeCallbackList(x->data.message.callbacks, x->data.message.argLength, x->data.message.args, thisTimeTag, x->myPacket->returnAddrOK ? x->myPacket->returnAddr : 0); PacketRemoveRef(x->myPacket); FreeQD(x); } } #ifdef PARANOID if (OSCTT_Compare(thisTimeTag, OSCQueueEarliestTimeTag(globals.TheQueue)) > 0) { fatal_error("OSCInvokeMessagesThatAreReady: corrupt queue!\n" " just did %llx; earliest in queue is now %llx", thisTimeTag, OSCQueueEarliestTimeTag(globals.TheQueue)); } #endif return OSCTT_Compare(OSCQueueEarliestTimeTag(globals.TheQueue), now) <= 0; }
int main(void) { nbNetVm *NetVMHandle= NULL; nbNetPe *PEHandle= NULL; nbNetVmByteCode *BytecodeHandle= NULL; nbNetVmPortLocalAdapter *Device= NULL; nbNetVmPortLocalAdapter *NetVMInPort= NULL; nbNetVmPortApplication *NetVMOutPort= NULL; int i; //Create an handle to the NetVM's state NetVMHandle = new nbNetVm(0); if (NetVMHandle == NULL) { printf("Cannot create the state of the virtual machine\n"); return nbFAILURE; } // Create a new PE state PEHandle = new nbNetPe(NetVMHandle); if (PEHandle == NULL) { printf("Cannot create NetPE\n"); goto end_failure; } // Open the bytecode of the ip filter BytecodeHandle = new nbNetVmByteCode(); if (BytecodeHandle == NULL) { printf("cannot create the temporary structure used to load the bytecode in the NetPE\n"); goto end_failure; } if( BytecodeHandle->CreateBytecodeFromAsmFile("../Modules/NetBee/Samples/NBeeNetVMPcap/ip_pull.asm") != nbSUCCESS ) { printf("Cannot open the bytecode\n"); delete BytecodeHandle; goto end_failure; } // Initialize the state of the PE using the data provided by the bytecode if ( PEHandle->InjectCode(BytecodeHandle) != nbSUCCESS ) { printf("Cannot inject the code\n"); delete BytecodeHandle; goto end_failure; } delete BytecodeHandle; // Create the port that will implement the exporter NetVMOutPort= new nbNetVmPortApplication; if (NetVMOutPort == NULL) { printf("Cannot create the output port\n"); goto end_failure; } // Attach this port to the virtual machine if (NetVMHandle->AttachPort(NetVMOutPort, nvmPORT_EXPORTER | nvmCONNECTION_PULL) == nbFAILURE) { printf("Cannot add the output port to the NetVM\n"); goto end_failure; } // Create the port that implements the capture socket Device= GetListAdapters(); if (Device == NULL) { printf("Cannot create the input port\n"); goto end_failure; } NetVMInPort= (nbNetVmPortLocalAdapter *)Device->Clone(Device->Name, Device->Description, nbNETDEVICE_PROMISCUOUS); if (NetVMInPort == NULL) { printf("Cannot create the local capture device\n" ); goto end_failure; } delete(Device); // Register the function that has to be called for getting data if (NetVMInPort->RegisterPollingFunction(nbNetVmPortLocalAdapter::SrcPollingFunctionWinPcap, NetVMInPort) == nbFAILURE) { printf("Cannot register the polling function on the data source\n" ); goto end_failure; } // This port will be connected to a push input port if (NetVMHandle->AttachPort(NetVMInPort, nvmPORT_COLLECTOR | nvmCONNECTION_PULL) == nbFAILURE) { printf("Cannot add the input port to the NetVM\n"); goto end_failure; } // Connect the collector port to the first port (port #0) of the PE if (NetVMHandle->ConnectPorts(NetVMInPort->GetNetVmPortDataDevice(), PEHandle->GetPEPort(0)) == nbFAILURE) { printf("Cannot create the connection between the external world and the PE: %s\n", NetVMHandle->GetLastError() ); goto end_failure; } // Connect the exporter port to the second port (port #1) of the PE if (NetVMHandle->ConnectPorts(PEHandle->GetPEPort(1), NetVMOutPort->GetNetVmPortDataDevice()) == nbFAILURE) { printf("Cannot create the connection between the external world and the PE:\n"); goto end_failure; } //start the virual machine if (NetVMHandle->Start() == nbFAILURE) { printf ("Cannot start the virtual machine\n"); goto end_failure; } i = 0; // BUFFERS_NUMBER packets are sent to the NetVM while(i < BUFFERS_NUMBER) { nbNetPkt *exbuff; NetVMOutPort->Read(&exbuff, NULL); PrintPacket(exbuff); NetVMOutPort->ReleaseExBuf(exbuff); i++; } return nbSUCCESS; end_failure: if (NetVMHandle) delete NetVMHandle; if (PEHandle) delete PEHandle; return nbFAILURE; }
static void DoAliasing(int fd, int direction) { int bytes; int origBytes; char buf[IP_MAXPACKET]; struct sockaddr_in addr; int wrote; int status; int addrSize; struct ip* ip; char msgBuf[80]; if (assignAliasAddr) { SetAliasAddressFromIfName(ifName); assignAliasAddr = 0; } /* * Get packet from socket. */ addrSize = sizeof addr; origBytes = recvfrom(fd, buf, sizeof buf, 0, (struct sockaddr *)&addr, &addrSize); if (origBytes == -1) { if (errno != EINTR) Warn("read from divert socket failed"); return; } /* * This is a IP packet. */ ip = (struct ip *)buf; if (direction == DONT_KNOW) { if (addr.sin_addr.s_addr == INADDR_ANY) direction = OUTPUT; else direction = INPUT; } if (verbose) { /* * Print packet direction and protocol type. */ printf(direction == OUTPUT ? "Out " : "In "); switch (ip->ip_p) { case IPPROTO_TCP: printf("[TCP] "); break; case IPPROTO_UDP: printf("[UDP] "); break; case IPPROTO_ICMP: printf("[ICMP] "); break; default: printf("[%d] ", ip->ip_p); break; } /* * Print addresses. */ PrintPacket(ip); } if (direction == OUTPUT) { /* * Outgoing packets. Do aliasing. */ PacketAliasOut(buf, IP_MAXPACKET); } else { /* * Do aliasing. */ status = PacketAliasIn(buf, IP_MAXPACKET); if (status == PKT_ALIAS_IGNORED && dropIgnoredIncoming) { if (verbose) printf(" dropped.\n"); if (logDropped) SyslogPacket(ip, LOG_WARNING, "denied"); return; } } /* * Length might have changed during aliasing. */ bytes = ntohs(ip->ip_len); /* * Update alias overhead size for outgoing packets. */ if (direction == OUTPUT && bytes - origBytes > aliasOverhead) aliasOverhead = bytes - origBytes; if (verbose) { /* * Print addresses after aliasing. */ printf(" aliased to\n"); printf(" "); PrintPacket(ip); printf("\n"); } /* * Put packet back for processing. */ wrote = sendto(fd, buf, bytes, 0, (struct sockaddr *)&addr, sizeof addr); if (wrote != bytes) { if (errno == EMSGSIZE) { if (direction == OUTPUT && ifMTU != -1) SendNeedFragIcmp(icmpSock, (struct ip *)buf, ifMTU - aliasOverhead); } else if (errno == EACCES && logIpfwDenied) { sprintf(msgBuf, "failed to write packet back"); Warn(msgBuf); } } }
main() { OSCbuf myBuf; OSCbuf *b = &myBuf; char bytes[SIZE]; OSCTimeTag tt; printf("OSC_initBuffer\n"); OSC_initBuffer(b, SIZE, bytes); PrintBuf(b); printf("Testing one-message packet\n"); if (OSC_writeAddress(b, "/blah/bleh/singlemessage")) { printf("** ERROR: %s\n", OSC_errorMessage); } if (OSC_writeFloatArg(b, 1.23456f)) { printf("** ERROR: %s\n", OSC_errorMessage); } { float floatarray[10]; int i; for (i = 0; i < 10; ++i) { floatarray[i] = i * 10.0f; } if (OSC_writeFloatArgs(b, 10, floatarray)) { printf("** ERROR: %s\n", OSC_errorMessage); } } if (OSC_writeIntArg(b, 123456)) { printf("** ERROR: %s\n", OSC_errorMessage); } if (OSC_writeStringArg(b, "This is a cool string, dude.")) { printf("** ERROR: %s\n", OSC_errorMessage); } PrintBuf(b); PrintPacket(b); printf("Resetting\n"); OSC_resetBuffer(b); printf("Testing time tags\n"); tt = OSCTT_CurrentTime(); printf("Time now is %llx\n", tt); printf("Testing bundles\n"); if (OSC_openBundle(b, tt)) { printf("** ERROR: %s\n", OSC_errorMessage); } if (OSC_writeAddress(b, "/a/hello")) { printf("** ERROR: %s\n", OSC_errorMessage); } if (OSC_writeIntArg(b, 16)) { printf("** ERROR: %s\n", OSC_errorMessage); } if (OSC_writeIntArg(b, 32)) { printf("** ERROR: %s\n", OSC_errorMessage); } if (OSC_openBundle(b, OSCTT_PlusSeconds(tt, 1.0f))) { printf("** ERROR: %s\n", OSC_errorMessage); } if (OSC_writeAddress(b, "/b/hello")) { printf("** ERROR: %s\n", OSC_errorMessage); } if (OSC_writeAddress(b, "/c/hello")) { printf("** ERROR: %s\n", OSC_errorMessage); } OSC_closeAllBundles(b); PrintBuf(b); PrintPacket(b); }