/************************************************************** * * initiateNDDS - Initialize a NDDS Domain for communications * ***************************************************************/ void initiateNDDS(int debuglevel) { char localIP[80]; /* NDDS_ID nddsCreate(int domain, int debuglevel, int multicast, char *nicIP) */ #ifndef NO_MULTICAST NDDS_Domain = nddsCreate(0,debuglevel,MULTICAST_ENABLE,(char*)getHostIP(ConsoleNicHostname,localIP)); #else NDDS_Domain = nddsCreate(0,debuglevel,MULTICAST_DISABLE,(char*)getHostIP(ConsoleNicHostname,localIP)); #endif if ( NDDS_Domain == NULL) errLogQuit(ErrLogOp,debugInfo,"Expproc: initiateNDDS(): NDDS domain failed to initialize!!!\n" ); }
bool Win32Network::getHostName(const NetAddress &addr,char*name,int maxLen){ in_addr in; hostent * entry; in.s_addr = addr.address; entry = gethostbyaddr ((char *) & in, sizeof (in_addr), AF_INET); if (entry == NULL) return getHostIP(addr, name, maxLen); strncpy (name, entry -> h_name, maxLen); return true; }
int main() { char url[200]; char buf[BUFSIZ+1]; int port = 80; struct sockaddr_in serverAddress; struct hostent* dnsResolved; printf("Enter the url to acces the images from\n"); printf("Ex: http://www-archive.mozilla.org/quality/networking/testing/datatests.html\n"); scanf("%s",url); printf("length of url is %d\n",strlen(url)); char domainname[strlen(url)]; char *page = (char *)malloc(strlen(url)); //puts(url); getDomainName(url,domainname,page); printf("The domain name is %s\n",domainname); char *ip = (char *)malloc(strlen(url)); char *isDomainName = strstr(domainname, "www"); if(isDomainName != NULL) { getHostIP(domainname,&dnsResolved); ip = (char *)inet_ntoa(dnsResolved->h_addr_list[0]);//"127.0.0.1"; printf("the Ip is %s\n",inet_ntoa(dnsResolved->h_addr_list[0])); } else { ip = domainname; } int sockid = createTcpSocket(); assignAddressToSocket(sockid,&serverAddress,port,"127.0.0.1"); connectSocket(sockid,(struct sockaddr *) &serverAddress,(int)sizeof(serverAddress)); char *getQuery = build_get_query(ip,page); sendQuery(sockid,getQuery); fetchHtmlPage(sockid,buf); int status = closeSocket(sockid); return 0; }
retCode connectRemote(char *where, int port, ioEncoding encoding, logical waitForMe, ioPo *inC, ioPo *outC) { int sock; struct sockaddr_in serv_addr; char *host = getHostname(where); struct in_addr *addr = host != NULL ? getHostIP(host, 0) : NULL; if (addr != NULL) { /* Attempt to establish links to the server */ memset((char *) &serv_addr, 0, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr = *addr; serv_addr.sin_port = htons((u_short) port); /* Create the socket ... */ if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { return Error; } else { ioPo conn = O_IO(newObject(sockClass, host, sock, encoding, ioREAD | ioWRITE)); configureIo(O_FILE(conn), (waitForMe ? turnOnBlocking : turnOffBlocking)); while (connect(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) != 0) { switch (errno) { // Unix version case EACCES: case EADDRNOTAVAIL: outMsg(logFile, "Address %U not available", host); markHostUnavail(host); closeFile(O_IO(conn)); return Error; case ECONNREFUSED: outMsg(logFile, "Connection to %U refused", host); markHostUnavail(host); closeFile(O_IO(conn)); return Error; case ETIMEDOUT: outMsg(logFile, "Connection to %U timed out", host); markHostUnavail(host); closeFile(O_IO(conn)); return Error; case ENETUNREACH: outMsg(logFile, "Network down or %U unreachable", host); markHostUnavail(host); closeFile(O_IO(conn)); return Error; case EALREADY: case EINTR: case EWOULDBLOCK: case EINPROGRESS: closeFile(O_IO(conn)); return Fail; default: outMsg(logFile, "Connection to %U refused", host); markHostUnavail(host); closeFile(O_IO(conn)); return Error; } } *inC = conn; *outC = conn; return Ok; } } else { outMsg(logFile, "cant resolve host %U", where); return Error; } }
//***************************************************************************** // //! This function obtains the current time from a SNTP server if required due //! to not having current time (when booting up) or periodically to update //! the time //! //! \param None //! //! \return 0 on success else error code //! \return Error Number of failure // //***************************************************************************** long GetCurrentTime() { int iSocketDesc; long lRetVal = -1; // // Get the time and date currently stored in the RTC // getDeviceTimeDate(); // // Calculate time difference between the last time we obtained time from a NTP server // timeDifference = dateTime.sl_tm_hour - hourSet; // This roughly works, it does however reset after midnight. // // Get the NTP time to use with the SSL process. Only call this every 6 hours to update the RTC // As we do not want to be calling the NTP server too often // if (timeDifference > 6 || timeDifference < 0) { // // Create UDP socket // iSocketDesc = sl_Socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if(iSocketDesc < 0) { CLI_Write("Could not create UDP socket.\n\r"); close(iSocketDesc); return iSocketDesc; } else { CLI_Write("Socket successfully created\n\r"); } g_sAppData.iSockID = iSocketDesc; // // Get the NTP server host IP address using the DNS lookup // lRetVal = getHostIP((char*)g_acSNTPserver, &g_sAppData.ulDestinationIP); if( lRetVal >= 0) { // // Configure the recieve timeout // struct SlTimeval_t timeVal; timeVal.tv_sec = SERVER_RESPONSE_TIMEOUT; // Seconds timeVal.tv_usec = 0; // Microseconds. 10000 microseconds resolution lRetVal = sl_SetSockOpt(g_sAppData.iSockID,SL_SOL_SOCKET,SL_SO_RCVTIMEO, (unsigned char*)&timeVal, sizeof(timeVal)); if(lRetVal < 0) { CLI_Write("Could not configure socket option (receive timeout).\n\r"); close(iSocketDesc); return lRetVal; } } else { CLI_Write("DNS lookup failed."); } // // Get current time from the SNTP server // CLI_Write("Fetching Time From SNTP Server\n\r"); lRetVal = GetSNTPTime(GMT_DIFF_TIME_HRS, GMT_DIFF_TIME_MINS); if(lRetVal < 0) { CLI_Write("Server Get Time failed.\n\r"); close(iSocketDesc); return lRetVal; } else { hourSet = dateTime.sl_tm_hour; // Set to current hour as we did get the time successfully CLI_Write("Server Get Time Successful\n\n\r"); } // // Close the socket // close(iSocketDesc); } return 0; }