void httpgetsend(ENetAddress &ad, char *hostname, char *req, char *ref, char *agent) { if (ad.host == ENET_HOST_ANY) { printf("looking up %s...\n", hostname); enet_address_set_host(&ad, hostname); if (ad.host == ENET_HOST_ANY) return; }; if (mssock != ENET_SOCKET_NULL) enet_socket_destroy(mssock); mssock = enet_socket_create(ENET_SOCKET_TYPE_STREAM); if (mssock == ENET_SOCKET_NULL) { printf("could not open socket\n"); return; }; if (enet_socket_connect(mssock, &ad) < 0) { printf("could not connect\n"); return; }; ENetBuffer buf; IString httpget; std::sprintf(httpget, "GET %s HTTP/1.0\nHost: %s\nReferer: %s\nUser-Agent: %s\n\n", req, hostname, ref, agent); buf.data = httpget; buf.dataLength = strlen((char *) buf.data); printf("sending request to %s...\n", hostname); enet_socket_send(mssock, NULL, &buf, 1); }
void LANGameFinderAndAnnouncer::announceGame() { ENetAddress destinationAddress; destinationAddress.host = ENET_HOST_BROADCAST; destinationAddress.port = LANGAME_ANNOUNCER_PORT; NetworkPacket_AnnounceGame announcePacket; memset(&announcePacket, 0, sizeof(NetworkPacket_AnnounceGame)); announcePacket.magicNumber = SDL_SwapLE32(LANGAME_ANNOUNCER_MAGICNUMBER); announcePacket.type = NETWORKPACKET_ANNOUNCEGAME; strncpy(announcePacket.serverName, serverName.c_str(), LANGAME_ANNOUNCER_MAXGAMENAMESIZE); strncpy(announcePacket.serverVersion, VERSIONSTRING, LANGAME_ANNOUNCER_MAXGAMEVERSIONSIZE); announcePacket.serverPort = SDL_SwapLE16(serverPort); strncpy(announcePacket.mapName, mapName.c_str(), LANGAME_ANNOUNCER_MAXMAPNAMESIZE); announcePacket.numPlayers = numPlayers; announcePacket.maxPlayers = maxPlayers; ENetBuffer enetBuffer; enetBuffer.data = &announcePacket; enetBuffer.dataLength = sizeof(NetworkPacket_AnnounceGame); int err = enet_socket_send(announceSocket, &destinationAddress, &enetBuffer, 1); if(err==0) { // blocked } else if(err < 0) { throw std::runtime_error("LANGameFinderAndAnnouncer: Announcing failed!"); } else { lastAnnounce = SDL_GetTicks(); } }
void serverms(int mode, int numplayers, int minremain, char *smapname, int seconds, bool isfull) { checkmasterreply(); updatemasterserver(seconds); // reply all server info requests ENetBuffer buf; ENetAddress addr; u8 pong[MAXTRANS], *p; int len; enet_uint32 events = ENET_SOCKET_WAIT_RECEIVE; buf.data = pong; while (enet_socket_wait(pongsock, &events, 0) >= 0 && events) { buf.dataLength = sizeof(pong); len = enet_socket_receive(pongsock, &addr, &buf, 1); if (len < 0) return; p = &pong[len]; putint(p, PROTOCOL_VERSION); putint(p, mode); putint(p, numplayers); putint(p, minremain); string mname; strcpy_s(mname, isfull ? "[FULL] " : ""); strcat_s(mname, smapname); sendstring(mname, p); sendstring(serverdesc, p); buf.dataLength = p - pong; enet_socket_send(pongsock, &addr, &buf, 1); } }
void BroadcastHost::update() { int length; ENetAddress addr; ENetBuffer bcbuffer; bcbuffer.data = new char[1024]; bcbuffer.dataLength = 1024; // Look for broadcast messages while ((length = enet_socket_receive(bcastsocket, &addr, &bcbuffer, 1)) != 0) { if (length < 0) { std::cout << "Socket error: " << errno << std::endl; break; } else { // Send answer ENetBuffer replybuffer; if (reply) { replybuffer.data = reply->getData(); replybuffer.dataLength = reply->getSize(); } else { replybuffer.data = (void*)"PONG"; replybuffer.dataLength = 5; } enet_socket_send(bcastsocket, &addr, &replybuffer, 1); } } delete[] (char*)bcbuffer.data; }
bool logline(int level, const char *msg, ...) { if(!enabled) return false; if(level < 0 || level >= ACLOG_NUM) return false; defvformatstring(sf, msg, msg); filtertext(sf, sf, 2); const char *ts = timestamp ? timestring(true, "%b %d %H:%M:%S ") : "", *ld = levelprefix[level]; char *p, *l = sf; do { // break into single lines first if((p = strchr(l, '\n'))) *p = '\0'; if(consolethreshold <= level) printf("%s%s%s\n", ts, ld, l); if(fp && filethreshold <= level) fprintf(fp, "%s%s%s\n", ts, ld, l); if(syslogthreshold <= level) #ifdef AC_USE_SYSLOG syslog(levels[level], "%s", l); #else { defformatstring(text)("<%d>%s: %s", (16 + facility) * 8 + levels[level], ident, l); // no TIMESTAMP, no hostname: syslog will add this ENetBuffer buf; buf.data = text; buf.dataLength = strlen(text); enet_socket_send(logsock, &logdest, &buf, 1); } #endif l = p + 1; } while(p); if(consolethreshold <= level) fflush(stdout); if(fp && filethreshold <= level) fflush(fp); return consolethreshold <= level; }
void TraversalClient::ResendPacket(OutgoingTraversalPacketInfo* info) { info->sendTime = enet_time_get(); info->tries++; ENetBuffer buf; buf.data = &info->packet; buf.dataLength = sizeof(info->packet); if (enet_socket_send(m_NetHost->socket, &m_ServerAddress, &buf, 1) == -1) OnFailure(SocketSendError); }
void WakeupThread(ENetHost* host) { // Send ourselves a spurious message. This is hackier than it should be. // comex reported this as https://github.com/lsalzman/enet/issues/23, so // hopefully there will be a better way to do it in the future. ENetAddress address; if (host->address.port != 0) address.port = host->address.port; else enet_socket_get_address(host->socket, &address); address.host = 0x0100007f; // localhost u8 byte = 0; ENetBuffer buf; buf.data = &byte; buf.dataLength = 1; enet_socket_send(host->socket, &address, &buf, 1); }
/** Send an OOB packet for NAT punching. ENetHost->String->Int->val_true **/ static value udpr_send_oob(value h, value ip, value port, value data) { ENetAddress address; ENetHost *host; ENetBuffer buf; val_check_kind(h,k_udprhost); val_check(data,string); host = (ENetHost *)val_data(h); if(host == NULL) neko_error(); if(populate_address(&address, ip, port) != val_true) neko_error(); buf.data = val_string(data); buf.dataLength = val_strlen(data); enet_socket_send (host->socket, &address, &buf, 1); return val_true; }
void LANGameFinderAndAnnouncer::refreshServerList() const { ENetAddress destinationAddress; destinationAddress.host = ENET_HOST_BROADCAST; destinationAddress.port = LANGAME_ANNOUNCER_PORT; NetworkPacket_RequestGame requestPacket; memset(&requestPacket, 0, sizeof(NetworkPacket_RequestGame)); requestPacket.magicNumber = SDL_SwapLE32(LANGAME_ANNOUNCER_MAGICNUMBER); requestPacket.type = NETWORKPACKET_REQUESTANNOUNCE; ENetBuffer enetBuffer; enetBuffer.data = &requestPacket; enetBuffer.dataLength = sizeof(NetworkPacket_RequestGame); int err = enet_socket_send(announceSocket, &destinationAddress, &enetBuffer, 1); if(err==0) { // blocked } else if(err < 0) { throw std::runtime_error("LANGameFinderAndAnnouncer: Refreshing server list failed!"); } }
void LANGameFinderAndAnnouncer::sendRemoveGameAnnouncement() { ENetAddress destinationAddress; destinationAddress.host = ENET_HOST_BROADCAST; destinationAddress.port = LANGAME_ANNOUNCER_PORT; NetworkPacket_RemoveGameAnnouncement removeAnnouncementPacket; memset(&removeAnnouncementPacket, 0, sizeof(NetworkPacket_RemoveGameAnnouncement)); removeAnnouncementPacket.magicNumber = SDL_SwapLE32(LANGAME_ANNOUNCER_MAGICNUMBER); removeAnnouncementPacket.type = NETWORKPACKET_REMOVEGAMEANNOUNCEMENT; removeAnnouncementPacket.serverPort = SDL_SwapLE16(serverPort); ENetBuffer enetBuffer; enetBuffer.data = &removeAnnouncementPacket; enetBuffer.dataLength = sizeof(NetworkPacket_RemoveGameAnnouncement); int err = enet_socket_send(announceSocket, &destinationAddress, &enetBuffer, 1); if(err==0) { // would have blocked, need to resend later } else if(err < 0) { throw std::runtime_error("LANGameFinderAndAnnouncer: Removing game announcement failed!"); } }
void TraversalClient::HandleServerPacket(TraversalPacket* packet) { u8 ok = 1; switch (packet->type) { case TraversalPacketAck: if (!packet->ack.ok) { OnFailure(ServerForgotAboutUs); break; } for (auto it = m_OutgoingTraversalPackets.begin(); it != m_OutgoingTraversalPackets.end(); ++it) { if (it->packet.requestId == packet->requestId) { m_OutgoingTraversalPackets.erase(it); break; } } break; case TraversalPacketHelloFromServer: if (m_State != Connecting) break; if (!packet->helloFromServer.ok) { OnFailure(VersionTooOld); break; } m_HostId = packet->helloFromServer.yourHostId; m_State = Connected; if (m_Client) m_Client->OnTraversalStateChanged(); break; case TraversalPacketPleaseSendPacket: { // security is overrated. ENetAddress addr = MakeENetAddress(&packet->pleaseSendPacket.address); if (addr.port != 0) { char message[] = "Hello from Dolphin Netplay..."; ENetBuffer buf; buf.data = message; buf.dataLength = sizeof(message) - 1; enet_socket_send(m_NetHost->socket, &addr, &buf, 1); } else { // invalid IPV6 ok = 0; } break; } case TraversalPacketConnectReady: case TraversalPacketConnectFailed: { if (!m_PendingConnect || packet->connectReady.requestId != m_ConnectRequestId) break; m_PendingConnect = false; if (!m_Client) break; if (packet->type == TraversalPacketConnectReady) m_Client->OnConnectReady(MakeENetAddress(&packet->connectReady.address)); else m_Client->OnConnectFailed(packet->connectFailed.reason); break; } default: WARN_LOG(NETPLAY, "Received unknown packet with type %d", packet->type); break; } if (packet->type != TraversalPacketAck) { TraversalPacket ack = {}; ack.type = TraversalPacketAck; ack.requestId = packet->requestId; ack.ack.ok = ok; ENetBuffer buf; buf.data = &ack; buf.dataLength = sizeof(ack); if (enet_socket_send(m_NetHost->socket, &m_ServerAddress, &buf, 1) == -1) OnFailure(SocketSendError); } }
static int enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int checkForTimeouts) { size_t packetsSent = 1; ENetProtocolHeader header; ENetPeer * currentPeer; int sentLength; while (packetsSent > 0) for (currentPeer = host -> peers, packetsSent = 0; currentPeer < & host -> peers [host -> peerCount]; ++ currentPeer) { if (currentPeer -> state == ENET_PEER_STATE_DISCONNECTED || currentPeer -> state == ENET_PEER_STATE_ZOMBIE) continue; host -> commandCount = 0; host -> bufferCount = 1; host -> packetSize = sizeof (ENetProtocolHeader); if (enet_list_empty (& currentPeer -> acknowledgements) == 0) enet_protocol_send_acknowledgements (host, currentPeer); if (host -> commandCount < sizeof (host -> commands) / sizeof (ENetProtocol)) { if (checkForTimeouts != 0 && enet_list_empty (& currentPeer -> sentReliableCommands) == 0 && ENET_TIME_GREATER_EQUAL (timeCurrent, currentPeer -> nextTimeout) && enet_protocol_check_timeouts (host, currentPeer, event) == 1) return 1; } if (enet_list_empty (& currentPeer -> outgoingReliableCommands) == 0) enet_protocol_send_reliable_outgoing_commands (host, currentPeer); else if (enet_list_empty (& currentPeer -> sentReliableCommands) && ENET_TIME_DIFFERENCE (timeCurrent, currentPeer -> lastReceiveTime) >= ENET_PEER_PING_INTERVAL && currentPeer -> mtu - host -> packetSize >= sizeof (ENetProtocolPing)) { enet_peer_ping (currentPeer); enet_protocol_send_reliable_outgoing_commands (host, currentPeer); } if (host -> commandCount < sizeof (host -> commands) / sizeof (ENetProtocol) && enet_list_empty (& currentPeer -> outgoingUnreliableCommands) == 0) enet_protocol_send_unreliable_outgoing_commands (host, currentPeer); if (host -> commandCount == 0) continue; if (currentPeer -> packetLossEpoch == 0) currentPeer -> packetLossEpoch = timeCurrent; else if (ENET_TIME_DIFFERENCE (timeCurrent, currentPeer -> packetLossEpoch) >= ENET_PEER_PACKET_LOSS_INTERVAL && currentPeer -> packetsSent > 0) { enet_uint32 packetLoss = currentPeer -> packetsLost * ENET_PEER_PACKET_LOSS_SCALE / currentPeer -> packetsSent; #ifdef ENET_DEBUG #ifdef WIN32 printf ("peer %u: %f%%+-%f%% packet loss, %u+-%u ms round trip time, %f%% throttle, %u/%u outgoing, %u/%u incoming\n", currentPeer -> incomingPeerID, currentPeer -> packetLoss / (float) ENET_PEER_PACKET_LOSS_SCALE, currentPeer -> packetLossVariance / (float) ENET_PEER_PACKET_LOSS_SCALE, currentPeer -> roundTripTime, currentPeer -> roundTripTimeVariance, currentPeer -> packetThrottle / (float) ENET_PEER_PACKET_THROTTLE_SCALE, enet_list_size (& currentPeer -> outgoingReliableCommands), enet_list_size (& currentPeer -> outgoingUnreliableCommands), currentPeer -> channels != NULL ? enet_list_size (& currentPeer -> channels -> incomingReliableCommands) : 0, enet_list_size (& currentPeer -> channels -> incomingUnreliableCommands)); #else fprintf (stderr, "peer %u: %f%%+-%f%% packet loss, %u+-%u ms round trip time, %f%% throttle, %u/%u outgoing, %u/%u incoming\n", currentPeer -> incomingPeerID, currentPeer -> packetLoss / (float) ENET_PEER_PACKET_LOSS_SCALE, currentPeer -> packetLossVariance / (float) ENET_PEER_PACKET_LOSS_SCALE, currentPeer -> roundTripTime, currentPeer -> roundTripTimeVariance, currentPeer -> packetThrottle / (float) ENET_PEER_PACKET_THROTTLE_SCALE, enet_list_size (& currentPeer -> outgoingReliableCommands), enet_list_size (& currentPeer -> outgoingUnreliableCommands), currentPeer -> channels != NULL ? enet_list_size (& currentPeer -> channels -> incomingReliableCommands) : 0, enet_list_size (& currentPeer -> channels -> incomingUnreliableCommands)); #endif #endif currentPeer -> packetLossVariance -= currentPeer -> packetLossVariance / 4; if (packetLoss >= currentPeer -> packetLoss) { currentPeer -> packetLoss += (packetLoss - currentPeer -> packetLoss) / 8; currentPeer -> packetLossVariance += (packetLoss - currentPeer -> packetLoss) / 4; } else { currentPeer -> packetLoss -= (currentPeer -> packetLoss - packetLoss) / 8; currentPeer -> packetLossVariance += (currentPeer -> packetLoss - packetLoss) / 4; } currentPeer -> packetLossEpoch = timeCurrent; currentPeer -> packetsSent = 0; currentPeer -> packetsLost = 0; } header.peerID = ENET_HOST_TO_NET_16 (currentPeer -> outgoingPeerID); header.flags = 0; header.commandCount = host -> commandCount; header.sentTime = ENET_HOST_TO_NET_32 (timeCurrent); header.challenge = currentPeer -> challenge; host -> buffers -> data = & header; host -> buffers -> dataLength = sizeof (ENetProtocolHeader); currentPeer -> lastSendTime = timeCurrent; ++ packetsSent; sentLength = enet_socket_send (host -> socket, & currentPeer -> address, host -> buffers, host -> bufferCount); enet_protocol_remove_sent_unreliable_commands (currentPeer); if (sentLength < 0) return -1; } return 0; }