--- a/Makefile 2014-07-05 08:12:15.000000000 +0100 +++ b/Makefile 2014-07-07 14:18:19.416856829 +0100 @@ -21,19 +21,19 @@ # include $(dir $(lastword $(MAKEFILE_LIST))).config.mk -PROG := $(BUILDDIR)/tvheadend +PROG := $(BUILDDIR)/tvheadend # # Common compiler flags # -CFLAGS += -g -O2 -CFLAGS += -Wall -Werror -Wwrite-strings -Wno-deprecated-declarations -CFLAGS += -Wmissing-prototypes -CFLAGS += -fms-extensions -funsigned-char -fno-strict-aliasing -CFLAGS += -D_FILE_OFFSET_BITS=64 -CFLAGS += -I${BUILDDIR} -I${ROOTDIR}/src -I${ROOTDIR} -LDFLAGS += -ldl -lpthread -lm +CFLAGS += -g -O2 +CFLAGS += -Wall -Werror -Wwrite-strings -Wno-deprecated-declarations +CFLAGS += -Wmissing-prototypes +CFLAGS += -fms-extensions -funsigned-char -fno-strict-aliasing +CFLAGS += -D_FILE_OFFSET_BITS=64 +CFLAGS += -I${BUILDDIR} -I${ROOTDIR}/src -I${ROOTDIR} +LDFLAGS += -ldl -lpthread -lm ifeq ($(CONFIG_LIBICONV),yes) LDFLAGS += -liconv endif @@ -44,9 +44,9 @@ endif ifeq ($(COMPILER), clang) -CFLAGS += -Wno-microsoft -Qunused-arguments -Wno-unused-function -CFLAGS += -Wno-unused-value -Wno-tautological-constant-out-of-range-compare -CFLAGS += -Wno-parentheses-equality -Wno-incompatible-pointer-types +CFLAGS += -Wno-microsoft -Qunused-arguments -Wno-unused-function +CFLAGS += -Wno-unused-value -Wno-tautological-constant-out-of-range-compare +CFLAGS += -Wno-parentheses-equality -Wno-incompatible-pointer-types endif vpath %.c $(ROOTDIR) @@ -70,9 +70,9 @@ # ifndef V -ECHO = printf "%-16s%s\n" $(1) $(2) -BRIEF = CC MKBUNDLE CXX -MSG = $(subst $(BUILDDIR)/,,$@) +ECHO = printf "%-16s%s\n" $(1) $(2) +BRIEF = CC MKBUNDLE CXX +MSG = $(subst $(BUILDDIR)/,,$@) $(foreach VAR,$(BRIEF), \ $(eval $(VAR) = @$$(call ECHO,$(VAR),$$(MSG)); $($(VAR)))) endif @@ -80,7 +80,7 @@ # # Core # -SRCS = src/version.c \ +SRCS = src/version.c \ src/uuid.c \ src/main.c \ src/tvhlog.c \ @@ -117,7 +117,7 @@ src/trap.c \ src/avg.c \ src/htsstr.c \ - src/tvhpoll.c \ + src/tvhpoll.c \ src/huffman.c \ src/filebundle.c \ src/config.c \ @@ -157,12 +157,12 @@ src/parsers/parser_h264.c \ src/parsers/parser_latm.c \ src/parsers/parser_avc.c \ - src/parsers/parser_teletext.c \ + src/parsers/parser_teletext.c -SRCS += src/epggrab/module.c\ - src/epggrab/channel.c\ - src/epggrab/module/pyepg.c\ - src/epggrab/module/xmltv.c\ +SRCS += src/epggrab/module.c \ + src/epggrab/channel.c \ + src/epggrab/module/pyepg.c \ + src/epggrab/module/xmltv.c SRCS += src/plumbing/tsfix.c \ src/plumbing/globalheaders.c @@ -170,21 +170,21 @@ SRCS += src/dvr/dvr_db.c \ src/dvr/dvr_rec.c \ src/dvr/dvr_autorec.c \ - src/dvr/dvr_cutpoints.c \ + src/dvr/dvr_cutpoints.c SRCS += src/webui/webui.c \ src/webui/comet.c \ src/webui/extjs.c \ src/webui/simpleui.c \ src/webui/statedump.c \ - src/webui/html.c\ - src/webui/webui_api.c\ + src/webui/html.c \ + src/webui/webui_api.c SRCS += src/muxer.c \ src/muxer/muxer_pass.c \ src/muxer/muxer_tvh.c \ src/muxer/tvh/ebml.c \ - src/muxer/tvh/mkmux.c \ + src/muxer/tvh/mkmux.c # # Optional code @@ -204,31 +204,31 @@ src/input/mpegts/dvb_psi.c \ src/input/mpegts/tsdemux.c \ src/input/mpegts/mpegts_mux_sched.c \ - src/input/mpegts/mpegts_network_scan.c \ + src/input/mpegts/mpegts_network_scan.c # MPEGTS DVB SRCS-${CONFIG_MPEGTS_DVB} += \ - src/input/mpegts/mpegts_network_dvb.c \ - src/input/mpegts/mpegts_mux_dvb.c \ - src/input/mpegts/scanfile.c + src/input/mpegts/mpegts_network_dvb.c \ + src/input/mpegts/mpegts_mux_dvb.c \ + src/input/mpegts/scanfile.c # MPEGTS EPG SRCS-$(CONFIG_MPEGTS) += \ src/epggrab/otamux.c\ src/epggrab/module/eit.c \ src/epggrab/support/freesat_huffman.c \ - src/epggrab/module/opentv.c \ + src/epggrab/module/opentv.c # LINUX DVB SRCS-${CONFIG_LINUXDVB} += \ - src/input/mpegts/linuxdvb/linuxdvb.c \ - src/input/mpegts/linuxdvb/linuxdvb_adapter.c \ - src/input/mpegts/linuxdvb/linuxdvb_frontend.c \ - src/input/mpegts/linuxdvb/linuxdvb_satconf.c \ - src/input/mpegts/linuxdvb/linuxdvb_lnb.c \ - src/input/mpegts/linuxdvb/linuxdvb_switch.c \ - src/input/mpegts/linuxdvb/linuxdvb_rotor.c \ - src/input/mpegts/linuxdvb/linuxdvb_en50494.c + src/input/mpegts/linuxdvb/linuxdvb.c \ + src/input/mpegts/linuxdvb/linuxdvb_adapter.c \ + src/input/mpegts/linuxdvb/linuxdvb_frontend.c \ + src/input/mpegts/linuxdvb/linuxdvb_satconf.c \ + src/input/mpegts/linuxdvb/linuxdvb_lnb.c \ + src/input/mpegts/linuxdvb/linuxdvb_switch.c \ + src/input/mpegts/linuxdvb/linuxdvb_rotor.c \ + src/input/mpegts/linuxdvb/linuxdvb_en50494.c # SATIP SRCS-${CONFIG_SATIP_CLIENT} += \ @@ -240,27 +240,27 @@ # IPTV SRCS-${CONFIG_IPTV} += \ src/input/mpegts/iptv/iptv.c \ - src/input/mpegts/iptv/iptv_mux.c \ - src/input/mpegts/iptv/iptv_service.c \ - src/input/mpegts/iptv/iptv_http.c \ - src/input/mpegts/iptv/iptv_udp.c \ + src/input/mpegts/iptv/iptv_mux.c \ + src/input/mpegts/iptv/iptv_service.c \ + src/input/mpegts/iptv/iptv_http.c \ + src/input/mpegts/iptv/iptv_udp.c # TSfile SRCS-$(CONFIG_TSFILE) += \ - src/input/mpegts/tsfile/tsfile.c \ - src/input/mpegts/tsfile/tsfile_input.c \ - src/input/mpegts/tsfile/tsfile_mux.c \ + src/input/mpegts/tsfile/tsfile.c \ + src/input/mpegts/tsfile/tsfile_input.c \ + src/input/mpegts/tsfile/tsfile_mux.c # Timeshift SRCS-${CONFIG_TIMESHIFT} += \ src/timeshift.c \ src/timeshift/timeshift_filemgr.c \ src/timeshift/timeshift_writer.c \ - src/timeshift/timeshift_reader.c \ + src/timeshift/timeshift_reader.c # Inotify SRCS-${CONFIG_INOTIFY} += \ - src/dvr/dvr_inotify.c \ + src/dvr/dvr_inotify.c # Avahi SRCS-$(CONFIG_AVAHI) += src/avahi.c @@ -271,7 +271,7 @@ # libav SRCS-$(CONFIG_LIBAV) += src/libav.c \ src/muxer/muxer_libav.c \ - src/plumbing/transcoding.c \ + src/plumbing/transcoding.c # Tvhcsa SRCS-${CONFIG_TVHCSA} += \ @@ -279,34 +279,35 @@ # CWC SRCS-${CONFIG_CWC} += \ - src/descrambler/cwc.c \ - + src/descrambler/cwc.c + # CAPMT SRCS-${CONFIG_CAPMT} += \ - src/descrambler/capmt.c + src/descrambler/capmt.c \ + src/descrambler/ccw.c # FFdecsa ifneq ($(CONFIG_DVBCSA),yes) FFDECSA-$(CONFIG_CAPMT) = yes -FFDECSA-$(CONFIG_CWC) = yes +FFDECSA-$(CONFIG_CWC) = yes endif ifeq ($(FFDECSA-yes),yes) SRCS-yes += src/descrambler/ffdecsa/ffdecsa_interface.c \ - src/descrambler/ffdecsa/ffdecsa_int.c -SRCS-${CONFIG_MMX} += src/descrambler/ffdecsa/ffdecsa_mmx.c + src/descrambler/ffdecsa/ffdecsa_int.c +SRCS-${CONFIG_MMX} += src/descrambler/ffdecsa/ffdecsa_mmx.c SRCS-${CONFIG_SSE2} += src/descrambler/ffdecsa/ffdecsa_sse2.c -${BUILDDIR}/src/descrambler/ffdecsa/ffdecsa_mmx.o : CFLAGS += -mmmx -${BUILDDIR}/src/descrambler/ffdecsa/ffdecsa_sse2.o : CFLAGS += -msse2 +${BUILDDIR}/src/descrambler/ffdecsa/ffdecsa_mmx.o : CFLAGS += -mmmx +${BUILDDIR}/src/descrambler/ffdecsa/ffdecsa_sse2.o : CFLAGS += -msse2 endif # File bundles -SRCS-${CONFIG_BUNDLE} += bundle.c -BUNDLES-yes += docs/html docs/docresources src/webui/static -BUNDLES-yes += data/conf -BUNDLES-${CONFIG_DVBSCAN} += data/dvb-scan -BUNDLES = $(BUNDLES-yes) -ALL-$(CONFIG_DVBSCAN) += check_dvb_scan +SRCS-${CONFIG_BUNDLE} += bundle.c +BUNDLES-yes += docs/html docs/docresources src/webui/static +BUNDLES-yes += data/conf +BUNDLES-${CONFIG_DVBSCAN} += data/dvb-scan +BUNDLES = $(BUNDLES-yes) +ALL-$(CONFIG_DVBSCAN) += check_dvb_scan # # Add-on modules @@ -318,10 +319,10 @@ # Variable transformations # -SRCS += $(SRCS-yes) -OBJS = $(SRCS:%.c=$(BUILDDIR)/%.o) -OBJS_EXTRA = $(SRCS_EXTRA:%.c=$(BUILDDIR)/%.so) -DEPS = ${OBJS:%.o=%.d} +SRCS += $(SRCS-yes) +OBJS = $(SRCS:%.c=$(BUILDDIR)/%.o) +OBJS_EXTRA = $(SRCS_EXTRA:%.c=$(BUILDDIR)/%.so) +DEPS = ${OBJS:%.o=%.d} # # Build Rules @@ -398,8 +399,8 @@ check_dvb_scan: $(ROOTDIR)/data/dvb-scan/.stamp # dvb-s / enigma2 / satellites.xml -$(ROOTDIR)/data/dvb-scan/dvb-s/.stamp: $(ROOTDIR)/data/satellites.xml \ - $(ROOTDIR)/data/dvb-scan/.stamp +$(ROOTDIR)/data/dvb-scan/dvb-s/.stamp: $(ROOTDIR)/data/satellites.xml \ + $(ROOTDIR)/data/dvb-scan/.stamp @echo "Generating data/dvb-scan/dvb-s from data/satellites.xml" @if ! test -s $(ROOTDIR)/data/satellites.xml ; then echo "Put your satellites.xml file to $(ROOTDIR)/data/satellites.xml"; exit 1; fi @if ! test -d $(ROOTDIR)/data/dvb-scan/dvb-s ; then mkdir $(ROOTDIR)/data/dvb-scan/dvb-s ; fi diff -U 3 -H -d -r -N -- a/src/descrambler/capmt.c b/src/descrambler/capmt.c --- a/src/descrambler/capmt.c 2014-07-05 08:12:15.000000000 +0100 +++ b/src/descrambler/capmt.c 2014-07-07 13:00:27.416825228 +0100 @@ -1824,6 +1824,7 @@ capmt->capmt_port); td->td_nicename = strdup(buf); td->td_service = s; + td->dtype = 0; td->td_stop = capmt_service_destroy; td->td_caid_change = capmt_caid_change; LIST_INSERT_HEAD(&t->s_descramblers, td, td_service_link); diff -U 3 -H -d -r -N -- a/src/descrambler/ccw.c b/src/descrambler/ccw.c --- a/src/descrambler/ccw.c 1970-01-01 01:00:00.000000000 +0100 +++ b/src/descrambler/ccw.c 2014-07-07 15:28:11.740885185 +0100 @@ -0,0 +1,670 @@ + /* + * tvheadend, CCW + * Copyright (C) 2012 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "tvheadend.h" +#include "tcp.h" +#include "ccw.h" +#include "notify.h" +#include "atomic.h" +#include "dtable.h" +#include "subscriptions.h" +#include "service.h" +#include "input.h" +#include "input/mpegts/tsdemux.h" +#include "tvhcsa.h" +#include "input/mpegts/linuxdvb/linuxdvb_private.h" + +/** + * + */ +TAILQ_HEAD(ccw_queue, ccw); +LIST_HEAD(ccw_service_list, ccw_service); +static struct ccw_queue ccws; +static pthread_cond_t ccw_config_changed; + +/** + * + */ +typedef struct ccw_service { + th_descrambler_t; + + mpegts_service_t *ct_service; + + struct ccw *ct_ccw; + + LIST_ENTRY(ccw_service) ct_link; + + /** + * Status of the key(s) in ct_keys + */ + enum { + CT_UNKNOWN, + CT_RESOLVED, + CT_FORBIDDEN + } ct_keystate; + + tvhcsa_t cs_csa; + + /* buffer for keystruct */ +#if ENABLE_DVBCSA + struct dvbcsa_bs_key_s *ct_key_even; + struct dvbcsa_bs_key_s *ct_key_odd; +#else + void *ct_keys; +#endif + + /* CSA */ + int ct_cluster_size; + uint8_t *ct_tsbcluster; + int ct_fill; +#if ENABLE_DVBCSA + struct dvbcsa_bs_batch_s *ct_tsbbatch_even; + struct dvbcsa_bs_batch_s *ct_tsbbatch_odd; + int ct_fill_even; + int ct_fill_odd; +#endif + + uint8_t ccw_evenkey[8]; + uint8_t ccw_oddkey[8]; + +} ccw_service_t; + +/** + * + */ +typedef struct ccw { + pthread_cond_t ccw_cond; + + TAILQ_ENTRY(ccw) ccw_link; /* Linkage protected via global_lock */ + + struct ccw_service_list ccw_services; + + uint16_t ccw_caid; // CAID + uint16_t ccw_tid; // Transponder ID + uint16_t ccw_sid; // Channel ID + uint8_t ccw_confedkey[8]; // Key + char *ccw_comment; + char *ccw_id; + + int ccw_enabled; + int ccw_running; + int ccw_reconfigure; + +} ccw_t; + +/** + * global_lock is held + * s_stream_mutex is held + */ +static void +ccw_service_destroy(th_descrambler_t *td) +{ + tvhlog(LOG_INFO, "ccw", "Removing CCW key from service"); + + ccw_service_t *ct = (ccw_service_t *)td; + + LIST_REMOVE(td, td_service_link); + LIST_REMOVE(ct, ct_link); + +#if ENABLE_DVBCSA + dvbcsa_bs_key_free(ct->ct_key_odd); + dvbcsa_bs_key_free(ct->ct_key_even); + free(ct->ct_tsbbatch_odd); + free(ct->ct_tsbbatch_even); +#else + free_key_struct(ct->ct_keys); +#endif + free(ct->ct_tsbcluster); + + tvhcsa_destroy(&ct->cs_csa); + free(ct); +} + +/** + * + */ +#if ENABLE_DVBCSA +static int +ccw_descramble(th_descrambler_t *td, service_t *t, struct elementary_stream *st, + const uint8_t *tsb) +{ + ccw_service_t *ct = (ccw_service_t *)td; + uint8_t *pkt; + int xc0; + int ev_od; + int len; + int offset; + int n; + // FIXME: //int residue; + int i; + uint8_t *t0; + + tvhlog(LOG_DEBUG, "ccw", "ccw_descramble"); + + if(ct->ct_keystate == CT_FORBIDDEN) + return 1; + + if(ct->ct_keystate != CT_RESOLVED) + return -1; + + pkt = ct->ct_tsbcluster + ct->ct_fill * 188; + memcpy(pkt, tsb, 188); + ct->ct_fill++; + + do { // handle this packet + xc0 = pkt[3] & 0xc0; + if(xc0 == 0x00) { // clear + break; + } + if(xc0 == 0x40) { // reserved + break; + } + if(xc0 == 0x80 || xc0 == 0xc0) { // encrypted + ev_od = (xc0 & 0x40) >> 6; // 0 even, 1 odd + pkt[3] &= 0x3f; // consider it decrypted now + if(pkt[3] & 0x20) { // incomplete packet + offset = 4 + pkt[4] + 1; + len = 188 - offset; + n = len >> 3; + // FIXME: //residue = len - (n << 3); + if(n == 0) { // decrypted==encrypted! + break; // this doesn't need more processing + } + } else { + len = 184; + offset = 4; + // FIXME: //n = 23; + // FIXME: //residue = 0; + } + if(ev_od == 0) { + ct->ct_tsbbatch_even[ct->ct_fill_even].data = pkt + offset; + ct->ct_tsbbatch_even[ct->ct_fill_even].len = len; + ct->ct_fill_even++; + } else { + ct->ct_tsbbatch_odd[ct->ct_fill_odd].data = pkt + offset; + ct->ct_tsbbatch_odd[ct->ct_fill_odd].len = len; + ct->ct_fill_odd++; + } + } + } while(0); + + if(ct->ct_fill != ct->ct_cluster_size) + return 0; + + if(ct->ct_fill_even) { + ct->ct_tsbbatch_even[ct->ct_fill_even].data = NULL; + dvbcsa_bs_decrypt(ct->ct_key_even, ct->ct_tsbbatch_even, 184); + ct->ct_fill_even = 0; + } + if(ct->ct_fill_odd) { + ct->ct_tsbbatch_odd[ct->ct_fill_odd].data = NULL; + dvbcsa_bs_decrypt(ct->ct_key_odd, ct->ct_tsbbatch_odd, 184); + ct->ct_fill_odd = 0; + } + + t0 = ct->ct_tsbcluster; + for(i = 0; i < ct->ct_fill; i++) { + ts_recv_packet2((mpegts_service_t*)t, t0); + t0 += 188; + } + ct->ct_fill = 0; + return 0; +} +#else +static int +ccw_descramble(th_descrambler_t *td, service_t *t, struct elementary_stream *st, + const uint8_t *tsb) +{ + ccw_service_t *ct = (ccw_service_t *)td; + int r, i; + unsigned char *vec[3]; + uint8_t *t0; + + tvhlog(LOG_DEBUG, "ccw", "ccw_descramble"); + + if(ct->ct_keystate == CT_FORBIDDEN) + return 1; + + if(ct->ct_keystate != CT_RESOLVED) + return -1; + + memcpy(ct->ct_tsbcluster + ct->ct_fill * 188, tsb, 188); + ct->ct_fill++; + + if(ct->ct_fill != ct->ct_cluster_size) + return 0; + + ct->ct_fill = 0; + + vec[0] = ct->ct_tsbcluster; + vec[1] = ct->ct_tsbcluster + ct->ct_cluster_size * 188; + vec[2] = NULL; + + while(1) { + t0 = vec[0]; + r = decrypt_packets(ct->ct_keys, vec); + if(r == 0) + break; + for(i = 0; i < r; i++) { + ts_recv_packet2((mpegts_service_t*)t, t0); + t0 += 188; + } + } + return 0; +} +#endif + +/** + * + */ +static inline elementary_stream_t * +ccw_find_stream_by_caid(service_t *t, int caid) +{ + elementary_stream_t *st; + caid_t *c; + + TAILQ_FOREACH(st, &t->s_components, es_link) { + LIST_FOREACH(c, &st->es_caids, link) { + if(c->caid == caid) + return st; + } + } + return NULL; +} + +/** + * Check if our CAID's matches, and if so, link + * + * global_lock is held + */ +void +ccw_service_start(service_t *t) +{ + ccw_t *ccw; + ccw_service_t *ct; + th_descrambler_t *td; + mpegts_service_t *ms = (mpegts_service_t*)t; + linuxdvb_frontend_t *lfe = (linuxdvb_frontend_t*)ms->s_dvb_active_input; + int adapter_num = lfe->lfe_adapter->la_dvb_number; + char buf[512]; + + lock_assert(&global_lock); + + extern const idclass_t mpegts_service_class; + if (!idnode_is_instance(&t->s_id, &mpegts_service_class)) + return; + + TAILQ_FOREACH(ccw, &ccws, ccw_link) { + if(ccw->ccw_caid == 0) + continue; + + if(ccw_find_stream_by_caid(t, ccw->ccw_caid) == NULL) + continue; + + if(ms->s_dvb_service_id != ccw->ccw_sid) + continue; + + if(ms->s_dvb_mux->mm_tsid != ccw->ccw_tid) + continue; + + tvhlog(LOG_INFO, "ccw","Starting ccw key for service \"%s\" on tuner %d", ms->s_dvb_svcname,adapter_num); + + /* create new ccw service */ + ct = calloc(1, sizeof(ccw_service_t)); +#if ENABLE_DVBCSA + ct->ct_cluster_size = dvbcsa_bs_batch_size(); +#else + ct->ct_cluster_size = get_suggested_cluster_size(); +#endif + ct->ct_tsbcluster = malloc(ct->ct_cluster_size * 188); + +#if ENABLE_DVBCSA + ct->ct_tsbbatch_even = malloc((ct->ct_cluster_size + 1) * + sizeof(struct dvbcsa_bs_batch_s)); + ct->ct_tsbbatch_odd = malloc((ct->ct_cluster_size + 1) * + sizeof(struct dvbcsa_bs_batch_s)); +#endif + +#if ENABLE_DVBCSA + ct->ct_key_even = dvbcsa_bs_key_alloc(); + ct->ct_key_odd = dvbcsa_bs_key_alloc(); +#else + ct->ct_keys = get_key_struct(); +#endif + ct->ct_ccw = ccw; + ct->ct_service = ms; + + memcpy(ct->ccw_evenkey,ccw->ccw_confedkey,8); + memcpy(ct->ccw_oddkey,ccw->ccw_confedkey,8); + +#if ENABLE_DVBCSA + dvbcsa_bs_key_set(ct->ccw_evenkey, ct->ct_key_even); + dvbcsa_bs_key_set(ct->ccw_oddkey, ct->ct_key_odd); +#else + set_even_control_word(ct->ct_keys, ct->ccw_evenkey); + set_odd_control_word(ct->ct_keys, ct->ccw_oddkey); +#endif + + if(ct->ct_keystate != CT_RESOLVED) + tvhlog(LOG_INFO, "ccw", "Obtained key for service \"%s\"",ms->s_dvb_svcname); + + tvhlog(LOG_DEBUG, "ccw", + "Key for service \"%s\" " + "even: %02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x" + " odd: %02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x", + ms->s_dvb_svcname, + ct->ccw_evenkey[0], ct->ccw_evenkey[1], ct->ccw_evenkey[2], ct->ccw_evenkey[3], + ct->ccw_evenkey[4], ct->ccw_evenkey[5], ct->ccw_evenkey[6], ct->ccw_evenkey[7], + ct->ccw_oddkey[0], ct->ccw_oddkey[1], ct->ccw_oddkey[2], ct->ccw_oddkey[3], + ct->ccw_oddkey[4], ct->ccw_oddkey[5], ct->ccw_oddkey[6], ct->ccw_oddkey[7]); + ct->ct_keystate = CT_RESOLVED; + + td = (th_descrambler_t *)ct; + tvhcsa_init(td->td_csa = &ct->cs_csa); + snprintf(buf, sizeof(buf), "ccw-%i-%i", ccw->ccw_tid, ccw->ccw_sid); + td->td_nicename = strdup(buf); + td->td_service = t; + td->dtype = 1; + td->td_stop = ccw_service_destroy; + td->td_descramble = ccw_descramble; + LIST_INSERT_HEAD(&t->s_descramblers, td, td_service_link); + + LIST_INSERT_HEAD(&ccw->ccw_services, ct, ct_link); + } +} + +/** + * + */ +static void +ccw_destroy(ccw_t *ccw) +{ + lock_assert(&global_lock); + TAILQ_REMOVE(&ccws, ccw, ccw_link); + ccw->ccw_running = 0; + pthread_cond_signal(&ccw->ccw_cond); +} + +/** + * + */ +static ccw_t * +ccw_entry_find(const char *id, int create) +{ +// pthread_attr_t attr; +// pthread_t ptid; + char buf[20]; + ccw_t *ccw; + static int tally; + + if(id != NULL) { + TAILQ_FOREACH(ccw, &ccws, ccw_link) + if(!strcmp(ccw->ccw_id, id)) + return ccw; + } + if(create == 0) + return NULL; + + if(id == NULL) { + tally++; + snprintf(buf, sizeof(buf), "%d", tally); + id = buf; + } else { + tally = MAX(atoi(id), tally); + } + + ccw = calloc(1, sizeof(ccw_t)); + pthread_cond_init(&ccw->ccw_cond, NULL); + ccw->ccw_id = strdup(id); + ccw->ccw_running = 1; + + TAILQ_INSERT_TAIL(&ccws, ccw, ccw_link); + +// pthread_attr_init(&attr); +// pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); +// pthread_create(&ptid, &attr, ccw_thread, ccw); +// pthread_attr_destroy(&attr); + + return ccw; +} + +/** + * + */ +static htsmsg_t * +ccw_record_build(ccw_t *ccw) +{ + htsmsg_t *e = htsmsg_create_map(); + char buf[100]; + + htsmsg_add_str(e, "id", ccw->ccw_id); + htsmsg_add_u32(e, "enabled", !!ccw->ccw_enabled); + + htsmsg_add_u32(e, "caid", ccw->ccw_caid); + htsmsg_add_u32(e, "tid", ccw->ccw_tid); + htsmsg_add_u32(e, "sid", ccw->ccw_sid); + + snprintf(buf, sizeof(buf), + "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", + ccw->ccw_confedkey[0], + ccw->ccw_confedkey[1], + ccw->ccw_confedkey[2], + ccw->ccw_confedkey[3], + ccw->ccw_confedkey[4], + ccw->ccw_confedkey[5], + ccw->ccw_confedkey[6], + ccw->ccw_confedkey[7]); + + htsmsg_add_str(e, "key", buf); + htsmsg_add_str(e, "comment", ccw->ccw_comment ?: ""); + + return e; +} + +/** + * + */ +static int +nibble(char c) +{ + switch(c) { + case '0' ... '9': + return c - '0'; + case 'a' ... 'f': + return c - 'a' + 10; + case 'A' ... 'F': + return c - 'A' + 10; + default: + return 0; + } +} + +/** + * + */ +static htsmsg_t * +ccw_entry_update(void *opaque, const char *id, htsmsg_t *values, int maycreate) +{ + ccw_t *ccw; + const char *s; + uint32_t u32; + uint8_t key[8]; + int u, l, i; + + if((ccw = ccw_entry_find(id, maycreate)) == NULL) + return NULL; + + lock_assert(&global_lock); + + if(!htsmsg_get_u32(values, "caid", &u32)) + ccw->ccw_caid = u32; + if(!htsmsg_get_u32(values, "tid", &u32)) + ccw->ccw_tid = u32; + if(!htsmsg_get_u32(values, "sid", &u32)) + ccw->ccw_sid = u32; + + if((s = htsmsg_get_str(values, "key")) != NULL) { + for(i = 0; i < 8; i++) { + while(*s != 0 && !isxdigit(*s)) s++; + if(*s == 0) + break; + u = nibble(*s++); + while(*s != 0 && !isxdigit(*s)) s++; + if(*s == 0) + break; + l = nibble(*s++); + key[i] = (u << 4) | l; + } + memcpy(ccw->ccw_confedkey, key, 8); + } + + if((s = htsmsg_get_str(values, "comment")) != NULL) { + free(ccw->ccw_comment); + ccw->ccw_comment = strdup(s); + } + + if(!htsmsg_get_u32(values, "enabled", &u32)) + ccw->ccw_enabled = u32; + + ccw->ccw_reconfigure = 1; + + pthread_cond_signal(&ccw->ccw_cond); + + pthread_cond_broadcast(&ccw_config_changed); + + return ccw_record_build(ccw); +} + +/** + * + */ +static int +ccw_entry_delete(void *opaque, const char *id) +{ + ccw_t *ccw; + + pthread_cond_broadcast(&ccw_config_changed); + + if((ccw = ccw_entry_find(id, 0)) == NULL) + return -1; + ccw_destroy(ccw); + return 0; +} + +/** + * + */ +static htsmsg_t * +ccw_entry_get_all(void *opaque) +{ + htsmsg_t *r = htsmsg_create_list(); + ccw_t *ccw; + + TAILQ_FOREACH(ccw, &ccws, ccw_link) + htsmsg_add_msg(r, NULL, ccw_record_build(ccw)); + + return r; +} + +/** + * + */ +static htsmsg_t * +ccw_entry_get(void *opaque, const char *id) +{ + ccw_t *ccw; + + + if((ccw = ccw_entry_find(id, 0)) == NULL) + return NULL; + return ccw_record_build(ccw); +} + +/** + * + */ +static htsmsg_t * +ccw_entry_create(void *opaque) +{ + pthread_cond_broadcast(&ccw_config_changed); + return ccw_record_build(ccw_entry_find(NULL, 1)); +} + +/** + * + */ +static const dtable_class_t ccw_dtc = { + .dtc_record_get = ccw_entry_get, + .dtc_record_get_all = ccw_entry_get_all, + .dtc_record_create = ccw_entry_create, + .dtc_record_update = ccw_entry_update, + .dtc_record_delete = ccw_entry_delete, + .dtc_read_access = ACCESS_ADMIN, + .dtc_write_access = ACCESS_ADMIN, + .dtc_mutex = &global_lock, +}; + +/** + * + */ +void +ccw_init(void) +{ + dtable_t *dt; + + TAILQ_INIT(&ccws); + + pthread_cond_init(&ccw_config_changed, NULL); + + dt = dtable_create(&ccw_dtc, "ccw", NULL); + dtable_load(dt); + +} + +/** + * + */ +void +ccw_done(void) +{ + ccw_t *ccw; + + dtable_delete("ccw"); + pthread_mutex_lock(&global_lock); + //pthread_mutex_lock(&ccw_mutex); + while ((ccw = TAILQ_FIRST(&ccws)) != NULL) + ccw_destroy(ccw); + //pthread_mutex_unlock(&ccw_mutex); + pthread_mutex_unlock(&global_lock); +} diff -U 3 -H -d -r -N -- a/src/descrambler/ccw.h b/src/descrambler/ccw.h --- a/src/descrambler/ccw.h 1970-01-01 01:00:00.000000000 +0100 +++ b/src/descrambler/ccw.h 2014-07-07 14:57:01.756872537 +0100 @@ -0,0 +1,35 @@ + /* + * tvheadend, CCW + * Copyright (C) 2012 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef CCW_H_ +#define CCW_H_ + +#if ENABLE_DVBCSA +#include +#else +#include "ffdecsa/FFdecsa.h" +#endif + +void ccw_init(void); + +void ccw_done(void); + +void ccw_service_start(struct service *t); + +#endif /* CCW_H_ */ + diff -U 3 -H -d -r -N -- a/src/descrambler/cwc.c b/src/descrambler/cwc.c --- a/src/descrambler/cwc.c 2014-07-05 08:12:15.000000000 +0100 +++ b/src/descrambler/cwc.c 2014-07-07 13:44:58.872843298 +0100 @@ -1997,6 +1997,7 @@ snprintf(buf, sizeof(buf), "cwc-%s-%i", cwc->cwc_hostname, cwc->cwc_port); td->td_nicename = strdup(buf); td->td_service = t; + td->dtype = 0; td->td_stop = cwc_service_destroy; LIST_INSERT_HEAD(&t->s_descramblers, td, td_service_link); diff -U 3 -H -d -r -N -- a/src/descrambler/descrambler.c b/src/descrambler/descrambler.c --- a/src/descrambler/descrambler.c 2014-07-05 08:12:15.000000000 +0100 +++ b/src/descrambler/descrambler.c 2014-07-07 13:48:48.664844852 +0100 @@ -19,6 +19,7 @@ #include "tvheadend.h" #include "descrambler.h" #include "cwc.h" +#include "ccw.h" #include "capmt.h" #include "ffdecsa/FFdecsa.h" #include "input.h" @@ -86,6 +87,7 @@ #endif #if ENABLE_CAPMT capmt_init(); + ccw_init(); #endif #if (ENABLE_CWC || ENABLE_CAPMT) && !ENABLE_DVBCSA ffdecsa_init(); @@ -97,6 +99,7 @@ { #if ENABLE_CAPMT capmt_done(); + ccw_done(); #endif #if ENABLE_CWC cwc_done(); @@ -118,6 +121,7 @@ #endif #if ENABLE_CAPMT capmt_service_start(t); + ccw_service_start(t); #endif if (t->s_descramble == NULL) { t->s_descramble = dr = calloc(1, sizeof(th_descrambler_runtime_t)); @@ -254,6 +258,8 @@ } if (td->td_keystate != DS_RESOLVED) continue; + if (td->dtype == 1) + continue; if (dr->dr_buf.sb_ptr > 0) { for (off = 0, size = dr->dr_buf.sb_ptr; off < size; off += 188) { tsb2 = dr->dr_buf.sb_data + off; diff -U 3 -H -d -r -N -- a/src/descrambler.h b/src/descrambler.h --- a/src/descrambler.h 2014-07-05 08:12:15.000000000 +0100 +++ b/src/descrambler.h 2014-07-07 13:50:39.412845601 +0100 @@ -52,6 +52,11 @@ void (*td_stop) (struct th_descrambler *d); void (*td_caid_change)(struct th_descrambler *d); + int dtype; + + int (*td_descramble)(struct th_descrambler *d, struct service *t, + struct elementary_stream *st, const uint8_t *tsb); + } th_descrambler_t; typedef struct th_descrambler_runtime { diff -U 3 -H -d -r -N -- a/src/input/mpegts/tsdemux.c b/src/input/mpegts/tsdemux.c --- a/src/input/mpegts/tsdemux.c 2014-07-05 08:12:15.000000000 +0100 +++ b/src/input/mpegts/tsdemux.c 2014-07-07 13:54:04.816846990 +0100 @@ -156,7 +156,8 @@ (mpegts_service_t *t, const uint8_t *tsb, int64_t *pcrp, int table) { elementary_stream_t *st; - int pid, r; + int pid, n, m, r; + th_descrambler_t *td; int error = 0; int64_t pcr = PTS_UNSET; @@ -198,6 +199,11 @@ pid = (tsb[1] & 0x1f) << 8 | tsb[2]; + if(pid==0x1FFF){ // ignore NULL stream + pthread_mutex_unlock(&t->s_stream_mutex); + return 0; + } + st = service_stream_find((service_t*)t, pid); /* Extract PCR */ @@ -224,6 +230,23 @@ t->s_scrambled_seen |= service_is_encrypted((service_t*)t); /* scrambled stream */ + n = m = 0; + + LIST_FOREACH(td, &t->s_descramblers, td_service_link) { + n++; + + if(td->dtype == 1) { + r = td->td_descramble(td, (service_t*)t, st, tsb); + if(r == 0) { + pthread_mutex_unlock(&t->s_stream_mutex); + return 1; + } + } + + if(r == 1) + m++; + } + r = descrambler_descramble((service_t *)t, st, tsb); if(r > 0) { pthread_mutex_unlock(&t->s_stream_mutex); diff -U 3 -H -d -r -N -- a/src/version.c b/src/version.c --- a/src/version.c 1970-01-01 01:00:00.000000000 +0100 +++ b/src/version.c 2014-07-07 14:18:30.796856906 +0100 @@ -0,0 +1 @@ +const char *tvheadend_version = "0.0.0~unknown"; diff -U 3 -H -d -r -N -- a/src/webui/extjs.c b/src/webui/extjs.c --- a/src/webui/extjs.c 2014-07-05 08:12:15.000000000 +0100 +++ b/src/webui/extjs.c 2014-07-07 13:55:31.760847578 +0100 @@ -150,6 +150,7 @@ #endif #if ENABLE_CAPMT extjs_load(hq, "static/app/capmteditor.js"); + extjs_load(hq, "static/app/ccweditor.js"); #endif extjs_load(hq, "static/app/tvadapters.js"); extjs_load(hq, "static/app/idnode.js"); diff -U 3 -H -d -r -N -- a/src/webui/static/app/ccweditor.js b/src/webui/static/app/ccweditor.js --- a/src/webui/static/app/ccweditor.js 1970-01-01 01:00:00.000000000 +0100 +++ b/src/webui/static/app/ccweditor.js 2014-07-07 12:49:32.100820796 +0100 @@ -0,0 +1,87 @@ +tvheadend.ccweditor = function() { + var fm = Ext.form; + + var enabledColumn = new Ext.grid.CheckColumn({ + header : "Enabled", + dataIndex : 'enabled', + width : 60 + }); + + function setMetaAttr(meta, record) { + var enabled = record.get('enabled'); + if (enabled == 1) { + meta.attr = 'style="color:green;"'; + } + else { + meta.attr = 'style="color:red;"'; + } + } + + var cm = new Ext.grid.ColumnModel({ + defaultSortable: true, + columns: [ enabledColumn, { + header: "CAID", + dataIndex: 'caid', + renderer: function(value, metadata, record, row, col, store) { + setMetaAttr(metadata, record); + return value; + }, + editor: new fm.TextField({allowBlank: false}) + }, { + header: "Mux ID", + dataIndex: 'tid', + renderer: function(value, metadata, record, row, col, store) { + setMetaAttr(metadata, record); + return value; + }, + editor: new fm.TextField({allowBlank: false}) + }, { + header: "SID", + dataIndex: 'sid', + renderer: function(value, metadata, record, row, col, store) { + setMetaAttr(metadata, record); + return value; + }, + editor: new fm.TextField({allowBlank: false}) + }, { + header: "Key", + dataIndex: 'key', + width: 200, + renderer: function(value, metadata, record, row, col, store) { + setMetaAttr(metadata, record); + return value; + }, + editor: new fm.TextField({allowBlank: false}) + }, { + header : "Comment", + dataIndex : 'comment', + width : 400, + renderer : function(value, metadata, record, row, col, store) { + setMetaAttr(metadata, record); + return value; + }, + editor : new fm.TextField() + } ]}); + + var rec = Ext.data.Record.create([ 'enabled','caid','tid','sid','key','comment' ]); + + store = new Ext.data.JsonStore({ + root: 'entries', + fields: rec, + url: "tablemgr", + autoLoad: true, + id: 'id', + baseParams: {table: 'ccw', op: "get"} + }); + + tvheadend.comet.on('ccwStatus', function(server) { + var rec = store.getById(server.id); + if(rec){ + rec.set('connected', server.connected); + } + }); + + return new tvheadend.tableEditor('CCW Keys', 'ccw', cm, rec, + [enabledColumn], store, + 'config_ccw.html', 'key'); +} diff -U 3 -H -d -r -N -- a/src/webui/static/app/tvheadend.js b/src/webui/static/app/tvheadend.js --- a/src/webui/static/app/tvheadend.js 2014-07-05 08:12:15.000000000 +0100 +++ b/src/webui/static/app/tvheadend.js 2014-07-07 13:57:53.308848536 +0100 @@ -285,8 +285,10 @@ tabs2 = []; if (tvheadend.capabilities.indexOf('cwc') !== -1) tabs2.push(new tvheadend.cwceditor); - if (tvheadend.capabilities.indexOf('capmt') !== -1) + if (tvheadend.capabilities.indexOf('capmt') !== -1) { tabs2.push(new tvheadend.capmteditor); + tabs2.push(new tvheadend.ccweditor); + } if (tabs2.length > 0) { tvheadend.conf_csa = new Ext.TabPanel({ activeTab: 0,