From 552ed78d7faa242b694b45dc5b78c3da87b47113 Mon Sep 17 00:00:00 2001 From: dieter kovacic Date: Tue, 24 Sep 2002 08:17:56 +0000 Subject: jdl AT xdv.org improved promiscous~ to listen to only one interface svn path=/trunk/externals/ext13/; revision=151 --- doc/promiscous~.pd | 19 +++++----- promiscous~.c | 107 +++++++++++++++++++++++++++++++++++------------------ 2 files changed, 79 insertions(+), 47 deletions(-) diff --git a/doc/promiscous~.pd b/doc/promiscous~.pd index eea18cf..eca5e11 100644 --- a/doc/promiscous~.pd +++ b/doc/promiscous~.pd @@ -1,10 +1,9 @@ -#N canvas 0 0 450 300 10; -#X obj 136 209 promiscous~; -#X obj 127 246 dac~; -#X msg 201 35 bang; -#X msg 201 61 \; pd dsp 1; -#X text 69 166 thats passing by \, into a sound signal.; -#X text 68 146 you must be root to turn all network traffic \,; -#X connect 0 0 1 0; -#X connect 0 0 1 1; -#X connect 2 0 3 0; +#N canvas 291 56 450 300 10; +#X obj 89 151 dac~; +#X text 15 19 promiscous~ converts all network traffic; +#X text 15 41 from a given interface to signal.; +#X text 15 196 pd must run with root permissions \, to catch the traffic +; +#X obj 100 93 promiscous~ eth0; +#X connect 4 0 0 0; +#X connect 4 0 0 1; diff --git a/promiscous~.c b/promiscous~.c index 1075eb7..49cc4e5 100644 --- a/promiscous~.c +++ b/promiscous~.c @@ -2,6 +2,7 @@ #include "ext13.h" #include #include +#include #include #include #include @@ -12,6 +13,7 @@ #include #include #include +#include @@ -36,14 +38,37 @@ typedef struct _promiscous_tilde static int setnic_promisc(char *nic_name){ int sock; // socket desc struct ifreq f; + struct sockaddr_ll sll; - if( (sock = socket(AF_INET, SOCK_PACKET, htons(ETH_P_ALL))) < 0){ + memset(&sll, 0, sizeof(sll)); + sll.sll_family = AF_PACKET; + sll.sll_protocol = htons(ETH_P_ALL); + + //if( (sock = socket(AF_INET, SOCK_PACKET, htons(ETH_P_ALL))) < 0){ + if( (sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0){ post("promiscous~ failed open socket, must be root (euid)"); return(-1); } - strcpy(f.ifr_name, nic_name); + //strcpy(f.ifr_name, nic_name); + strncpy(f.ifr_name, nic_name, sizeof(f.ifr_name)); + + if (ioctl(sock, SIOCGIFINDEX, &f) < 0) { + post ("promiscous~: error on setting if index"); + } + +#ifdef DEBUG + post("promiscous~: ifname : %s",f.ifr_name); + post("promiscous~: ifindex: %d",f.ifr_ifindex); +#endif + + sll.sll_ifindex = f.ifr_ifindex; + + if (bind(sock, (struct sockaddr *) &sll, sizeof(sll)) < 0) { + post("promiscous~: couldnt bind to interface: %s", f.ifr_name); + } + if( ioctl(sock, SIOCGIFFLAGS, &f) < 0) { - post("promisous~ failed to get interface flags, continue anyway"); + post("promiscous~ failed to get interface flags, continue anyway"); return(sock); } f.ifr_flags |= IFF_PROMISC; @@ -56,37 +81,42 @@ static int setnic_promisc(char *nic_name){ t_int *promiscous_tilde_perform(t_int *w) { - t_promiscous_tilde* x = (t_promiscous_tilde*)(w[1]); - int n = (int)(w[3]);/*number of samples*/ - int l = 0; - int ll = 0; - int r; - static unsigned char packet; - fd_set fdset; - struct timeval timeout; - t_float* out = (t_float *)w[2]; - int cptr[n]; - - if (x->opened){ - timeout.tv_sec = 0; - timeout.tv_usec = 0; - FD_ZERO(&fdset); - FD_SET(x->sock,&fdset); - if (r = select(x->sock+1,&fdset,NULL,NULL,&timeout) && x->sock){ - /* l = recv(x->sock, &cptr,n, 0);*/ - l = read(x->sock, (char*) &cptr,n); - }; - if (l < 0) l = 0; - while (l--){ - *out++ = cptr[n-l] / 32767.; - ll++; - } - } - while (ll < n){ - *out++ = 0.; - ll++; - } - return (w + 4); + t_promiscous_tilde* x = (t_promiscous_tilde*)(w[1]); + int n = (int)(w[3]);/*number of samples*/ + int l = 0; + int ll = 0; + int r; + int t; + static unsigned char packet; + fd_set fdset; + struct timeval timeout; + t_float* out = (t_float *)w[2]; + int cptr[n]; + + if (x->opened){ + timeout.tv_sec = 0; + timeout.tv_usec = 0; + FD_ZERO(&fdset); + FD_SET(x->sock,&fdset); + if (r = select(x->sock+1,&fdset,NULL,NULL,&timeout) && x->sock){ + l = recv(x->sock, &cptr,n, 0); + //l = read(x->sock, (char*) &cptr,n); + }; + if (l < 0) l = 0; + while (l--){ + t = cptr[n-l]; + //post("sample: %d",t); + //*out++ = t / 32767.; + *out++ = (float) t / (float) pow(2, 32); + ll++; + } + } + while (ll < n){ + *out++ = 0.; + //post("sample: %f",*out); + ll++; + } + return (w + 4); } static void promiscous_tilde_dsp(t_promiscous_tilde *x, t_signal **sp) @@ -101,12 +131,15 @@ static void promiscous_tilde_free(t_promiscous_tilde *x){ } -static void *promiscous_tilde_new() +static void *promiscous_tilde_new(t_symbol *ifname) { t_promiscous_tilde *x = (t_promiscous_tilde *)pd_new(promiscous_tilde_class); outlet_new(&x->x_obj, gensym("signal")); - x->interface = gensym("eth0"); + if (*ifname->s_name) + x->interface = ifname; + else + x->interface = gensym(DEFAULT_NIC); if((x->sock = setnic_promisc(x->interface->s_name))<0){ post ("could not open interface"); @@ -121,6 +154,6 @@ static void *promiscous_tilde_new() void promiscous_tilde_setup(void) { promiscous_tilde_class = class_new(gensym("promiscous~"), (t_newmethod) promiscous_tilde_new, 0, - sizeof(t_promiscous_tilde), CLASS_NOINLET, 0); + sizeof(t_promiscous_tilde), CLASS_NOINLET, A_DEFSYM, 0); class_addmethod(promiscous_tilde_class, (t_method) promiscous_tilde_dsp, gensym("dsp"), 0); } -- cgit v1.2.1