/** * Opens the socket client for communication. * @param transport Transport handle * @param host Hostname or IP address (using local address if NULL) * @param port Port number for listen port * @return Returns LWPB_ERR_OK if successful. */ lwpb_err_t lwpb_transport_socket_client_open(lwpb_transport_t transport, const char *host, u16_t port) { struct lwpb_transport_socket_client *socket_client = (struct lwpb_transport_socket_client *) transport; lwpb_err_t ret = LWPB_ERR_OK; int status; struct addrinfo hints; struct addrinfo *res; struct sockaddr_in *addr; char tmp[16]; int yes = 1; if (socket_client->socket != -1) { LWPB_INFO("Socket client already opened"); return LWPB_ERR_OK; } // Allocate receive buffer ret = lwpb_transport_alloc_buf(&socket_client->super, &socket_client->buf, &socket_client->len); if (ret != LWPB_ERR_OK) return ret; socket_client->pos = socket_client->buf; // Resolve hostname LWPB_DEBUG("Resolving hostname '%s'", host); memset(&hints, 0, sizeof hints); hints.ai_family = AF_INET; // IPv4 hints.ai_socktype = SOCK_STREAM; // TCP stream sockets hints.ai_flags = AI_PASSIVE; // Fill in the IP snprintf(tmp, sizeof(tmp), "%d", port); if ((status = getaddrinfo(host, tmp, &hints, &res)) != 0) { LWPB_ERR("getaddrinfo error: %s\n", gai_strerror(status)); ret = LWPB_ERR_NET_INIT; goto out; } addr = (struct sockaddr_in *) res->ai_addr; inet_ntop(res->ai_family, &addr->sin_addr, tmp, sizeof(tmp)); // Create client socket LWPB_DEBUG("Creating client socket"); socket_client->socket = socket(res->ai_family, res->ai_socktype, res->ai_protocol); if (socket_client->socket == -1) { LWPB_ERR("Cannot create client socket"); ret = LWPB_ERR_NET_INIT; goto out; } // Reuse address if necessary if (setsockopt(socket_client->socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) { LWPB_ERR("Cannot set SO_REUSEADDR (error: %d)", errno); ret = LWPB_ERR_NET_INIT; goto out; } // Connect to server LWPB_DEBUG("Connecting to %s:%d", tmp, port); if (connect(socket_client->socket, res->ai_addr, res->ai_addrlen) == -1) { LWPB_ERR("Cannot open connection (errno: %d)", errno); ret = LWPB_ERR_NET_INIT; goto out; } // Make non-blocking make_nonblock(socket_client->socket); out: freeaddrinfo(res); return ret; }
isc_result_t isc_entropy_createfilesource(isc_entropy_t *ent, const char *fname) { int fd; struct stat _stat; isc_boolean_t is_usocket = ISC_FALSE; isc_boolean_t is_connected = ISC_FALSE; isc_result_t ret; isc_entropysource_t *source; REQUIRE(VALID_ENTROPY(ent)); REQUIRE(fname != NULL); LOCK(&ent->lock); source = NULL; if (stat(fname, &_stat) < 0) { ret = isc__errno2result(errno); goto errout; } /* * Solaris 2.5.1 does not have support for sockets (S_IFSOCK), * but it does return type S_IFIFO (the OS believes that * the socket is a fifo). This may be an issue if we tell * the program to look at an actual FIFO as its source of * entropy. */ #if defined(S_ISSOCK) if (S_ISSOCK(_stat.st_mode)) is_usocket = ISC_TRUE; #endif #if defined(S_ISFIFO) if (S_ISFIFO(_stat.st_mode)) is_usocket = ISC_TRUE; #endif if (is_usocket) fd = socket(PF_UNIX, SOCK_STREAM, 0); else fd = open(fname, O_RDONLY | O_NONBLOCK, 0); if (fd < 0) { ret = isc__errno2result(errno); goto errout; } ret = make_nonblock(fd); if (ret != ISC_R_SUCCESS) goto closefd; if (is_usocket) { struct sockaddr_un sname; memset(&sname, 0, sizeof(sname)); sname.sun_family = AF_UNIX; strncpy(sname.sun_path, fname, sizeof(sname.sun_path)); sname.sun_path[sizeof(sname.sun_path)-1] = '0'; #ifdef ISC_PLATFORM_HAVESALEN #if !defined(SUN_LEN) #define SUN_LEN(su) \ (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path)) #endif sname.sun_len = SUN_LEN(&sname); #endif if (connect(fd, (struct sockaddr *) &sname, sizeof(struct sockaddr_un)) < 0) { if (errno != EINPROGRESS) { ret = isc__errno2result(errno); goto closefd; } } else is_connected = ISC_TRUE; } source = isc_mem_get(ent->mctx, sizeof(isc_entropysource_t)); if (source == NULL) { ret = ISC_R_NOMEMORY; goto closefd; } /* * From here down, no failures can occur. */ source->magic = SOURCE_MAGIC; source->ent = ent; source->total = 0; source->bad = ISC_FALSE; memset(source->name, 0, sizeof(source->name)); ISC_LINK_INIT(source, link); if (is_usocket) { source->sources.usocket.handle = fd; if (is_connected) source->sources.usocket.status = isc_usocketsource_connected; else source->sources.usocket.status = isc_usocketsource_connecting; source->sources.usocket.sz_to_recv = 0; source->type = ENTROPY_SOURCETYPE_USOCKET; } else { source->sources.file.handle = fd; source->type = ENTROPY_SOURCETYPE_FILE; } /* * Hook it into the entropy system. */ ISC_LIST_APPEND(ent->sources, source, link); ent->nsources++; UNLOCK(&ent->lock); return (ISC_R_SUCCESS); closefd: (void)close(fd); errout: if (source != NULL) isc_mem_put(ent->mctx, source, sizeof(isc_entropysource_t)); UNLOCK(&ent->lock); return (ret); }
int process_telnet() { char cmd[1024]; int cmd_i=0; int i; char* ch; dmx_pes_type_t pestype; unsigned long freq=0; unsigned long srate=0; /* Open a new telnet session if a client is trying to connect */ if (ns==-1) { if ((ns = accept(socketIn, (struct sockaddr *)&fsin, &fromlen)) > 0) { make_nonblock(ns); cmd_i=0; cmd[0]=0; printf("Opened connection\n"); writes(ns,"220-DVBSTREAM - "); writes(ns,hostname); writes(ns,"\r\nDONE\r\n"); connectionOpen=1; } } /* If a telnet session is open, receive and process any input */ if (connectionOpen) { /* Read in at most a line of text - any ctrl character ends the line */ while (read(ns,&in_ch,1)>0) { if (in_ch < 32) break; /* Prevent buffer overflows */ if (cmd_i < 1024-1) { cmd[cmd_i++]=in_ch; cmd[cmd_i]=0; } } if (in_ch > 0) { if (cmd_i > 0) { printf("CMD: \"%s\"\n",cmd); if (strcasecmp(cmd,"QUIT")==0) { writes(ns,"DONE\r\n"); close(ns); ns=-1; connectionOpen=0; printf("Closed connection\n"); } else if (strcasecmp(cmd,"STOP")==0) { writes(ns,"STOP\n"); for (i=0;i<npids;i++) { if (ioctl(fd[i], DMX_STOP) < 0) { perror("DMX_STOP"); } } for (i=0;i<8192;i++) { hi_mappids[i]=(i >> 8); lo_mappids[i]=(i&0xff); } writes(ns,"DONE\r\n"); } else if (strncasecmp(cmd,"ADD",3)==0) { i=4; if ((cmd[3]=='V') || (cmd[3]=='v')) pestype=DMX_PES_VIDEO; else if ((cmd[3]=='A') || (cmd[3]=='a')) pestype=DMX_PES_AUDIO; else if ((cmd[3]=='T') || (cmd[3]=='t')) pestype=DMX_PES_TELETEXT; else { pestype=DMX_PES_OTHER; i=3; } while (cmd[i]==' ') i++; if ((ch=(char*)strstr(&cmd[i],":"))!=NULL) { pid2=atoi(&ch[1]); ch[0]=0; } else { pid2=-1; } pid=atoi(&cmd[i]); if (pid) { if (npids == MAX_CHANNELS) { fprintf(stderr,"\nsorry, you can only set up to 8 filters.\n\n"); return(-1); } else { pestypes[npids]=pestype; pestype=DMX_PES_OTHER; pids[npids]=pid; if (pid2!=-1) { hi_mappids[pid]=pid2>>8; lo_mappids[pid]=pid2&0xff; fprintf(stderr,"Mapping %d to %d\n",pid,pid2); } if((fd[npids] = open(demuxdev[card],O_RDWR|O_NONBLOCK)) < 0){ fprintf(stderr,"FD %i: ",i); perror("DEMUX DEVICE: "); } else { set_ts_filt(fd[npids],pids[npids],pestypes[npids]); npids++; } } }