Beispiel #1
0
//send the handshake to all our peers
void handshake_all(bt_args_t *args){
  int i;
  int sockfd;
  struct sockaddr_in sockaddr, handshake_addr;
  bt_msg_t msg; //message structure
  char *fname = args->bt_info->name;
  char *ip;
  int port;
  char init_stats[80];
  for (i=0;i<MAX_CONNECTIONS;i++){
    if (args->peers[i]){
      //initiate socket and send out handshake
      //keep track of the sockfd in the args struct
      if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
        perror("socket");
      }

      sockaddr = args->peers[i]->sockaddr;
      handshake_addr = args->peers[i]->sockaddr;

      if (connect(sockfd, (struct sockaddr *)&sockaddr, sizeof(struct sockaddr)) < 0){
        perror("connect");
        continue;
      }
      
      args->peers[i]->sockfd = sockfd; //backup the socket 
      ip = inet_ntoa(handshake_addr.sin_addr);
      port = ntohs(handshake_addr.sin_port);
      sprintf(init_stats, "HANDSHAKE INIT to peer: %s on port: %d\n", ip, port);
      LOGGER(args->log_file, 1, init_stats);
      if (seeder_handshake(sockfd, fname, args->id, handshake_addr)){ 
        sprintf(init_stats, "HANDSHAKE SUCCESS from peer: %s on port: %d\n", ip, port);
        LOGGER(args->log_file, 1, init_stats);
        args->sockets[i] = sockfd;
        args->poll_sockets[i].fd = sockfd;
        args->poll_sockets[i].events = POLLIN;
        
        //send our bitfield here
        msg.length = sizeof(bt_bitfield_t);
        msg.bt_type = BT_BITFIELD;
        msg.payload.bitfield = args->bitfield;
        send_to_peer(args->peers[i], &msg); //send out the message
        read_from_peer(args->peers[i], &msg, args); //listen for bitfield
        unchoke_message(&msg);
        send_to_peer(args->peers[i], &msg); //send out the unchoke message
        
      }
      else{
        sprintf(init_stats, "HANDSHAKE DECLINED from peer: %s on port: %d\n", ip, port);
        LOGGER(args->log_file, 1, init_stats);

      }
    }
  }
}
static inline int
shardcache_client_set_internal(shardcache_client_t *c, void *key, size_t klen, void *data, size_t dlen, uint32_t expire, int inx)
{
    int fd = -1;
    char *addr = select_node(c, key, klen, &fd);
    if (fd < 0) {
        c->errno = SHARDCACHE_CLIENT_ERROR_NETWORK;
        snprintf(c->errstr, sizeof(c->errstr), "Can't connect to '%s'", addr);
        return -1;
    }

    int rc = -1;
    if (inx)
        rc = add_to_peer(addr, (char *)c->auth, SHC_HDR_SIGNATURE_SIP, key, klen, data, dlen, expire, fd, 1);
    else
        rc = send_to_peer(addr, (char *)c->auth, SHC_HDR_SIGNATURE_SIP, key, klen, data, dlen, expire, fd, 1);

    if (rc == -1) {
        close(fd);
        c->errno = SHARDCACHE_CLIENT_ERROR_NODE;
        snprintf(c->errstr, sizeof(c->errstr), "Can't set new data on node '%s'", addr);
    } else {
        connections_pool_add(c->connections, addr, fd);
        c->errno = SHARDCACHE_CLIENT_OK;
        c->errstr[0] = 0;
    }
    return rc;
}
void *rx_from_ob_to_click_thread(void *p)
{
//pthread_mutex_lock(&(pluginInfo->pluginInfo_mutex));
//pthread_mutex_unlock(&(pluginInfo->pluginInfo_mutex));

  int readbytes;
  char buffer[BUFFERSIZE];
  int index = 0;
  struct packet_header *ph = NULL;

  int i;

  readbytes = 1;

  while( rx_running == 1 ) {
    if ( ph == NULL ) {
      if ( readbytes > 0 ) printf("Serial: Wait for new frame\n");
      
      readbytes = read_obd_serial(fd, &buffer[index], sizeof(struct packet_header) - index);  //read header
      
      if ( readbytes > 0 ) {
        printf("Serial: Read %d Bytes: ",readbytes);
        for ( i = 0; i < readbytes; i++) printf("%d ",buffer[index + i]); 
        printf("\n");
      
        index += readbytes;
        if ( index == sizeof(struct packet_header) ) {
          ph = (struct packet_header *)buffer;
        }
      }
    } else {
      readbytes = read_obd_serial(fd, &buffer[index], (ph->length - index + sizeof(struct packet_header)));
      
      if ( readbytes > 0 ) {
        printf("Serial: Read %d Bytes: ",readbytes);
        for ( i = 0; i < readbytes; i++) printf("%d ",buffer[index + i]); 
        printf("\n");

        index+=readbytes;
      
        if ( index == (ph->length + sizeof(struct packet_header)) ) {
          if ( ph->type == PACKET_DATA ) {
            send_to_peer(con, &buffer[sizeof(struct packet_header)], ph->length);
       	  } else if ( ph->type == DEBUG_PRINT ) {
      	    buffer[sizeof(struct packet_header) + ph->length] = 0;
      	    printf("%s",&(buffer[sizeof(struct packet_header)]));
	  }	
 
          index = 0;  
          ph = NULL;
        }
      }	
    }
  }
  
  pthread_exit(NULL);
}
Beispiel #4
0
void ProcessWAPIProtocol(int new_asue_socket)
{
	char *userID = "2";
	int asu_socket;
	int auth_result=FALSE;

	EAP_auth_active eap_auth_active_packet;
	EAP_access_auth_requ eap_access_auth_requ_packet;
	EAP_access_auth_resp eap_access_auth_resp_packet;

	EAP_certificate_auth_requ eap_certificate_auth_requ_packet;//New code
	EAP_certificate_auth_resp eap_certificate_auth_resp_packet;//New code
	
	Testmode = 1;
	tick();

	//1) ProcessWAPIProtocolAuthActive,send to asue
	if(annotation == 1)
		printf("\n***\n 1) 认证激活分组(网络硬盘录像机->摄像机): \n");
	else if(annotation == 2)
		printf("\n***\n 1) ProcessWAPIProtocolAuthActive: \n");

	memset((BYTE *)&eap_auth_active_packet, 0, sizeof(eap_auth_active_packet));
	eap_auth_active_packet.eap_header.code=1;
	eap_auth_active_packet.eap_header.identifier=0;
	eap_auth_active_packet.eap_header.length=sizeof(eap_auth_active_packet);
	eap_auth_active_packet.eap_header.type=192;

	ProcessWAPIProtocolAuthActive(userID, &eap_auth_active_packet.auth_active_packet);
	send_to_peer(new_asue_socket, (BYTE *)&eap_auth_active_packet, sizeof(eap_auth_active_packet));

	//2) ProcessWAPIProtocolAccessAuthRequest, recv from asue
	if (annotation == 1)
		printf("\n***\n 2) 接入认证请求分组(摄像机->网络硬盘录像机,网络硬盘录像机处理该分组): \n");
	else if (annotation == 2)
		printf("\n***\n 2) HandleWAPIProtocolAccessAuthRequest: \n");
	if (annotation == 2)
		printf("recv auth active packet from ASUE...\n");

	memset((BYTE *)&eap_access_auth_requ_packet, 0, sizeof(EAP_access_auth_requ));
	recv_from_peer(new_asue_socket, (BYTE *)&eap_access_auth_requ_packet, sizeof(eap_access_auth_requ_packet));
	//verify access_auth_requ_packet
	HandleWAPIProtocolAccessAuthRequest(userID, &eap_auth_active_packet.auth_active_packet, &eap_access_auth_requ_packet.access_auth_requ_packet);

	//3) ProcessWAPIProtocolCertAuthRequest, send to asu
	if (annotation == 1)
		printf("\n***\n 网络硬盘录像机开始连接认证服务器: \n");
	else if (annotation == 2)
		printf("\n***\n Connect to asu.\n");
    asu_socket = connect_to_asu();
	if (annotation == 1)
		printf("\n***\n 网络硬盘录像机连接认证服务器成功! \n");

	if (annotation == 1)
		printf("\n***\n 3) 证书认证请求分组(网络硬盘录像机->认证服务器): \n");
	else if (annotation == 2)
		printf("\n***\n 3) ProcessWAPIProtocolCertAuthRequest: \n");
	//stop for keyboard
	//getchar();
	
	memset((BYTE *)&eap_certificate_auth_requ_packet, 0, sizeof(eap_certificate_auth_requ_packet));//New code
	eap_certificate_auth_requ_packet.eap_header.code=1;//New code
	eap_certificate_auth_requ_packet.eap_header.identifier=2;//New code
	eap_certificate_auth_requ_packet.eap_header.length=sizeof(eap_certificate_auth_requ_packet);//New code
	eap_certificate_auth_requ_packet.eap_header.type=192;//New code
	

	ProcessWAPIProtocolCertAuthRequest(userID, &eap_access_auth_requ_packet.access_auth_requ_packet,&eap_certificate_auth_requ_packet.certificate_auth_requ_packet);
	send_to_peer(asu_socket,(BYTE *)&eap_certificate_auth_requ_packet, sizeof(eap_certificate_auth_requ_packet));

	//4) ProcessWAPIProtocolCertAuthResp, recv from asu
	if (annotation == 1)
		printf("\n***\n 4) 证书认证响应分组(认证服务器->网络硬盘录像机,认证服务器处理该分组): \n");
	else if (annotation == 2)
	{
		printf("\n***\n 4) HandleWAPIProtocolCertAuthResp: \n");
		printf("recv Cert Auth Resp packet from ASU...\n");
	}

	recv_from_peer(asu_socket, (BYTE *)&eap_certificate_auth_resp_packet, sizeof(eap_certificate_auth_resp_packet));
	memset((BYTE *)&eap_access_auth_resp_packet, 0, sizeof(eap_access_auth_resp_packet));

	//该函数的主要工作是查看证书验证结果,并填充接入认证响应分组
	auth_result = HandleProcessWAPIProtocolCertAuthResp(userID,&eap_certificate_auth_requ_packet.certificate_auth_requ_packet, &eap_certificate_auth_resp_packet.certificate_auth_resp_packet,&eap_access_auth_resp_packet.access_auth_resp_packet);

	//5) ProcessWAPIProtocolAccessAuthResp, send to asue
	if (annotation == 1)
		printf("\n***\n 5) 证书认证响应分组(认证服务器->网络硬盘录像机,网络硬盘录像机处理该分组): \n");
	else if (annotation == 2)
		printf("\n***\n 5) ProcessWAPIProtocolAccessAuthResp: \n");

	//stop for keyboard
	//getchar();


	eap_access_auth_resp_packet.eap_header.code=1;
	eap_access_auth_resp_packet.eap_header.identifier=1;
	eap_access_auth_resp_packet.eap_header.length=sizeof(eap_auth_active_packet);
	eap_access_auth_resp_packet.eap_header.type=192;

	ProcessWAPIProtocolAccessAuthResp(userID, &eap_access_auth_requ_packet.access_auth_requ_packet, &eap_access_auth_resp_packet.access_auth_resp_packet);
	send_to_peer(new_asue_socket, (BYTE *)&eap_access_auth_resp_packet, sizeof(eap_access_auth_resp_packet));

	tock();
	// pid is global variable

	//run ffmpeg
	if(auth_result){
		if(pid < 0){
			char abuf[INET_ADDRSTRLEN];
			struct sockaddr_in asueaddr;
			socklen_t length = sizeof(asueaddr);
			getpeername(new_asue_socket, (struct sockaddr*) &asueaddr, &length);
			inet_ntop(AF_INET, &asueaddr.sin_addr, abuf, INET_ADDRSTRLEN);
			printf("\n");
			char *ffmpeg_prog_dir="";//"/home/yaoyao/ffmpeg_sources/ffmpeg/";
			char ffmpeg_cmd[256];
			//snprintf(ffmpeg_cmd,255,"%sffmpeg -debug ts -i rtsp://%s:8557/PSIA/Streaming/channels/2?videoCodecType=H.264 -vcodec copy -an http://localhost:8090/feed1.ffm",ffmpeg_prog_dir, abuf);
			snprintf(ffmpeg_cmd, 255,
			"%sffmpeg -debug ts -i rtsp://192.168.115.40:8557/PSIA/Streaming/channels/2?videoCodecType=H.264 -vcodec copy -an http://localhost:8090/feed1.ffm >/dev/null 2>/dev/null",
			ffmpeg_prog_dir);
			
			printf("%s", ffmpeg_cmd);
			printf("\n");

			if((pid = fork()) < 0){
				perror("fork()");
			}else if(pid == 0){
				if(execl("/bin/sh", "sh", "-c", ffmpeg_cmd, (char *)0) < 0){
					perror("execl failed");
				}
				pid++;
			}else{}
		}
	}
	else{
		//int status;
		printf("kill %d\n",pid);
		kill(pid,SIGABRT);
		wait(NULL);
		pid++;
		printf("kill %d\n",pid);
		kill(pid,SIGABRT);
		wait(NULL);
		
		pid = -1;
	}

}