Esempio n. 1
0
int main(int argc, char *argv[]) {
  struct nodeID *local_node;
  struct cloud_helper_context *cloud_ctx;
  struct psample_context *ps_ctx;
  uint8_t buff[1024];

  local_node = net_helper_init("127.0.0.1", 1234, "");
  cloud_ctx = cloud_helper_init(local_node,
                "provder=delegate,delegate_lib=my_helper.so," \
                "param1=value1,...");
  ps_ctx = psample_init(local_node, NULL, 0,
                        "protocol=cloudcast,cache_size=25");

  /* Perform first cycle to initialize the protocol */
  psample_parse_data(con->ps_context, NULL, 0);
  while(1) {
    const struct timeval tout = {1, 0};
    int data_source = -1;
    int news = 0;

    /* Wait for incoming data either from peers or cloud */
    news = wait4any_threaded(local_node, cloud_ctx, &tout, NULL,
                             &data_source);
    if (news > 0) {
      struct nodeID *remote = NULL;
      int len = 0;

      /* Incoming data available, perform passive thread */
      if (data_source == DATA_SOURCE_NET) {
        len = recv_from_peer(local_node, &remote, buff, 1024);
      }
      else if (data_source == DATA_SOURCE_CLOUD) {
        len = recv_from_cloud(cloud_ctx, buff, 1024);
      }
      psample_parse_data(con->ps_context, buff, len);
      if (remote) nodeid_free(remote);
    } else {
      /* Let the peer sampler handles active cycles and
         sleep periods */
      psample_parse_data(ps_ctx, NULL, 0);
    }
  }
}
Esempio n. 2
0
static enum signaling_type sig_receive(struct nodeID *my_sock, struct nodeID **r, int *id, struct chunkID_set **cset)
{
    static uint8_t buff[BUFFSIZE];
    int ret;
    struct nodeID *owner;
    struct nodeID *remote;
    enum signaling_type sig_type;
    int max_deliver = 0;
    uint16_t trans_id = 0;

    ret = recv_from_peer(my_sock, &remote, buff, BUFFSIZE);

    /* TODO: Error check! */
    if (buff[0] != MSG_TYPE_SIGNALLING) {
      fprintf(stderr, "Wrong message type!!!\n");

      return -1;
    }
    fprintf(stdout,"Received message of %d bytes\n",ret);
    ret = parseSignaling(buff + 1, ret - 1, &owner, cset, &max_deliver, &trans_id, &sig_type);
    fprintf(stdout,"Parsed %d bytes: trans_id %d, Max_deliver %d\n", ret, trans_id, max_deliver);
    switch (sig_type) {
        case sig_accept:
            fprintf(stdout, "Accept of %d chunks\n", chunkID_set_size(*cset));
            break;
        case sig_deliver:
            fprintf(stdout, "Deliver of %d chunks\n", chunkID_set_size(*cset));
            break;
        case sig_send_buffermap:
            fprintf(stdout, "I received a buffer of %d chunks\n", chunkID_set_size(*cset));
            break;
    }
    if(owner)nodeid_free(owner);
    if (r) {
      *r = remote;
    } else {
      nodeid_free(remote);
    }
    if (id) *id = trans_id;

    return sig_type;
}
Esempio n. 3
0
static void *recv_loop(void *p)
{
  struct nodeID *s = p;
  int done = 0;
#define BUFFSIZE 1024
  static uint8_t buff[BUFFSIZE];
  
  while (!done) {
    int len;
    struct nodeID *remote;

    len = recv_from_peer(s, &remote, buff, BUFFSIZE);
    pthread_mutex_lock(&neigh_lock);
    psample_parse_data(context, buff, len);
    pthread_mutex_unlock(&neigh_lock);
    nodeid_free(remote);
  }

  return NULL;
}
Esempio n. 4
0
void *tx_from_click_to_ob_thread(void *p)
{
//pthread_mutex_lock(&(pluginInfo->pluginInfo_mutex));
//pthread_mutex_unlock(&(pluginInfo->pluginInfo_mutex));
  int readbytes,i;
  char buffer[BUFFERSIZE];
  int index = 0;
  struct packet_header *ph = (struct packet_header *)buffer;

  while( tx_running == 1 ) {
    readbytes = recv_from_peer(con, &buffer[sizeof(struct packet_header)], BUFFERSIZE - sizeof(struct packet_header));  //read header

    printf("SOCKET: Read %d Bytes: ",readbytes); 
    for ( i = 0; i < readbytes; i++) printf("%d ",buffer[sizeof(struct packet_header) + i]); 
    printf("\n");

    ph->type = PACKET_DATA;
    ph->length = readbytes;
    write_obd_serial(fd, buffer, ph->length + sizeof(struct packet_header));
  }

  pthread_exit(NULL);
}
Esempio n. 5
0
File: ae.c Progetto: IoTsecurity/ae
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;
	}

}
Esempio n. 6
0
static void loop(struct context *con)
{
  int done = 0;
#define BUFFSIZE 1024
  static uint8_t buff[BUFFSIZE];
  int cnt = 0;

  psample_parse_data(con->ps_context, NULL, 0);
  while (!done) {
    int len;
    int news;
    int data_source;
    const struct timeval tout = {1, 0};
    struct timeval t1;

    t1 = tout;
    news = wait4any_threaded(con->net_context, con->cloud_context, &t1, NULL, &data_source);
    if (news > 0) {
      struct nodeID *remote = NULL;
      //      printf("Got data from: %d\n", data_source);
      if (data_source == DATA_SOURCE_NET)
        len = recv_from_peer(con->net_context, &remote, buff, BUFFSIZE);
      else if (data_source == DATA_SOURCE_CLOUD)
        len = recv_from_cloud(con->cloud_context, buff, BUFFSIZE);
      psample_parse_data(con->ps_context, buff, len);
      if (remote) nodeid_free(remote);
    } else {
      if (psample_parse_data(con->ps_context, NULL, 0) != 0){
        fprintf(stderr, "Error parsing data... quitting\n");
        exit(1);
      }
      if (cnt % 10 == 0) {
        const struct nodeID *const *neighbourhoods;
        char addr[256];
        int n, i;

        neighbourhoods = psample_get_cache(con->ps_context, &n);
        printf("I have %d neighbours:\n", n);
        for (i = 0; i < n; i++) {
          node_addr(neighbourhoods[i], addr, 256);
          printf("\t%d: %s\n", i, addr);
        }
        fflush(stdout);
        if (fprefix) {
          FILE *f;
          char fname[64];
          fprintf(stderr, "ci sono??? %s\n", fprefix);
          sprintf(fname, "%s-%d.txt", fprefix, port);
          f = fopen(fname, "w");
          if (f) fprintf(f, "#Cache size: %d\n", n);
          for (i = 0; i < n; i++) {
            node_addr(neighbourhoods[i], addr, 256);
            if (f) fprintf(f, "%d\t\t%d\t%s\n", port, i, addr);
          }
          fclose(f);
        }
      }
      cnt++;
    }
  }
}