This blob has been accessed 1,164 times via Git panel.
- #include "kalasag.h"
- #include "kalasag_io.h"
- #include "kalasag_util.h"
- /* A replacement for strncpy that covers mistakes a little better */
- char *SafeStrncpy(char *dest, const char *src, size_t size)
- {
- if (!dest) {
- dest = NULL;
- return (NULL);
- } else if (size < 1) {
- dest = NULL;
- return (NULL);
- }
- /* Null terminate string. Why the hell strncpy doesn't do this */
- /* for you is mystery to me. God I hate C. */
- return (dest);
- }
- /************************************************************************/
- /* Generic safety function to process an IP address and remove anything */
- /* that is: */
- /* 1) Not a number. */
- /* 2) Not a period. */
- /* 3) Greater than IPMAXBUF (15) */
- /************************************************************************/
- char *CleanIpAddr(char *cleanAddr, const char *dirtyAddr)
- {
- int count = 0, maxdot = 0, maxoctet = 0;
- #ifdef DEBUG
- Log("debug: cleanAddr: Cleaning Ip address: %s", dirtyAddr);
- #endif
- /* dirtyAddr must be valid */
- if (dirtyAddr == NULL)
- return (cleanAddr);
- for (count = 0; count < IPMAXBUF - 1; count++) {
- if (++maxoctet > 3) {
- cleanAddr[count] = '\0';
- break;
- }
- cleanAddr[count] = dirtyAddr[count];
- } else if (dirtyAddr[count] == '.') {
- if (++maxdot > 3) {
- cleanAddr[count] = '\0';
- break;
- }
- maxoctet = 0;
- cleanAddr[count] = dirtyAddr[count];
- } else {
- cleanAddr[count] = '\0';
- break;
- }
- }
- #ifdef DEBUG
- Log("debug: cleanAddr: Cleaned IpAddress: %s Dirty IpAddress: %s",
- cleanAddr, dirtyAddr);
- #endif
- return (cleanAddr);
- }
- /************************************************************************/
- /* Generic safety function to process an unresolved address and remove */
- /* anything that is: */
- /* 1) Not a number. */
- /* 2) Not a period. */
- /* 3) Greater than DNSMAXBUF (255) */
- /* 4) Not a legal DNS character (a-z, A-Z, 0-9, - ) */
- /* */
- /* XXX THIS FUNCTION IS NOT COMPLETE */
- /************************************************************************/
- int CleanAndResolve(char *resolvedHost, const char *unresolvedHost)
- {
- struct hostent *hostPtr = NULL;
- struct in_addr addr;
- #ifdef DEBUG
- Log("debug: CleanAndResolv: Resolving address: %s", unresolvedHost);
- #endif
- /* unresolvedHost must be valid */
- if (unresolvedHost == NULL)
- return (ERROR);
- /* Not a valid address */
- if ((inet_aton(unresolvedHost, &addr)) == 0)
- return (ERROR);
- hostPtr =
- gethostbyaddr((char *) &addr.s_addr, sizeof(addr.s_addr), AF_INET);
- if (hostPtr != NULL)
- else
- #ifdef DEBUG
- Log("debug: CleanAndResolve: Cleaned Resolved: %s Dirty Unresolved: %s", resolvedHost, unresolvedHost);
- #endif
- return (TRUE);
- }