Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Platform specific default values for address families and DLT_RAW #78

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions pcap4j-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule</artifactId>
<version>1.6.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-classloading-xstream</artifactId>
<version>1.6.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.pcap4j</groupId>
<artifactId>pcap4j-packetfactory-static</artifactId>
Expand Down
276 changes: 162 additions & 114 deletions pcap4j-core/src/main/java/org/pcap4j/Pcap4jPropertiesLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package org.pcap4j;

import com.sun.jna.Platform;
import org.pcap4j.util.PropertiesLoader;

/**
Expand All @@ -15,119 +16,166 @@
*/
public final class Pcap4jPropertiesLoader {

private static final String KEY_PREFIX
= Pcap4jPropertiesLoader.class.getPackage().getName();

/**
*
*/
public static final String PCAP4J_PROPERTIES_PATH_KEY
= KEY_PREFIX + ".properties";

/**
*
*/
public static final String AF_INET_KEY = KEY_PREFIX + ".af.inet";

/**
*
*/
public static final String AF_INET6_KEY = KEY_PREFIX + ".af.inet6";

/**
*
*/
public static final String AF_PACKET_KEY = KEY_PREFIX + ".af.packet";

/**
*
*/
public static final String AF_LINK_KEY = KEY_PREFIX + ".af.link";

/**
*
*/
public static final String DLT_RAW_KEY = KEY_PREFIX + ".dlt.raw";

private static final Pcap4jPropertiesLoader INSTANCE = new Pcap4jPropertiesLoader();

private PropertiesLoader loader
= new PropertiesLoader(
System.getProperty(
PCAP4J_PROPERTIES_PATH_KEY,
KEY_PREFIX.replace('.', '/') + "/pcap4j.properties"
),
true,
true
);

private Pcap4jPropertiesLoader() {}

/**
*
* @return the singleton instance of Pcap4jPropertiesLoader.
*/
public static Pcap4jPropertiesLoader getInstance() {
return INSTANCE;
}

/**
*
* @return address family number for IPv4 addresses.
*/
public Integer getAfInet() {
return loader.getInteger(
AF_INET_KEY ,
null
);
}

/**
*
* @return address family numbers for IPv6 addresses.
*/
public Integer getAfInet6() {
return loader.getInteger(
AF_INET6_KEY ,
null
);
}

/**
* For Linux
*
* @return address family numbers for link layer addresses.
*/
public Integer getAfPacket() {
return loader.getInteger(
AF_PACKET_KEY ,
null
);
}

/**
* For BSD including Mac OS X
*
* @return address family numbers for link layer addresses.
*/
public Integer getAfLink() {
return loader.getInteger(
AF_LINK_KEY ,
null
);
}


/**
* DLT_RAW
*
* @return the value of DLT_RAW
*/
public Integer getDltRaw() {
return loader.getInteger(
DLT_RAW_KEY ,
null
);
}
private static final String KEY_PREFIX
= Pcap4jPropertiesLoader.class.getPackage().getName();

/**
*
*/
public static final String PCAP4J_PROPERTIES_PATH_KEY
= KEY_PREFIX + ".properties";

/**
*
*/
public static final String AF_INET_KEY = KEY_PREFIX + ".af.inet";

/**
*
*/
public static final String AF_INET6_KEY = KEY_PREFIX + ".af.inet6";

/**
*
*/
public static final String AF_PACKET_KEY = KEY_PREFIX + ".af.packet";

/**
*
*/
public static final String AF_LINK_KEY = KEY_PREFIX + ".af.link";

/**
*
*/
public static final String DLT_RAW_KEY = KEY_PREFIX + ".dlt.raw";


private static final int AF_INET_DEFAULT = 2;

private static final int AF_PACKET_DEFAULT = 17;

private static final int AF_LINK_DEFAULT = 18;

private static final int DLT_RAW_DEFAULT = 12;

private static final int DLT_RAW_OPENBSD = 14;

private static final int AF_INET6_DEFAULT = 23;

private static final int AF_INET6_LINUX = 10;

private static final int AF_INET6_FREEBSD = 28;

private static final int AF_INET6_MAC = 30;

private static final Pcap4jPropertiesLoader INSTANCE = new Pcap4jPropertiesLoader();

private PropertiesLoader loader
= new PropertiesLoader(
System.getProperty(
PCAP4J_PROPERTIES_PATH_KEY,
KEY_PREFIX.replace('.', '/') + "/pcap4j.properties"
),
true,
true
);

private Pcap4jPropertiesLoader() {
}

/**
* @return the singleton instance of Pcap4jPropertiesLoader.
*/
public static Pcap4jPropertiesLoader getInstance() {
return INSTANCE;
}

/**
* @return address family number for IPv4 addresses.
*/
public Integer getAfInet() {
return loader.getInteger(
AF_INET_KEY,
AF_INET_DEFAULT
);
}

/**
* @return address family numbers for IPv6 addresses.
*/
public Integer getAfInet6() {
return loader.getInteger(
AF_INET6_KEY,
getDefaultAfInet6()
);
}

/**
* For Linux
*
* @return address family numbers for link layer addresses.
*/
public Integer getAfPacket() {
return loader.getInteger(
AF_PACKET_KEY,
AF_PACKET_DEFAULT
);
}

/**
* For BSD including Mac OS X
*
* @return address family numbers for link layer addresses.
*/
public Integer getAfLink() {
return loader.getInteger(
AF_LINK_KEY,
AF_LINK_DEFAULT
);
}


/**
* DLT_RAW
*
* @return the value of DLT_RAW
*/
public Integer getDltRaw() {
return loader.getInteger(
DLT_RAW_KEY,
getDefaultDltRaw()
);
}

/**
* @return The default address family for IPv6 addresses (platform specific)
*/
private int getDefaultAfInet6() {
switch (Platform.getOSType()) {
case Platform.MAC:
return AF_INET6_MAC;
case Platform.FREEBSD:
case Platform.KFREEBSD:
return AF_INET6_FREEBSD;
case Platform.LINUX:
case Platform.ANDROID:
return AF_INET6_LINUX;
default:
return AF_INET6_DEFAULT;
}
}

/**
* @return The default value for DLT_RAW (platform specific)
*/
private int getDefaultDltRaw() {
switch (Platform.getOSType()) {
case Platform.OPENBSD:
return DLT_RAW_OPENBSD;
default:
return DLT_RAW_DEFAULT;
}
}

}
12 changes: 6 additions & 6 deletions pcap4j-core/src/main/java/org/pcap4j/util/PropertiesLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public String getString(String key, String defaultValue) {
);
}
else {
logger.warn(
logger.info(
"[{}] Could not get value by {}, use default value: {}",
new Object[] {resourceName, key, defaultValue}
);
Expand Down Expand Up @@ -201,7 +201,7 @@ public Integer getInteger(String key, Integer defaultValue) {
}
}
else {
logger.warn(
logger.info(
"[{}] Could not get value by {}, use default value: {}",
new Object[] {resourceName, key, defaultValue}
);
Expand Down Expand Up @@ -259,7 +259,7 @@ public Boolean getBoolean(String key, Boolean defaultValue) {
);
}
else {
logger.warn(
logger.info(
"[{}] Could not get value by {}, use default value: {}",
new Object[] {resourceName, key, defaultValue}
);
Expand Down Expand Up @@ -353,7 +353,7 @@ public <T> Class<? extends T> getClass(
}
}
else {
logger.warn(
logger.info(
"[{}] Could not get value by {}, use default value: {}",
new Object[] {resourceName, key, defaultValue}
);
Expand Down Expand Up @@ -426,7 +426,7 @@ public InetAddress getInetAddress(String key, InetAddress defaultValue) {
}
}
else {
logger.warn(
logger.info(
"[{}] Could not get value by {}, use default value: {}",
new Object[] {resourceName, key, defaultValue}
);
Expand Down Expand Up @@ -508,7 +508,7 @@ public int[] getIntArray(String key, int[] defaultValue) {
}
}
else {
logger.warn(
logger.info(
"[{}] Could not get value by {}, use default value: {}",
new Object[] {resourceName, key, Arrays.toString(defaultValue)}
);
Expand Down
Loading