예제 #1
0
int udp_send(float *data, unsigned int length)
{
	int i, bytes_sent, return_flag;
	struct ifaddrs *ifa;
	char *msg;
	msg = malloc(FLEN*length); // allocate FLEN characters for each float in data
	if (sockfd == -1)
		set_up_socket();
	
	sprintf(msg, "%f", data[0]);
	// convert the array of floats into a string in msg
	// we have already turned the first element of data into a string, so we set i to 1
	for(i = 1; i<length; ++i) {
		// we copy the ith element of data into a buffer in which
		// there are 12 characters allocated for each element of data)
		sprintf(msg, "%s,%f", msg, data[i]);
	}
	
	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
		return_flag = 0;
		if (ifa->ifa_addr->sa_family==AF_INET) {
			struct sockaddr_in *ba = (struct sockaddr_in *)ifa->ifa_broadaddr;
			bytes_sent = sendto(sockfd, msg, strlen(msg), 0, (struct sockaddr *) ba, sizeof(struct sockaddr));
			if (bytes_sent == -1) {
				printf("Error: sendto function failed on interface: %s\n", ifa->ifa_name);
				return_flag = -1;
			}
		}
	}
	//printf("%s\n", msg);
	free(msg);
	return return_flag;
}
예제 #2
0
//gcc router.c -o router graph.c socket_open_read_write.c -w message_encode_decode.c -w
//./router port_number
int main(int argc, char *argv[]){
    /*Initializing globals*/
	memset(message_history_table,0,TABLE_SIZE);
    ROUTER_ID=atoi(argv[1]);
    message_count=0;
    
    //Setting up socket
	int port_number=ROUTER_BASE_PORT+ROUTER_ID;
    int my_socket=set_up_socket(port_number);
    char *msg_string;
    char c;
    msg *received_message;
	//listening now
    listen_again:
    initialize_graph();
    msg_string=read_from_socket(my_socket);
    received_message=decode_message(msg_string);    
   // free(msg_string);
    do_stuff(received_message);
   // printf("Press y to continue listening(you may have to press twice for assurance).n to kill the socket and exit.\n");
   // c=getchar();
   // c=getchar();
    //if(c=='y')
        goto listen_again;       
    close(my_socket);
    return 0; 

}
예제 #3
0
int main(int argc, char ** argv)
{
   int s = -1, s_new = -1, s_ctl = -1;
   connection_t conn;
   struct sockaddr_storage their_addr[2];
   u[0].storage = &their_addr[0];
   u[1].storage = &their_addr[1];
   socklen_t addr_size;
   struct pollfd fd;

   /* Parses input and sets the global variables */
   parse_input(argc, argv);

#ifdef _WIN32
   if ( daemonize )
      FreeConsole();
#else
   /* Should we fork and kill our parent? :p */
   if ( daemonize )
   {
      if ( debug )
         log_printf("Forking into background ...\n");
      int i = fork();
      if ( i < 0 ) exit(1);
      if ( i > 0 ) exit(0);
      /* Forking into background */
   }
#endif

#ifdef HAVE_SYSLOG
   if (use_syslog)
      openlog("rsd", 0, LOG_USER);
#endif

   /* Sets up listening socket */
   s = set_up_socket();

   if ( s < 0 )
   {
      log_printf("Couldn't set up listening socket. Exiting ...\n");
      exit(1);
   }

   // Need to have a global socket so that we can cleanly close the socket in the signal handling routines.
   listen_socket = s;

   if ( debug )
      log_printf("Listening for connection ...\n");

   fd.fd = s;
   fd.events = POLLIN;

   /* Set up listening socket */
   if ( listen(s, 10) == -1 )
   {
      log_printf("Couldn't listen for connections \"%s\"...\n", strerror(errno));
      exit(1);
   }

#ifdef _WIN32
   atexit(cleanup);
#else

   /* Sets up interface for cleanly shutting down the server */
   write_pid_file();
   signal(SIGINT, cleanup);
   signal(SIGTERM, cleanup);
   // SIGPIPE may cause trouble ;)
   signal(SIGPIPE, SIG_IGN);
#endif

   /* In case our backend API needs some initializing functions */
   initialize_audio();

#ifdef _WIN32
   	printf(	"==============================================================================\n"
			":: RSD server : Win32 : %s - Copyright (C) 2010-2011 Hans-Kristian Arntzen ::\n"
			"==============================================================================\n", RSD_VERSION);
#endif


   /* We're accepting two connects after each other, as we have one stream socket for audio data
      and one for controlling the server. Currently, the control socket is only useful for
      determining quickly when the client has hung up the connection. In case a control socket
      isn't supplied in a short time window (nmap, port scanners, etc),
      we shut down the connection. The connection, if accepted, will be handled in a new thread. */

   for(;;)
   {
      addr_size = sizeof (their_addr[0]);
      s_new = accept(s, u[0].addr, &addr_size);

      if ( s_new == -1 )
      {
         log_printf("Accepting failed... Errno: %d\n", errno);
         log_printf("%s\n", strerror( errno ) ); 
         continue;
      }

      /* Accepts a ctl socket. They have to come from same source. 
       * Times out very quickly (in case the server is being queried from an unknown source. */

      if (poll(&fd, 1, 200) < 0)
      {
         perror("poll");
         close(s_new);
         close(s_ctl);
         close(s);
         exit(1);
      }

      /* Accepts the connection if there's one pending */
      if (fd.revents & POLLIN)
      {
         addr_size = sizeof (their_addr[0]);
         s_ctl = accept(s, u[1].addr, &addr_size);
      }
      /* We didn't get a control socket, so we don't care about it :) 
         If s_ctl is 0, the backend will not perform any operations on it. */
      else 
      {
         if ( debug )
            log_printf("CTL-socket timed out. Ignoring CTL-socket. \n");

         s_ctl = 0;
      }

      if ( s_ctl == -1 )
      {
         close(s_new); s_new = -1;
         log_printf("%s\n", strerror( errno ) ); 
         continue;
      }

      /* Checks if they are from same source, if not, close the connection. */
      /* Check will be ignored if there is no ctl-socket active. */
      /* TODO: Security here is *retarded* :D */
      if ( (s_ctl > 0) && valid_ips(their_addr) < 0 )
      {
         close(s_new); s_new = -1;
         close(s_ctl); s_ctl = -1;
         continue;
      }

      conn.socket = s_new;
      conn.ctl_socket = s_ctl;
      new_sound_thread(conn);
      s_new = -1; // Makes sure that we cannot clutter the backend connection in any way.
      s_ctl = -1;
   }

   return 0;
}