/* * IP address helper function library * * By Ravenous Bugblatter Beast * ravenousbugblatterbeast@hotmail.com * http://www.ravenousbugblatterbeast.pwp.blueyonder.co.uk * * This include file contains a function * to convert IP addresses supplied by * adminmod into 32-Bit integers so they * can be manipulated using bit-wise arithmatic * e.g. for subnet masking. * * It also converts the numeric IP address * back into dotted string representation. * * Note: The 1st octet is stored in the upper * octet of the integer and the 4th octed in the lower. */ #if defined _ip_included #endinput #endif #define _ip_included #include <admin> #include <core> #include <string> #define SHIFT8 256 #define SHIFT16 65536 #define SHIFT24 16777216 /* Converts an string representation of an IP address * in Data into a 32-bit numeric representation in IP * * Ignores any optional port on the end of the IP address * e.g. In 1.2.3.4:27015 the :27015 is ignored. * * Returns 1 if conversion successful, or 0 if Data[] * did not contain a valid IP address. * * Note: If you use this with the HLIP argument in plugin_connect * you must call convert_string on it first - e.g. * * public plugin_connect(HLUserName, HLIP, UserIndex) { * new Data[MAX_DATA_LENGTH]; * convert_string(HLIP,Data,MAX_DATA_LENGTH); * new IP = strtoip(Data,IP); * */ stock HLIPtoip(HLIP, &IP) { new strIP[25]; convert_string(HLIP,strIP,25); return strtoip(strIP,IP); } stock strtoip(Data[], &IP) { new sepIP[4][10]; new numIP[4]; new pos=index(Data,':'); if (pos>0) { Data[pos] = NULL_CHAR; } new c=strsplit(Data,".",sepIP[0],10,sepIP[1],10,sepIP[2],10,sepIP[3],10); if (c!=4) { return 0; } new i; for (i=0;i<4;i++) { numIP[i]=strtonum(sepIP[i]); if ((numIP[i]<0) || (numIP[i]>255)) { return 0; } } IP= numIP[0] * SHIFT24 + numIP[1] * SHIFT16 + numIP[2] * SHIFT8 + numIP[3]; return 1; } /* Converts the numeric representation in IP to a string * representation in Data[]. You must pass an array with * at least 16 characters of space */ stock iptostr(IP,Data[]) { new strIP[4][10]; /* Workaround for signed arithmatic - don't want negative IP addresses! */ new hi=(IP & 0xFF000000) / SHIFT24; if (hi <0) { hi += 256; } numtostr(hi,strIP[0]); numtostr((IP & 0xFF0000) / SHIFT16,strIP[1]); numtostr((IP & 0xFF00) / SHIFT8,strIP[2]); numtostr((IP & 0xFF),strIP[3]); snprintf(Data,16,"%s.%s.%s.%s",strIP[0],strIP[1],strIP[2],strIP[3]); return 1; }