int daemon_mode(char *hostname, char* port, int mode) { // ---------------- FOR DAEMON USE, OPEN SOCKETS --------------------------------- // If we choose daemon mode, we will need to open a socket for communication // This needs to be done BEFORE enabling the interrupt handler, as we want // the handler to send its code to the LamPI-daemon process if (mode == SOCK_DGRAM) { if ((sockfd = open_udp_socket()) < 0) { fprintf(stderr,"Error opening UDP socket for host %s. Exiting program\n\n", hostname); exit (1); } else if (verbose) { printf("daemon mode:: Success opening ocket\n"); } } else { // Open a TCP socket if ((sockfd = open_tcp_socket(hostname,port)) < 0) { fprintf(stderr,"Error opening TCP socket for host %s. Exiting program\n\n", hostname); exit (1); } else if (verbose) { printf("daemon mode:: Success connecting TCP to %s:%s\n",hostname,port); } FD_ZERO(&fds); FD_SET(sockfd, &fds); } return(0); }
bool rtspcl_connect(struct rtspcl_data *rtspcld, const char *host, short destport, const char *sid, GError **error_r) { assert(rtspcld->tcp_socket == NULL); unsigned short myport = 0; struct sockaddr_in name; socklen_t namelen = sizeof(name); int fd = open_tcp_socket(NULL, &myport, error_r); if (fd < 0) return false; if (!get_tcp_connect_by_host(fd, host, destport, error_r)) return false; getsockname(fd, (struct sockaddr*)&name, &namelen); memcpy(&rtspcld->local_addr, &name.sin_addr,sizeof(struct in_addr)); sprintf(rtspcld->url, "rtsp://%s/%s", inet_ntoa(name.sin_addr), sid); getpeername(fd, (struct sockaddr*)&name, &namelen); memcpy(&rtspcld->host_addr, &name.sin_addr, sizeof(struct in_addr)); rtspcld->tcp_socket = tcp_socket_new(fd, &rtsp_client_socket_handler, rtspcld); return true; }
static int message_callback(ssh_session session, ssh_message message, void *userdata) { ssh_channel channel; int socket_fd, *pFd; struct ssh_channel_callbacks_struct *cb_chan; struct event_fd_data_struct *event_fd_data; (void)session; (void)message; (void)userdata; _ssh_log(SSH_LOG_PACKET, "=== message_callback", "Message type: %d", ssh_message_type(message)); _ssh_log(SSH_LOG_PACKET, "=== message_callback", "Message Subtype: %d", ssh_message_subtype(message)); if (ssh_message_type(message) == SSH_REQUEST_CHANNEL_OPEN) { _ssh_log(SSH_LOG_PROTOCOL, "=== message_callback", "channel_request_open"); if (ssh_message_subtype(message) == SSH_CHANNEL_DIRECT_TCPIP) { channel = ssh_message_channel_request_open_reply_accept(message); if (channel == NULL) { _ssh_log(SSH_LOG_WARNING, "=== message_callback", "Accepting direct-tcpip channel failed!"); return 1; } else { _ssh_log(SSH_LOG_PROTOCOL, "=== message_callback", "Connected to channel!"); socket_fd = open_tcp_socket(message); if (-1 == socket_fd) { return 1; } pFd = malloc(sizeof *pFd); cb_chan = malloc(sizeof *cb_chan); event_fd_data = malloc(sizeof *event_fd_data); (*pFd) = socket_fd; event_fd_data->channel = channel; event_fd_data->p_fd = pFd; event_fd_data->stacked = 0; event_fd_data->cb_chan = cb_chan; cb_chan->userdata = event_fd_data; cb_chan->channel_eof_function = my_channel_eof_function; cb_chan->channel_close_function = my_channel_close_function; cb_chan->channel_data_function = my_channel_data_function; cb_chan->channel_exit_status_function = my_channel_exit_status_function; ssh_callbacks_init(cb_chan); ssh_set_channel_callbacks(channel, cb_chan); ssh_event_add_fd(mainloop, (socket_t)*pFd, POLLIN, my_fd_data_function, event_fd_data); return 0; } } } return 1; }
static int raopcl_stream_connect(raopcl_data_t *raopcld) { u_int16_t myport=0; if((raopcld->sfd=open_tcp_socket(NULL, &myport))==-1) return -1; if(get_tcp_connect_by_host(raopcld->sfd, raopcld->addr, rtspcl_get_server_port(raopcld->rtspcl))) { close(raopcld->sfd); raopcld->sfd=-1; return -1; } return 0; }
int rtspcl_connect(rtspcl_t *p, char *host, uint16_t destport, char *sid) { uint16_t myport=0; struct sockaddr_in name; socklen_t namelen=sizeof(name); rtspcl_data_t *rtspcld; if(!p) return -1; rtspcld=(rtspcl_data_t *)p; rtspcld->session=NULL; if((rtspcld->fd=open_tcp_socket(NULL, &myport))==-1) return -1; if(get_tcp_connect_by_host(rtspcld->fd, host, destport)) return -1; getsockname(rtspcld->fd, (struct sockaddr*)&name, &namelen); memcpy(&rtspcld->local_addr,&name.sin_addr,sizeof(struct in_addr)); sprintf(rtspcld->url,"rtsp://%s/%s",inet_ntoa(name.sin_addr),sid); getpeername(rtspcld->fd, (struct sockaddr*)&name, &namelen); memcpy(&rtspcld->host_addr,&name.sin_addr,sizeof(struct in_addr)); return 0; }
EXTERNAL ICBINN * icbinn_clnt_create_tcp (const char *host, int port) { int sockfd; struct sockaddr_in sin; ICBINN *ret; sockfd = open_tcp_socket (host, port, &sin); if (sockfd < 0) return NULL; ret = icbinn_clnt_create_by_fd (sockfd, (struct sockaddr *) &sin, sizeof (sin)); if (!ret) close (sockfd); return ret; }
static void us894_test_sslversion (const SSL_METHOD *m, int expect_fail) { BIO *conn; SSL *ssl; SSL_CTX *ssl_ctx = NULL; int rv; ssl_ctx = SSL_CTX_new(m); CU_ASSERT(ssl_ctx != NULL); /* * Now that the SSL context is ready, open a socket * with the server and bind that socket to the context. */ conn = open_tcp_socket("127.0.0.1", "16894"); CU_ASSERT(conn != NULL); /* * Create an SSL session context */ ssl = SSL_new(ssl_ctx); SSL_set_bio(ssl, conn, conn); /* * Now that we have everything ready, let's initiate the TLS * handshake. */ rv = SSL_connect(ssl); if (!expect_fail) { CU_ASSERT(rv > 0); } else { CU_ASSERT(rv <= 0); } /* * Cleanup all the data */ SSL_shutdown(ssl); SSL_free(ssl); SSL_CTX_free(ssl_ctx); }
static int raopcl_stream_connect(raopcl_data_t *raopcld) { u_int16_t myport=0; if((raopcld->sfd=open_tcp_socket(NULL, &myport))==-1) return -1; if(get_tcp_connect_by_host(raopcld->sfd, raopcld->addr, rtspcl_get_server_port(raopcld->rtspcl))) { close(raopcld->sfd); if((raopcld->sfd=open_udp_socket(NULL, &myport))==-1) return -1; if(get_tcp_connect_by_host(raopcld->sfd, raopcld->addr, rtspcl_get_server_port(raopcld->rtspcl))) { ERRMSG("%s: connect failed\n", __func__); close(raopcld->sfd); raopcld->sfd=-1; return -1; } else printf("RAOP stream connected via UDP\n"); } else printf("RAOP stream connected via TCP\n"); return 0; }
void ares__send_query(ares_channel channel, struct query *query, time_t now) { struct send_request *sendreq; struct server_state *server; server = &channel->servers[query->server]; if (query->using_tcp) { int tryWrite = 0; /* Make sure the TCP socket for this server is set up and queue * a send request. */ if (server->tcp_socket == -1) { if (open_tcp_socket(channel, server) == -1) { query->skip_server[query->server] = 1; next_server(channel, query, now); return; } if ( channel->poll_cb_func ) { // printf("ares_send_q: pollopen tcp fd=%d\n", server->tcp_socket); (*(channel->poll_cb_func))( channel->poll_cb_data, channel, query->server, server->tcp_socket, ARES_POLLACTION_OPEN); } } sendreq = malloc(sizeof(struct send_request)); if (!sendreq) end_query(channel, query, ARES_ENOMEM, NULL, 0); sendreq->data = query->tcpbuf; sendreq->len = query->tcplen; sendreq->next = NULL; if (server->qtail) { server->qtail->next = sendreq; } else { server->qhead = sendreq; tryWrite = 1; } server->qtail = sendreq; query->timeout = 0; if ( tryWrite ) { #if 0 time_t now; time(&now); write_tcp_data(channel, query->server, now); /* XXX: the write code doesn't seem to handle EAGAIN properly! */ #else if ( channel->poll_cb_func ) (*(channel->poll_cb_func))( channel->poll_cb_data, channel, query->server, server->tcp_socket, ARES_POLLACTION_WRITEON); #endif } } else { if (server->udp_socket == -1) { if (open_udp_socket(channel, server) == -1) { //fprintf(stderr,"kennard:ares:send_query:open_udp failed\n"); query->skip_server[query->server] = 1; next_server(channel, query, now); return; } if ( channel->poll_cb_func ) { // printf("ares_send_q: pollopen udp fd=%d\n", server->udp_socket); (*(channel->poll_cb_func))( channel->poll_cb_data, channel, query->server, server->udp_socket, ARES_POLLACTION_OPEN); } } if (send(server->udp_socket, query->qbuf, query->qlen, 0) == -1) { //fprintf(stderr,"kennard:ares:send_query:send_udp failed\n"); query->skip_server[query->server] = 1; next_server(channel, query, now); return; } query->timeout = now + ((query->itry == 0) ? channel->timeout : channel->timeout << query->itry / channel->nservers); } }
int main(int argc, char** argv) { int target = TARGET_UDP_SOCKET; int input = INPUT_FILE; int output_mode = OUTPUT_MODE_CLICK; filter_code = 0xFF; filter_rate = 0xFFFF; filter_Nrx = 0xFF; char *csi_node = "0.0.0.0"; char *tx_node = "0.0.0.1"; int debug_level = 0; if ( (argc < 5) || ((argc > 1) && (strcmp(argv[1], "help") == 0)) ) { print_help(); exit(0); } target = io_type(argv[1][2],TARGET); input = io_type(argv[1][0],INPUT); output_mode = format_type(argv[4][0]); printf("Config: %s (%s -> %s) %s %s %s\n", argv[1], input_types[input], output_types[target], argv[2], argv[3], format_types[output_mode]); if ( argc > 5 ) { csi_node = argv[5]; tx_node = argv[6]; } if ( argc > 7 ) { debug_level = atoi(argv[7]); } struct cn_msg *cmsg; unsigned char cmsg_input_buf[BUF_SIZE]; char sendline[3000]; unsigned char buf[BUF_SIZE]; int ret; int count = 0; char hostname[1024]; gethostname(hostname, 1024); uint32_t csi_node_addr = (uint32_t)inet_addr(csi_node); uint32_t tx_node_addr = (uint32_t)inet_addr(tx_node); unsigned short l, l2; /* Make sure usage is correct */ check_usage(argc, argv); /* Set up the "caught_signal" function as this program's sig handler */ signal(SIGINT, caught_signal); /* Prepare Input */ switch ( input ) { case INPUT_SOCKET: netlink_sock_fd = open_iwl_netlink_socket(); break; case INPUT_FILE: netlink_sock_fd = open_file(argv[3], "r"); break; } /* Prepare Output */ switch ( target ) { case TARGET_TCP_SOCKET: out_fd = open_tcp_socket(argv[2], 32000); break; case TARGET_UDP_SOCKET: out_fd = open_udp_socket(argv[2], 32000); break; case TARGET_FILE: out_fd = open_file(argv[2], "w"); break; case TARGET_STDOUT: out_fd = 1; break; } /* Poll socket forever waiting for a message */ u_char *buf_p; int len_p, len_sendline; while (1) { /* Receive from socket with infinite timeout */ //ret = recv(sock_fd, buf, sizeof(buf), 0); /* Read the next entry size */ if (DEBUG_LEVEL_DEV) { printf("\n----- Next Data -----\n\n"); } switch (input) { case INPUT_FILE: /* Read the next entry size */ ret = read(netlink_sock_fd, &l2, 1 * sizeof(unsigned short)); if ( ret != 0 ) { l = ntohs(l2); /* Sanity-check the entry size */ if (l == 0) { fprintf(stderr, "Error: got entry size=0\n"); exit_program(-1); } else if (l > BUF_SIZE) { fprintf(stderr, "Error: got entry size %u > BUF_SIZE=%u\n", l, BUF_SIZE); exit_program(-2); } /* Read in the entry */ read(netlink_sock_fd, buf, l * sizeof(*buf)); cmsg = (struct cn_msg*)&cmsg_input_buf[0]; cmsg->id.idx = 0; cmsg->id.val = 0; cmsg->seq = 0; cmsg->ack = 0; cmsg->len = l; cmsg->flags = 0; memcpy(cmsg->data,buf,l); } if ( ret == 0 ) ret = -1; break; case INPUT_SOCKET: ret = iwl_netlink_recv(netlink_sock_fd, &buf_p, &len_p, &cmsg); break; } if (ret == -1) exit_program_err(-1, "recv"); if (cmsg == NULL) { printf("cmsg == NULL\n"); continue; } struct iwl5000_bfee_notif *bfee = NULL; bfee = (struct iwl5000_bfee_notif *)&(cmsg->data[1]); /* Filter */ if ( (filter_code != 0xFF) && (cmsg->data[0] != filter_code) ) continue; if ( (filter_rate != 0xFFFF) && (bfee->fake_rate_n_flags != filter_rate) ) continue; if ( (filter_Nrx != 0xFF) && (bfee->Nrx != filter_Nrx) ) continue; if (DEBUG_LEVEL_DEV) printf("Entry size=%d, code=0x%X\n", cmsg->len, cmsg->data[0]); /* Evaluation */ double eff_snrs[MAX_NUM_RATES][4]; if ( cmsg->data[0] == IWL_CONN_BFEE_NOTIF /*0xBB*/) { /* Beamforming packet */ calc_eff_snrs(bfee, eff_snrs); struct timeval timeVal; gettimeofday (&timeVal, NULL); if (DEBUG_LEVEL_INFO) printf("Rcvd pkt at <%ld.%06ld>\n", (long int)(timeVal.tv_sec), (long int)(timeVal.tv_usec)); if (DEBUG_LEVEL_DEV) { /* Beamforming packet */ printf("\nBeamforming: rate=0x%x\n", bfee->fake_rate_n_flags); /* Pull out the message portion and print some stats */ if (count % SLOW_MSG_CNT == 0) printf("Received %d bytes: id: %d val: %d seq: %d clen: %d\n", cmsg->len, cmsg->id.idx, cmsg->id.val, cmsg->seq, cmsg->len); printf("\n--- Effektive SNR ---\n\n"); int i; for ( i = 0; i < MAX_NUM_RATES; i++) { printf("%d: %f %f %f %f\n", i, db(eff_snrs[i][0]), db(eff_snrs[i][1]), db(eff_snrs[i][2]), db(eff_snrs[i][3])); } printf("\n---------------------\n\n"); } } /* Log the data remote */ /* Puffer mit Text füllen */ switch (output_mode) { case OUTPUT_MODE_FORMAT: sprintf(sendline, "%s, Received %d bytes: id: %d val: %d seq: %d clen: %d\n", hostname, cmsg->len, cmsg->id.idx, cmsg->id.val, cmsg->seq, cmsg->len); len_sendline = strlen(sendline); break; case OUTPUT_MODE_BFEE: if ( bfee != NULL) { calc_eff_snrs_tostr(bfee, eff_snrs, sendline, hostname); } else { sprintf(sendline, "bfee == NULL\n"); } if (DEBUG_LEVEL_DEV) printf("To tx:\n%s\n", sendline); break; case OUTPUT_MODE_CLICK: len_sendline = click_output(sendline, bfee, cmsg, eff_snrs, csi_node_addr, tx_node_addr); break; default: /* Log the data to file */ l = (unsigned short) cmsg->len; l2 = htons(l); memcpy(sendline, &l2, 1 * sizeof(unsigned short)); len_sendline = 1 * sizeof(unsigned short); memcpy(&(sendline[len_sendline]), cmsg->data, 1 * l); len_sendline += 1 * l; if ((count % 100 == 0) && (DEBUG_LEVEL_DEV)) printf("wrote %d bytes [msgcnt=%u]\n", len_sendline, count); } switch ( target ) { case TARGET_FILE: case TARGET_TCP_SOCKET: ret = write(out_fd, sendline, len_sendline); break; case TARGET_UDP_SOCKET: sendto(out_fd, sendline, len_sendline, 0, (struct sockaddr *)&servaddr, sizeof(servaddr)); break; case TARGET_STDOUT: dprintf(out_fd,"%s",sendline); break; } ++count; } exit_program(0); return 0; }