static CFDataRef IPCCallback(__unused CFMessagePortRef port, SInt32 messageID, __unused CFDataRef data, __unused void *info) { switch(messageID) { case ServerCommandGetIP: { if(publicIPState == IPStateInvalid) { SCNetworkReachabilityFlags flags; SCNetworkReachabilityGetFlags(reachability, &flags); ReachabilityCallback(reachability, flags, NULL); } if(publicIPData) return publicIPData; return CFStringCreateExternalRepresentation(kCFAllocatorDefault, CFSTR(""), kCFStringEncodingUTF8, 0); } case ServerCommandStop: { CFRunLoopStop(CFRunLoopGetMain()); return NULL; } case ServerCommandReload: { FetchIPAddress(); return NULL; } default: return NULL; } }
static void ReachabilityCallback(__unused SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, __unused void *info) { if(flags & kSCNetworkReachabilityFlagsReachable) { FetchIPAddress(); } else { CFRelease(publicIPData); publicIPData = NULL; publicIPState = IPStateInvalid; } }
int CheckForNewIPAddress(unsigned int *LocalAddr) { IPECHO_MESSAGE Msg ; int Socket ; fd_set SocketArray ; int Result, ResponseLength ; struct sockaddr_in sin; int Tries = 10 ; unsigned int EchoServerIPAddress ; struct timeval tv = {0,0} ; int BytesSent ; //if ((EchoServerIPAddress = FetchIPAddress(ECHO_SERVER_TO_USE)) == 0) { if ((EchoServerIPAddress = FetchIPAddress(TZO_ECHO_SERVER)) == 0) { printf("115 EchoServerIPAddress = %d \n",EchoServerIPAddress); printf("Unable to contact TZO Echo Servers\n") ; return(-1) ; } printf("119 EchoServerIPAddress = %d \n",EchoServerIPAddress); if ((Socket = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { printf("Unable to create a socket\n") ; return(-1) ; } // test // EchoServerIPAddress = 0x401BA664;--->"No resonce from server\n" // EchoServerIPAddress = 0x64A61B40 ; sin.sin_family = AF_INET ; sin.sin_addr.s_addr = EchoServerIPAddress ; sin.sin_port = htons(IPECHO_PORT) ; Msg.ID = IPECHO_ID_CLIENT ; Msg.Request = IPECHO_REQUEST_ECHO ; Msg.IPAddress = *LocalAddr ; Msg.Signature = CalcIPEchoSignature(&Msg) ; ResponseLength = 0 ; printf("LocalAddr =%d \n",Msg.IPAddress); if ((BytesSent = sendto(Socket, (unsigned char *)&Msg, sizeof(Msg), 0, (const struct sockaddr *)&sin, sizeof(sin))) < 0) { //sin.sin_addr.s_addr =0x64A61B40 ;//0x401BA664; //if ((BytesSent = sendto(Socket, (unsigned char *)&Msg, sizeof(Msg), 0, (const struct sockaddr *)&sin, sizeof(sin))) < 0) { printf("Unable to sendto socket\n") ; close(Socket) ; return(-1) ; } while (Tries--) { tv.tv_sec = 0 ; tv.tv_usec = 0 ; FD_ZERO(&SocketArray) ; FD_SET(Socket, &SocketArray) ; tv.tv_sec = 1 ; tv.tv_usec = 0 ; Result = select(Socket+1, &SocketArray, NULL, NULL, &tv) ; if (Result) break ; sleep(1) ; } if (!Result) { printf("No resonce from server\n") ; close(Socket) ; return(-1) ; } if ((ResponseLength = recvfrom(Socket, (unsigned char *)&Msg, 16, 0, 0, 0)) <= 0) { printf("Bad responce from server\n") ; close(Socket) ; return(-1) ; } close(Socket) ; if (ResponseLength == sizeof(IPECHO_MESSAGE)) { *LocalAddr = FlipByteOrder(Msg.IPAddress) ; return 1 ; } printf("strange server response, are you connecting to the right server?\n") ; return(-1) ; }