/ src / core / version.cc
version.cc
  1  /*
  2   * version.cc
  3   *
  4   * The sole purpose of this module is to report package version information:
  5   * keyword expansion should be allowed for the 'URL' tag (svn ps svn:keywords URL)
  6   * Reported version is computed using this file's location in the source archive;
  7   * development versions return 'development' whereas released versions return
  8   * version numbers like A.01.04
  9   *
 10   */
 11  #include "version.h"
 12  #include "config.h"
 13  #include <stdlib.h>
 14  #include <string.h>
 15  #include <ctype.h>
 16  #ifdef REMOTE_VERSION_CHECK
 17  #include <netinet/in.h>
 18  #include <arpa/nameser.h>
 19  #include <resolv.h>
 20  #include <sys/types.h>
 21  
 22  #ifndef PACKETSZ
 23  #define PACKETSZ 512
 24  #endif
 25  #endif
 26  
 27  const char *getpackageversion()
 28  {
 29    if (VERSION)
 30      return VERSION;
 31  
 32    return "unknown";
 33  }
 34  
 35  #ifdef REMOTE_VERSION_CHECK
 36  static char *txtquery(const char *name, const char *domain, unsigned int *ttl)
 37  {
 38    unsigned char answer[PACKETSZ], *pt;
 39    char host[128], *txt;
 40    int len, exp, cttl, size, txtlen, type;
 41  
 42    if(res_init() < 0)
 43      return NULL;
 44  
 45    memset(answer, 0, PACKETSZ);
 46    if((len = res_querydomain(name, domain, C_IN, T_TXT, answer, PACKETSZ)) < 0)
 47      return NULL;
 48  
 49    pt = answer + sizeof(HEADER);
 50  
 51    if((exp = dn_expand(answer, answer + len, pt, host, sizeof(host))) < 0)
 52      return NULL;
 53  
 54    pt += exp;
 55  
 56    GETSHORT(type, pt);
 57    if(type != T_TXT)
 58      return NULL;
 59  
 60    pt += INT16SZ; /* class */
 61  
 62    if((exp = dn_expand(answer, answer + len, pt, host, sizeof(host))) < 0)
 63      return NULL;
 64  
 65    pt += exp;
 66    GETSHORT(type, pt);
 67    if(type != T_TXT)
 68      return NULL;
 69  
 70    pt += INT16SZ; /* class */
 71    GETLONG(cttl, pt);
 72    if(ttl)
 73      *ttl = cttl;
 74    GETSHORT(size, pt);
 75    txtlen = *pt;
 76  
 77    if(txtlen >= size || !txtlen)
 78      return NULL;
 79  
 80    if(!(txt = (char*)malloc(txtlen + 1)))
 81      return NULL;
 82  
 83    pt++;
 84    strncpy(txt, (char*)pt, txtlen);
 85    txt[txtlen] = 0;
 86  
 87    return txt;
 88  }
 89  #endif
 90  
 91  const char * checkupdates()
 92  {
 93  #ifdef REMOTE_VERSION_CHECK
 94    static char *latest = NULL;
 95  
 96    if(!latest)
 97      latest = txtquery(PACKAGE, "ezix.org", NULL);
 98  
 99    return latest;
100  #else
101    return NULL;
102  #endif
103  }