Skip to content
connection.hxx 3.61 KiB
Newer Older
Seblu's avatar
Seblu committed
/*
  This file is part of SLL.
Seblu's avatar
Seblu committed
  Copyright (C) 2008 Sebastien LUTTRINGER <contact@seblu.net>

  SLL is free software; you can redistribute it and/or modify
Seblu's avatar
Seblu committed
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; version 2 of the License.

  SLL is distributed in the hope that it will be useful,
Seblu's avatar
Seblu committed
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with SLLo; if not, write to the Free Software
Seblu's avatar
Seblu committed
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

Seblu's avatar
Seblu committed
# include "error.hh"

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>

Seblu's avatar
Seblu committed
/*******************************************************************************
 ** Public method
 ******************************************************************************/

Seblu's avatar
Seblu committed
/**
 * Return the connection sate
 * Cannot be const, du to mutex locking
 *
 * @return see description
 */
bool Connection::connected() {
  // get connection mutex

  pthread_mutex_lock(&c_mutex_);

  bool ret = (socket_fd_ >= 0);

  pthread_mutex_unlock(&c_mutex_);

  return ret;
}

Seblu's avatar
Seblu committed
/**
 * Send @param data on socket
 */
void Connection::send(const string &data) {
  send(data.c_str(), data.length());
}

/**
 * Send @param data followed by '\n' on socket
 */
void Connection::sendln(const string &data) {
Seblu's avatar
Seblu committed
  string tosend = data + "\n";
  send(tosend.c_str(), tosend.length());
Seblu's avatar
Seblu committed
}

/*******************************************************************************
 ** Protected method
 ******************************************************************************/

Seblu's avatar
Seblu committed
/**
 * Free a socket
 * No mutex used !
 */
void Connection::disconnect_() {
Seblu's avatar
Seblu committed
  assert(socket_fd_ >= 0);
Seblu's avatar
Seblu committed

  close(socket_fd_);
  socket_fd_ = -1;
}

/**
 * Reserve a socket
 * No mutex used !
 */
void Connection::socket_() {
  assert(socket_fd_ == -1);
  int yes = 1;

  if ((socket_fd_ = ::socket(AF_INET, SOCK_STREAM, 0)) == -1)
    throw Error(ERR_NET, (string) "Unable to open socket: " + strerror(errno));

  if (::setsockopt(socket_fd_, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
    throw Error(ERR_NET, (string) "Unable to set socket options: " + strerror(errno));
}

/**
 * Set local ip
 * No mutex used !
 */
void Connection::setlocalip_() {
  struct sockaddr_in addr;
  socklen_t len = sizeof addr;

Seblu's avatar
Seblu committed
  if (::getsockname(socket_fd_, (struct sockaddr*) &addr, &len) != 0)
    throw Error(ERR_NET, (string) "Unable to set local ip: " + strerror(errno));
Seblu's avatar
Seblu committed
  local_ip_ = inet_ntoa(addr.sin_addr);
}

/**
 * Set local port
 * No mutex used !
 */
void Connection::setlocalport_() {
  struct sockaddr_in addr;
  socklen_t len = sizeof addr;

Seblu's avatar
Seblu committed
  if (::getsockname(socket_fd_, (struct sockaddr*) &addr, &len) != 0)
    throw Error(ERR_NET, (string) "Unable to set local port: " + strerror(errno));
Seblu's avatar
Seblu committed
  local_port_ = ntohs(addr.sin_port);
}

/**
 * Set remote ip
 * No mutex used !
 */
void Connection::setremoteip_() {
  struct sockaddr_in addr;
  socklen_t len = sizeof addr;

Seblu's avatar
Seblu committed
  if (::getpeername(socket_fd_, (struct sockaddr*) &addr, &len) != 0)
    throw Error(ERR_NET, (string) "Unable to set remote ip: " + strerror(errno));
Seblu's avatar
Seblu committed
  remote_ip_ = inet_ntoa(addr.sin_addr);
}

/**
 * Set remote port
 * No mutex used !
 */
void Connection::setremoteport_() {
  struct sockaddr_in addr;
  socklen_t len = sizeof addr;

Seblu's avatar
Seblu committed
  if (::getpeername(socket_fd_, (struct sockaddr*) &addr, &len) != 0)
    throw Error(ERR_NET, (string) "Unable to set remote port: " + strerror(errno));
Seblu's avatar
Seblu committed
  remote_port_ = ntohs(addr.sin_port);
}