int AdhocPtpHostStart(void) { unsigned char mac[6]; unsigned char clientmac[6]; unsigned short clientport; AdhocPtpHostId = -1; AdhocPtpClientId = -1; int ret = sceNetGetLocalEtherAddr(mac); if(ret < 0) { printf("Adhoc error: sceNetGetLocalEtherAddr\n"); return 0; } ret = sceNetAdhocPtpListen(mac, 1, 8192, 200*1000, 300, 1, 0); if(ret < 0) { printf("Adhoc error: sceNetAdhocPtpListen 0x%08X\n", ret); if(AdhocPtpHostId > 0) sceNetAdhocPtpClose(AdhocPtpHostId, 0); if(AdhocPtpClientId > 0) sceNetAdhocPtpClose(AdhocPtpClientId, 0); return ret; } AdhocPtpHostId = ret; ret = sceNetAdhocPtpAccept(AdhocPtpHostId, &clientmac[0], &clientport, 0, 0); if(ret < 0) { printf("Adhoc error: sceNetAdhocPtpAccept\n"); if(AdhocPtpHostId > 0) sceNetAdhocPtpClose(AdhocPtpHostId, 0); if(AdhocPtpClientId > 0) sceNetAdhocPtpClose(AdhocPtpClientId, 0); return ret; } AdhocPtpClientId = ret; return ret; }
/** * Local MAC Check * @param saddr To-be-checked MAC Address * @return 1 if valid or... 0 */ int _IsLocalMAC(const SceNetEtherAddr * addr) { // Get Local MAC Address SceNetEtherAddr saddr; sceNetGetLocalEtherAddr(&saddr); // Compare MAC Addresses int match = memcmp((const void *)addr, (const void *)&saddr, ETHER_ADDR_LEN); // Return Result return (match == 0); }
/*---------------------------------------------------------------------- | 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; }
int AdhocPtpClientStart(const char *servermac) { unsigned char mac[6]; sceNetEtherStrton(servermac, mac); unsigned char clientmac[6]; int ret = sceNetGetLocalEtherAddr(clientmac); if(ret < 0) { printf("Adhoc error: sceNetGetLocalEtherAddr\n"); return ret; } ret = sceNetAdhocPtpOpen(clientmac, 0, mac, 1, 8192, 200*1000, 300, 0); if(ret < 0) { printf("Adhoc error: sceNetAdhocPtpOpen\n"); if(AdhocPtpClientId > 0) sceNetAdhocPtpClose(AdhocPtpClientId, 0); return ret; } AdhocPtpClientId = ret; while(1) { ret = sceNetAdhocPtpConnect(AdhocPtpClientId, 0, 1); if(ret < 0 && ret != (int)0x80410709) { printf("Adhoc error: sceNetAdhocPtpConnect 0x%08X\n", ret); if(AdhocPtpClientId > 0) sceNetAdhocPtpClose(AdhocPtpClientId, 0); return ret; } if(ret == 0) break; sceKernelDelayThread(200*1000); } return ret; }
/** * Create Matching Context * @param mode Operating Mode (HOST, CLIENT, P2P) * @param maxnum Member Limit * @param port PDP Port * @param rxbuflen Receive Buffer Size * @param hello_int Hello Interval (Microseconds) * @param keepalive_int Keep-Alive Interval (Microseconds) * @param init_count Resend & Keep-Alive Counter Initial Value * @param rexmt_int Message Resend Interval (Microseconds) * @return ID > 0 on success or... ADHOC_MATCHING_NOT_INITIALIZED, ADHOC_MATCHING_INVALID_MAXNUM, ADHOC_MATCHING_RXBUF_TOO_SHORT, ADHOC_MATCHING_INVALID_ARG, ADHOC_MATCHING_NO_SPACE, ADHOC_MATCHING_ID_NOT_AVAIL, ADHOC_MATCHING_PORT_IN_USE */ int proNetAdhocMatchingCreate(int mode, int maxnum, uint16_t port, int rxbuflen, uint32_t hello_int, uint32_t keepalive_int, int init_count, uint32_t rexmt_int, SceNetAdhocMatchingHandler handler) { // Library initialized if(_init == 1) { // Valid Member Limit if(maxnum > 1 && maxnum <= 16) { // Valid Receive Buffer size if(rxbuflen >= 1024) // Maybe 2048? { // Valid Arguments if(mode >= 1 && mode <= 3) { // Iterate Matching Contexts SceNetAdhocMatchingContext * item = _contexts; for(; item != NULL; item = item->next) { // Port Match found if(item->port == port) return ADHOC_MATCHING_PORT_IN_USE; } // Allocate Context Memory SceNetAdhocMatchingContext * context = (SceNetAdhocMatchingContext *)_malloc(sizeof(SceNetAdhocMatchingContext)); // Allocated Memory if(context != NULL) { // Create PDP Socket SceNetEtherAddr localmac; sceNetGetLocalEtherAddr(&localmac); int socket = sceNetAdhocPdpCreate(&localmac, port, rxbuflen, 0); // Created PDP Socket if(socket > 0) { // Clear Memory memset(context, 0, sizeof(SceNetAdhocMatchingContext)); // Allocate Receive Buffer context->rxbuf = (uint8_t *)_malloc(rxbuflen); // Allocated Memory if(context->rxbuf != NULL) { // Clear Memory memset(context->rxbuf, 0, rxbuflen); // Fill in Context Data context->id = _findFreeMatchingID(); context->mode = mode; context->maxpeers = maxnum; context->port = port; context->socket = socket; context->rxbuflen = rxbuflen; context->hello_int = hello_int; context->keepalive_int = 500000; //context->keepalive_int = keepalive_int; context->resendcounter = init_count; context->keepalivecounter = 100; //context->keepalivecounter = init_count; context->resend_int = rexmt_int; context->handler = handler; // Fill in Selfpeer context->mac = localmac; // Link Context context->next = _contexts; _contexts = context; // Return Matching ID return context->id; } // Close PDP Socket sceNetAdhocPdpDelete(socket, 0); } // Free Memory _free(context); // Port in use if(socket < 1) return ADHOC_MATCHING_PORT_IN_USE; } // Out of Memory return ADHOC_MATCHING_NO_SPACE; } // Invalid Arguments return ADHOC_MATCHING_INVALID_ARG; } // Invalid Receive Buffer Size return ADHOC_MATCHING_RXBUF_TOO_SHORT; } // Invalid Member Limit return ADHOC_MATCHING_INVALID_MAXNUM; } // Uninitialized Library return ADHOC_MATCHING_NOT_INITIALIZED; }