use of java.net.SocketException in project Openfire by igniterealtime.
the class ConferenceReceiver method initLoneReceiverChannel.
private void initLoneReceiverChannel(int loneReceiverPort) {
if (this.loneReceiverPort != loneReceiverPort && loneReceiverChannel != null) {
close();
}
this.loneReceiverPort = loneReceiverPort;
try {
selector = Selector.open();
} catch (IOException e) {
Logger.println("Conference receiver failed to open selector " + e.getMessage());
return;
}
if (loneReceiverPort == 0) {
return;
}
Logger.println("Init lone channel using port " + loneReceiverPort);
try {
loneReceiverChannel = DatagramChannel.open();
if (Logger.logLevel >= Logger.LOG_INFO) {
Logger.println("Opened lone receiver channel " + loneReceiverChannel);
}
} catch (IOException e) {
Logger.println("Conference receiver failed to open DatagramChannel " + " " + e.getMessage());
return;
}
try {
loneReceiverChannel.configureBlocking(false);
} catch (IOException e) {
Logger.println("Conference receiver failed to configureBlocking to false " + e.getMessage());
return;
}
DatagramSocket socket = loneReceiverChannel.socket();
try {
socket.setReceiveBufferSize(RtpSocket.MAX_RECEIVE_BUFFER);
} catch (SocketException e) {
Logger.println("ConferenceReceiver failed to set receive buffer size " + e.getMessage());
return;
}
try {
socket.setSoTimeout(0);
} catch (SocketException e) {
Logger.println("ConferenceReceiver failed to set timeout " + e.getMessage());
return;
}
InetSocketAddress bridgeAddress = Bridge.getLocalBridgeAddress();
InetSocketAddress isa = new InetSocketAddress(bridgeAddress.getAddress(), loneReceiverPort);
try {
socket.bind(isa);
} catch (IOException e) {
Logger.println("Conference receiver unable to bind to " + loneReceiverPort + " " + e.getMessage());
return;
}
try {
SelectionKey selectionKey = loneReceiverChannel.register(selector, SelectionKey.OP_READ);
} catch (Exception e) {
Logger.println("Conference receiver unable to register: " + e.getMessage());
return;
}
memberCount++;
Logger.println("Lone Channel uses port " + loneReceiverPort);
}
use of java.net.SocketException in project Openfire by igniterealtime.
the class ConferenceManager method getConference.
public static ConferenceManager getConference(String conferenceId, String mediaPreference, String displayName, boolean permanent) {
ConferenceManager conferenceManager;
try {
conferenceManager = findConferenceManager(conferenceId);
if (Logger.logLevel >= Logger.LOG_INFO) {
Logger.println("found existing conference: '" + conferenceId + "'");
}
return conferenceManager;
} catch (ParseException e) {
}
try {
conferenceManager = new ConferenceManager(conferenceId, mediaPreference, displayName);
} catch (SocketException e) {
Logger.error("Can't create conference " + conferenceId + " " + e.getMessage());
return null;
}
synchronized (conferenceList) {
conferenceList.add(conferenceManager);
}
Logger.println("starting new conference: '" + conferenceId + "'. " + " conferences in progress: " + conferenceList.size());
conferenceManager.setPermanent(permanent);
String id = conferenceManager.getId();
if (displayName != null) {
id += ":" + mediaPreference + ":" + displayName;
}
ConferenceManager.conferenceEventNotification(new ConferenceEvent(ConferenceEvent.CONFERENCE_STARTED, id));
return conferenceManager;
}
use of java.net.SocketException in project Openfire by igniterealtime.
the class ConferenceMember method initializeChannel.
private void initializeChannel() throws IOException {
datagramChannel = conferenceManager.getConferenceReceiver().getChannel(cp);
if (datagramChannel != null) {
synchronized (datagramChannel) {
if (loneRtcpReceiver == null) {
int rtcpPort = datagramChannel.socket().getLocalPort() + 1;
Logger.println("Starting lone RtcpReceiver on port " + rtcpPort);
loneRtcpReceiver = new RtcpReceiver(new DatagramSocket(rtcpPort), true);
}
rtcpReceiver = loneRtcpReceiver;
}
return;
}
/*
* We are trying to find a pair of sockets with consecutive port nums.
* The first socket must have an even port.
*
* If we find a socket that we don't like, we have to keep it open
* otherwise when we try to find another socket, we may get the same
* one as before.
*
* So we make a list of the bad sockets and close them all
* after we're done.
*/
ArrayList badChannels = new ArrayList();
int nextRtpPort = firstRtpPort;
try {
while (true) {
datagramChannel = DatagramChannel.open();
if (Logger.logLevel >= Logger.LOG_DETAIL) {
Logger.println("Call " + cp + " Opened datagram channel " + datagramChannel);
}
datagramChannel.configureBlocking(false);
DatagramSocket socket = datagramChannel.socket();
socket.setReceiveBufferSize(RtpSocket.MAX_RECEIVE_BUFFER);
socket.setSoTimeout(0);
InetSocketAddress bridgeAddress = Bridge.getLocalBridgeAddress();
InetSocketAddress isa = new InetSocketAddress(bridgeAddress.getAddress(), nextRtpPort);
if (nextRtpPort > 0) {
nextRtpPort += 2;
if (lastRtpPort != 0 && (nextRtpPort + 1) > lastRtpPort) {
Logger.println("No more RTP ports available, last is " + lastRtpPort);
closeBadChannels(badChannels);
throw new IOException("No more RTP ports available, last is " + lastRtpPort);
}
}
try {
socket.bind(isa);
int localPort = socket.getLocalPort();
if ((localPort & 1) != 0) {
/*
* Port is odd, can't use this datagramSocket
*/
if (Logger.logLevel >= Logger.LOG_INFO) {
Logger.println("Call " + cp + " skipping DatagramSocket with odd port " + localPort);
}
badChannels.add(datagramChannel);
continue;
}
Logger.writeFile("Call " + cp + " RTCP Port " + (localPort + 1));
rtcpReceiver = new RtcpReceiver(new DatagramSocket(localPort + 1), false);
break;
} catch (SocketException e) {
/*
* Couldn't bind, can't use this DatagramSocket.
*/
if (Logger.logLevel >= Logger.LOG_INFO) {
Logger.println("Call " + cp + " skipping DatagramSocket " + e.getMessage());
}
badChannels.add(datagramChannel);
continue;
}
}
} catch (Exception e) {
closeBadChannels(badChannels);
throw new IOException("Call " + cp + " MemberReceiver exception! " + e.getMessage());
}
closeBadChannels(badChannels);
if (Logger.logLevel >= Logger.LOG_INFO) {
Logger.println("Call " + cp + " port " + datagramChannel.socket().getLocalPort());
}
}
use of java.net.SocketException in project k-9 by k9mail.
the class SmtpTransport method open.
@Override
public void open() throws MessagingException {
try {
boolean secureConnection = false;
InetAddress[] addresses = InetAddress.getAllByName(mHost);
for (int i = 0; i < addresses.length; i++) {
try {
SocketAddress socketAddress = new InetSocketAddress(addresses[i], mPort);
if (mConnectionSecurity == ConnectionSecurity.SSL_TLS_REQUIRED) {
mSocket = mTrustedSocketFactory.createSocket(null, mHost, mPort, mClientCertificateAlias);
mSocket.connect(socketAddress, SOCKET_CONNECT_TIMEOUT);
secureConnection = true;
} else {
mSocket = new Socket();
mSocket.connect(socketAddress, SOCKET_CONNECT_TIMEOUT);
}
} catch (SocketException e) {
if (i < (addresses.length - 1)) {
// there are still other addresses for that host to try
continue;
}
throw new MessagingException("Cannot connect to host", e);
}
// connection success
break;
}
// RFC 1047
mSocket.setSoTimeout(SOCKET_READ_TIMEOUT);
mIn = new PeekableInputStream(new BufferedInputStream(mSocket.getInputStream(), 1024));
mOut = new BufferedOutputStream(mSocket.getOutputStream(), 1024);
// Eat the banner
executeCommand(null);
InetAddress localAddress = mSocket.getLocalAddress();
String localHost = getCanonicalHostName(localAddress);
String ipAddr = localAddress.getHostAddress();
if (localHost.equals("") || localHost.equals(ipAddr) || localHost.contains("_")) {
// characters (see issue 2143), so use IP address.
if (!ipAddr.equals("")) {
if (localAddress instanceof Inet6Address) {
localHost = "[IPv6:" + ipAddr + "]";
} else {
localHost = "[" + ipAddr + "]";
}
} else {
// If the IP address is no good, set a sane default (see issue 2750).
localHost = "android";
}
}
Map<String, String> extensions = sendHello(localHost);
m8bitEncodingAllowed = extensions.containsKey("8BITMIME");
mEnhancedStatusCodesProvided = extensions.containsKey("ENHANCEDSTATUSCODES");
if (mConnectionSecurity == ConnectionSecurity.STARTTLS_REQUIRED) {
if (extensions.containsKey("STARTTLS")) {
executeCommand("STARTTLS");
mSocket = mTrustedSocketFactory.createSocket(mSocket, mHost, mPort, mClientCertificateAlias);
mIn = new PeekableInputStream(new BufferedInputStream(mSocket.getInputStream(), 1024));
mOut = new BufferedOutputStream(mSocket.getOutputStream(), 1024);
/*
* Now resend the EHLO. Required by RFC2487 Sec. 5.2, and more specifically,
* Exim.
*/
extensions = sendHello(localHost);
secureConnection = true;
} else {
/*
* This exception triggers a "Certificate error"
* notification that takes the user to the incoming
* server settings for review. This might be needed if
* the account was configured with an obsolete
* "STARTTLS (if available)" setting.
*/
throw new CertificateValidationException("STARTTLS connection security not available");
}
}
boolean authLoginSupported = false;
boolean authPlainSupported = false;
boolean authCramMD5Supported = false;
boolean authExternalSupported = false;
boolean authXoauth2Supported = false;
if (extensions.containsKey("AUTH")) {
List<String> saslMech = Arrays.asList(extensions.get("AUTH").split(" "));
authLoginSupported = saslMech.contains("LOGIN");
authPlainSupported = saslMech.contains("PLAIN");
authCramMD5Supported = saslMech.contains("CRAM-MD5");
authExternalSupported = saslMech.contains("EXTERNAL");
authXoauth2Supported = saslMech.contains("XOAUTH2");
}
parseOptionalSizeValue(extensions);
if (!TextUtils.isEmpty(mUsername) && (!TextUtils.isEmpty(mPassword) || AuthType.EXTERNAL == mAuthType || AuthType.XOAUTH2 == mAuthType)) {
switch(mAuthType) {
/*
* LOGIN is an obsolete option which is unavailable to users,
* but it still may exist in a user's settings from a previous
* version, or it may have been imported.
*/
case LOGIN:
case PLAIN:
// try saslAuthPlain first, because it supports UTF-8 explicitly
if (authPlainSupported) {
saslAuthPlain(mUsername, mPassword);
} else if (authLoginSupported) {
saslAuthLogin(mUsername, mPassword);
} else {
throw new MessagingException("Authentication methods SASL PLAIN and LOGIN are unavailable.");
}
break;
case CRAM_MD5:
if (authCramMD5Supported) {
saslAuthCramMD5(mUsername, mPassword);
} else {
throw new MessagingException("Authentication method CRAM-MD5 is unavailable.");
}
break;
case XOAUTH2:
if (authXoauth2Supported && oauthTokenProvider != null) {
saslXoauth2(mUsername);
} else {
throw new MessagingException("Authentication method XOAUTH2 is unavailable.");
}
break;
case EXTERNAL:
if (authExternalSupported) {
saslAuthExternal(mUsername);
} else {
/*
* Some SMTP servers are known to provide no error
* indication when a client certificate fails to
* validate, other than to not offer the AUTH EXTERNAL
* capability.
*
* So, we treat it is an error to not offer AUTH
* EXTERNAL when using client certificates. That way, the
* user can be notified of a problem during account setup.
*/
throw new CertificateValidationException(MissingCapability);
}
break;
/*
* AUTOMATIC is an obsolete option which is unavailable to users,
* but it still may exist in a user's settings from a previous
* version, or it may have been imported.
*/
case AUTOMATIC:
if (secureConnection) {
// try saslAuthPlain first, because it supports UTF-8 explicitly
if (authPlainSupported) {
saslAuthPlain(mUsername, mPassword);
} else if (authLoginSupported) {
saslAuthLogin(mUsername, mPassword);
} else if (authCramMD5Supported) {
saslAuthCramMD5(mUsername, mPassword);
} else {
throw new MessagingException("No supported authentication methods available.");
}
} else {
if (authCramMD5Supported) {
saslAuthCramMD5(mUsername, mPassword);
} else {
/*
* We refuse to insecurely transmit the password
* using the obsolete AUTOMATIC setting because of
* the potential for a MITM attack. Affected users
* must choose a different setting.
*/
throw new MessagingException("Update your outgoing server authentication setting. AUTOMATIC auth. is unavailable.");
}
}
break;
default:
throw new MessagingException("Unhandled authentication method found in the server settings (bug).");
}
}
} catch (MessagingException e) {
close();
throw e;
} catch (SSLException e) {
close();
throw new CertificateValidationException(e.getMessage(), e);
} catch (GeneralSecurityException gse) {
close();
throw new MessagingException("Unable to open connection to SMTP server due to security error.", gse);
} catch (IOException ioe) {
close();
throw new MessagingException("Unable to open connection to SMTP server.", ioe);
}
}
use of java.net.SocketException in project Openfire by igniterealtime.
the class Channel method run.
/**
* Thread override method
*/
@Override
public void run() {
try {
while (enabled) {
// Block until a datagram appears:
packet = new DatagramPacket(buf, buf.length);
dataSocket.receive(packet);
if (handle(packet)) {
boolean resend = true;
for (DatagramListener dl : listeners) {
boolean send = dl.datagramReceived(packet);
if (resend && !send) {
resend = false;
}
}
if (resend) {
relayPacket(packet);
}
}
}
} catch (UnknownHostException uhe) {
if (enabled) {
Log.error("Unknown Host", uhe);
}
} catch (SocketException se) {
if (enabled) {
Log.error("Socket closed", se);
}
} catch (IOException ioe) {
if (enabled) {
Log.error("Communication error", ioe);
}
}
}
Aggregations