?? intercom.c
字號:
/* intercom.c -- A real-time audio communication utility Copyright (C) 2001-2003 Shane Wegner This file is part of Intercom. Intercom is free software; you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as published by the Free Software Foundation. Intercom is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of version 2 of the GNU General Public License along with Intercom; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA To contact the author, please send email to shane@cm.nu. *//* $Id: intercom.c,v 1.83 2003/02/13 20:22:49 shane Exp $ */#ifdef HAVE_CONFIG_H#include "config.h"#endif#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#ifdef HAVE_STDINT_H#include <stdint.h>#else#include <inttypes.h>#endif#ifdef HAVE_GETOPT_H#include <getopt.h>#else#include "subst/getopt.h"#endif#include <errno.h>#include <time.h>#include <signal.h>#include <fcntl.h>#include <math.h>#include <sys/types.h>#include <sys/time.h>#include <sys/stat.h>#include <sys/wait.h>#include <sys/ioctl.h>#if defined(HAVE_SYS_SOUNDCARD_H)#include <sys/soundcard.h>#elif defined(HAVE_MACHINE_SOUNDCARD_H)#include <machine/soundcard.h>#else#include <soundcard.h>#endif#include <sys/socket.h>#include <netinet/in.h>#ifdef HAVE_NETINET_IN_SYSTM_H#include <netinet/in_systm.h>#endif#ifdef HAVE_NETINET_IP_H#include <netinet/ip.h>#endif#include <arpa/inet.h>#include <netdb.h>#ifdef USE_GETTEXT#ifdef HAVE_LOCALE_H#include <locale.h>#endif#include <libintl.h>#define _(String) gettext(String)#define gettext_noop(String) (String)#define N_(String) gettext_noop(String)#else#define _(String) (String)#define N_(String) (String)#endif#include "intercom.h"#include "protocol.h"#include "iobuff.h"#include "console.h"#include "pkthandler.h"#include "userdefs.h"#include "codecs.h"#include "hooks.h"#ifdef USE_CRYPTO#include "crypto.h"#endif/* Global variables */int done = 0;#ifdef USE_CRYPTO#define CALL_INIT_CRYPTO .cryptinfo = {CALL_CRYPT_NONE, },#else#define CALL_INIT_CRYPTO#endif#define CALL_INIT_STATE (struct call_info){ \.state = CALL_STATE_IDLE, .csock = -1, .dsock = -1, .loopback = 0, \.rdsp = -1, .pdsp = -1, \.snd_sequence = 0, .rcv_sequence = 0, \.ogain = NULL, .igain = NULL, \CALL_INIT_CRYPTO \.connstats = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }struct call_info call = CALL_INIT_STATE;struct compression_format compression_table[]={{AUDIO_COMPRESSION_NONE, "none", "None"},#ifdef HAVE_LIBGSM{AUDIO_COMPRESSION_GSM, "gsm", "GSM 6.10"},#endif{AUDIO_COMPRESSION_G711U, "g.711-ulaw", "CCITT G.711 U-LAW"},{AUDIO_COMPRESSION_G711A, "g.711-alaw", "CCITT G.711 A-LAW"},#ifdef HAVE_G72X{AUDIO_COMPRESSION_G721, "g.721", "CCITT G.721"},{AUDIO_COMPRESSION_G723_24, "g.723-24", "CCITT G.723 (24 kbit/s)"},{AUDIO_COMPRESSION_G723_40, "g.723-40", "CCITT G.723 (40 kbit/s)"},#endif{0, NULL, NULL}};/* Local variables */static fd_set f_rset, f_wset;static char audiopkt[4096];#ifdef USE_CRYPTOstatic char cr_audiopkt[4096];#endifstatic int isplaying = 1, dropskip = 0;static int listener;/* Config variables */static float silence_time;static int silence_thresh, min_buffsize, max_buffsize;void refresh_userdefs(void){double g;/* handle gain changes */g = opt_get_float("input_gain");if (g != 0.0 && (call.igain == NULL || call.igain->gain_db != g)) {if (call.igain != NULL)gain_free(call.igain);call.igain = gain_init(g);} else if (g == 0.0 && call.igain != NULL) {gain_free(call.igain);call.igain = NULL;}g = opt_get_float("output_gain");if (g != 0.0 && (call.ogain == NULL || call.ogain->gain_db != g)) {if (call.ogain != NULL)gain_free(call.ogain);call.ogain = gain_init(g);} else if (g == 0.0 && call.ogain != NULL) {gain_free(call.ogain);call.ogain = NULL;}silence_time = opt_get_float("silence_time");silence_thresh = opt_get_int("silence_thresh");min_buffsize = opt_get_int("udp_minsize");max_buffsize = opt_get_int("udp_maxsize");}void sighandler(int signum){switch(signum) {case SIGHUP:hk_trigger(hk_sighup);break;case SIGINT:case SIGTERM:done = 1;break;}signal(signum, sighandler);}static char **ic_environ = NULL;static int ic_envlen = 0;void icsetenv(const char *var, const char *value){int i, exists = 0;char rv[strlen(var) + 4];strcpy(rv, "ic_");strcpy(&rv[3], var);for (i = 0; i < ic_envlen; i++)if (!strcmp(rv, ic_environ[i]))exists = 1;if (!exists) {ic_environ = realloc(ic_environ, sizeof(char **) * (ic_envlen + 1));ic_environ[ic_envlen++] = strdup(rv);}setenv(rv, value, 1);}int do_connect(const char *hostname, int port, struct sockaddr_in *r_address){int sock;struct in_addr addr;struct sockaddr_in address;struct hostent *host = gethostbyname(hostname);if (host == NULL) {herror("gethostbyname");return -1;}memcpy(&addr, host->h_addr_list[0], sizeof(addr));address.sin_family = AF_INET;address.sin_port = htons(port);address.sin_addr.s_addr = addr.s_addr;if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {perror("socket");return -1;}if (connect(sock, (struct sockaddr *)&address, sizeof(address)) == -1) {perror("connect");close(sock);return -1;}/* if (fcntl(sock, F_SETFL, O_RDWR | O_NONBLOCK) == -1) {perror("fcntl");close(sock);return -1;} */if (r_address != NULL)memcpy(r_address, &address, sizeof(address));return sock;} /* do_connect */static int do_listen(int port){int sock;struct in_addr addr;struct sockaddr_in address;struct hostent *host = gethostbyname("0.0.0.0");memcpy(&addr, host->h_addr_list[0], sizeof(addr));address.sin_family = AF_INET;address.sin_port = htons(port);address.sin_addr.s_addr = addr.s_addr;if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {perror("socket");return -1;}if (bind(sock, (struct sockaddr *)&address, sizeof(address)) == -1) {perror("bind");close(sock);return -1;}if (fcntl(sock, F_SETFL, O_RDWR | O_NONBLOCK) == -1) {perror("fcntl");close(sock);return -1;}if (listen(sock, 1) == -1) {perror("listen");close(sock);return -1;}return sock;} /* do_listen */int dsp_setup(const char *device, int mode, struct audio_params *params){int fd, r, caps;if ((fd = open(device, (mode|O_NONBLOCK))) == -1) {perror("open");return -1;}if (ioctl(fd, SNDCTL_DSP_GETCAPS, &caps) == -1) {perror("sndctl_dsp_getcaps failed:");goto err;}if ((mode & O_RDWR) == O_RDWR) {if ((caps & DSP_CAP_DUPLEX) != DSP_CAP_DUPLEX) {fputs(_("Error: missing duplex capability\n"), stderr);goto err;}if (ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0) == -1)perror("SNDCTL_DSP_SETDUPLEX");}if (ioctl(fd, SNDCTL_DSP_RESET, 0) == -1) {perror("sound ioctl(SNDCTL_DSP_RESET)");goto err;}/* r = APF_NETWORK;if (ioctl(fd, SNDCTL_DSP_PROFILE, &r) == -1)perror("sound ioctl(SNDCTL_DSP_PROFILE)");*/r = (0x02 << 16) + logb(params->rate * (params->bitwidth / 8) / 20);if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &r) == -1)perror("sound ioctl(SNDCTL_DSP_SETFRAGMENT)");r = AFMT_S16_LE;if (ioctl(fd, SNDCTL_DSP_SETFMT, &r) == -1) {perror("sound ioctl(SNDCTL_DSP_SETFMT)");goto err;}r = 0;if (ioctl(fd, SNDCTL_DSP_STEREO, &r) == -1) {perror("sound ioctl(SNDCTL_DSP_CHANNELS)");goto err;}r = params->rate;if (ioctl(fd, SNDCTL_DSP_SPEED, &r) == -1) {perror("sound ioctl(SNDCTL_DSP_SPEED)");goto err;}if ((caps & DSP_CAP_TRIGGER) == DSP_CAP_TRIGGER) {if ((mode & O_RDWR) == O_RDWR)r = (PCM_ENABLE_INPUT|PCM_ENABLE_OUTPUT);else if ((mode & O_WRONLY) == O_WRONLY)r = PCM_ENABLE_OUTPUT;elser = PCM_ENABLE_INPUT;if (ioctl(fd, SNDCTL_DSP_SETTRIGGER, &r) == -1)perror("SNDCTL_DSP_SETTRIGGER failed:");}return fd;err:close(fd);return -1;} /* dsp_setup */#if (DEBUG >= 2)void hexdump(const char *buff, size_t n){size_t i;for (i = 0; i < n; i++)printf(" %02x", (unsigned int)buff[i] & 0xff);}static void pkthexdump(const char *buff, size_t n){fputs(_("debug: Packet hex dump:"), stdout);hexdump(buff, n);putchar('\n');}#endifvoid queuepacket(struct iobuff *iob, uint16_t type, char *pkt, size_t pktlen){char buff[sizeof(struct pkt_header) + pktlen];struct pkt_header *ph = (struct pkt_header *)buff;ph->p_type = type;ph->p_len = sizeof(ph) + pktlen;memcpy(&buff[sizeof(*ph)], pkt, pktlen);call.connstats.cpkts_sent++;call.connstats.cbytes_sent += ph->p_len;if (opt_get_int("verbose")) {#if (DEBUG >= 1)printf(_("debug: Queueing packet type %u of %u bytes.\n"),type, ph->p_len);#if (DEBUG >= 2)pkthexdump(buff, ph->p_len);#endif#endif}iob_queue(iob, buff, ph->p_len);}void closecall(void){int i;if (TEST_BIT(call.state, CALL_STATE_CONNECTED)) {if (call.csock != -1) {shutdown(call.csock, 2);close(call.csock);}if (call.dsock != -1) {shutdown(call.dsock, 2);close(call.dsock);}iob_flush(&call.csrq);iob_flush(&call.cssq);iob_flush(&call.rdspq);iob_flush(&call.rdspq);if (call.rdsp != -1) {close(call.rdsp);free(call.rdsp_buff);if (call.pdsp == call.rdsp)call.pdsp = -1;}if (call.pdsp != -1) {close(call.pdsp);free(call.pdsp_buff);}/* Free the environment */for (i = 0; i < ic_envlen; i++) {unsetenv(ic_environ[i]);free(ic_environ[i]);}free(ic_environ);ic_environ = NULL;ic_envlen = 0;/* And repopulate with userdefs. TODO: optimize this out. */for (i = 0; useropts[i].key != NULL; i++)if (strcmp(useropts[i].key, "crypt_passphrase"))icsetenv(useropts[i].key, useropts[i].value);memcpy(&call, &CALL_INIT_STATE, sizeof(call));iob_init(&call.cssq);iob_setminbytes(&call.cssq, IOB_PAGESIZE);iob_init(&call.csrq);iob_setminbytes(&call.csrq, IOB_PAGESIZE);iob_init(&call.rdspq);iob_init(&call.pdspq);codecs_init();}}int setup_udpsock(struct sockaddr_in *addr){unsigned char tos;int l = sizeof(*addr);int s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);if (s == -1) {perror("socket");return -1;}addr->sin_family = AF_INET;/* On Linux at least, this gives us a random port. *//* TODO: verify portability */addr->sin_port = 0;addr->sin_addr.s_addr = INADDR_ANY;if (bind(s, (struct sockaddr *)addr, sizeof(*addr)) == -1) {perror("bind");close(s);return -1;}getsockname(s, (struct sockaddr *)addr, &l);if (fcntl(s, F_SETFL, O_RDWR | O_NONBLOCK) == -1) {perror("fcntl");close(s);return -1;}#ifdef HAVE_NETINET_IP_Hif (opt_get_int("iptos_lowdelay") > 0) {tos = IPTOS_LOWDELAY;if (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, 1) == -1)if (opt_get_int("verbose"))perror("setsockopt failed for IP_LOWDELAY");}#endifreturn s;}void send_errorpkt(struct iobuff *iob, uint16_t errorcode){static const struct pkt_error err_list[]= {{PKT_ERROR_PKTUNKNOWN, N_("Unknown packet type")},{PKT_ERROR_PKTORDER, N_("Packet ordering error")},{PKT_ERROR_AUDIOFMT, N_("Audio format error")},{PKT_ERROR_PROTO_VERSION, N_("Protocol version mismatch")},{PKT_ERROR_NOCRYPT, N_("Encryption format not supported")},};static size_t err_list_nr = sizeof(err_list) / sizeof(struct pkt_error);size_t i;struct pkt_error errorpkt;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -