void UdpSocket::SetMulticastLoop(bool x) { if (GetSocket() == INVALID_SOCKET) { CreateConnection(); } #ifdef ENABLE_IPV6 #ifdef IPPROTO_IPV6 if (IsIpv6()) { int val = x ? 1 : 0; if (setsockopt(GetSocket(), IPPROTO_IPV6, IPV6_MULTICAST_LOOP, (char *)&val, sizeof(int)) == -1) { Handler().LogError(this, "SetMulticastLoop(ipv6)", Errno, StrError(Errno), LOG_LEVEL_WARNING); } return; } #endif #endif int val = x ? 1 : 0; if (setsockopt(GetSocket(), SOL_IP, IP_MULTICAST_LOOP, (char *)&val, sizeof(int)) == -1) { Handler().LogError(this, "SetMulticastLoop(ipv4)", Errno, StrError(Errno), LOG_LEVEL_WARNING); } }
bool UdpSocket::IsMulticastLoop() { if (GetSocket() == INVALID_SOCKET) { CreateConnection(); } #ifdef ENABLE_IPV6 #ifdef IPPROTO_IPV6 if (IsIpv6()) { int is_loop = 0; socklen_t size = sizeof(int); if (getsockopt(GetSocket(), IPPROTO_IPV6, IPV6_MULTICAST_LOOP, (char *)&is_loop, &size) == -1) { Handler().LogError(this, "IsMulticastLoop(ipv6)", Errno, StrError(Errno), LOG_LEVEL_WARNING); } return is_loop ? true : false; } #endif #endif int is_loop = 0; socklen_t size = sizeof(int); if (getsockopt(GetSocket(), SOL_IP, IP_MULTICAST_LOOP, (char *)&is_loop, &size) == -1) { Handler().LogError(this, "IsMulticastLoop(ipv4)", Errno, StrError(Errno), LOG_LEVEL_WARNING); } return is_loop ? true : false; }
void JackNetMasterInterface::Exit() { jack_log("JackNetMasterInterface::Exit, ID %u", fParams.fID); // stop process fRunning = false; // send a 'multicast euthanasia request' - new socket is required on macosx jack_info("Exiting '%s' %s", fParams.fName, fMulticastIP); SetPacketType(&fParams, KILL_MASTER); JackNetSocket mcast_socket(fMulticastIP, fSocket.GetPort()); session_params_t net_params; memset(&net_params, 0, sizeof(session_params_t)); SessionParamsHToN(&fParams, &net_params); if (mcast_socket.NewSocket() == SOCKET_ERROR) { jack_error("Can't create socket : %s", StrError(NET_ERROR_CODE)); } if (mcast_socket.SendTo(&net_params, sizeof(session_params_t), 0, fMulticastIP) == SOCKET_ERROR) { jack_error("Can't send suicide request : %s", StrError(NET_ERROR_CODE)); } mcast_socket.Close(); }
int JackNetMasterInterface::Recv ( size_t size, int flags ) { int rx_bytes; if ( ( ( rx_bytes = fSocket.Recv ( fRxBuffer, size, flags ) ) == SOCKET_ERROR ) && fRunning ) { net_error_t error = fSocket.GetError(); //no data isn't really a network error, so just return 0 avalaible read bytes if ( error == NET_NO_DATA ) return 0; else if ( error == NET_CONN_ERROR ) { //fatal connection issue, exit jack_error ( "'%s' : %s, exiting.", fParams.fName, StrError ( NET_ERROR_CODE ) ); //ask to the manager to properly remove the master Exit(); // UGLY temporary way to be sure the thread does not call code possibly causing a deadlock in JackEngine. ThreadExit(); } else jack_error ( "Error in master receive : %s", StrError ( NET_ERROR_CODE ) ); } packet_header_t* header = reinterpret_cast<packet_header_t*>(fRxBuffer); PacketHeaderNToH(header, header); return rx_bytes; }
/** * IOチャネル入力時の処理 */ static void fi_input(ioent_t *io, event_t *evt, int exec) { DWORD error; // printf("fi_input: enter(exec=%d, trans=%d)\n", exec, evt->trans); // fflush(stdout); if (evt->trans == 0) { aio_count++; /* 新規に読み込みIO発行 */ if (!ReadFileEx(io->handle, io->buf, io->bufsz, &io->ctlblk, read_completion_handler)) { error = GetLastError(); switch (error) { case ERROR_IO_PENDING: break; case ERROR_HANDLE_EOF: aio_count--; io_read_complete(io, 0); close_file(io); break; default: perr(PERR_SYSTEM, "ReadFile", StrError(error), __FILE__, __LINE__); return; } } return; } if (!SetEvent(io->ctlblk.hEvent)) { error = GetLastError(); perr(PERR_SYSTEM, "SetEvent", StrError(error), __FILE__, __LINE__); return; } TAILQ_INSERT_TAIL(&__prc__mioq, io, mlink); }
bool Socket::SetNonblocking(bool bNb, SOCKET s) { #ifdef _WIN32 unsigned long l = bNb ? 1 : 0; int n = ioctlsocket(s, FIONBIO, &l); if (n != 0) { Handler().LogError(this, "ioctlsocket(FIONBIO)", Errno, ""); return false; } return true; #else if (bNb) { if (fcntl(s, F_SETFL, O_NONBLOCK) == -1) { Handler().LogError(this, "fcntl(F_SETFL, O_NONBLOCK)", Errno, StrError(Errno), LOG_LEVEL_ERROR); return false; } } else { if (fcntl(s, F_SETFL, 0) == -1) { Handler().LogError(this, "fcntl(F_SETFL, 0)", Errno, StrError(Errno), LOG_LEVEL_ERROR); return false; } } return true; #endif }
bool JackNetMasterInterface::Init() { jack_log("JackNetMasterInterface::Init : ID %u", fParams.fID); session_params_t host_params; uint attempt = 0; int rx_bytes = 0; // socket if (fSocket.NewSocket() == SOCKET_ERROR) { jack_error("Can't create socket : %s", StrError(NET_ERROR_CODE)); return false; } // timeout on receive (for init) if (fSocket.SetTimeOut(MASTER_INIT_TIMEOUT) < 0) { jack_error("Can't set init timeout : %s", StrError(NET_ERROR_CODE)); } // connect if (fSocket.Connect() == SOCKET_ERROR) { jack_error("Can't connect : %s", StrError(NET_ERROR_CODE)); return false; } // send 'SLAVE_SETUP' until 'START_MASTER' received jack_info("Sending parameters to %s...", fParams.fSlaveNetName); do { session_params_t net_params; memset(&net_params, 0, sizeof(session_params_t)); SetPacketType(&fParams, SLAVE_SETUP); SessionParamsHToN(&fParams, &net_params); if (fSocket.Send(&net_params, sizeof(session_params_t), 0) == SOCKET_ERROR) { jack_error("Error in send : %s", StrError(NET_ERROR_CODE)); } memset(&net_params, 0, sizeof(session_params_t)); if (((rx_bytes = fSocket.Recv(&net_params, sizeof(session_params_t), 0)) == SOCKET_ERROR) && (fSocket.GetError() != NET_NO_DATA)) { jack_error("Problem with network"); return false; } SessionParamsNToH(&net_params, &host_params); } while ((GetPacketType(&host_params) != START_MASTER) && (++attempt < SLAVE_SETUP_RETRY)); if (attempt == SLAVE_SETUP_RETRY) { jack_error("Slave doesn't respond, exiting"); return false; } return true; }
void SctpSocket::OnException() { if (Connecting()) { #ifdef ENABLE_SOCKS4 if (Socks4()) OnSocks4ConnectFailed(); else #endif if (GetConnectionRetry() == -1 || (GetConnectionRetry() && GetConnectionRetries() < GetConnectionRetry() )) { // even though the connection failed at once, only retry after // the connection timeout // should we even try to connect again, when CheckConnect returns // false it's because of a connection error - not a timeout... } else { SetConnecting(false); // tnx snibbe SetCloseAndDelete(); OnConnectFailed(); } return; } // %! exception doesn't always mean something bad happened, this code should be reworked // errno valid here? int err = SoError(); Handler().LogError(this, "exception on select", err, StrError(err), LOG_LEVEL_FATAL); SetCloseAndDelete(); }
void SctpSocket::OnRead() { /* int sctp_recvmsg(int sd, void * msg, size_t * len, struct sockaddr * from, socklen_t * fromlen, struct sctp_sndrcvinfo * sinfo, int * msg_flags); DESCRIPTION sctp_recvmsg is a wrapper library function that can be used to receive a message from a socket while using the advanced features of SCTP. sd is the socket descriptor on which the message pointed to by msg of length len is received. If from is not NULL, the source address of the message is filled in. The argument fromlen is a value-result parameter. initialized to the size of the buffer associated with from , and modified on return to indicate the actual size of the address stored. sinfo is a pointer to a sctp_sndrcvinfo structure to be filled upon receipt of the message. msg_flags is a pointer to a integer that is filled with any message flags like MSG_NOTIFICATION or MSG_EOR. */ struct sockaddr sa; socklen_t sa_len = 0; struct sctp_sndrcvinfo sinfo; int flags = 0; int n = sctp_recvmsg(GetSocket(), m_buf, SCTP_BUFSIZE_READ, &sa, &sa_len, &sinfo, &flags); if (n == -1) { Handler().LogError(this, "SctpSocket", Errno, StrError(Errno), LOG_LEVEL_FATAL); SetCloseAndDelete(); } else { OnReceiveMessage(m_buf, n, &sa, sa_len, &sinfo, flags); } }
void HttpPutSocket::SetFile(const std::string& file) { //struct stat st; // Added by Amir Krifa std::ifstream f; f.open(file.c_str(), std::ios_base::binary | std::ios_base::in); //if (!f.good() || f.eof() || !f.is_open()) { return 0; } if (!f.good() || f.eof() || !f.is_open()) { Handler().LogError(this, "SetFile", Errno, StrError(Errno), LOG_LEVEL_FATAL); SetCloseAndDelete(); }else { f.seekg(0, std::ios_base::beg); std::ifstream::pos_type begin_pos = f.tellg(); f.seekg(0, std::ios_base::end); m_filename = file; m_content_length = static_cast<off_t>(f.tellg() - begin_pos); } //if (!stat(file.c_str(), &st)) //{ // m_filename = file; // m_content_length = st.st_size; //} //else //{ // Handler().LogError(this, "SetFile", Errno, StrError(Errno), LOG_LEVEL_FATAL); // SetCloseAndDelete(); //} }
void UdpSocket::SetMulticastDefaultInterface(in6_addr a, int if_index) { if (setsockopt(GetSocket(), IPPROTO_IPV6, IPV6_MULTICAST_IF, &if_index, sizeof(if_index)) == -1) { Handler().LogError(this, "SetMulticastDefaultInterface(ipv6)", Errno, StrError(Errno), LOG_LEVEL_WARNING); } }
int UdpSocket::Bind(SocketAddress& ad, int range) { if (GetSocket() == INVALID_SOCKET) { Attach(CreateSocket(ad.GetFamily(), SOCK_DGRAM, "udp")); } if (GetSocket() != INVALID_SOCKET) { SetNonblocking(true); int n = bind(GetSocket(), ad, ad); int tries = range; while (n == -1 && tries--) { ad.SetPort(ad.GetPort() + 1); n = bind(GetSocket(), ad, ad); } if (n == -1) { Handler().LogError(this, "bind", Errno, StrError(Errno), LOG_LEVEL_FATAL); SetCloseAndDelete(); #ifdef ENABLE_EXCEPTIONS throw Exception("bind() failed for UdpSocket, port:range: " + Utility::l2string(ad.GetPort()) + ":" + Utility::l2string(range)); #endif return -1; } m_bind_ok = true; m_port = ad.GetPort(); return 0; } return -1; }
void Socket::OnException() { // %! exception doesn't always mean something bad happened, this code should be reworked // errno valid here? int err = SoError(); Handler().LogError(this, "exception on select", err, StrError(err), LOG_LEVEL_FATAL); SetCloseAndDelete(); }
void UdpSocket::SetMulticastDefaultInterface(ipaddr_t a, int if_index) { struct in_addr x; memcpy(&x.s_addr, &a, sizeof(a)); if (setsockopt(GetSocket(), IPPROTO_IP, IP_MULTICAST_IF, (char *)&x, sizeof(x)) == -1) { Handler().LogError(this, "SetMulticastDefaultInterface(ipv4)", Errno, StrError(Errno), LOG_LEVEL_WARNING); } }
void JackNetMasterInterface::FatalSendError() { // fatal connection issue, exit jack_error("Send connection lost error = %s, '%s' exiting", StrError(NET_ERROR_CODE), fParams.fName); // ask to the manager to properly remove the master Exit(); // UGLY temporary way to be sure the thread does not call code possibly causing a deadlock in JackEngine. ThreadExit(); }
bool Socket::SetSoSndbufforce(int x) { if (setsockopt(GetSocket(), SOL_SOCKET, SO_SNDBUFFORCE, (char *)&x, sizeof(x)) == -1) { Handler().LogError(this, "setsockopt(SOL_SOCKET, SO_SNDBUFFORCE)", Errno, StrError(Errno), LOG_LEVEL_FATAL); return false; } return true; }
bool Socket::SoPeercred(struct ucred& ucr) { if (setsockopt(GetSocket(), SOL_SOCKET, SO_PEERCRED, (char *)&ucr, sizeof(ucr)) == -1) { Handler().LogError(this, "setsockopt(SOL_SOCKET, SO_PEERCRED)", Errno, StrError(Errno), LOG_LEVEL_FATAL); return false; } return true; }
bool Socket::SetSoPriority(int x) { if (setsockopt(GetSocket(), SOL_SOCKET, SO_PRIORITY, (char *)&x, sizeof(x)) == -1) { Handler().LogError(this, "setsockopt(SOL_SOCKET, SO_PRIORITY)", Errno, StrError(Errno), LOG_LEVEL_FATAL); return false; } return true; }
bool Socket::SetSoBindtodevice(const std::string& intf) { if (setsockopt(GetSocket(), SOL_SOCKET, SO_BINDTODEVICE, (char *)intf.c_str(), intf.size()) == -1) { Handler().LogError(this, "setsockopt(SOL_SOCKET, SO_BINDTODEVICE)", Errno, StrError(Errno), LOG_LEVEL_FATAL); return false; } return true; }
bool Socket::SetIpMtudiscover(bool x) { int optval = x ? 1 : 0; if (setsockopt(GetSocket(), IPPROTO_IP, IP_MTU_DISCOVER, (char *)&optval, sizeof(optval)) == -1) { Handler().LogError(this, "setsockopt(IPPROTO_IP, IP_MTU_DISCOVER)", Errno, StrError(Errno), LOG_LEVEL_FATAL); return false; } return true; }
bool Socket::SetSoTimestamp(bool x) { int optval = x ? 1 : 0; if (setsockopt(GetSocket(), SOL_SOCKET, SO_TIMESTAMP, (char *)&optval, sizeof(optval)) == -1) { Handler().LogError(this, "setsockopt(SOL_SOCKET, SO_TIMESTAMP)", Errno, StrError(Errno), LOG_LEVEL_FATAL); return false; } return true; }
bool Socket::SetSoPasscred(bool x) { int optval = x ? 1 : 0; if (setsockopt(GetSocket(), SOL_SOCKET, SO_PASSCRED, (char *)&optval, sizeof(optval)) == -1) { Handler().LogError(this, "setsockopt(SOL_SOCKET, SO_PASSCRED)", Errno, StrError(Errno), LOG_LEVEL_FATAL); return false; } return true; }
bool Socket::SetIpPktinfo(bool x) { int optval = x ? 1 : 0; if (setsockopt(GetSocket(), IPPROTO_IP, IP_PKTINFO, (char *)&optval, sizeof(optval)) == -1) { Handler().LogError(this, "setsockopt(IPPROTO_IP, IP_PKTINFO)", Errno, StrError(Errno), LOG_LEVEL_FATAL); return false; } return true; }
bool Socket::SetSoBsdcompat(bool x) { int optval = x ? 1 : 0; if (setsockopt(GetSocket(), SOL_SOCKET, SO_BSDCOMPAT, (char *)&optval, sizeof(optval)) == -1) { Handler().LogError(this, "setsockopt(SOL_SOCKET, SO_BSDCOMPAT)", Errno, StrError(Errno), LOG_LEVEL_FATAL); return false; } return true; }
bool Socket::SetIpRouterAlert(bool x) { int optval = x ? 1 : 0; if (setsockopt(GetSocket(), IPPROTO_IP, IP_ROUTER_ALERT, (char *)&optval, sizeof(optval)) == -1) { Handler().LogError(this, "setsockopt(IPPROTO_IP, IP_ROUTER_ALERT)", Errno, StrError(Errno), LOG_LEVEL_FATAL); return false; } return true; }
void JackNetInterface::SetRcvTimeOut() { if (!fSetTimeOut) { if (fSocket.SetTimeOut(fPacketTimeOut) == SOCKET_ERROR) { jack_error("Can't set rx timeout : %s", StrError(NET_ERROR_CODE)); return; } fSetTimeOut = true; } }
int Socket::IpMtu() { int mtu = 0; socklen_t len = sizeof(mtu); if (getsockopt(GetSocket(), IPPROTO_IP, IP_MTU, (char *)&mtu, &len) == -1) { Handler().LogError(this, "getsockopt(IPPROTO_IP, IP_MTU)", Errno, StrError(Errno), LOG_LEVEL_FATAL); } return mtu; }
bool Socket::SetSoNosigpipe(bool x) { int optval = x ? 1 : 0; if (setsockopt(GetSocket(), SOL_SOCKET, SO_NOSIGPIPE, (char *)&optval, sizeof(optval)) == -1) { Handler().LogError(this, "setsockopt(SOL_SOCKET, SO_NOSIGPIPE)", Errno, StrError(Errno), LOG_LEVEL_FATAL); return false; } return true; }
static void close_file(ioent_t *io) { // printf("close_file: enter\n"); // fflush(stdout); if (!CloseHandle(io->handle)) { perr(PERR_SYSTEM, "CloseHandle", StrError(GetLastError()), __FILE__, __LINE__); return ; } ioent_delete(io); }
void UdpSocket::SetMulticastTTL(int ttl) { if (GetSocket() == INVALID_SOCKET) { CreateConnection(); } if (setsockopt(GetSocket(), SOL_IP, IP_MULTICAST_TTL, (char *)&ttl, sizeof(int)) == -1) { Handler().LogError(this, "SetMulticastTTL", Errno, StrError(Errno), LOG_LEVEL_WARNING); } }