Beispiel #1
0
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
	char ssochead[1024];  
    ssochead[0] = '\0';  
	char receive_buff[102400];  
    unsigned n_receive_len = 102400;  
    receive_buff[0] = '\0';
	char* strIP = (char*)lpParam;

	//init socket
	if(soc_init(strIP,ssochead) != 0)
	{
		return -1;
	}
	//connect
	SOCKET sock = soc_connect(strIP);
	if(sock == NULL)
	{
		return -1;
	}
	//send HTTP head
	int nret = soc_send(sock,ssochead,strlen(ssochead));
	if(nret < 0)
	{
		return -2;
	}
	// recv
	soc_recv(sock);
	//close
	soc_close(sock);
}
kal_int32 ntyHttpSend(int sockId, U8 *buffer, int length) {
	kal_int32	ret_val;

	ret_val = soc_send(sockId, buffer, length, 0);
	
	if (ret_val > 0) {
		return ret_val;
	} else if (ret_val == SOC_WOULDBLOCK||ret_val == 0) {
		return 0;
	} else {
		return -1;
	}
}
Beispiel #3
0
int main(int argc, char *argv[]) {

  command_number number;
  typedef enum {None, Udp, Tcp, Ipm} proto_list;
  proto_list proto;
  soc_token soc = NULL;
  int port_no;
  char buff[500];
  request_message_t request;
  report_message_t report;
  int i, n, res;
  soc_host  my_host;
  soc_port  my_port;


  /* printf("Req_size: %d\n", sizeof(request_message_t)); */
  /* printf("Rep_size: %d\n", sizeof(report_message_t)); */

  proto = None;
  if (argc == 4) {
    if (strcmp (argv[1], "-u") == 0) {
      proto = Udp;
    } else if (strcmp (argv[1], "-t") == 0) {
      proto = Tcp;
    } else if (strcmp (argv[1], "-m") == 0) {
      proto = Ipm;
    }
  }
  if (proto == None) {
    fprintf(stderr, "Error. Three args -u/-t/-m <hostname> <port_name/num> expected.\n");
    exit(1);
  }

  /* Socket stuff */
  res = soc_open(&soc, (proto == Tcp ? tcp_header_socket : udp_socket));
  if (res != SOC_OK) {
    perror("soc_open");
    terror("soc_open", res);
    exit(1);
  }

  /* Dest to host (tcp/udp) or lan (ipm) */
  port_no = atoi(argv[3]);
  if (port_no <= 0) {
    res  = soc_set_dest_name_service(soc, argv[2], proto == Ipm, argv[3]);
    if (res != SOC_OK) {
      perror("soc_set_dest_name_service");
      terror("soc_set_dest_name_service", res);
      exit (1);
    }
  } else {
    res  = soc_set_dest_name_port(soc, argv[2], proto == Ipm, port_no);
    if (res != SOC_OK) {
      perror("soc_set_dest_name_port");
      terror("soc_open", res);
      exit (1);
    }
  }
  if (proto != Tcp) {
    res = soc_link_dynamic(soc);
    if (res != SOC_OK) {
      perror("soc_link_dynamic");
      terror("soc_link_dynamic", res);
      exit(1);
    }
  }
  res = soc_set_blocking(soc, FALSE);
  if (res != SOC_OK) {
    perror("soc_set_blocking");
    terror("soc_set_blocking", res);
    exit(1);
  }

  /* Get current host and port */
  res = soc_get_local_host_id(&my_host);
  if (res != SOC_OK) {
    perror("soc_get_local_host");
    terror("soc_get_local_host", res);
    exit(1);
  }
  my_port = 0;
  if (proto != Tcp) {
    res = soc_get_linked_port(soc, &my_port);
    if (res != SOC_OK) {
      perror("soc_get_linked_port");
      terror("soc_get_linked_port", res);
      exit(1);
    }
  }
  printf ("I am host %u port %u\n", my_host.integer, my_port);

  number = 0;
  for (;;) {
    printf ("\n");

    printf ("Start Kill Exit Ping Read (s k e p r) ? ");
    i = get_line (NULL, buff, sizeof(buff));

    if (strcmp(buff, "s") == 0) {
      /* Start */
      memset(request.start_req.command_text, 0,
             sizeof(request.start_req.command_text));
      memset(request.start_req.environ_variables, 0,
             sizeof(request.start_req.environ_variables));
      request.kind = start_command;
      printf ("Number: %d\n", number);
      request.start_req.number = number;
      number += 1;
      printf ("Command ? ");
      i = get_line (NULL, request.start_req.command_text,
                  sizeof(request.start_req.command_text));
      for (;;) {
        printf ("Argument (Empty to end) ? ");
        i = get_line (NULL, buff, sizeof(buff));
        if (i == 0) break;
        cat_str (request.start_req.command_text, buff);
      }
      for (n = 1;;n++) {
        printf ("Environ (Empty to end) ? ");
        i = get_line (NULL, buff, sizeof(buff));
        if (i == 0) break;
        if (n == 1) {
          strcpy (request.start_req.environ_variables, buff);
        } else {
          cat_str (request.start_req.environ_variables, buff);
        }
      }

      printf ("Current dir ? ");
      i = get_line (NULL, request.start_req.currend_dir,
                    sizeof(request.start_req.currend_dir));

      printf ("Output flow ? ");
      i = get_line (NULL, request.start_req.output_flow,
                    sizeof(request.start_req.output_flow));
      if (i != 0) {
        for (;;) {
          printf ("  Append (t or [f]) ? ");
          i = get_line (NULL, buff, sizeof(buff));
          if (strcmp(buff, "t") == 0) {
            request.start_req.append_output = 1;
            break;
          } else if ( (i == 0) || (strcmp(buff, "f") == 0) ) {
            request.start_req.append_output = 0;
            break;
          }
        }
      }

      printf ("Error flow ? ");
      i = get_line (NULL, request.start_req.error_flow,
                    sizeof(request.start_req.error_flow));
      if (i != 0) {
        for (;;) {
          printf ("  Append (t or [f]) ? ");
          i = get_line (NULL, buff, sizeof(buff));
          if (strcmp(buff, "t") == 0) {
            request.start_req.append_error = 1;
            break;
          } else if ( (i == 0) || (strcmp(buff, "f") == 0) ) {
            request.start_req.append_error = 0;
            break;
          }
        }
      }

      res = soc_send(soc, (char*)&request, sizeof(request));
      if (res != SOC_OK) {
        perror("soc_send");
        terror("soc_send", res);
      }

    } else if (strcmp(buff, "k") == 0) {
      /* Kill */
      request.kind = kill_command;
      for (;;) {
        printf ("Number ? ");
        i = get_line (NULL, buff, sizeof(buff));
        i = atoi(buff);
        if (i >= 0) break;
      }
      request.start_req.number = i;
      for (;;) {
        printf ("Signal ? ");
        i = get_line (NULL, buff, sizeof(buff));
        i = atoi(buff);
        if (i >= 0) break;
      }
      request.kill_req.signal_number = i;

      res = soc_send(soc, (char*)&request, sizeof(request));
      if (res != SOC_OK) {
        perror("soc_send");
        terror("soc_send", res);
      }

    } else if (strcmp(buff, "e") == 0) {
      /* Exit */
      request.kind = fexit_command;
      for (;;) {
        printf ("Exit code ? ");
        i = get_line (NULL, buff, sizeof(buff));
        i = atoi(buff);
        if (i >= 0) break;
      }
      request.fexit_req.exit_code = i;

      res = soc_send(soc, (char*)&request, sizeof(request));
      if (res != SOC_OK) {
        perror("soc_send");
        terror("soc_send", res);
      }

    } else if (strcmp(buff, "p") == 0) {
      /* Exit */
      request.kind = ping_command;

      res = soc_send(soc, (char*)&request, sizeof(request));
      if (res != SOC_OK) {
        perror("soc_send");
        terror("soc_send", res);
      }

    } else if (strcmp(buff, "r") == 0) {
      /* Read report */
      n = soc_receive(soc, &report, sizeof(report), FALSE);
      if (n == sizeof(report)) {
        switch (report.kind) {
          case start_report :
            printf ("Start: command %d pid %d\n", report.start_rep.number,
                    report.start_rep.started_pid);
          break;
          case kill_report :
            printf ("Kill: command %d pid %d\n", report.kill_rep.number,
                    report.kill_rep.killed_pid);
          break;
          case exit_report :
            printf ("Exit: command %d pid %d code %d ", report.exit_rep.number,
                    report.exit_rep.exit_pid, report.exit_rep.exit_status);
            if (WIFEXITED(report.exit_rep.exit_status)) {
              printf ("exit normally code %d\n",
                      WEXITSTATUS(report.exit_rep.exit_status));
            } else if (WIFSIGNALED(report.exit_rep.exit_status)) {
              printf ("exit on signal %d\n",
                      WTERMSIG(report.exit_rep.exit_status));
            } else {
              printf ("stopped on signal %d\n",
                      WSTOPSIG(report.exit_rep.exit_status));
            }
          break;
          case fexit_report :
            printf ("Forker exited\n");
          break;
          case pong_report :
            printf ("Pong\n");
          break;
          default :
           printf ("Unknown report kind\n");
          break;
        }
      } else if (n == SOC_WOULD_BLOCK) {
        printf ("No report\n");
      } else if (n == SOC_READ_0) {
        printf ("Disconnected\n");
        exit (0);
      } else {
        perror("soc_receive");
      }
    }
  }
}
Beispiel #4
0
int main (int argc, char *argv[]) {

  timeout_t timeout;
  int timeout_ms;
  int accuracy_ms;
  soc_token soc = init_soc;
  soc_port port_no;
  char lan_name[80];
  synchro_msg_t synchro_msg;
  soc_length length;
  int cr, fd;
  boolean read;
  unsigned int travel_ms;

  timeout_t request_time, reply_time, travel_delta;
  timeout_t accuracy_timeout, wait_timeout;


  if ( (argc != 2) && (argc != 3) && (argc != 4) ) {
    fprintf (stderr, "SYNTAX error : Wrong number of arguments\n");
    USAGE();
    exit (1);
  }

  if (argc >= 3) {
    timeout_ms = atoi(argv[2]);
    if (timeout_ms <= 0) {
      fprintf (stderr, "SYNTAX error : Wrong timeout value\n");
      USAGE();
      exit (1);
    }
  } else {
    timeout_ms = DEFAULT_TIMEOUT_MS;
  }
  wait_timeout.tv_sec = timeout_ms / 1000; 
  wait_timeout.tv_usec = (timeout_ms % 1000) * 1000;

  if (argc == 4) {
    accuracy_ms = atoi(argv[3]);
    if (accuracy_ms <= 0) {
      fprintf (stderr, "SYNTAX error : Wrong accuracy value\n");
      USAGE();
      exit (1);
    }
  } else {
    accuracy_ms = DEFAULT_ACCURACY_MS;
  }
  accuracy_timeout.tv_sec = accuracy_ms / 1000;
  accuracy_timeout.tv_usec = (accuracy_ms % 1000) * 1000;


  if (soc_get_local_lan_name(lan_name, sizeof(lan_name)) != SOC_OK) {
    perror ("getting lan name");
    exit (1);
  }

  if (soc_open(&soc, udp_socket) != SOC_OK) {
    perror ("opening socket");
    exit (1);
  }

  port_no = atoi(argv[1]);
  if (port_no <= 0) {
    if (soc_set_dest_name_service(soc, lan_name, true, argv[1]) != SOC_OK) {
      perror ("setting destination service");
      exit (1);
    }
  } else {
    if (soc_set_dest_name_port(soc, lan_name, true, port_no) != SOC_OK) {
      perror ("setting destination port");
      exit (1);
    }
  }

  if (soc_get_dest_port(soc, &port_no) != SOC_OK) {
    perror ("getting destination port no");
    exit (1);
  }

  if (soc_link_port(soc, port_no) != SOC_OK) {
    perror ("linking socket");
    exit (1);
  }

  /* List for wait */
  if (soc_get_id(soc, &fd) != SOC_OK) {
    perror ("getting socket id");
    exit (1);
  }
  if (evt_add_fd(fd, TRUE) != WAIT_OK) {
    perror("Adding fd");
    exit (1);
  }

  for (;;) {
    synchro_msg.magic_number = magic_request_value;
    get_time (&(synchro_msg.request_time));

    cr = soc_send (soc, (soc_message) &synchro_msg, sizeof(synchro_msg));
    if (cr != SOC_OK) {
      perror ("sending request");
      exit (1);
    }

    request_time = synchro_msg.request_time;

    for (;;) {

      timeout = wait_timeout;
      if (evt_wait (&fd, &read, &timeout) != WAIT_OK) {
        perror ("waiting for event");
        exit (1);
      }

      if (fd == NO_EVENT) {
        printf ("Timeout expired. Giving up.\n");
        exit (2);
      } else if (fd <= 0) {
        /* Other non fd event */
        continue;
      }

      length = sizeof (synchro_msg);
      cr = soc_receive (soc, (soc_message) &synchro_msg, length, FALSE);
      get_time (&reply_time);

      if ( cr == sizeof (synchro_msg) ) {
        if (synchro_msg.magic_number == magic_request_value) {
          ;
        } else if ( (synchro_msg.magic_number == magic_reply_value)
                 && (synchro_msg.request_time.tv_sec == request_time.tv_sec)
                 && (synchro_msg.request_time.tv_usec == request_time.tv_usec) ) {

          travel_delta = reply_time;
          (void) sub_time (&travel_delta, &request_time);
          if (comp_time(&travel_delta, &accuracy_timeout) > 0) {
              printf ("Insuficient accuracy. Skipping...\n");
              break;
          }

          /* Compute time (reply + travel/2) and settimofday */
          travel_ms = travel_delta.tv_sec * 1000;
          travel_ms += travel_delta.tv_usec / 1000;
          incr_time (&synchro_msg.server_time, travel_ms / 2);

          (void) sub_time (&reply_time, &synchro_msg.server_time);
          printf ("Synchro %ld.%06d s\n", reply_time.tv_sec,
                                         (int)reply_time.tv_usec);

          if (settimeofday (&synchro_msg.server_time, NULL) < 0) {
             perror ("settimeofday");
             exit (1);
          }
          exit (0);
        } else {
          fprintf (stderr, "Error : wrong reply received");
        }

      } else if (cr != SOC_OK) {
        perror ("receiving reply");
      } else {
        fprintf (stderr, "Error : wrong reply received");
      }
    }

    /* Reset dest */
    if (soc_set_dest_name_port(soc, lan_name, true, port_no) != SOC_OK) {
      perror ("linking socket");
      exit (1);
    }
  }
}
Beispiel #5
0
//-----------------------------------------------------------------------------
int gps_soc_tcp_send_request2(void)
{
	kal_uint8 val = 1; // non-blocking mode, default
	kal_int32 res;
	kal_int8 error; 
	kal_int32 detail_cause;
	kal_bool set_header = KAL_FALSE;

	gps_soc_transaction.socket_id = soc_create(PF_INET, SOCK_STREAM, 0, MOD_GPS_TCPIP, gps_soc_transaction.nwt_acount_id);
	gps_soc_log("soc_create, socket_id: %d", gps_soc_transaction.socket_id);
	if (gps_soc_transaction.socket_id < 0)
	{
		if (gps_soc_transaction.callback != NULL)
		{
	    	gps_soc_transaction.callback(0, NULL, 0);
		}
		return GPS_SOC_ERROR;
	}

	val = 0;	// blocking mode
	res = soc_setsockopt(gps_soc_transaction.socket_id, SOC_NBIO, &val, sizeof(val));
	gps_soc_log("soc_setsockopt, val: %d, result: %d", val, res);
	if (res < 0)
	{
		goto ERROR_HANDLE;
	}
	val = SOC_READ | SOC_WRITE | SOC_CLOSE | SOC_CONNECT;
	res = soc_setsockopt(gps_soc_transaction.socket_id, SOC_ASYNC, &val, sizeof(val));
	gps_soc_log("soc_setsockopt, val: %d, result: %d", val, res);
	if (res < 0)
	{
		goto ERROR_HANDLE;
	}
	
	gps_soc_log("Connect to %d.%d.%d.%d and port: %d",
				    gps_soc_transaction.server_ip_addr.addr[0],
				    gps_soc_transaction.server_ip_addr.addr[1],
				    gps_soc_transaction.server_ip_addr.addr[2],
				    gps_soc_transaction.server_ip_addr.addr[3],
				    gps_soc_transaction.server_ip_addr.port);
	
	res = soc_connect(gps_soc_transaction.socket_id, &gps_soc_transaction.server_ip_addr);
	gps_soc_log("soc_connect res: %d", res);
	if (res < 0)
	{
		res = soc_get_last_error(gps_soc_transaction.socket_id, &error, &detail_cause);
		gps_soc_log("res: %d soc_connect error: %d, detail_cause: %d", 
					res, error, detail_cause);

		goto ERROR_HANDLE;
	}
	
	while (gps_soc_transaction.snd_counter < gps_soc_transaction.snd_data_len)
	{
		const GPS_GPRMC_Packed_Struct_t *pPack;
		char *pBuff;
		int i, count;

		pPack = (GPS_GPRMC_Packed_Struct_t *)gps_soc_transaction.rcvd_buffer;
		pPack += gps_soc_transaction.snd_counter;
		pBuff = gps_soc_transaction.snd_buffer;
		pBuff[0] = '\0';
		if (!set_header)
		{
			sprintf(pBuff, "#%s#%s#%s#%s#%d\r\n", 
					gps_imei_str,
					gps_gprs_username, 
					gps_gprs_userpwd, 
					gps_soc_upldtype_str(gps_soc_transaction.cause_type),
					gps_soc_transaction.snd_data_len);
			set_header = KAL_TRUE;
		}
		count = (gps_soc_transaction.snd_data_len - gps_soc_transaction.snd_counter) <= GPS_SEND_ITEMS_ONETIME ?
				(gps_soc_transaction.snd_data_len - gps_soc_transaction.snd_counter) :
				GPS_SEND_ITEMS_ONETIME;
		for (i = 0; i < count; i++)
		{
		    Result_t result = RESULT_ERROR;

			pBuff = gps_soc_transaction.snd_buffer + strlen(gps_soc_transaction.snd_buffer);
			sprintf(pBuff, "#");
			pBuff = gps_soc_transaction.snd_buffer + strlen(gps_soc_transaction.snd_buffer);
			sprintf(pBuff, "%04x%04x", pPack->lac, pPack->cid) ;
			pBuff = gps_soc_transaction.snd_buffer + strlen(gps_soc_transaction.snd_buffer);
			result = GPS_APP_GPRMC_Packed2Str(pBuff, pPack);
			pPack++;
		}

		if (gps_soc_transaction.snd_counter + count >= (kal_int32) gps_soc_transaction.snd_data_len)
		{
			pBuff = gps_soc_transaction.snd_buffer + strlen(gps_soc_transaction.snd_buffer);
			sprintf(pBuff, "##\r\n");
		}

	    gps_soc_log("send data len: %d, data: %s", 
		   			strlen(gps_soc_transaction.snd_buffer),
		   			gps_soc_transaction.snd_buffer);
		res = soc_send(gps_soc_transaction.socket_id, 
					   (kal_uint8*)gps_soc_transaction.snd_buffer, 
					   strlen(gps_soc_transaction.snd_buffer), 
					   0);
		gps_soc_log("Http send request result, sent_bytes: %d", res);
		if (res < 0)
		{
			res = soc_get_last_error(gps_soc_transaction.socket_id, &error, &detail_cause);
			gps_soc_log("res: %d soc_send error: %d, detail_cause: %d", 
						res, error, detail_cause);
			break;
		}
		kal_sleep_task(250);

		gps_soc_transaction.snd_counter += count;
	}
  
ERROR_HANDLE:
	if (gps_soc_transaction.callback != NULL)
	{
	   	gps_soc_transaction.callback(0, NULL, 0);
	}

	kal_sleep_task(250);
	res = soc_close(gps_soc_transaction.socket_id);
	gps_soc_log("soc_close res: %d", res);
	if (res < 0)
	{
		res = soc_get_last_error(gps_soc_transaction.socket_id, &error, &detail_cause);
		gps_soc_log("res: %d soc_close error: %d, detail_cause: %d", 
					res, error, detail_cause);
	}
	kal_sleep_task(250);

	soc_close_nwk_account_by_id(MOD_GPS_TCPIP, gps_soc_transaction.nwt_acount_id);

	return GPS_SOC_SUCCESS;
}
Beispiel #6
0
//-----------------------------------------------------------------------------
int gps_soc_tcp_send_request(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    gps_soc_log("gps_soc_tcp_send_request, transaction state: %d", gps_soc_transaction.state);

    if (gps_soc_transaction.state == GPS_SOC_TCP_CON_CREATING)
    {
        kal_int8 ret;

        gps_soc_log("connect to %d.%d,%d,%d and port: %d",
            gps_soc_transaction.server_ip_addr.addr[0],
            gps_soc_transaction.server_ip_addr.addr[1],
            gps_soc_transaction.server_ip_addr.addr[2],
            gps_soc_transaction.server_ip_addr.addr[3],
            gps_soc_transaction.server_ip_addr.port);

        ret = soc_connect(gps_soc_transaction.socket_id, &gps_soc_transaction.server_ip_addr);
        gps_soc_log("connect result is %d", ret);

        if (ret == SOC_SUCCESS)
        {
            gps_soc_tcp_send_request();
            return GPS_SOC_SUCCESS;
        }
        else if (ret == SOC_WOULDBLOCK)
        {
            /* waits for socket notify */
            // msgId: MSG_ID_APP_SOC_NOTIFY_IND, 
            // it will be handled by gps_soc_socket_notify
            return GPS_SOC_SUCCESS;
        }
        else
        {
            if (ret == SOC_ERROR)
            {
                gps_soc_output_result(GPS_SOC_PEER_NOT_REACHABLE, NULL, 0);
                return GPS_SOC_PEER_NOT_REACHABLE;
            }
            else
            {
                gps_soc_output_result(GPS_SOC_ERROR, NULL, 0);
                return GPS_SOC_ERROR;
            }
        }
    }
    else if(gps_soc_transaction.state == GPS_SOC_TCP_CON_CREATED || 
			gps_soc_transaction.state == GPS_SOC_REQ_SEND_RETRY ||
            gps_soc_transaction.state == GPS_SOC_REQ_SENDING)
    {
        kal_int32 ret;
		const GPS_GPRMC_Packed_Struct_t *pPack;
		char *pBuff;
		int i, count;

		pBuff = gps_soc_transaction.snd_buffer;
		pBuff[0] = '\0';
        if (gps_soc_transaction.state != GPS_SOC_REQ_SENDING)
        {
            gps_soc_transaction.snd_counter = 0;
			sprintf(pBuff, "#%s#%s#%s#%s#%d\r\n", 
					gps_imei_str,
					gps_gprs_username, 
					gps_gprs_userpwd, 
					gps_soc_upldtype_str(gps_soc_transaction.cause_type),
					gps_soc_transaction.snd_data_len);
        }
        gps_soc_transaction.state = GPS_SOC_REQ_SENDING;
        gps_soc_log("send request to %d.%d,%d,%d and port: %d",
            gps_soc_transaction.server_ip_addr.addr[0],
            gps_soc_transaction.server_ip_addr.addr[1],
            gps_soc_transaction.server_ip_addr.addr[2],
            gps_soc_transaction.server_ip_addr.addr[3],
            gps_soc_transaction.server_ip_addr.port);

		pPack = (GPS_GPRMC_Packed_Struct_t *)gps_soc_transaction.rcvd_buffer;
		pPack += gps_soc_transaction.snd_counter;
		count = (gps_soc_transaction.snd_data_len - gps_soc_transaction.snd_counter) <= GPS_SEND_ITEMS_ONETIME ?
				(gps_soc_transaction.snd_data_len - gps_soc_transaction.snd_counter) :
				GPS_SEND_ITEMS_ONETIME;
		for (i = 0; i < count; i++)
		{
		    Result_t result = RESULT_ERROR;

			pBuff = gps_soc_transaction.snd_buffer + strlen(gps_soc_transaction.snd_buffer);
			sprintf(pBuff, "#") ;
			pBuff = gps_soc_transaction.snd_buffer + strlen(gps_soc_transaction.snd_buffer);
			sprintf(pBuff, "%04x%04x", pPack->lac, pPack->cid) ;
			pBuff = gps_soc_transaction.snd_buffer + strlen(gps_soc_transaction.snd_buffer);
			result = GPS_APP_GPRMC_Packed2Str(pBuff, pPack);
			pPack++;
		}

		if (gps_soc_transaction.snd_counter + count >= (kal_int32) gps_soc_transaction.snd_data_len)
		{
			pBuff = gps_soc_transaction.snd_buffer + strlen(gps_soc_transaction.snd_buffer);
			sprintf(pBuff, "##\r\n");
		}

    	gps_soc_log("send data len: %d, data: %s", 
	    			strlen(gps_soc_transaction.snd_buffer),
	    			gps_soc_transaction.snd_buffer);
        ret = soc_send(
                gps_soc_transaction.socket_id,
                (kal_uint8*)gps_soc_transaction.snd_buffer,
                strlen(gps_soc_transaction.snd_buffer),
                0);
        gps_soc_log("send request result, sent_bytes: %d", ret);

        if (ret > 0)
        {
            gps_soc_transaction.snd_counter += count;
            if (gps_soc_transaction.snd_counter >= (kal_int32) gps_soc_transaction.snd_data_len)
            {
                gps_soc_transaction.state = GPS_SOC_REQ_SENT;
            }
			kal_sleep_task(250);
			gps_soc_tcp_send_request();
            return GPS_SOC_SUCCESS;
        }
        else
        {
            if (ret == SOC_WOULDBLOCK)
            {
                /* waits for socket notify */
            	// msgId: MSG_ID_APP_SOC_NOTIFY_IND, 
            	// it will be handled by gps_soc_socket_notify
                return GPS_SOC_SUCCESS;
            }
            else
            {
                if (ret == SOC_ERROR)
                {
                    gps_soc_output_result(GPS_SOC_PEER_NOT_REACHABLE, NULL, 0);
                    return GPS_SOC_PEER_NOT_REACHABLE;
                }
                else
                {
                    gps_soc_output_result(GPS_SOC_ERROR, NULL, 0);
                    return GPS_SOC_ERROR;
                }
            }
        }
    }
    else if(gps_soc_transaction.state == GPS_SOC_REQ_SENT)
    {
        gps_soc_output_result(GPS_SOC_SUCCESS, NULL, 0);
        return GPS_SOC_SUCCESS;
    }
    else
    {
        gps_soc_output_result(GPS_SOC_ERROR, NULL, 0);
        return GPS_SOC_ERROR;
    }

    return GPS_SOC_ERROR;
}
Beispiel #7
0
/* THE MAIN */
int main (const int argc, const char * argv[]) {

  /* Socket data */
  soc_token soc = init_soc;
  soc_host lan;
  soc_port port;
  int soc_fd, fd;

  /* Socket message */
  msg_type msg;

  /* Times and timeouts */
  timeout_t start_time, end_time, current_time;
  timeout_t wait_timeout;
  double local_time, remote_time;

  /* Dynamic list of server infos */
  dlist list;
  info_type info;

  /* Utilities */
  boolean for_read;
  char buff[256];
  int res;
  char *index;

  /*********/
  /* Start */
  /*********/
  /* Save prog name */
  strcpy (prog, argv[0]);
  strcpy (prog, basename (prog));

  /*******************/
  /* Parse arguments */
  /*******************/
  /* Check args */
  if (argc != 2) {
    error ("Invalid argument");
  }
  /* Parse IPM address and port */
  strcpy (buff, argv[1]);
  index = strstr (buff, ":");
  if (index == NULL) {
     error ("Invalid argument");
  }
  *index = '\0';
  index++;
  if (soc_str2host (buff, &lan) != SOC_OK) {
    sprintf (buff, "Invalid ipm address %s", buff);
    error (buff);
  }
  if (soc_str2port (index, &port) != SOC_OK) {
    sprintf (buff, "Invalid port num %s", index);
    error (buff);
  }

  /**************/
  /* Initialize */
  /**************/
  /* Init dynamic list */
  dlist_init (& list, sizeof(info_type));
  /* Init socket */
  if (soc_open (&soc, udp_socket) != SOC_OK) {
    perror ("opening socket");
    error ("Socket initialization failed");
  }
  if (soc_set_dest_host_port (soc, &lan, port) != SOC_OK) {
    perror ("setting destination");
    error ("Socket initialization failed");
  }
  if (soc_link_port (soc, port) != SOC_OK) {
    perror ("linking to port");
    error ("Socket initialization failed");
  }
  if (soc_get_dest_host (soc, &lan) != SOC_OK) {
    perror ("getting dest lan");
    error ("Socket initialization failed");
  }
  if (soc_get_dest_port (soc, &port) != SOC_OK) {
    perror ("getting dest port");
    error ("Socket initialization failed");
  }
  /* Add socket to waiting point */
  if (soc_get_id(soc, &soc_fd) != SOC_OK) {
    perror ("getting socket id");
    error ("Socket initialization failed");

  }
  if (evt_add_fd(soc_fd, TRUE) != WAIT_OK) {
    perror("Adding fd");
    error ("Socket initialization failed");
  }
  /* Activate signal catching */
  activate_signal_handling();
  /* Report starting */
  buff[0]='\0';
  addr_image (&lan, buff);
  printf ("%s mcasting at address %s on port %d.\n", prog, buff, (int) port);
  /* Init times */
  get_time (&start_time);
  current_time = start_time;
  end_time = start_time;
  incr_time (&end_time, DELAY_CLIENT_MS);
  /* Send initial ping request */
  msg.ping = TRUE;
  msg.time = start_time;
  if (soc_send (soc, (soc_message) &msg, sizeof(msg)) != SOC_OK) {
    perror ("sending ping");
    error ("Sending ping request failed");
  }

  /*************/
  /* Main loop */
  /*************/
  for (;;) {
    /* First step is to loop until timeout */
    if (wait_timeout.tv_sec != -1) {
      wait_timeout = end_time;
      res = sub_time (&wait_timeout, &current_time);
      if (res <= 0) {
        break;
      }
    }
    if (evt_wait (&fd, &for_read, &wait_timeout) != WAIT_OK) {
      perror ("waiting for event");
      error ("Waiting for events failed");
    }
    if (! for_read) {
      error ("Write event received");
    }
    /* Termination signal */
    if (fd == SIG_EVENT) {
      if (get_signal () == SIG_TERMINATE) {
        break;
      }
    } else if (fd == NO_EVENT) {
      /* Timeout: first step ends with a dump of servers */
      if (dlist_length(&list) != 0) {
        dlist_rewind (&list, TRUE);
        for (;;) {
          dlist_read (&list, &info);
          /* Get host name if possible, else dump address */
          res = soc_host_name_of (&info.host, buff, sizeof(buff));
          if (res != SOC_OK) {
            buff[0]='\0';
            addr_image (&info.host, buff);
          }
          /* Compute (Start_time + Reception_time) / 2 */
          local_time = (time_to_double (&start_time)
                        + time_to_double (&info.reception_time) ) / 2.0;
          remote_time = time_to_double (&info.server_time);
          printf ("Host %s is shifted by %4.03fs\n", buff, remote_time - local_time);

          /* Done when last record has been put */
          if (dlist_get_pos (&list, FALSE) == 1) {
            break;
          }
          dlist_move (&list, TRUE);
        }
      }
      /* Now entering second step: infinite timeout */
      wait_timeout.tv_sec = -1;
      wait_timeout.tv_usec = -1;
      printf ("%s ready.\n", prog);
    } else if (fd != soc_fd) {
      sprintf (buff, "Invalid fd %d received", fd);
      error (buff);
    } else {
      /* Now this is the socket, read message */
      res = soc_receive (soc, (soc_message) &msg, sizeof(msg), TRUE);
      if (res < 0) {
        perror ("reading from socket");
        error ("Reading message failed");
      } else if (res != sizeof(msg)) {
        sprintf (buff, "Invalid size received, expected %d, got %d",
                       (int)sizeof(msg), res);
        error (buff);
      }
      get_time (&current_time);
      /* Client and server different behaviours */
      if ((wait_timeout.tv_sec != -1) && !msg.ping) {
        /* First step: store the address and time of server, if pong */
        if (soc_get_dest_host (soc, &(info.host)) != SOC_OK) {
          perror ("getting dest host");
          error ("Getting server address failed");
        }
        info.server_time = msg.time;
        info.reception_time = current_time;
        dlist_insert (&list, &info, TRUE);

      } else if ( (wait_timeout.tv_sec == -1) && msg.ping) {
        /* Second step: reply pong and time to ping */
        msg.time = current_time;
        msg.ping = FALSE;
        if (soc_send (soc, (soc_message) &msg, sizeof(msg)) != SOC_OK) {
          perror ("sending pong");
          error ("Sending pong request failed");
        }
      }
    }
  } /* End of main loop */


  /* Clean - Close */
  dlist_delete_all (&list);
  (void) evt_del_fd (soc_fd, TRUE);
  (void) soc_close (&soc);
  printf ("Done.\n");
  exit (0);
}
Beispiel #8
0
int main (int argc, char *argv[]) {
  boolean dest_lan;
  char host_name[1024];
  soc_host host_no;
  char port_name[256];
  soc_port port_no;
  soc_token soc = init_soc;
  char buffer[1024];
  int i, res, len;
  boolean interactive;

  /* Parse command line arguments */
  /* Syntax is udp_send lan/host <dest>:<port> [ <message> ] */
  if (argc < 3) error("Invalid number of arguments", "");

  if (strcmp(argv[1], "host") == 0) {
    dest_lan = false;
  } else if (strcmp(argv[1], "lan") == 0) {
    dest_lan = true;
  } else {
    error("Invalid argument", argv[1]);
  }
  interactive = (isatty(0) == 1);

  /* Locate ':' in dest:port */
  strcpy (host_name, argv[2]);
  res = -1;
  for (i = 0; i < (int) strlen(host_name); i++) {
    if (host_name[i] == ':') {
      if (res != -1) {
        error ("Invalid destination", host_name);
      }
      res = i;
    }
  }
  /* ':' must exist and not at beginning or end */
  if ( (res == -1) || (res == 0) || (res == (int)strlen (host_name) - 1) ) {
    error ("Invalid destination", host_name);
  }
  host_name[res] = '\0';
  strcpy (port_name, &host_name[res+1]);

  /* Create socket */
  res = soc_open(&soc, udp_socket);
  if (res != SOC_OK) {
      error("Socket creation", soc_error(res));
  }

  /* Set destination */
  if (soc_str2host (host_name, &host_no) == SOC_OK) {
    if (soc_str2port (port_name, &port_no) == SOC_OK) {
      res = soc_set_dest_host_port(soc, &host_no, port_no);
    } else {
      res = soc_set_dest_host_service (soc, &host_no, port_name);
    }
  } else {
    if (soc_str2port (port_name, &port_no) == SOC_OK) {
      res = soc_set_dest_name_port(soc, host_name, dest_lan, port_no);
    } else {
      res = soc_set_dest_name_service(soc, host_name, dest_lan, port_name);
    }
  }
  if (res != SOC_OK) {
    error("Setting destination", soc_error(res));
  }

  /* Link */
  if (soc_str2port (port_name, &port_no) == SOC_OK) {
    res = soc_link_port(soc, port_no);
  } else {
    res = soc_link_service (soc, port_name);
  }
  if (res != SOC_OK) {
    error("Linking to port", soc_error(res));
  }

  /* Build or read message */
  message[0] = '\0';
  if (argc > 3) {
    /* Concatenate arguments separated by spaces */
    for (i = 3; i < argc; i++) {
      if (i != 3) {
        strcat (message, " ");
      }
      strcat (message, argv[i]);
    }
    len = strlen(message);
  } else {
    /* Read from stdin */
    if (interactive) {
      printf ("Enter message to send (end with Ctrl-D): ");
      fflush (stdout);
    }
    len = 0;
    for (;;) {
      /* Read a buffer of data from stdin */
      res = (int) get_text (NULL, buffer, sizeof(buffer));
      if (res == 0) {
        /* End of input flow */
        break;
      }
      /* Concatenate to message */
      memmove (&message[len], buffer, res);
      len += res;
    }
    if (interactive) {
      printf ("\n");
    }
  }

  /* Send */
  res = soc_send (soc, message, (soc_length)len);
  if (res != SOC_OK) {
      error("Sending message", soc_error(res));
  }

  /* Receive */
  res = soc_set_blocking (soc, FALSE);
  sleep (1);
  for (;;) {
    res = soc_receive (soc, message, (soc_length)sizeof(message), TRUE);
    if (res > 0) {
      message[res] = '\0';
      printf ("-->%s<\n", message);
    } else {
      break;
    }
  }

  exit (0);
}