getdnsname.c

Go to the documentation of this file.
00001 
00033 #include <stdlib.h>
00034 #include <stdio.h>
00035 #include <string.h>
00036 
00037 #ifdef _MSC_VER
00038 #include <winsock2.h>
00039 #else
00040 #include <netdb.h>
00041 #include <unistd.h>
00042 #endif
00043 
00044 #include "getdnsname.h"
00045 
00046 char* getdnsname() {
00047 
00048   char *hostname = NULL;
00049   char host[MAX_PATH_LENGTH];
00050   struct hostent *hptr;
00051     
00052   memset(host, 0, MAX_PATH_LENGTH);
00053   gethostname(host, MAX_PATH_LENGTH);
00054   
00055   if((hptr = gethostbyname(host)) == NULL) {
00056     
00057     if(!(hostname = (char*)calloc((strlen(host) + 1), sizeof(char)))) {
00058       printf("Could not alloc memory for hostname!\n");
00059       fflush(stdout);
00060       return NULL;
00061     }
00062     
00063     memcpy(hostname, host, strlen(host));
00064     return hostname;
00065     
00066   }
00067   else {
00068     memset(host, 0, MAX_PATH_LENGTH);
00069     memcpy(host, hptr->h_name, strlen(hptr->h_name));
00070   }  
00071   
00072   if(!(hostname = (char*)calloc((strlen(host) + 1), sizeof(char)))) {
00073     printf("Could not alloc memory for hostname!\n");
00074     fflush(stdout);
00075     return NULL;
00076   }
00077   
00078   memcpy(hostname, host, strlen(host));
00079   
00080   return hostname;
00081 }
00082 

Generated on Fri Mar 9 19:59:53 2007 for MAD-FCL by  doxygen 1.5.0