17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * Copyright (C) 1993-2001 by Darren Reed.
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * See the IPFILTER.LICENCE file for details on licencing.
57c478bd9Sstevel@tonic-gate *
6*ab25eeb5Syz155240 * $Id: genmask.c,v 1.7 2003/11/11 13:40:15 darrenr Exp $
77c478bd9Sstevel@tonic-gate */
87c478bd9Sstevel@tonic-gate
97c478bd9Sstevel@tonic-gate #include "ipf.h"
107c478bd9Sstevel@tonic-gate
117c478bd9Sstevel@tonic-gate
genmask(msk,mskp)127c478bd9Sstevel@tonic-gate int genmask(msk, mskp)
137c478bd9Sstevel@tonic-gate char *msk;
147c478bd9Sstevel@tonic-gate u_32_t *mskp;
157c478bd9Sstevel@tonic-gate {
167c478bd9Sstevel@tonic-gate char *endptr = 0L;
177c478bd9Sstevel@tonic-gate int bits;
187c478bd9Sstevel@tonic-gate
197c478bd9Sstevel@tonic-gate if (strchr(msk, '.') || strchr(msk, 'x') || strchr(msk, ':')) {
207c478bd9Sstevel@tonic-gate /* possibly of the form xxx.xxx.xxx.xxx
217c478bd9Sstevel@tonic-gate * or 0xYYYYYYYY */
227c478bd9Sstevel@tonic-gate #ifdef USE_INET6
237c478bd9Sstevel@tonic-gate if (use_inet6) {
247c478bd9Sstevel@tonic-gate if (inet_pton(AF_INET6, msk, mskp) != 1)
257c478bd9Sstevel@tonic-gate return -1;
267c478bd9Sstevel@tonic-gate } else
277c478bd9Sstevel@tonic-gate #endif
287c478bd9Sstevel@tonic-gate if (inet_aton(msk, (struct in_addr *)mskp) == 0)
297c478bd9Sstevel@tonic-gate return -1;
307c478bd9Sstevel@tonic-gate } else {
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate * set x most significant bits
337c478bd9Sstevel@tonic-gate */
347c478bd9Sstevel@tonic-gate bits = (int)strtol(msk, &endptr, 0);
357c478bd9Sstevel@tonic-gate #ifdef USE_INET6
367c478bd9Sstevel@tonic-gate if ((*endptr != '\0') ||
377c478bd9Sstevel@tonic-gate ((bits > 32) && !use_inet6) || (bits < 0) ||
387c478bd9Sstevel@tonic-gate ((bits > 128) && use_inet6))
397c478bd9Sstevel@tonic-gate #else
407c478bd9Sstevel@tonic-gate if (*endptr != '\0' || bits > 32 || bits < 0)
417c478bd9Sstevel@tonic-gate #endif
427c478bd9Sstevel@tonic-gate return -1;
437c478bd9Sstevel@tonic-gate #ifdef USE_INET6
447c478bd9Sstevel@tonic-gate if (use_inet6)
457c478bd9Sstevel@tonic-gate fill6bits(bits, mskp);
467c478bd9Sstevel@tonic-gate else
477c478bd9Sstevel@tonic-gate #endif
487c478bd9Sstevel@tonic-gate if (bits == 0)
497c478bd9Sstevel@tonic-gate *mskp = 0;
507c478bd9Sstevel@tonic-gate else
517c478bd9Sstevel@tonic-gate *mskp = htonl(0xffffffff << (32 - bits));
527c478bd9Sstevel@tonic-gate }
537c478bd9Sstevel@tonic-gate return 0;
547c478bd9Sstevel@tonic-gate }
55