"Fossies" - the Fresh Open Source Software Archive 
Member "srg-1.3.6/src/ip2user.cc" (5 Aug 2009, 2879 Bytes) of package /linux/privat/old/srg-1.3.6.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.
1 /*
2 SRG - Squid Report Generator
3 IP address to username conversion
4
5 Copyright 2005 University of Waikato
6
7 This file is part of SRG.
8
9 SRG is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 SRG is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with SRG; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23 */
24
25 #include "srg.h"
26 #include "prototypes.h"
27
28 /* Global Data Structures */
29 list<ip2user_line> ip2users;
30
31 /* Initialise the IP address to user mappings */
32 void init_ip2user(void) {
33
34 /* Exit if no ip2user file specified */
35 if (!srg.ip2user)
36 return;
37
38 /* Read the file */
39 Line *iLine = new Line(srg.ip2user);
40 if (iLine->getError() || iLine->eof()) {
41 fprintf(stderr, "%s: Could not open the ip to user "
42 "conversion file: %s\n", progname, srg.ip2user);
43 exit(1);
44 }
45
46 /* Process each line of the file */
47 while(!iLine->eof()) {
48 ip2user_line thisline;
49 char *ipLine = iLine->getline();
50
51 /* Check for comment lines */
52 if (ipLine[0] == '\0' || ipLine[0] == '#')
53 continue;
54
55 /* Parse the IP Address and User */
56 char netmask[1024];
57 char address[1024];
58 char username[1024];
59 if (sscanf(ipLine, "%1023s %1023s %1023s", address,
60 netmask, username) != 3) {
61 fprintf(stderr, "%s: Error parsing ip to "
62 "user file: %s\n", progname, srg.ip2user);
63 exit(1);
64 }
65 thisline.username = strdup(username);
66 if (inet_aton(address, &thisline.ipaddress)==0) {
67 fprintf(stderr, "Invalid IP Address in ip to "
68 "user file: %s\n", address);
69 exit(1);
70 }
71 if (inet_aton(netmask, &thisline.netmask)==0) {
72 fprintf(stderr, "Invalid netmask in ip to "
73 "user file: %s\n", netmask);
74 exit(1);
75 }
76 /* Add this entry to the list */
77 ip2users.push_back(thisline);
78 }
79
80 /* Free memory */
81 delete iLine;
82
83 }
84
85 /* Looks up the specified ip address in the ip address to username mapping
86 * and returns the associated username if it exists via the username
87 * paramter. Returns true if a username was found, false otherwise */
88 bool ip2username(const in_addr ipaddress, char **username) {
89
90
91 list<ip2user_line>::const_iterator iter;
92 for (iter=ip2users.begin(); iter != ip2users.end(); iter++) {
93 if ((ipaddress.s_addr&(*iter).netmask.s_addr)==
94 (*iter).ipaddress.s_addr) {
95 *username = strdup((*iter).username);
96 return true;
97 }
98 }
99
100 /* otherwise return the original IP address. */
101 *username = strdup(inet_ntoa(ipaddress));
102 return false;
103
104 }