All pastes #905595 Raw Edit

Miscellany

public c v1 · immutable
·published 2008-02-15 22:05 UTC
rendered paste body
// converts an char containing a full or partial ip address and optional// prefix length into the ip as an int and the corresponding prefix.// if the given ip string is a range, it will be returned in cidr notation// otherwise it will be returned as an ip addressvoid G_admin_parse_ip( int *ipnum, int *prefixlen, char *ip ){	char tempIp[18];	char *tptr;	char newIp[18] = "255.255.255.255/32";	int ipChunk[4];	int i = 0;	int j = 0;	Q_strncpyz( tempIp, ip, sizeof( tempIp ) );	if (strlen(tempIp) == 0) // blank ip, set it to 0.0.0.0/32	{		ipChunk[0] = ipChunk[1] = ipChunk[2] = ipChunk[3] = (int) 0;		*prefixlen = (int) 32;	}	else	{		tptr = strtok(tempIp, ".");		for (i=0;i<4;i++)		{			if (tptr == NULL)			{				for (j=i;j<4;j++)				{					ipChunk[j] = 0;					}				*prefixlen = (int) i*8;				break;			}			ipChunk[i] = atoi(tptr);			if (!(i==3))			{				tptr = strtok(NULL, ".");			}		}		tptr = strtok(NULL, "/");		if (tptr == NULL || *prefixlen == 0)		{			*prefixlen = (int) 32;		}		else		{			*prefixlen = atoi(tptr);		}	}	if ( (ipChunk[0]>=0 && ipChunk[0]<=255) 		&& (ipChunk[1]>=0 && ipChunk[1]<=255)		&& (ipChunk[2]>=0 && ipChunk[2]<=255)		&& (ipChunk[3]>=0 && ipChunk[3]<=255) )	{		*ipnum = (int) (ipChunk[0]<<24) + (ipChunk[1]<<16) + (ipChunk[2]<<8) + (ipChunk[3]);	}	else	{		*ipnum = (int) 0;		ipChunk[0] = (int) 0;		ipChunk[1] = (int) 0;		ipChunk[2] = (int) 0;		ipChunk[3] = (int) 0;	}	if ( *prefixlen > 32 || *prefixlen < 0 )	{		*prefixlen = (int) 32;	}	if (!(*prefixlen == 32))	{		Com_sprintf( newIp, sizeof(newIp), "%d.%d.%d.%d/%d", ipChunk[0], ipChunk[1], ipChunk[2], ipChunk[3], *prefixlen );	}	else	{		Com_sprintf( newIp, sizeof(newIp), "%d.%d.%d.%d", ipChunk[0], ipChunk[1], ipChunk[2], ipChunk[3] );	}	ip = newIp;	return;}