static xjse_result_t xpnet_pollwait_apcon(XPNETCTX* pxpnctx) { #define XPNET_APCTLCONN_TIMEDOUT (12000) #define XPNET_APCTLCONN_POLLDURA (300) #ifdef XTPF_Linux // simulate apcon wait xjse_time_t tnow; while(1) { xjse_time_now(&tnow); if(tnow > pxpnctx->tlastcomm + 3 * 1000) { XJSE_TRACE("(X) exiting hoax apcon wait.(%lld, %lld)", tnow, pxpnctx->tlastcomm); break; } xjse_sleep(XPNET_APCTLCONN_POLLDURA); } pxpnctx->napconstatus = XPNETAPCTLSTAT_CONNED; #endif //XTPF_Linux #ifdef XTPF_PSP xjse_int_t ncnt = 0, nsceerr = 0, nstatelast = -1; while(1) { nsceerr = sceNetApctlGetState(&(pxpnctx->napconstatus)); if(nsceerr != 0) { XJSE_TRACE("(E) sceNetApctlGetState() failed!"); goto failed; } if(pxpnctx->napconstatus > nstatelast) { XJSE_TRACE("(X) connecting... (%d -> %d)", nstatelast, pxpnctx->napconstatus); nstatelast = pxpnctx->napconstatus; } if(pxpnctx->napconstatus == XPNETAPCTLSTAT_CONNED) break; // connected sceKernelDelayThread(XPNET_APCTLCONN_POLLDURA * 1000); if(ncnt++ > (XPNET_APCTLCONN_TIMEDOUT / XPNET_APCTLCONN_POLLDURA)) { XJSE_TRACE("(E) connection timed out!"); goto failed; } } #if 1 char szipaddr[32]; if(sceNetApctlGetInfo(8, szipaddr) != 0) { XJSE_TRACE("(E) no ip address!"); } else { XJSE_TRACE("(X) ip address retrieved: [%s]", szipaddr); } #endif #endif //XTPF_PSP return XJSE_SUCCESS; #ifdef XTPF_PSP failed: pxpnctx->napconstatus = XPNETAPCTLSTAT_NOTCONN; return XJSE_E_UNKNOWN; #endif //XTPF_PSP }
/* Select && Connect to an Access Point */ int selectAccessPoint(int selected) { int err; int stateLast = -1; int state; /* Connect using the first profile */ err = sceNetApctlConnect(selected); if (err != 0) { writeToLogFile("Error, sceNetApctlConnect returns %08X\n", err); wifiError = 1; return -1; } //printf("Connecting...\n"); while(1) { err = sceNetApctlGetState(&state); if(err != 0) { writeToLogFile("sceNetApctlGetState returns $%x\n", err); wifiError = 1; break; } if(state > stateLast) { //printf("Connection state %d of 4\n", state); stateLast = state; } /* Connected with IP Address*/ if(state == 4) break; /* Wait 50 ms before polling again */ sceKernelDelayThread(50*1000); } /* Now obtain IP Address */ while(1) { if(sceNetApctlGetInfo(8, AccessPoints[selected-1].pInfo) == 0) { AccessPoints[selected-1].color = DARK_GREEN; addPSP("SamuraiX", "11:11:11:11", "22:22:22:22", AccessPoints[selected-1].pInfo->ip, 5060, "88:88:88:88", "99:99:99:99", AccessPoints[selected-1].pInfo->ip, 5060, 0); break; } sceKernelDelayThread(1000 * 1000); } if(err != 0) { wifiError = 1; return -1; } return 1; }
/*---------------------------------------------------------------------- | NPT_NetworkInterface::GetNetworkInterfaces +---------------------------------------------------------------------*/ NPT_Result NPT_NetworkInterface::GetNetworkInterfaces(NPT_List<NPT_NetworkInterface*>& interfaces) { union SceNetApctlInfo info; int ret = sceNetApctlGetInfo(SCE_NET_APCTL_INFO_IP_ADDRESS, &info); if (ret < 0) { return NPT_FAILURE; } NPT_IpAddress primary_address; if (NPT_FAILED(primary_address.Parse(info.ip_address))) { return NPT_FAILURE; } NPT_IpAddress netmask; if (NPT_FAILED(netmask.Parse(info.netmask))) { return NPT_FAILURE; } NPT_IpAddress broadcast_address; NPT_Flags flags = 0; flags |= NPT_NETWORK_INTERFACE_FLAG_BROADCAST; flags |= NPT_NETWORK_INTERFACE_FLAG_MULTICAST; // get mac address SceNetEtherAddr mac_info; ret = sceNetGetLocalEtherAddr(&mac_info); if (ret < 0) { return NPT_FAILURE; } NPT_MacAddress mac(TYPE_IEEE_802_11, mac_info.data, SCE_NET_ETHER_ADDR_LEN); // create an interface object char iface_name[5]; iface_name[0] = 'i'; iface_name[1] = 'f'; iface_name[2] = '0'; iface_name[3] = '0'; iface_name[4] = '\0'; NPT_NetworkInterface* iface = new NPT_NetworkInterface(iface_name, mac, flags); // set the interface address NPT_NetworkInterfaceAddress iface_address( primary_address, broadcast_address, NPT_IpAddress::Any, netmask); iface->AddAddress(iface_address); // add the interface to the list interfaces.Add(iface); return NPT_SUCCESS; }
static PyObject* get_ip(PyObject *self, PyObject *args) { char ipaddr[32]; if (!PyArg_ParseTuple(args, "")) return NULL; if (sceNetApctlGetInfo(8, ipaddr) != 0) { PyErr_SetString(net_error, "Could not get IP"); return NULL; } return Py_BuildValue("s", ipaddr); }
/** * Friend Finder Thread (Receives Peer Information) * @param args Length of argp in Bytes (Unused) * @param argp Argument (Unused) * @return Unused Value - Return 0 */ int _friendFinder(SceSize args, void * argp) { // Receive Buffer int rxpos = 0; uint8_t rx[1024]; // Chat Packet SceNetAdhocctlChatPacketC2S chat; chat.base.opcode = OPCODE_CHAT; // Last Ping Time uint64_t lastping = 0; // Last Time Reception got updated uint64_t lastreceptionupdate = 0; // Finder Loop while(_init == 1) { // Acquire Network Lock _acquireNetworkLock(); // Ping Server if((sceKernelGetSystemTimeWide() - lastping) >= ADHOCCTL_PING_TIMEOUT) { // Update Ping Time lastping = sceKernelGetSystemTimeWide(); // Prepare Packet uint8_t opcode = OPCODE_PING; // Send Ping to Server sceNetInetSend(_metasocket, &opcode, 1, INET_MSG_DONTWAIT); } // Send Chat Messages while(popFromOutbox(chat.message)) { // Send Chat to Server sceNetInetSend(_metasocket, &chat, sizeof(chat), INET_MSG_DONTWAIT); } // Wait for Incoming Data int received = sceNetInetRecv(_metasocket, rx + rxpos, sizeof(rx) - rxpos, INET_MSG_DONTWAIT); // Free Network Lock _freeNetworkLock(); // Received Data if(received > 0) { // Fix Position rxpos += received; // Log Incoming Traffic #ifdef DEBUG printk("Received %d Bytes of Data from Server\n", received); #endif } // Handle Packets if(rxpos > 0) { // BSSID Packet if(rx[0] == OPCODE_CONNECT_BSSID) { // Enough Data available if(rxpos >= sizeof(SceNetAdhocctlConnectBSSIDPacketS2C)) { // Cast Packet SceNetAdhocctlConnectBSSIDPacketS2C * packet = (SceNetAdhocctlConnectBSSIDPacketS2C *)rx; // Update BSSID _parameter.bssid.mac_addr = packet->mac; // Change State _thread_status = ADHOCCTL_STATE_CONNECTED; // Notify Event Handlers int i = 0; for(; i < ADHOCCTL_MAX_HANDLER; i++) { // Active Handler if(_event_handler[i] != NULL) _event_handler[i](ADHOCCTL_EVENT_CONNECT, 0, _event_args[i]); } // Move RX Buffer memmove(rx, rx + sizeof(SceNetAdhocctlConnectBSSIDPacketS2C), sizeof(rx) - sizeof(SceNetAdhocctlConnectBSSIDPacketS2C)); // Fix RX Buffer Length rxpos -= sizeof(SceNetAdhocctlConnectBSSIDPacketS2C); } } // Chat Packet else if(rx[0] == OPCODE_CHAT) { // Enough Data available if(rxpos >= sizeof(SceNetAdhocctlChatPacketS2C)) { // Cast Packet SceNetAdhocctlChatPacketS2C * packet = (SceNetAdhocctlChatPacketS2C *)rx; // Fix for Idiots that try to troll the "ME" Nametag if(stricmp((char *)packet->name.data, "ME") == 0) strcpy((char *)packet->name.data, "NOT ME"); // Add Incoming Chat to HUD addChatLog((char *)packet->name.data, packet->base.message); // Move RX Buffer memmove(rx, rx + sizeof(SceNetAdhocctlChatPacketS2C), sizeof(rx) - sizeof(SceNetAdhocctlChatPacketS2C)); // Fix RX Buffer Length rxpos -= sizeof(SceNetAdhocctlChatPacketS2C); } } // Connect Packet else if(rx[0] == OPCODE_CONNECT) { // Enough Data available if(rxpos >= sizeof(SceNetAdhocctlConnectPacketS2C)) { // Log Incoming Peer #ifdef DEBUG printk("Incoming Peer Data...\n"); #endif // Cast Packet SceNetAdhocctlConnectPacketS2C * packet = (SceNetAdhocctlConnectPacketS2C *)rx; // Add User _addFriend(packet); // Update HUD User Count #ifdef LOCALHOST_AS_PEER setUserCount(_getActivePeerCount()); #else setUserCount(_getActivePeerCount()+1); #endif // Move RX Buffer memmove(rx, rx + sizeof(SceNetAdhocctlConnectPacketS2C), sizeof(rx) - sizeof(SceNetAdhocctlConnectPacketS2C)); // Fix RX Buffer Length rxpos -= sizeof(SceNetAdhocctlConnectPacketS2C); } } // Disconnect Packet else if(rx[0] == OPCODE_DISCONNECT) { // Enough Data available if(rxpos >= sizeof(SceNetAdhocctlDisconnectPacketS2C)) { // Log Incoming Peer Delete Request #ifdef DEBUG printk("Incoming Peer Data Delete Request...\n"); #endif // Cast Packet SceNetAdhocctlDisconnectPacketS2C * packet = (SceNetAdhocctlDisconnectPacketS2C *)rx; // Delete User by IP _deleteFriendByIP(packet->ip); // Update HUD User Count #ifdef LOCALHOST_AS_PEER setUserCount(_getActivePeerCount()); #else setUserCount(_getActivePeerCount()+1); #endif // Move RX Buffer memmove(rx, rx + sizeof(SceNetAdhocctlDisconnectPacketS2C), sizeof(rx) - sizeof(SceNetAdhocctlDisconnectPacketS2C)); // Fix RX Buffer Length rxpos -= sizeof(SceNetAdhocctlDisconnectPacketS2C); } } // Scan Packet else if(rx[0] == OPCODE_SCAN) { // Enough Data available if(rxpos >= sizeof(SceNetAdhocctlScanPacketS2C)) { // Log Incoming Network Information #ifdef DEBUG printk("Incoming Group Information...\n"); #endif // Cast Packet SceNetAdhocctlScanPacketS2C * packet = (SceNetAdhocctlScanPacketS2C *)rx; // Allocate Structure Data SceNetAdhocctlScanInfo * group = (SceNetAdhocctlScanInfo *)malloc(sizeof(SceNetAdhocctlScanInfo)); // Allocated Structure Data if(group != NULL) { // Clear Memory memset(group, 0, sizeof(SceNetAdhocctlScanInfo)); // Link to existing Groups group->next = _networks; // Copy Group Name group->group_name = packet->group; // Set Group Host group->bssid.mac_addr = packet->mac; // Link into Group List _networks = group; } // Move RX Buffer memmove(rx, rx + sizeof(SceNetAdhocctlScanPacketS2C), sizeof(rx) - sizeof(SceNetAdhocctlScanPacketS2C)); // Fix RX Buffer Length rxpos -= sizeof(SceNetAdhocctlScanPacketS2C); } } // Scan Complete Packet else if(rx[0] == OPCODE_SCAN_COMPLETE) { // Log Scan Completion #ifdef DEBUG printk("Incoming Scan complete response...\n"); #endif // Change State _thread_status = ADHOCCTL_STATE_DISCONNECTED; // Notify Event Handlers int i = 0; for(; i < ADHOCCTL_MAX_HANDLER; i++) { // Active Handler if(_event_handler[i] != NULL) _event_handler[i](ADHOCCTL_EVENT_SCAN, 0, _event_args[i]); } // Move RX Buffer memmove(rx, rx + 1, sizeof(rx) - 1); // Fix RX Buffer Length rxpos -= 1; } } // Reception Update required if((sceKernelGetSystemTimeWide() - lastreceptionupdate) > 5000000) { // Clone Time into Last Update Field lastreceptionupdate = sceKernelGetSystemTimeWide(); // Update Reception Level union SceNetApctlInfo info; if(sceNetApctlGetInfo(PSP_NET_APCTL_INFO_STRENGTH, &info) >= 0) setReceptionPercentage((int)info.strength); else setReceptionPercentage(0); } // Delay Thread (10ms) sceKernelDelayThread(10000); } // Set WLAN HUD Reception to 0% on Shutdown setReceptionPercentage(0); // Notify Caller of Shutdown _init = -1; // Log Shutdown printk("End of Friend Finder Thread\n"); // Reset Thread Status _thread_status = ADHOCCTL_STATE_DISCONNECTED; // Kill Thread sceKernelExitDeleteThread(0); // Return Success return 0; }
/** * Resolve Player Name to Linked List of Peers * @param nickname To-be-resolved Player Nickname * @param buflen IN: Buffer Length in Bytes OUT: Required / Used Buffer Length in Bytes * @param buf OUT: Resolved Peer Information (if NULL then buflen gets filled with required length) * @return 0 on success or... ADHOCCTL_NOT_INITIALIZED, ADHOCCTL_INVALID_ARG */ int proNetAdhocctlGetAddrByName(const SceNetAdhocctlNickname * nickname, int * buflen, SceNetAdhocctlPeerInfo * buf) { // Library initialized if(_init == 1) { // Valid Arguments if(nickname != NULL && buflen != NULL) { // Multithreading Lock _acquirePeerLock(); // Length Calculation Mode if(buf == NULL) *buflen = _getNicknameCount(nickname) * sizeof(SceNetAdhocctlPeerInfo); // Normal Information Mode else { // Clear Memory memset(buf, 0, *buflen); // Discovered Player Count int discovered = 0; // Calculate Requested Elements int requestcount = *buflen / sizeof(SceNetAdhocctlPeerInfo); // Minimum Space available if(requestcount > 0) { // Local Nickname Matches if(strcmp((char *)_parameter.nickname.data, (char *)nickname->data) == 0) { // Get Local IP Address union SceNetApctlInfo info; if(sceNetApctlGetInfo(PSP_NET_APCTL_INFO_IP, &info) == 0) { // Add Local Address buf[discovered].nickname = _parameter.nickname; sceWlanGetEtherAddr((void *)buf[discovered].mac_addr.data); sceNetInetInetAton(info.ip, &buf[discovered].ip_addr); buf[discovered++].last_recv = sceKernelGetSystemTimeWide(); } } // Peer Reference SceNetAdhocctlPeerInfo * peer = _friends; // Iterate Peers for(; peer != NULL && discovered < requestcount; peer = peer->next) { // Match found if(strcmp((char *)peer->nickname.data, (char *)nickname->data) == 0) { // Fake Receive Time peer->last_recv = sceKernelGetSystemTimeWide(); // Copy Peer Info buf[discovered++] = *peer; } } // Link List int i = 0; for(; i < discovered - 1; i++) { // Link Network buf[i].next = &buf[i + 1]; } // Fix Last Element if(discovered > 0) buf[discovered - 1].next = NULL; } // Fix Buffer Size *buflen = discovered * sizeof(SceNetAdhocctlPeerInfo); } // Multithreading Unlock _freePeerLock(); // Return Success return 0; } // Invalid Arguments return ADHOCCTL_INVALID_ARG; } // Library uninitialized return ADHOCCTL_NOT_INITIALIZED; }
/** * Adhoc Emulator PDP Send Call * @param id Socket File Descriptor * @param daddr Target MAC Address * @param dport Target Port * @param data Data Payload * @param len Payload Length * @param timeout Send Timeout * @param flag Nonblocking Flag * @return 0 on success or... ADHOC_INVALID_ARG, ADHOC_NOT_INITIALIZED, ADHOC_INVALID_SOCKET_ID, ADHOC_SOCKET_DELETED, ADHOC_INVALID_ADDR, ADHOC_INVALID_PORT, ADHOC_INVALID_DATALEN, ADHOC_SOCKET_ALERTED, ADHOC_TIMEOUT, ADHOC_THREAD_ABORTED, ADHOC_WOULD_BLOCK, NET_NO_SPACE, NET_INTERNAL */ int proNetAdhocPdpSend(int id, const SceNetEtherAddr * daddr, uint16_t dport, const void * data, int len, uint32_t timeout, int flag) { // Library is initialized if(_init) { // Valid Port if(dport != 0) { // Valid Data Length if(len > 0) { // Valid Socket ID if(id > 0 && id <= 255 && _pdp[id - 1] != NULL) { // Cast Socket SceNetAdhocPdpStat * socket = _pdp[id - 1]; // Valid Data Buffer if(data != NULL) { // Valid Destination Address if(daddr != NULL) { // Log Destination #ifdef DEBUG printk("Attempting PDP Send to %02X:%02X:%02X:%02X:%02X:%02X on Port %u\n", daddr->data[0], daddr->data[1], daddr->data[2], daddr->data[3], daddr->data[4], daddr->data[5], dport); #endif // Schedule Timeout Removal if(flag) timeout = 0; // Apply Send Timeout Settings to Socket sceNetInetSetsockopt(socket->id, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)); // Single Target if(!_isBroadcastMAC(daddr)) { // Fill in Target Structure SceNetInetSockaddrIn target; target.sin_family = AF_INET; target.sin_port = sceNetHtons(dport); // Get Peer IP if(_resolveMAC((SceNetEtherAddr *)daddr, &target.sin_addr) == 0) { // Acquire Network Lock _acquireNetworkLock(); // Send Data int sent = sceNetInetSendto(socket->id, data, len, ((flag != 0) ? (INET_MSG_DONTWAIT) : (0)), (SceNetInetSockaddr *)&target, sizeof(target)); // Free Network Lock _freeNetworkLock(); // Sent Data if(sent == len) { // Success return 0; } // Blocking Situation if(flag) return ADHOC_WOULD_BLOCK; // Timeout return ADHOC_TIMEOUT; } } // Broadcast Target else { // Acquire Network Lock _acquireNetworkLock(); #ifdef BROADCAST_TO_LOCALHOST // Get Local IP Address union SceNetApctlInfo info; if(sceNetApctlGetInfo(PSP_NET_APCTL_INFO_IP, &info) == 0) { // Fill in Target Structure SceNetInetSockaddrIn target; target.sin_family = AF_INET; sceNetInetInetAton(info.ip, &target.sin_addr); target.sin_port = sceNetHtons(dport); // Send Data sceNetInetSendto(socket->id, data, len, ((flag != 0) ? (INET_MSG_DONTWAIT) : (0)), (SceNetInetSockaddr *)&target, sizeof(target)); } #endif // Acquire Peer Lock _acquirePeerLock(); // Iterate Peers SceNetAdhocctlPeerInfo * peer = _getInternalPeerList(); for(; peer != NULL; peer = peer->next) { // Fill in Target Structure SceNetInetSockaddrIn target; target.sin_family = AF_INET; target.sin_addr = peer->ip_addr; target.sin_port = sceNetHtons(dport); // Send Data sceNetInetSendto(socket->id, data, len, ((flag != 0) ? (INET_MSG_DONTWAIT) : (0)), (SceNetInetSockaddr *)&target, sizeof(target)); } // Free Peer Lock _freePeerLock(); // Free Network Lock _freeNetworkLock(); // Broadcast never fails! return 0; } } // Invalid Destination Address return ADHOC_INVALID_ADDR; } // Invalid Argument return ADHOC_INVALID_ARG; } // Invalid Socket ID return ADHOC_INVALID_SOCKET_ID; } // Invalid Data Length return ADHOC_INVALID_DATALEN; } // Invalid Destination Port return ADHOC_INVALID_PORT; } // Library is uninitialized return ADHOC_NOT_INITIALIZED; }