/** * Initialize the UDP stream */ void udp_arch_periph_init(struct udp_periph* p, char* host, int port_out, int port_in, bool_t broadcast) { struct UdpNetwork* network = malloc(sizeof(struct UdpNetwork)); if (port_out >= 0) { // Create the output socket (enable reuse of the address, and broadcast if necessary) udp_create_socket(&network->socket_out, 0, TRUE, broadcast); // Setup the output address network->addr_out.sin_family = PF_INET; network->addr_out.sin_port = htons(port_out); network->addr_out.sin_addr.s_addr = inet_addr(host); } if (port_in >= 0) { // Creat the input socket (enable reuse of the address, and disable broadcast) udp_create_socket(&network->socket_in, 0, TRUE, FALSE); // Create the input address network->addr_in.sin_family = PF_INET; network->addr_in.sin_port = htons(port_in); network->addr_in.sin_addr.s_addr = htonl(INADDR_ANY); bind(network->socket_in, (struct sockaddr*)&network->addr_in, sizeof(network->addr_in)); } p->network = (void*)network; }
driver_dns_t *driver_dns_create(select_group_t *group, char *domain, dns_type_t type) { driver_dns_t *driver_dns = (driver_dns_t*) safe_malloc(sizeof(driver_dns_t)); /* Create the actual DNS socket. */ LOG_INFO("Creating UDP (DNS) socket"); driver_dns->s = udp_create_socket(0, "0.0.0.0"); if(driver_dns->s == -1) { LOG_FATAL("Couldn't create UDP socket!"); exit(1); } /* Set the domain and stuff. */ driver_dns->domain = domain; driver_dns->type = type; /* If it succeeds, add it to the select_group */ select_group_add_socket(group, driver_dns->s, SOCKET_TYPE_STREAM, driver_dns); select_set_recv(group, driver_dns->s, recv_socket_callback); select_set_closed(group, driver_dns->s, dns_data_closed); /* Subscribe to the messages we care about. */ message_subscribe(MESSAGE_PACKET_OUT, handle_message, driver_dns); /* TODO: Do I still need this? */ message_post_config_int("max_packet_length", MAX_DNSCAT_LENGTH(driver_dns->domain)); return driver_dns; }
int main(int argc, char** argv) { struct sockaddr_in server; struct sockaddr_in client; char buffer[BUFSIZE]; socklen_t len_client; int transfered = 0; char* msg; pthread_t thread_tweet_fetcher; if (argc < 2) { printf("USAGE: %s [port]\n", argv[0]); return 0; } atexit(at_exit); set_signal_handlers(); T = twitter_new(); printf("Reading tweets...\n"); fetch_tweets(T, 15); pthread_create(&thread_tweet_fetcher, NULL, thread_fetch_tweets, NULL); printf("Opening server...\n"); sock = udp_create_socket(&server, sizeof(server), htonl(INADDR_ANY), atoi(argv[1]), 0); if (sock < 0) error_and_exit("cannot create socket", __FILE__, __LINE__); if (bind(sock, (struct sockaddr*)&server, sizeof(server)) < 0) error_and_exit("Cannot bind to socket", __FILE__, __LINE__); len_client = sizeof(client); for (;;) { transfered = udp_receive(sock, buffer, BUFSIZE, &client, len_client); switch (transfered) { case -1: error_and_exit("Failed to receive message", __FILE__, __LINE__); case 0: error_and_exit("Client has shut down...", __FILE__, __LINE__); } msg = get_tweet(T); transfered = udp_send(sock, msg, &client, sizeof(client)); free(msg); if (!transfered) error_and_exit("Sending has failed!", __FILE__, __LINE__); } close(sock); return 0; }
/* * Setup UDP client socket and bind to it. * * Return bound socket. */ static int dns_client_setup(int port, char *ip) { int ret, sock; sock = udp_create_socket(); if (sock < 0) { goto error; } ret = udp_bind_socket(sock, port, ip); if (ret < 0) { goto error; } return sock; error: return -1; }
static void *udp_intertask_interface(void *args_p) { while(1) { MessageDef *receivedMessage; receive_msg(TASK_UDP, &receivedMessage); switch(receivedMessage->messageId) { case UDP_INIT: { UdpInit *init_p; init_p = &receivedMessage->msg.udpInit; udp_create_socket(init_p->port, init_p->address); } break; case UDP_DATA_REQ: { UdpDataReq *udp_data_req_p; struct sockaddr_in peer_addr; udp_data_req_p = &receivedMessage->msg.udpDataReq; memset(&peer_addr, 0, sizeof(struct sockaddr_in)); peer_addr.sin_family = AF_INET; peer_addr.sin_port = htons(udp_data_req_p->peer_port); peer_addr.sin_addr.s_addr = (udp_data_req_p->peer_address); UDP_DEBUG("Sending message of size %u to "IPV4_ADDR" and port %u\n", udp_data_req_p->buffer_length, IPV4_ADDR_FORMAT(udp_data_req_p->peer_address), udp_data_req_p->peer_port); sendto(udp_fd, udp_data_req_p->buffer, udp_data_req_p->buffer_length, 0, (struct sockaddr *)&peer_addr, sizeof(struct sockaddr_in)); } break; default: { UDP_DEBUG("Unknown message ID %d\n", receivedMessage->messageId); } break; } free(receivedMessage); receivedMessage = NULL; } return NULL; }
int main(int argc, char *argv[]) { settings_t *s = safe_malloc(sizeof(settings_t)); char c; int option_index; const char *option_name; /* Build the long-options array for parsing the options. */ struct option long_options[] = { /* General options. */ {"help", no_argument, 0, 0}, /* Help. */ {"h", no_argument, 0, 0}, {"H", no_argument, 0, 0}, {"name", required_argument, 0, 0}, /* Name. */ {"n", required_argument, 0, 0}, {"port", required_argument, 0, 0}, /* Port. */ {"p", required_argument, 0, 0}, {"source", required_argument, 0, 0}, /* Source. */ {"s", required_argument, 0, 0}, {"username", no_argument, 0, 0}, /* Username. */ {"u", no_argument, 0, 0}, {"version", no_argument, 0, 0}, /* Version. */ {"V", no_argument, 0, 0}, {"wait", required_argument, 0, 0}, /* Wait time. */ {"w", required_argument, 0, 0}, /* Actions (exactly one has to be selected). */ {"sniff", optional_argument, 0, 1}, /* View traffic. */ {"poison", optional_argument, 0, 1}, /* Send poisoned responses. */ {"conflict", optional_argument, 0, 1}, /* Send conflict responses. */ {0, 0, 0, 0} }; /* Initialize Winsock (if we're on Windows). */ winsock_initialize(); /* Get ready to randomize. */ srand((unsigned int)time(NULL)); /* Clear the settings. */ memset(s, sizeof(s), 0); /* Set some defaults. */ s->port = 137; s->source = NULL; s->name = NULL; s->user = "******"; s->wait = -1; /* Catch SIGINT. */ signal(SIGINT, interrupt); /* Catch all exit events. */ atexit(cleanup); /* Parse the commandline options. */ opterr = 0; while((c = getopt_long_only(argc, argv, "", long_options, &option_index)) != EOF) { switch(c) { case 0: option_name = long_options[option_index].name; /* General options. */ if(!strcmp(option_name, "help") || !strcmp(option_name, "h") || !strcmp(option_name, "H")) { usage(argv[0], NULL); } else if(!strcmp(option_name, "name") || !strcmp(option_name, "n")) { s->name = optarg; } else if(!strcmp(option_name, "port") || !strcmp(option_name, "p")) { s->port = atoi(optarg); } else if(!strcmp(option_name, "source") || !strcmp(option_name, "s")) { s->source = optarg; } else if(!strcmp(option_name, "username") || !strcmp(option_name, "u")) { s->user = optarg; } else if(!strcmp(option_name, "version") || !strcmp(option_name, "V")) { version(); } else if(!strcmp(option_name, "wait") || !strcmp(option_name, "w")) { s->wait = atoi(optarg); } break; case 1: { /* Set up the option name. */ option_name = long_options[option_index].name; /* Set the name. It's ok if optarg is NULL. */ if(!optarg) optarg = "*"; /* Figure out which request type we're settings. */ if(!strcmp(option_name, "poison")) s->poison = optarg; else if(!strcmp(option_name, "conflict")) s->conflict = optarg; else usage(argv[0], "Invalid request type."); } break; case '?': default: usage(argv[0], "Couldn't parse arguments"); break; } } /* Sanity checking. */ if(s->poison && !s->source) usage(argv[0], "--poison requires a source address to return (--source)."); if(s->port != 137) fprintf(stderr, "WARNING: You probably don't want to change the port.\n"); #ifndef WIN32 /* Check for the root user. */ if(getuid() != 0 && s->port < 1024) fprintf(stderr, "WARNING: If the bind() fails, please re-run as root (privileges will be dropped as soon as the socket is created).\n"); #endif /* Create a socket */ fprintf(stderr, "Creating a UDP socket.\n"); s->socket = udp_create_socket(s->port, "0.0.0.0"); /* Drop privileges, if they're running as root. */ drop_privileges(s->user); /* Set the global settings -- this lets us clean up when a signal is caught. */ global_settings = s; /* Let the user know what's going on. */ if(s->poison) fprintf(stderr, "Poisoning requests containing '%s' sent from '%s'\n", s->name ? s->name : "*", s->poison); if(s->conflict) fprintf(stderr, "Sending conflicts on requests containing '%s' sent from '%s'\n", s->name ? s->name : "*", s->conflict); /* Poll for data. */ nb_poll(s); return 0; }
int main(int argc, char** argv) { struct sockaddr_in server; int chan; PaStream *stream; PaStreamParameters output_parameters; PaError err; voice = NULL; srand(time(NULL)); atexit(at_exit); set_signal_handlers(); init(); if (argc != 4) return usage(argv[0]); set_output_parameters(&output_parameters, atoi(argv[3])); /* connect to server */ sock = udp_create_socket(&server, sizeof(server), inet_addr(argv[1]), atoi(argv[2]), WAIT_FOR_RECEIVE); if (sock < 0) error_and_exit("Cannot create socket", __FILE__, __LINE__); /* create speech thingies */ si.w = malloc(sizeof(cst_wave) * si.channel_count); si.pos = malloc(sizeof(long) * si.channel_count); si.done = malloc(sizeof(int) * si.channel_count); si.cur_delay = malloc(sizeof(double) * si.channel_count); si.rate_delay = malloc(sizeof(double) * si.channel_count); for (chan = 0; chan < si.channel_count; ++chan) { si.w[chan] = NULL; next_tweet(&server, chan); } err = Pa_OpenStream(&stream, NULL, &output_parameters, 44100., 0, paNoFlag, say_text_callback, &si); if (paNoError != err) { fprintf(stderr, "Cannot open stream: %s\n", Pa_GetErrorText(err)); exit(-1); } /* Pa_OpenDefaultStream(&stream, 0, si.channel_count, paInt16, si.w->sample_rate, 0, say_text_callback, &si); */ err = Pa_StartStream(stream); if (paNoError != err) { fprintf(stderr, "Cannot start stream: %s\n", Pa_GetErrorText(err)); exit(-1); } while (1) { usleep(100); for (chan = 0; chan < si.channel_count; ++chan) { if (si.done[chan]) next_tweet(&server, chan); } } //Pa_StopStream(stream); return 0; }