Exemplo n.º 1
0
static ortp_socket_t create_socket(int local_port){
	struct sockaddr_in laddr;
	ortp_socket_t sock;
	int optval;
	sock=socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);
	if (sock<0) {
		ms_error("Fail to create socket");
		return -1;
	}
	memset (&laddr,0,sizeof(laddr));
	laddr.sin_family=AF_INET;
	laddr.sin_addr.s_addr=INADDR_ANY;
	laddr.sin_port=htons(local_port);
	if (bind(sock,(struct sockaddr*)&laddr,sizeof(laddr))<0){
		ms_error("Bind socket to 0.0.0.0:%i failed: %s",local_port,getSocketError());
		close_socket(sock);
		return -1;
	}
	optval=1;
	if (setsockopt (sock, SOL_SOCKET, SO_REUSEADDR,
				(SOCKET_OPTION_VALUE)&optval, sizeof (optval))<0){
		ms_warning("Fail to set SO_REUSEADDR");
	}
	set_non_blocking_socket(sock);
	return sock;
}
Exemplo n.º 2
0
		bool poll() {
			if(listeners.size()==0 && conns.size()==0)
				return false;
			// first see if we have any new connections
			for(auto l:listeners) {
				while(true) {
					struct sockaddr_in addr;
#ifdef _MSC_VER
					int addrsize=sizeof(addr);
#else
					socklen_t addrsize=sizeof(addr);
#endif
					int newsock=accept(l.first,(struct sockaddr*)&addr,&addrsize);
					if (newsock!=-1) {
						set_non_blocking_socket(newsock);
						conns.push_back(std::shared_ptr<tcpconn>(new tcpconn(input_buffer_size,output_buffer_size)));
						conns.back()->sock=newsock;
						conns.back()->want_input=true;
						conns.back()->conn.reset(l.second());
						continue;
					} else {
						break;
					}
				}
			}
			// now see if we have new data
			// TODO: rewrite this to use kevent,etc
			conns.erase(
				std::remove_if(
					conns.begin(),
					conns.end(),
					[this](std::shared_ptr<tcpconn> c) {
						//printf("running conn:%p\n",&c);
						if (!work_conn(*c)) {
							//printf("Wanting to remove conn!\n");
							closesocket(c->sock);
							return true;
						} else {
							return false;
						}
					}
				),
				conns.end()
			);
			return true; // change this somehow?
		}
Exemplo n.º 3
0
		bool listen(int port,std::function<connection*()> spawn) {
			int sock=-1;
			struct sockaddr_in sockaddr;
			if (-1==(sock=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))) {
				return true;
			}
			memset(&sockaddr,0,sizeof(sockaddr));
			sockaddr.sin_family=AF_INET;
			sockaddr.sin_addr.s_addr=0; // default addr
			sockaddr.sin_port=htons(port);
			if (bind(sock,(struct sockaddr*)&sockaddr,sizeof(sockaddr))) {
				closesocket(sock);
				return true;
			}
			if (::listen(sock,SOMAXCONN)) {
				closesocket(sock);
				return true;
			}
			set_non_blocking_socket(sock);
			listeners.push_back(std::make_pair(sock,spawn));
			return false;
		}
/** 
 * @brief Connects to the host
 * 
 * @param sess Session Context
 * 		TODO - Move allocation part to separate function
 * 		To enable one time allocation of server context
 * 
 * @return Error Code
 */
static inline rtsp_error_t rtspc_connect(rtsp_session_t *sess){

	struct addrinfo hints;
	struct addrinfo *result, *rp;
	int const service_len = 32;
	char service[service_len];
	int ret = -1;

	if(!sess){
		return (RTSP_E_INVALID_ARG);
	}

	/* 
	 * Function will be called only valid url structure is present
	 */
	service[service_len-1]= '\0';
	strcpy(service, "rtsp");

	memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = AF_UNSPEC;     /* IPv4 or IPv6 */
	hints.ai_flags = 0;
	hints.ai_protocol = 0;
	switch(sess->rtsp_url.type){
		case RTSP_NW_TCP:
			hints.ai_socktype = SOCK_STREAM;
			break;
		case RTSP_NW_STAR:
		case RTSP_NW_UDP:
		default:
			ERR_LOG("URI Not supported");
			return (RTSP_E_UNSUPPORTED);
			break;
	}

	if(sess->rtsp_url.port > 0){
		snprintf(service, service_len,"%u", sess->rtsp_url.port);
		hints.ai_flags = AI_NUMERICSERV;
	} else {
		/* Default rtsp service name will pick default port 
		 * configured in the system /etc/services
		 */
	}

	ret = getaddrinfo(sess->rtsp_url.host,service, &hints, &result);
	if (ret != 0) {
		ERR_LOG("getaddrinfo: %s\n", gai_strerror(ret));
		return (RTSP_E_FAILURE);
	}

	/*
	 * Connect to address
	 * get Socket fd
	 */
	for (rp = result; rp != NULL; rp = rp->ai_next){
		sess->sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
		if (sess->sfd == -1)
			continue;

		if (connect(sess->sfd, rp->ai_addr, rp->ai_addrlen) != -1)
			break;  
		/* Success */
		close(sess->sfd);
	}
	
	freeaddrinfo(result);

	if(rp == NULL){
		ERR_LOG("Could not connect to host '%s'", sess->rtsp_url.host);
		return (RTSP_E_FAILURE);
	}

	if(0 != set_non_blocking_socket (sess->sfd)){
		ERR_LOG("Cannot Set Socket as Non-Blocking");
		return (RTSP_E_FAILURE);
	}

	STAT_LOG("Connected to host '%s'", sess->rtsp_url.host);
	return (RTSP_E_SUCCESS);
}