"Fossies" - the Fresh Open Source Software Archive 
Member "portsentry-2.0b1/portsentry_util.c" (8 Apr 2002, 5232 Bytes) of package /linux/privat/old/portsentry-2.0b1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "portsentry_util.c" see the
Fossies "Dox" file reference documentation.
1 /************************************************************************/
2 /* */
3 /* Psionic PortSentry */
4 /* */
5 /* Created: 10-12-1997 */
6 /* Modified: 03-26-2002 */
7 /* */
8 /* Send all changes/modifications/bugfixes to sentrysupport@psionic.com */
9 /* */
10 /* */
11 /* This software is Copyright(c) 1997-2002 Psionic Technologies, Inc. */
12 /* */
13 /* Disclaimer: */
14 /* */
15 /* All software distributed by Psionic Technologies is distributed */
16 /* AS IS and carries NO WARRANTY or GUARANTEE OF ANY KIND. End users of */
17 /* the software acknowledge that they will not hold Psionic Software */
18 /* liable for failure or non-function of the software product. YOU ARE */
19 /* USING THIS PRODUCT AT YOUR OWN RISK. */
20 /* */
21 /* Licensing restrictions apply. Commercial re-sell is prohibited under */
22 /* certain conditions. See the license that came with this package or */
23 /* visit http://www.psionic.com for more information. */
24 /* */
25 /* $Id: portsentry_util.c,v 1.14 2002/04/08 16:48:54 crowland Exp crowland $ */
26 /************************************************************************/
27
28 #include "portsentry.h"
29 #include "portsentry_io.h"
30 #include "portsentry_util.h"
31
32 /* A replacement for strncpy that covers mistakes a little better */
33 char *
34 SafeStrncpy (char *dest, const char *src, size_t size)
35 {
36 if (!dest)
37 {
38 dest = NULL;
39 return (NULL);
40 }
41 else if (size < 1)
42 {
43 dest = NULL;
44 return (NULL);
45 }
46
47 /* Null terminate string. */
48 memset (dest, '\0', size);
49 strncpy (dest, src, size - 1);
50
51 return (dest);
52 }
53
54
55 /************************************************************************/
56 /* Generic safety function to process an IP address and remove anything */
57 /* that is: */
58 /* 1) Not a number. */
59 /* 2) Not a period. */
60 /* 3) Greater than IPMAXBUF (15) */
61 /************************************************************************/
62 char *
63 CleanIpAddr (char *cleanAddr, const char *dirtyAddr)
64 {
65 int count = 0, maxdot = 0, maxoctet = 0;
66
67 #ifdef DEBUG
68 Log("debug: cleanAddr: Cleaning Ip address: %s", dirtyAddr);
69 #endif
70
71 memset (cleanAddr, '\0', IPMAXBUF);
72 /* dirtyAddr must be valid */
73 if(dirtyAddr == NULL)
74 return(cleanAddr);
75
76 for (count = 0; count < IPMAXBUF - 1; count++)
77 {
78 if (isdigit (dirtyAddr[count]))
79 {
80 if (++maxoctet > 3)
81 {
82 cleanAddr[count] = '\0';
83 break;
84 }
85 cleanAddr[count] = dirtyAddr[count];
86 }
87 else if (dirtyAddr[count] == '.')
88 {
89 if (++maxdot > 3)
90 {
91 cleanAddr[count] = '\0';
92 break;
93 }
94 maxoctet = 0;
95 cleanAddr[count] = dirtyAddr[count];
96 }
97 else
98 {
99 cleanAddr[count] = '\0';
100 break;
101 }
102 }
103
104 #ifdef DEBUG
105 Log("debug: cleanAddr: Cleaned IpAddress: %s Dirty IpAddress: %s", cleanAddr, dirtyAddr);
106 #endif
107
108 return (cleanAddr);
109 }
110
111
112 /************************************************************************/
113 /* Generic safety function to process an unresolved address and remove */
114 /* anything that is: */
115 /* 1) Not a number. */
116 /* 2) Not a period. */
117 /* 3) Greater than DNSMAXBUF (255) */
118 /* 4) Not a legal DNS character (a-z, A-Z, 0-9, - ) */
119 /* */
120 /* XXX THIS FUNCTION IS NOT COMPLETE */
121 /************************************************************************/
122 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost)
123 {
124 struct hostent *hostPtr = NULL;
125 struct in_addr addr;
126
127 #ifdef DEBUG
128 Log("debug: CleanAndResolv: Resolving address: %s", unresolvedHost);
129 #endif
130
131 memset (resolvedHost, '\0', DNSMAXBUF);
132 /* unresolvedHost must be valid */
133 if(unresolvedHost == NULL)
134 return(ERROR);
135
136 /* Not a valid address */
137 if ((inet_aton(unresolvedHost, &addr)) == 0)
138 return(ERROR);
139
140 hostPtr = gethostbyaddr ((char *) &addr.s_addr, sizeof (addr.s_addr), AF_INET);
141 if (hostPtr != NULL)
142 snprintf (resolvedHost, DNSMAXBUF, "%s", hostPtr->h_name);
143 else
144 snprintf (resolvedHost, DNSMAXBUF, "%s", unresolvedHost);
145
146 #ifdef DEBUG
147 Log("debug: CleanAndResolve: Cleaned Resolved: %s Dirty Unresolved: %s", resolvedHost, unresolvedHost);
148 #endif
149
150 return (TRUE);
151 }
152
153