void tcpreadall(int sockid, const char *buff, int len, int maxtime) { smallset_t set; postime_t end = getcurtime() + maxtime, cur; int ret, x; set.init(1); set.set(0, sockid); while (len > 0) { while ((cur = getcurtime()) <= end) { set.init(1); set.set(0, sockid); x = end.after(cur); if (x > 1000) x = 1000; set.wait(x); if (set.isdata(0) || posclient_quitflag) break; } // set.check(); if (!set.isdata(0)) { throw PException("Could not read TCP message: no data"); } ret = tcpread(sockid, buff, len); if (ret == 0) throw PException("TCP client hung up!"); buff += ret; len -= ret; } }
/* * Call select and return results. * Input: interesting file descriptors and absolute timeout * Output: select return value (-1 or num fds ready) and fd_sets * Return: 0 (for i/o available or timeout) or error code. */ krb5_error_code krb5int_cm_call_select (const struct select_state *in, struct select_state *out, int *sret) { struct timeval now, *timo; krb5_error_code e; *out = *in; e = getcurtime(&now); if (e) return e; if (out->end_time.tv_sec == 0) timo = 0; else { timo = &out->end_time; out->end_time.tv_sec -= now.tv_sec; out->end_time.tv_usec -= now.tv_usec; if (out->end_time.tv_usec < 0) { out->end_time.tv_usec += 1000000; out->end_time.tv_sec--; } if (out->end_time.tv_sec < 0) { *sret = 0; return 0; } } /*LINTED*/ dprint("selecting on max=%d sockets [%F] timeout %t\n", /*LINTED*/ out->max, &out->rfds, &out->wfds, &out->xfds, out->max, timo); *sret = select(out->max, &out->rfds, &out->wfds, &out->xfds, timo); e = SOCKET_ERRNO; /* Solaris Kerberos */ #ifdef DEBUG /*LINTED*/ dprint("select returns %d", *sret); if (*sret < 0) /*LINTED*/ dprint(", error = %E\n", e); else if (*sret == 0) /*LINTED*/ dprint(" (timeout)\n"); else /*LINTED*/ dprint(":%F\n", &out->rfds, &out->wfds, &out->xfds, out->max); #endif if (*sret < 0) return e; return 0; }
void tcpsendall(int sockid, const char *buff, int len, int maxtime) { smallset_t set; postime_t end = getcurtime() + maxtime, cur; int ret, x; set.init(1); set.set(0, sockid); while (len > 0) { while ((cur = getcurtime()) <= end) { set.init(1); set.set(0, sockid); x = end.after(cur); if (x > 1000) x = 1000; set.waitwrite(x); if (set.canwrite(0) || posclient_quitflag) break; } if (!set.canwrite(0)) throw PException("Could not send buffer"); ret = tcpsend(sockid, buff, len); buff += ret; len -= ret; } }
// Update official timestamp, own timestamp and sequence number in the RTP header. // The actual RTP message is stored in tx.udp_payload. int update_RTP(libnet_t *l, libnet_ptag_t t) { u_int8_t *ptr; struct mz_timestamp ts; tx.rtp_sqnr++; tx.rtp_stmp+=160; // TODO: different values for different codecs // update SQNR ptr = (u_int8_t*) &tx.rtp_sqnr; tx.udp_payload[2] = *(ptr+1); tx.udp_payload[3] = *ptr; // update official timestamp ptr = (u_int8_t*) &tx.rtp_stmp; tx.udp_payload[4] = *(ptr+3); tx.udp_payload[5] = *(ptr+2); tx.udp_payload[6] = *(ptr+1); tx.udp_payload[7] = *ptr; // update own timestamp getcurtime(&ts); // Now add TX timestamp: mops_hton4 ((u_int32_t*) &ts.sec, &tx.udp_payload[16]); mops_hton4 ((u_int32_t*) &ts.nsec, &tx.udp_payload[20]); t = libnet_build_udp(tx.sp, tx.dp, tx.udp_len, tx.udp_sum, tx.udp_payload, tx.udp_payload_s, l, t); if (t == -1) { fprintf(stderr," mz/send_frame: RTP header update failed!\n"); exit (1); } return 0; }
/*--------------------*/ void calthread(void *tag) { Ttime ct,deltaTtime; w32 delta; int sddburst=0; //printf("calthread:\n"); threadactive=1; if(DBGCMDS) { char msg[100]; sprintf(msg, "calthread start. DFS.ffala:%d", DFS->getffala()); prtLog(msg); }; while(1) { int ndit; heartbeat++; if(heartbeat>0xfffffff0) heartbeat=0; ndit= findnextcDET(); // find closest one in time. ndit: index into ACTIVEDETS //printf("det:%s %d %d\n", ACTIVEDETS[ndit].name, ACTIVEDETS[ndit].caltime.secs, ACTIVEDETS[ndit].caltime.usecs); if(ndit==-1) goto STP; // cal. triggers not needed, stop thread if(globalcalDET(ndit)==0) { delDET(ndit); if(NACTIVE==0) goto STP; continue; }; getcurtime(&ct); if( ACTIVEDETS[ndit].caltime.secs>0) { w32 milisecs; int rcgt; delta=diffnowbefore(&ACTIVEDETS[ndit].caltime, &ct); if(delta>100) { usleep(delta); }; if(globalcalDET(ndit)==0) { delDET(ndit); if(NACTIVE==0) goto STP; continue; }; // generate calib. trigger: if(cshmGlobFlag(FLGignoreGCALIB)==0) { w32 orbitn[3]; // orbitn tsec tusec rcgt= GenSwtrg(1, 'c', ACTIVEDETS[ndit].logroc&0xf, ACTIVEDETS[ndit].calbc,1<<ndit, swtriggers_gcalib, orbitn); ct.secs= orbitn[1]; ct.usecs= orbitn[2]; if(rcgt==12345678) { delDET(ndit); if(NACTIVE==0) goto STP; continue; } else { w32 rcgtold; rcgtold= rcgt; if((rcgt & 0xffffff00)== 0xffffff00) { // spec. case: one cal. trigger char line[80]; rcgt= rcgt & 0xff; // update pp_N.data file: det tsec tusec orbitid rc sprintf(line,"%x %x %x %x %x\n", ndit, orbitn[1], orbitn[2],orbitn[0], rcgt); DFS->write_df(ndit, line); } else { char msg[100]; sprintf(msg,"ERROR: got 0x%x instead of 0xffffff.., deactivating det. %d...", rcgtold, ndit); prtLog(msg); delDET(ndit); if(NACTIVE==0) goto STP; continue; }; }; ACTIVEDETS[ndit].attempts++; } else { rcgt=0; // cal. trigger not generated (disabled during start/stop part) }; // getcurtime(&ct); // time of cal. trigger just attempted to be generated. Commented out 8.1.2017 // movd up ACTIVEDETS[ndit].attempts++; if(rcgt==4) { // 4:l2a OK, generated if(ACTIVEDETS[ndit].lasttime.secs==0) { milisecs=0; } else { milisecs= diffnowbefore(&ct, &ACTIVEDETS[ndit].lasttime)/1000; }; ACTIVEDETS[ndit].sent++; //printf("CT:%s \t %d\t%d ms:%d\n", ACTIVEDETS[ndit].name, ct.secs, ct.usecs, milisecs); //printf("CT:%s ms:%d\n", ACTIVEDETS[ndit].name, milisecs); ACTIVEDETS[ndit].lasttime.secs= ct.secs; ACTIVEDETS[ndit].lasttime.usecs= ct.usecs; }; // usleep(100); // do we need this (waiting for the completion)? /* if(ACTIVEDETS[ndit].attempts > 200 ) { emptyDET(); goto STP; }; */ }; // the planning of next cal. trigger for ndit detector: // NextTime= CurrentTime + periodms[ndit] i.e. // IS THE SAME IN EITHER CASE (successful or unsuccsessful) if( ACTIVEDETS[ndit].caltime.secs==0) { // wait 2 secs before 1st cal. event char msg[200]; sprintf(msg, "1st planning:%s \t %d\t%d", ACTIVEDETS[ndit].name, ct.secs, ct.usecs); prtLog(msg); deltaTtime.secs=2; deltaTtime.usecs=0; } else { deltaTtime.secs=0; deltaTtime.usecs=1000*ACTIVEDETS[ndit].periodms; }; if(ndit==SDD) { // SDD 3x50ms every 15minutes sddburst++; if(sddburst==3) { sddburst=0; deltaTtime.usecs= 15*60*1000000; }; }; addtime(&ct, &deltaTtime); ACTIVEDETS[ndit].caltime.secs= ct.secs; ACTIVEDETS[ndit].caltime.usecs= ct.usecs; //printf("det:%s next:%d %d\n", ACTIVEDETS[ndit].name, ct.secs,ct.usecs); }; STP: threadactive=0; if(DBGCMDS) { prtLog("calthread stop."); }; }
void work(void) { int i, id, maxi, listenfd, clientfd, maxfd; char sip[INET_ADDRSTRLEN], dip[INET_ADDRSTRLEN], clientip[INET_ADDRSTRLEN]; int client[FD_SETSIZE], nready; sigset_t zeromask; ssize_t n; fd_set rset, allset; struct sockaddr_in clientaddr, servaddr; socklen_t socklen; xvalue ret[NUM]; char sql[300], *time; MYSQL my_conn; MYSQL_RES *res; MYSQL_ROW row; mysql_init(&my_conn); if ( ! mysql_real_connect(&my_conn, MY_HOST, MY_USER, MY_PASS, MY_DB, 3309, MY_SOCK,0)) { syslog(LOG_ERR, "Connection error %d: %s\n", mysql_errno(&my_conn), mysql_error(&my_conn)); exit(2); } sigemptyset(&zeromask); listenfd = socket(AF_INET, SOCK_STREAM, 0); if (listenfd == -1){ syslog(LOG_ERR, "socket: %s", strerror(errno)); exit(1); } servaddr.sin_family = AF_INET; servaddr.sin_port = htons(PORT); servaddr.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0){ syslog(LOG_ERR, "bind: %s", strerror(errno)); exit(1); } id = 1; if ((setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &id, sizeof(int))) < 0){ syslog(LOG_ERR, "setsockopt: %s", strerror(errno)); exit(1); } maxfd = listenfd; maxi = -1; for(i = 0; i < FD_SETSIZE; i++) client[i] = -1; FD_ZERO(&allset); FD_SET(listenfd, &allset); listen(listenfd, 1024); for(;;) { rset = allset; if ((nready = pselect(maxfd + 1, &rset, NULL, NULL, NULL, &zeromask)) < 0){ syslog(LOG_ERR, "pselect: %s", strerror(errno)); exit(1); } if (FD_ISSET(listenfd, &rset)){ socklen = sizeof(clientaddr); clientfd = accept(listenfd, (struct sockaddr *)&clientaddr, &socklen); for(i = 0; i < FD_SETSIZE; i++) if (client[i] < 0){ client[i] = clientfd; break; } FD_SET(clientfd, &allset); if (clientfd > maxfd) maxfd = clientfd; if (i > maxi) maxi = i; if (--nready <= 0) continue; } for(i = 0; i <= maxi; i++) { if (client[i] < 0) continue; if (FD_ISSET(client[i], &rset)){ socklen = sizeof(clientaddr); if (getpeername(client[i], &clientaddr, &socklen) >= 0) inet_ntop(AF_INET, &clientaddr.sin_addr.s_addr, clientip, INET_ADDRSTRLEN); else{ close(client[i]); FD_CLR(client[i], &allset); client[i] = -1; continue; } snprintf(sql, sizeof(sql), "select id from svr_info where ip = \"%s\"", clientip); if (mysql_query(&my_conn, sql)){ syslog(LOG_ERR, "Mysql query error %d: %s", mysql_errno(&my_conn), mysql_error(&my_conn)); exit(2); } res = mysql_store_result(&my_conn); if ((row = mysql_fetch_row(res)) != NULL) id = atoi(row[0]); else{ syslog(LOG_ERR, "Not find %s in svr_info", clientip); close(client[i]); FD_CLR(client[i], &allset); client[i] = -1; continue; } mysql_free_result(res); if ((n = Read(client[i], (char *)&ret, sizeof(ret))) < 0){ syslog(LOG_ERR, "read error code :%i", errno); close(client[i]); FD_CLR(client[i], &allset); client[i] = -1; continue; } if (errno == EHOSTDOWN){ close(client[i]); FD_CLR(client[i], &allset); client[i] = -1; } // syslog(LOG_NOTICE, "client:%s recv %i bytes", clientip, n); if (n > 0){ int j; for(j = 0; j < n / sizeof(xvalue); j++){ if (inet_ntop(AF_INET, &ret[j].v.sip, sip, INET_ADDRSTRLEN) == NULL){ syslog(LOG_ERR, "inet_ntop: %s", strerror(errno)); continue; } if ((time = getcurtime()) == NULL){ syslog(LOG_ERR, "Get cur time error"); exit(3); } if (ret[0].v.other == 0){ if (j == 0) snprintf(sql, sizeof(sql), "insert into external(svr_id, sip, packs, bytes, updatetime) values(%u, \"0.0.0.0\", %u, %lu, \"%s\")", id, ret[0].fpacket, ret[0].fbytes, time); else snprintf(sql, sizeof(sql), "insert into external(svr_id, sip, packs, fpacks, rpacks, tcp, udp, icmp, other, updatetime, bytes, rbytes, fbytes) values(%u, \"%s\", %u, %u, %u, %u, %u, %u, %u, \"%s\", %lu, %lu, %lu)", id, sip, ret[j].v.packets, ret[j].fpacket, ret[j].v.packets - ret[j].fpacket, ret[j].v.tcp, ret[j].v.udp, ret[j].v.icmp, ret[j].v.other, time, ret[j].v.bytes, ret[j].v.bytes - ret[j].fbytes, ret[j].fbytes); } else if (ret[0].v.other == 1){ if (j == 0) snprintf(sql, sizeof(sql), "insert into internal(svr_id, sip, packs, bytes, updatetime) values(%u, \"0.0.0.0\", %u, %lu, \"%s\")", id, ret[0].fpacket, ret[0].fbytes, time); else snprintf(sql, sizeof(sql), "insert into internal(svr_id, sip, packs, fpacks, rpacks, tcp, udp, icmp, other, updatetime, mth, bytes, rbytes, fbytes) values(%u, \"%s\", %u, %u, %u, %u, %u, %u, %u, \"%s\", \"%s\", %lu, %lu, %lu)", id, sip, ret[j].v.packets, ret[j].fpacket, ret[j].v.packets - ret[j].fpacket, ret[j].v.tcp, ret[j].v.udp, ret[j].v.icmp, ret[j].v.other, time, &time[17], ret[j].v.bytes, ret[j].v.bytes - ret[j].fbytes, ret[j].fbytes); } free(time); //syslog(LOG_NOTICE, sql); if (mysql_query(&my_conn, sql)){ syslog(LOG_ERR, "Mysql query error %d: %s", mysql_errno(&my_conn), mysql_error(&my_conn)); exit(2); } } } if (--nready <= 0) break; } } } mysql_close(&my_conn); }
int create_rtp_packet() { u_int8_t byte1, byte2; u_int16_t seqnr; u_int8_t ssrc[4] = {0,0,0,0} ; int ssrc_s = 0; u_int8_t *ptr; char argval[MAX_PAYLOAD_SIZE]; unsigned int rtp_payload_size=160; struct mz_timestamp ts; if ( (getarg(tx.arg_string,"help", NULL)==1) && (mode==RTP) ) { if (mz_port) { cli_print(gcli, "%s", MZ_RTP_HELP); return -1; } else { fprintf(stderr,"\n" MAUSEZAHN_VERSION "\n%s", MZ_RTP_HELP); exit(0); } } if (getarg(tx.arg_string,"pld", argval)==1) { rtp_payload_size = (unsigned int) str2int(argval); } if (getarg(tx.arg_string,"codec", argval)==1) { tx.delay = 20000; } if (getarg(tx.arg_string,"ssrc", argval)==1) { ssrc_s = str2hex(argval, ssrc, 4); if (ssrc_s<0) { fprintf(stderr, " mz/rtp: invalid ssrc!\n"); return -1; } } // TODO: Optional arguments for RTP // Create header: // // Byte 1 // // +--+--+--+--+--+--+--+--+ // | ver | P| X| CSRC Count| // +--+--+--+--+--+--+--+--+ // // Default: ver=2, Padding=0, Extension_Header=1, CSRC_Count=0 => 10 0 1 0000 = 0x90 byte1 = 0x90; // Byte 2 // // +--+--+--+--+--+--+--+--+ // | M| Payload Type | // +--+--+--+--+--+--+--+--+ // // Marker=0, Payload Type=0 (or 8 alternatively) byte2 = 0x00; // Bytes 3,4 // // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ // | Sequence Number | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ seqnr = 0x0000; // Bytes 5,6,7,8 // // Timestamp /* done below */ // // Bytes 9,10,11,12 // // Synchronization Source Identifier // if (ssrc_s==0) str2hex("ca:fe:fe:ed", ssrc, 4); // Bytes 13,14,15,16 // // CSRC - Contributing Source Identifiers (optional, only used by mixers) // // csrc = 0x00000000; // Bytes 17,18,19,20 // // Header Extension (optional) NOT USED HERE! // // !!! Thus payload begins with index 16 in a C array !!! // ------------ Now combine all fields: ---------------- tx.udp_payload[0] = byte1; tx.udp_payload[1] = byte2; ptr = (u_int8_t*) &seqnr; tx.udp_payload[2] = *(ptr+1); tx.udp_payload[3] = *ptr; // TIMESTAMP: will be linearly increased, e.g. using 20msec G.711: 0, 160, 320, ... tx.udp_payload[4] = 0x00; tx.udp_payload[5] = 0x00; tx.udp_payload[6] = 0x00; tx.udp_payload[7] = 0x00; tx.udp_payload[8] = ssrc[0]; tx.udp_payload[9] = ssrc[1]; tx.udp_payload[10] = ssrc[2]; tx.udp_payload[11] = ssrc[3]; /* ptr = (u_int8_t*) &csrc; tx.udp_payload[12] = *(ptr+3); tx.udp_payload[13] = *(ptr+2); tx.udp_payload[14] = *(ptr+1); tx.udp_payload[15] = *ptr; */ // Add the NEW Mausezahn extension header (see mops_ext_rtp.c) tx.udp_payload[12] = 0xca; // identifier tx.udp_payload[13] = 0xca; tx.udp_payload[14] = 0x00; tx.udp_payload[15] = 0x04; // length getcurtime(&ts); // Now add TX timestamp: mops_hton4 ((u_int32_t*) &ts.sec, &tx.udp_payload[16]); mops_hton4 ((u_int32_t*) &ts.nsec, &tx.udp_payload[20]); // NOTE: The remaining 8 bytes of this extension header are set to zero // via the following code. memset(&tx.udp_payload[24], 0x00, (rtp_payload_size-12)); // payload (considering our 8 byte timestamp) tx.udp_payload_s = 12 + rtp_payload_size; // the latter ist the payload size // ---------- now hand over to UDP ----------------- tx.dp = 30000; tx.sp = 30000; tx.udp_len = 8 + tx.udp_payload_s; return 0; }
/*--------------------*/ void calthread(void *tag) { Ttime ct,deltaTtime; w32 delta; int sddburst=0; //printf("calthread:\n"); threadactive=1; if(DBGCMDS) { prtLog("calthread start."); }; while(1) { int ndit; heartbeat++; if(heartbeat>0xfffffff0) heartbeat=0; ndit= findnextcDET(); // find closest one in time //printf("det:%s %d %d\n", ACTIVEDETS[ndit].name, ACTIVEDETS[ndit].caltime.secs, ACTIVEDETS[ndit].caltime.usecs); if(ndit==-1) goto STP; if(globalcalDET(ndit)==0) { delDET(ndit); if(NACTIVE==0) goto STP; continue; }; getcurtime(&ct); if( ACTIVEDETS[ndit].caltime.secs>0) { w32 milisecs; int rcgt; delta=diffnowbefore(&ACTIVEDETS[ndit].caltime, &ct); if(delta>100) { usleep(delta); }; if(globalcalDET(ndit)==0) { delDET(ndit); if(NACTIVE==0) goto STP; continue; }; // generate calib. trigger: if(cshmGlobFlag(FLGignoreGCALIB)==0) { w32 orbitn; rcgt= GenSwtrg(1, 'c', ACTIVEDETS[ndit].roc, ACTIVEDETS[ndit].calbc,1<<ndit, swtriggers_gcalib, &orbitn); if(rcgt==12345678) { delDET(ndit); if(NACTIVE==0) goto STP; continue; }; ACTIVEDETS[ndit].attempts++; } else { rcgt=0; // cal. trigger not generated (disabled during start/stop part) }; getcurtime(&ct); // time of cal. trigger just attempted to be generated // movd up ACTIVEDETS[ndit].attempts++; if(rcgt==1) { if(ACTIVEDETS[ndit].lasttime.secs==0) { milisecs=0; } else { milisecs= diffnowbefore(&ct, &ACTIVEDETS[ndit].lasttime)/1000; }; ACTIVEDETS[ndit].sent++; //printf("CT:%s \t %d\t%d ms:%d\n", ACTIVEDETS[ndit].name, ct.secs, ct.usecs, milisecs); //printf("CT:%s ms:%d\n", ACTIVEDETS[ndit].name, milisecs); ACTIVEDETS[ndit].lasttime.secs= ct.secs; ACTIVEDETS[ndit].lasttime.usecs= ct.usecs; }; // usleep(100); // do we need this (waiting for the completion)? /* if(ACTIVEDETS[ndit].attempts > 200 ) { emptyDET(); goto STP; }; */ }; // the planning of next cal. trigger for ndit detector: // NextTime= CurrentTime + period[ndit] i.e. // IS THE SAME IN EITHER CASE (successful or unsuccsessful) if( ACTIVEDETS[ndit].caltime.secs==0) { // wait 2 secs before 1st cal. event char msg[200]; sprintf(msg, "1st planning:%s \t %d\t%d", ACTIVEDETS[ndit].name, ct.secs, ct.usecs); prtLog(msg); deltaTtime.secs=2; deltaTtime.usecs=0; } else { deltaTtime.secs=0; deltaTtime.usecs=1000*ACTIVEDETS[ndit].period; }; if(ndit==SDD) { // SDD 3x50ms every 15minutes sddburst++; if(sddburst==3) { sddburst=0; deltaTtime.usecs= 15*60*1000000; }; }; addtime(&ct, &deltaTtime); ACTIVEDETS[ndit].caltime.secs= ct.secs; ACTIVEDETS[ndit].caltime.usecs= ct.usecs; //printf("det:%s next:%d %d\n", ACTIVEDETS[ndit].name, ct.secs,ct.usecs); }; STP: threadactive=0; if(DBGCMDS) { prtLog("calthread stop."); }; }
static krb5_boolean service_fds(krb5_context context, struct select_state *selstate, int interval, struct conn_state *conns, struct select_state *seltemp, int (*msg_handler)(krb5_context, const krb5_data *, void *), void *msg_handler_data, struct conn_state **winner_out) { int e, selret; struct timeval now; struct conn_state *state; *winner_out = NULL; e = getcurtime(&now); if (e) return 1; selstate->end_time = now; selstate->end_time.tv_sec += interval; e = 0; while (selstate->nfds > 0) { e = krb5int_cm_call_select(selstate, seltemp, &selret); if (e == EINTR) continue; if (e != 0) break; dprint("service_fds examining results, selret=%d\n", selret); if (selret == 0) /* Timeout, return to caller. */ return 0; /* Got something on a socket, process it. */ for (state = conns; state != NULL; state = state->next) { int ssflags; if (state->fd == INVALID_SOCKET) continue; ssflags = 0; if (FD_ISSET(state->fd, &seltemp->rfds)) ssflags |= SSF_READ; if (FD_ISSET(state->fd, &seltemp->wfds)) ssflags |= SSF_WRITE; if (FD_ISSET(state->fd, &seltemp->xfds)) ssflags |= SSF_EXCEPTION; if (!ssflags) continue; if (state->service(context, state, selstate, ssflags)) { int stop = 1; if (msg_handler != NULL) { krb5_data reply; reply.data = state->x.in.buf; reply.length = state->x.in.pos - state->x.in.buf; stop = (msg_handler(context, &reply, msg_handler_data) != 0); } if (stop) { dprint("fd service routine says we're done\n"); *winner_out = state; return 1; } } } } if (e != 0) return 1; return 0; }