Public Member Functions | |
CarrierApi () | |
Construct a Carrier API object More... | |
CarrierApi (string path) | |
Construct a Carrier API object and Load the data file from the provided path. More... | |
void | LoadDataFromFile (string path) |
Load the data file from the provided path. The data file is reloaded every time this method is called. More... | |
string | GetDataFileCopyright () |
Returns the data file copyright text. More... | |
string | GetDataFileCreationDate () |
Returns the data file creation date in ISO8601 format. More... | |
string | GetDataFileVersion () |
Returns the version of the data file. More... | |
Properties | GetProperties (string ipv4) |
Get the Carrier properties for a given IP address. More... | |
Properties | GetProperties (Dictionary< string, string > keyVals) |
Property | GetProperty (string ipv4, string propertyName) |
Property | GetProperty (Dictionary< string, string > keyVals, string propertyName) |
ReadOnlyCollection< PropertyName > | GetPropertyNames () |
A set of all the possible property names. The Set contains PropertyName objects that each have a string name and an associated data type. More... | |
string | GetIp (Dictionary< string, string > keyVals) |
Get the most suitable IP address from the given keyVals set of HTTP headers. This function checks the headers defined in HEADERS_TO_CHECK. More... | |
string | ExtractIp (string headerName, string headerValue) |
Static Public Member Functions | |
static bool | IsPublicIp (string ipStr) |
An IP address is considered public if it is not in any of the following ranges: More... | |
Static Public Attributes | |
static readonly string | VERSION = "1.1" |
static readonly string[] | HEADERS_TO_CHECK |
A list of HTTP headers to choose the original client IP address from. In addition to these the RemoteAddr (REMOTE_ADDR) is also used as a final fallback. More... | |
Protected Member Functions | |
void | DataLoaded () |
Check if the given propertyName is not null and exists in the data file More... | |
void | PropertyNameExists (string propertyName) |
Throws an exception if the given propertyName is null or not exists in the data file. Calls to this method must be sure that the data object is already loaded. If More... | |
The main class for the Carrier Identification API. Used to load the data file and to perform lookups using IPv4 addresses. For usage in a Web Container it is recommended to also include the CarrierApiWeb jar file as this facilitates passing a HttpRequest object.
Please note that it is advisable to load only a single instance of this class to avoid multiple loadings of the data file. This can be done in a Web Container by storing a reference to it in the ServletContext.
Example usage: string ip = "62.40.34.220"; CarrierApi carrierApi = new CarrierApi(); carrierApi.LoadDataFromFile("/path/to/sample.dat");
// get all properties Properties props = carrierApi.GetProperties(ip); // .... use the properties .... if (props.ContainsKey("networkOperator")) { Property property = props.Get("networkOperator"); String operatorName = property.AsString(); Response.Write("networkOperator: "+operatorName); }
// get a single property Property mccProp = carrierApi.GetProperty(ip, "mcc"); if (mccProp != null) { string mcc = mccProp.AsString(); Response.Write("MCC: "+mcc); }
Please see the code in the Examples directory for additional samples.
|
inline |
Construct a Carrier API object
|
inline |
Construct a Carrier API object and Load the data file from the provided path.
path | The path on disk to the data file. |
IOException | Thrown when there is a problem loading the file. |
DataFileException | thrown when there is a problem with the data file. |
|
inlineprotected |
Check if the given propertyName is not null and exists in the data file
|
inline |
Extracts and cleans an IP address from the headerValue. Some headers such as "X-Forwarded-For" can contain multiple IP addresses such as: clientIP, proxy1, proxy2...
This method splits up the headerValue and takes the most appropriate value as the IP.
headerName | The name of the HTTP Header |
headerValue | The value for the HTTP Header |
|
inline |
Returns the data file copyright text.
|
inline |
Returns the data file creation date in ISO8601 format.
|
inline |
Returns the version of the data file.
|
inline |
Get the most suitable IP address from the given keyVals set of HTTP headers. This function checks the headers defined in HEADERS_TO_CHECK.
keyVals | A hash of HTTP headers {header-name:value} |
|
inline |
Get the Carrier properties for a given IP address.
ipv4 | The IP address to find carrier properties for. |
|
inline |
Get the Carrier properties for a given Map of HTTP Headers. The API will choose the most appropriate IP to use.
The following headers are checked in order to find the most suitable IP address.
keyVals | A map of HTTP header name to HTTP header value. Both must be strings. |
|
inline |
Try and get a specific property for a given IP address.
Note: if multiple properties are needed for the same IP it is more efficient to call GetProperties() once than repeated calls to GetProperty().
ipv4 | The IP address to find carrier properties for. |
propertyName | The name of the property to return. |
InvalidPropertyNameException | Thrown if the property name does not exist. |
|
inline |
Try and get a specific property for a given Map of HTTP Headers. The API will choose the most appropriate IP to use.
Note: if multiple properties are needed for the same IP it is more efficient to call GetProperties() once than repeated calls to GetProperty().
keyVals | A map of HTTP header name to HTTP header value. Both must be strings. |
propertyName | The name of the property to return |
InvalidPropertyNameException | Thrown if the property name does not exist. |
|
inline |
A set of all the possible property names. The Set contains PropertyName objects that each have a string name and an associated data type.
|
inlinestatic |
An IP address is considered public if it is not in any of the following ranges:
1) any local address IP: 0
2) a local loopback address range: 127/8
3) a site local address i.e. IP is in any of the ranges: range: 10/8 range: 172.16/12 range: 192.168/16
4) a link local address range: 169.254/16
ip | The IP address to check |
|
inline |
Load the data file from the provided path. The data file is reloaded every time this method is called.
path | The path on disk to the data file. |
IOException | Thrown when there is a problem loading the file. |
DataFileException | thrown when there is a problem with the data file. |
|
inlineprotected |
Throws an exception if the given propertyName is null or not exists in the data file. Calls to this method must be sure that the data object is already loaded. If
propertyName | The name of the property to check. |
InvalidPropertyNameException |
|
static |
A list of HTTP headers to choose the original client IP address from. In addition to these the RemoteAddr (REMOTE_ADDR) is also used as a final fallback.