Example #1
0
/** @return false and set error code and description string on failure  */
bool ArSocket::create(Type type)
{
  myError = NoErr;
  myErrorStr.clear();

  if ((type == TCP) && ((myFD=socket(AF_INET, SOCK_STREAM, 0)) < 0))
  {
    myError = NetFail;
    myErrorStr="Failure to make TCP socket";
    perror("socket");
    return(false);
  }
  else if ((type == UDP) && ((myFD=socket(AF_INET, SOCK_DGRAM, 0)) < 0))
  {
    myError = NetFail;
    myErrorStr="Failure to make UDP socket";
    perror("socket");
    return(false);
  }

  ArUtil::setFileCloseOnExec(myFD);

  myType=type;

  if (getSockName())
    return(true);
  else
    return(false);
}
Example #2
0
/** @return false and set error code and description string on failure  */
bool ArSocket::create(Type type)
{
  myError = NoErr;
  myErrorStr.clear();

  if ((type == TCP) && ((myFD=socket(AF_INET, SOCK_STREAM, 0)) < 0))
  {
    myError = NetFail;
    myErrorStr="Failure to make TCP socket";
    ArLog::logErrorFromOS(ArLog::Normal, "ArSocket::create: could not create tcp socket");
    return(false);
  }
  else if ((type == UDP) && ((myFD=socket(AF_INET, SOCK_DGRAM, 0)) < 0))
  {
    myError = NetFail;
    myErrorStr="Failure to make UDP socket";
    ArLog::logErrorFromOS(ArLog::Normal, "ArSocket::create: could not create udp socket");
    return(false);
  }

  ArUtil::setFileCloseOnExec(myFD);

  myType=type;

  if (getSockName())
    return(true);
  else
    return(false);
}
Example #3
0
bool ArSocket::create(Type type)
{
  if ((type == TCP) && ((myFD=socket(AF_INET, SOCK_STREAM, 0)) < 0))
  {
    myErrorStr="Failure to make TCP socket";
    perror("socket");
    return(false);
  }
  else if ((type == UDP) && ((myFD=socket(AF_INET, SOCK_DGRAM, 0)) < 0))
  {
    myErrorStr="Failure to make UDP socket";
    perror("socket");
    return(false);
  }

  myType=type;

  if (getSockName())
    return(true);
  else
    return(false);
}
Example #4
0
int CSocket::getSockName(string &strAddress, uint16_t &iPort)
{
    if(m_iDomain == AF_UNIX){
        return getSockName(strAddress);
    }

    struct sockaddr_in stPeer;
    bzero(&stPeer, sizeof(struct sockaddr));
    socklen_t iLen = sizeof(sockaddr);
    if(::getsockname(m_iFd, (struct sockaddr *)&stPeer, &iLen) != 0){
        TDOEH_SET_ERROR_NUMBER_RETURN(errno);
    }

    char szAddr[20];
    if(NULL == inet_ntop(m_iDomain, &(stPeer.sin_addr), szAddr, sizeof(szAddr))){
        TDOEH_SET_ERROR_NUMBER_RETURN(errno);
    }

    strAddress = szAddr;
    iPort = ntohs(stPeer.sin_port);
    return E_SUCCESS;
}
Example #5
0
/*
 *  创建指定type的套接字
 * */
bool RobotSocket::create(NetType type)
{
	//创建TCP套接字
	if ((type == TCP) && ((myFD = socket(AF_INET, SOCK_STREAM, 0)) < 0))
	{
		myErrorStr = "[socket]创建TCP套接字失败";
		perror(myErrorStr.c_str());
		return(false);
	}
	//创建UDP套接字
	else if ((type == UDP) && ((myFD = socket(AF_INET, SOCK_DGRAM, 0)) < 0))
	{
		myErrorStr = "[socket]创建UDP套接字失败";
		perror(myErrorStr.c_str());
		return(false);
	}
	//保存套接字类型
	myType = type;
	//获取与该套接字关联的套接字结构体和测试该套接字是否建立有效
	if (getSockName())
		return(true);
	else
		return(false);
}
Example #6
0
static int createSocket()
{
    int         socketFd;
    ksocklen_t  addrlen;
    struct stat s;
    const char  *sock=getSockName();
    int stat_err=lstat(sock, &s);

    if(!stat_err && S_ISLNK(s.st_mode))
    {
        kWarning() << "Someone is running a symlink attack on you" ;
        if(unlink(sock))
        {
            kWarning() << "Could not delete symlink" ;
            return -1;
        }
    }

    if (!access(sock, R_OK|W_OK))
    {
        kWarning() << "stale socket exists" ;
        if (unlink(sock))
        {
            kWarning() << "Could not delete stale socket" ;
            return -1;
        }
    }

    socketFd = socket(PF_UNIX, SOCK_STREAM, 0);
    if (socketFd < 0)
    {
        kError() << "socket(): " << strerror(errno);
        return -1;
    }

    struct sockaddr_un addr;
    addr.sun_family = AF_UNIX;
    strncpy(addr.sun_path, sock, sizeof(addr.sun_path)-1);
    addr.sun_path[sizeof(addr.sun_path)-1] = '\000';
    addrlen = SUN_LEN(&addr);
    if (bind(socketFd, (struct sockaddr *)&addr, addrlen) < 0)
    {
        kError() << "bind(): " << strerror(errno);
        return -1;
    }

    struct linger lin;
    lin.l_onoff = lin.l_linger = 0;
    if (setsockopt(socketFd, SOL_SOCKET, SO_LINGER, (char *) &lin,
                   sizeof(linger)) < 0)
    {
        kError() << "setsockopt(SO_LINGER): " << strerror(errno);
        return -1;
    }

    int opt = 1;
    if (setsockopt(socketFd, SOL_SOCKET, SO_REUSEADDR, (char *) &opt,
                   sizeof(opt)) < 0)
    {
        kError() << "setsockopt(SO_REUSEADDR): " << strerror(errno);
        return -1;
    }
    opt = 1;
    if (setsockopt(socketFd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt,
                   sizeof(opt)) < 0)
    {
        kError() << "setsockopt(SO_KEEPALIVE): " << strerror(errno);
        return -1;
    }
    chmod(sock, 0600);
    if (listen(socketFd, 1) < 0)
    {
        kError() << "listen(): " << strerror(errno);
        return -1;
    }

    return socketFd;
}