Squid Web Cache master
Loading...
Searching...
No Matches
radius-util.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9// 2008-05-14: rename to radius-util.* to avoid name clashes with squid util.*
10/*
11 *
12 * RADIUS
13 * Remote Authentication Dial In User Service
14 *
15 *
16 * Livingston Enterprises, Inc.
17 * 6920 Koll Center Parkway
18 * Pleasanton, CA 94566
19 *
20 * Copyright 1992 Livingston Enterprises, Inc.
21 * Copyright 1997 Cistron Internet Services B.V.
22 *
23 * Permission to use, copy, modify, and distribute this software for any
24 * purpose and without fee is hereby granted, provided that this
25 * copyright and permission notice appear on all copies and supporting
26 * documentation, the name of Livingston Enterprises, Inc. not be used
27 * in advertising or publicity pertaining to distribution of the
28 * program without specific prior permission, and notice be given
29 * in supporting documentation that copying and distribution is by
30 * permission of Livingston Enterprises, Inc.
31 *
32 * Livingston Enterprises, Inc. makes no representations about
33 * the suitability of this software for any purpose. It is
34 * provided "as is" without express or implied warranty.
35 *
36 */
37
38/*
39 * util.c Miscellaneous generic functions.
40 *
41 */
42
44 "@(#)util.c 1.5 Copyright 1992 Livingston Enterprises Inc\n"
45 " 2.1 Copyright 1997 Cistron Internet Services B.V.";
46
47#include "squid.h"
49#include "compat/netdb.h"
50#include "compat/socket.h"
51#include "md5.h"
52
53#include <cctype>
54#include <csignal>
55#include <ctime>
56#if HAVE_NETINET_IN_H
57#include <netinet/in.h>
58#endif
59#if HAVE_PWD_H
60#include <pwd.h>
61#endif
62
63/*
64 * Check for valid IP address in standard dot notation.
65 */
66static int good_ipaddr(char *addr)
67{
68 int dot_count;
69 int digit_count;
70
71 dot_count = 0;
72 digit_count = 0;
73 while (*addr != '\0' && *addr != ' ') {
74 if (*addr == '.') {
75 ++dot_count;
76 digit_count = 0;
77 } else if (!isdigit(*addr)) {
78 dot_count = 5;
79 } else {
80 ++digit_count;
81 if (digit_count > 3) {
82 dot_count = 5;
83 }
84 }
85 ++addr;
86 }
87 if (dot_count != 3) {
88 return(-1);
89 } else {
90 return(0);
91 }
92}
93
94/*
95 * Return an IP address in host long notation from
96 * one supplied in standard dot notation.
97 */
98static uint32_t ipstr2long(char *ip_str)
99{
100 char buf[6];
101 char *ptr;
102 int i;
103 int count;
104 uint32_t ipaddr;
105 int cur_byte;
106
107 ipaddr = (uint32_t)0;
108 for (i = 0; i < 4; ++i) {
109 ptr = buf;
110 count = 0;
111 *ptr = '\0';
112 while (*ip_str != '.' && *ip_str != '\0' && count < 4) {
113 if (!isdigit(*ip_str)) {
114 return((uint32_t)0);
115 }
116 *ptr = *ip_str;
117 ++ptr;
118 ++ip_str;
119 ++count;
120 }
121 if (count >= 4 || count == 0) {
122 return((uint32_t)0);
123 }
124 *ptr = '\0';
125 cur_byte = atoi(buf);
126 if (cur_byte < 0 || cur_byte > 255) {
127 return((uint32_t)0);
128 }
129 ++ip_str;
130 ipaddr = ipaddr << 8 | (uint32_t)cur_byte;
131 }
132 return(ipaddr);
133}
134
135/*
136 * Return an IP address in host long notation from a host
137 * name or address in dot notation.
138 */
139uint32_t get_ipaddr(char *host)
140{
141 struct hostent *hp;
142
143 if (good_ipaddr(host) == 0) {
144 return(ipstr2long(host));
145 } else if (!(hp = xgethostbyname(host))) {
146 return((uint32_t)0);
147 }
148 return(ntohl(*(uint32_t *)hp->h_addr));
149}
150
struct hostent * xgethostbyname(const char *name)
POSIX gethostbyname(3) equivalent.
Definition netdb.h:25
static int good_ipaddr(char *addr)
char util_sccsid[]
uint32_t get_ipaddr(char *host)
static uint32_t ipstr2long(char *ip_str)