kalasag.org

kalasag.org

Git

This blob has been accessed 609 times via Git panel.

  1. #include "kalasag.h"
  2. #include "kalasag_io.h"
  3. #include "kalasag_util.h"
  4.  
  5. /* A replacement for strncpy that covers mistakes a little better */
  6. char *SafeStrncpy(char *dest, const char *src, size_t size)
  7. {
  8.     if (!dest) {
  9.         dest = NULL;
  10.         return (NULL);
  11.     } else if (size < 1) {
  12.         dest = NULL;
  13.         return (NULL);
  14.     }
  15.  
  16.     /* Null terminate string. Why the hell strncpy doesn't do this */
  17.     /* for you is mystery to me. God I hate C. */
  18.     memset(dest, '\0', size);
  19.     strncpy(dest, src, size - 1);
  20.  
  21.     return (dest);
  22. }
  23.  
  24.  
  25. /************************************************************************/
  26. /* Generic safety function to process an IP address and remove anything */
  27. /* that is:                                                             */
  28. /* 1) Not a number.                                                     */
  29. /* 2) Not a period.                                                     */
  30. /* 3) Greater than IPMAXBUF (15)                                        */
  31. /************************************************************************/
  32. char *CleanIpAddr(char *cleanAddr, const char *dirtyAddr)
  33. {
  34.     int count = 0, maxdot = 0, maxoctet = 0;
  35.  
  36. #ifdef DEBUG
  37.     Log("debug: cleanAddr: Cleaning Ip address: %s", dirtyAddr);
  38. #endif
  39.  
  40.     memset(cleanAddr, '\0', IPMAXBUF);
  41.     /* dirtyAddr must be valid */
  42.     if (dirtyAddr == NULL)
  43.         return (cleanAddr);
  44.  
  45.     for (count = 0; count < IPMAXBUF - 1; count++) {
  46.         if (isdigit(dirtyAddr[count])) {
  47.             if (++maxoctet > 3) {
  48.                 cleanAddr[count] = '\0';
  49.                 break;
  50.             }
  51.             cleanAddr[count] = dirtyAddr[count];
  52.         } else if (dirtyAddr[count] == '.') {
  53.             if (++maxdot > 3) {
  54.                 cleanAddr[count] = '\0';
  55.                 break;
  56.             }
  57.             maxoctet = 0;
  58.             cleanAddr[count] = dirtyAddr[count];
  59.         } else {
  60.             cleanAddr[count] = '\0';
  61.             break;
  62.         }
  63.     }
  64.  
  65. #ifdef DEBUG
  66.     Log("debug: cleanAddr: Cleaned IpAddress: %s Dirty IpAddress: %s",
  67.         cleanAddr, dirtyAddr);
  68. #endif
  69.  
  70.     return (cleanAddr);
  71. }
  72.  
  73.  
  74. /************************************************************************/
  75. /* Generic safety function to process an unresolved address and remove  */
  76. /* anything that is:                                                    */
  77. /* 1) Not a number.                                                     */
  78. /* 2) Not a period.                                                     */
  79. /* 3) Greater than DNSMAXBUF (255)                                      */
  80. /* 4) Not a legal DNS character (a-z, A-Z, 0-9, - )                     */
  81. /*                                                                      */
  82. /* XXX THIS FUNCTION IS NOT COMPLETE                                    */
  83. /************************************************************************/
  84. int CleanAndResolve(char *resolvedHost, const char *unresolvedHost)
  85. {
  86.     struct hostent *hostPtr = NULL;
  87.     struct in_addr addr;
  88.  
  89. #ifdef DEBUG
  90.     Log("debug: CleanAndResolv: Resolving address: %s", unresolvedHost);
  91. #endif
  92.  
  93.     memset(resolvedHost, '\0', DNSMAXBUF);
  94.     /* unresolvedHost must be valid */
  95.     if (unresolvedHost == NULL)
  96.         return (ERROR);
  97.  
  98.     /* Not a valid address */
  99.     if ((inet_aton(unresolvedHost, &addr)) == 0)
  100.         return (ERROR);
  101.  
  102.     hostPtr =
  103.         gethostbyaddr((char *) &addr.s_addr, sizeof(addr.s_addr), AF_INET);
  104.     if (hostPtr != NULL)
  105.         snprintf(resolvedHost, DNSMAXBUF, "%s", hostPtr->h_name);
  106.     else
  107.         snprintf(resolvedHost, DNSMAXBUF, "%s", unresolvedHost);
  108.  
  109. #ifdef DEBUG
  110.     Log("debug: CleanAndResolve: Cleaned Resolved: %s Dirty Unresolved: %s", resolvedHost, unresolvedHost);
  111. #endif
  112.  
  113.     return (TRUE);
  114. }
  115.  
filedropkalasag.git-2834a11.tar.bz2 new
20.84 KB
225 downloads
filedropkalasag.git-2834a11.zip
25.72 KB
94 downloads
filedropkalasag.git-afd7b31.tar.bz2
20.84 KB
170 downloads
filedropkalasag.git-afd7b31.zip
25.71 KB
55 downloads
filedropkalasag.git-97c89e1.tar.bz2
20.82 KB
253 downloads
filedropkalasag.git-97c89e1.zip
25.68 KB
151 downloads
filedropkalasag.git-1141d13.tar.bz2
20.65 KB
793 downloads
filedropkalasag.git-1141d13.zip
25.37 KB
146 downloads
filedropkalasag.git-ee3c17b.tar.bz2
20.65 KB
765 downloads
filedropkalasag.git-ee3c17b.zip
25.34 KB
139 downloads
filedropkalasag.git-4032c54.tar.bz2
20.63 KB
255 downloads
filedropkalasag.git-4032c54.zip
25.13 KB
158 downloads
filedropkalasag.git-e51a2a6.tar.bz2
20.65 KB
254 downloads
filedropkalasag.git-e51a2a6.zip
25.13 KB
132 downloads
filedropkalasag.git-599c93a.tar.bz2
20.63 KB
246 downloads
filedropkalasag.git-599c93a.zip
25.11 KB
1,449 downloads
filedropkalasag.git-acdc640.tar.bz2
20.63 KB
233 downloads
filedropkalasag.git-acdc640.zip
25.10 KB
141 downloads