Changeset 5192
- Timestamp:
- 05/07/11 18:08:39 (13 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/globals.h
r5164 r5192 1315 1315 extern void cs_sleepms(uint32_t); 1316 1316 extern void cs_sleepus(uint32_t); 1317 extern int32_t bytes_available(int);1318 1317 extern void cs_setpriority(int); 1319 1318 extern struct s_auth *find_user(char *); -
trunk/module-cccam.c
r5190 r5192 422 422 423 423 int32_t cc_recv_to(struct s_client *cl, uint8_t *buf, int32_t len) { 424 fd_set fds;425 struct timeval timeout;426 424 int32_t rc; 427 428 timeout.tv_sec = 2; 429 timeout.tv_usec = 0; 430 431 while (1) { 432 FD_ZERO(&fds); 433 FD_SET(cl->udp_fd, &fds); 434 435 rc=select(cl->udp_fd+1, &fds, 0, 0, &timeout); 436 if (rc<0) { 425 struct pollfd pfd; 426 427 while (1) { 428 pfd.fd = cl->udp_fd; 429 pfd.events = POLLIN | POLLPRI; 430 431 rc = poll(&pfd, 1, 2000); 432 433 if (rc < 0) { 437 434 if (errno==EINTR) continue; 438 435 return(-1); //error!! 439 436 } 440 437 441 if (FD_ISSET(cl->udp_fd,&fds))438 if (rc == 1) 442 439 break; 443 440 return (-2); //timeout!! -
trunk/oscam-reader.c
r5100 r5192 75 75 int32_t casc_recv_timer(struct s_reader * reader, uchar *buf, int32_t l, int32_t msec) 76 76 { 77 struct timeval tv; 78 fd_set fds; 79 int32_t rc; 80 struct s_client *cl = reader->client; 81 82 if (!cl->pfd) return(-1); 77 int32_t rc; 78 struct s_client *cl = reader->client; 79 80 if (!cl->pfd) return(-1); 83 81 84 if (!reader->ph.recv) { 85 cs_log("reader %s: unsupported protocol!", reader->label); 86 return(-1); 87 } 88 89 tv.tv_sec = msec/1000; 90 tv.tv_usec = (msec%1000)*1000; 91 FD_ZERO(&fds); 92 FD_SET(cl->pfd, &fds); 93 select(cl->pfd+1, &fds, 0, 0, &tv); 94 rc=0; 95 if (FD_ISSET(cl->pfd, &fds)) 96 if (!(rc=reader->ph.recv(cl, buf, l))) 97 rc=-1; 98 99 return(rc); 100 } 101 102 #define MSTIMEOUT 0x800000 103 #define DEFAULT_CONNECT_TIMEOUT 500 104 105 int32_t network_select(int32_t forRead, int32_t timeout) 106 { 107 int32_t sd = cur_client()->udp_fd; 108 if(sd>=0) { 109 fd_set fds; 110 FD_ZERO(&fds); FD_SET(sd,&fds); 111 struct timeval tv; 112 if(timeout&MSTIMEOUT) { tv.tv_sec=0; tv.tv_usec=(timeout&~MSTIMEOUT)*1000; } 113 else { tv.tv_sec=0; tv.tv_usec=timeout*1000; } 114 int32_t r=select(sd+1,forRead ? &fds:0,forRead ? 0:&fds,0,&tv); 115 if(r>0) return 1; 116 else if(r<0) { 117 cs_debug_mask(D_READER, "socket: select failed (errno=%d %s)", errno, strerror(errno)); 118 return -1; 119 } 120 else { 121 if(timeout>0) { 122 cs_debug_mask(D_READER, "socket: select timed out (%d %s)",timeout&~MSTIMEOUT,(timeout&MSTIMEOUT)?"ms":"secs"); 123 } 124 errno=ETIMEDOUT; 125 return 0; 126 } 127 } 128 return -1; 129 } 82 if (!reader->ph.recv) { 83 cs_log("reader %s: unsupported protocol!", reader->label); 84 return(-1); 85 } 86 87 struct pollfd pfd; 88 pfd.fd = cl->pfd; 89 pfd.events = POLLIN | POLLPRI; 90 91 int32_t p_rc = poll(&pfd, 1, msec); 92 93 rc=0; 94 if (p_rc == 1) 95 if (!(rc=reader->ph.recv(cl, buf, l))) 96 rc=-1; 97 98 return(rc); 99 } 130 100 131 101 // according to documentation getaddrinfo() is thread safe … … 208 178 int32_t network_tcp_connection_open() 209 179 { 210 struct s_client *cl = cur_client(); 211 struct s_reader *rdr = cl->reader; 212 cs_log("connecting to %s", rdr->device); 213 214 in_addr_t last_ip = cl->ip; 215 if (!hostResolve(rdr)) 216 return -1; 217 218 if (last_ip != cl->ip) //clean blocking delay on ip change: 219 clear_block_delay(rdr); 220 if (is_connect_blocked(rdr)) { //inside of blocking delay, do not connect! 221 cs_log("tcp connect blocking delay asserted for %s", rdr->label); 222 return -1; 223 } 180 struct s_client *cl = cur_client(); 181 struct s_reader *rdr = cl->reader; 182 cs_log("connecting to %s", rdr->device); 183 184 in_addr_t last_ip = cl->ip; 185 if (!hostResolve(rdr)) 186 return -1; 187 188 if (last_ip != cl->ip) //clean blocking delay on ip change: 189 clear_block_delay(rdr); 190 191 if (is_connect_blocked(rdr)) { //inside of blocking delay, do not connect! 192 cs_log("tcp connect blocking delay asserted for %s", rdr->label); 193 return -1; 194 } 224 195 225 int32_t flag = 1; 226 setsockopt(cl->udp_fd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)); 196 //int32_t flag = 1; 197 //setsockopt(cl->udp_fd, IPPROTO_TCP, SO_DEBUG, (char *) &flag, sizeof(int)); 198 199 int32_t sd = cl->udp_fd; 200 int32_t fl = fcntl(sd, F_GETFL); 201 fcntl(sd, F_SETFL, O_NONBLOCK); //set to nonblocking mode to avoid "endless" connecting loops and pipe-overflows: 202 203 int32_t res = connect(sd, (struct sockaddr *)&cl->udp_sa, sizeof(cl->udp_sa)); 204 if (res == 0) { 205 fcntl(sd, F_SETFL, fl); //connect sucessfull, restore blocking mode 206 clear_block_delay(rdr); 207 return sd; 208 } 209 210 if (errno == EINPROGRESS || errno == EALREADY) { 211 struct pollfd pfd; 212 pfd.fd = cl->udp_fd; 213 pfd.events = POLLOUT; 214 int32_t rc = poll(&pfd, 1, 500); 215 if (rc>0) { //if connect is in progress, wait apr. 500ms 216 int32_t r = -1; 217 uint32_t l = sizeof(r); 218 if (getsockopt(sd, SOL_SOCKET, SO_ERROR, &r, (socklen_t*)&l) == 0) { 219 if (r == 0) { 220 fcntl(sd, F_SETFL, fl); 221 return sd; //now we are connected 222 } 223 } 224 } 225 } 226 //else we are not connected - or already connected: 227 else if (errno == EISCONN) { 228 cs_log("already connected!"); 229 fcntl(sd, F_SETFL, fl); 230 clear_block_delay(rdr); 231 return sd; 232 } 233 234 cs_log("connect(fd=%d) failed: (errno=%d %s)", sd, errno, strerror(errno)); 235 236 fcntl(sd, F_SETFL, fl); //restore blocking mode 227 237 228 int32_t sd = cl->udp_fd; 229 int32_t fl = fcntl(sd, F_GETFL); 230 fcntl(sd, F_SETFL, O_NONBLOCK); //set to nonblocking mode to avoid "endless" connecting loops and pipe-overflows: 231 int32_t res =connect(sd, (struct sockaddr *)&cl->udp_sa, sizeof(cl->udp_sa)); 232 if (res == 0) { 233 fcntl(sd, F_SETFL, fl); //connect sucessfull, restore blocking mode 234 clear_block_delay(rdr); 235 return sd; 236 } 237 238 if (errno == EINPROGRESS || errno == EALREADY) { 239 if (network_select(0, DEFAULT_CONNECT_TIMEOUT) > 0) { //if connect is in progress, wait apr. 500ms 240 int32_t r = -1; 241 uint32_t l = sizeof(r); 242 if (getsockopt(sd, SOL_SOCKET, SO_ERROR, &r, (socklen_t*)&l) == 0) { 243 if (r == 0) { 244 fcntl(sd, F_SETFL, fl); 245 return sd; //now we are connected 246 } 247 } 248 } 249 } 250 //else we are not connected - or already connected: 251 else if (errno == EISCONN) { 252 cs_log("already connected!"); 253 fcntl(sd, F_SETFL, fl); 254 clear_block_delay(rdr); 255 return sd; 256 } 257 258 if (errno == EBADF || errno == ENOTSOCK) { 259 cs_log("connect failed: bad socket/descriptor %d", sd); 260 } 261 else if (errno == ETIMEDOUT) { 262 cs_log("connect failed: timeout"); 263 } 264 else if (errno == ECONNREFUSED) { 265 cs_log("connection refused"); 266 } 267 else if (errno == ENETUNREACH) { 268 cs_log("connect failed: network unreachable!"); 269 } 270 else if (errno == EADDRINUSE) { 271 cs_log("connect failed: address in use!"); 272 } 273 else 274 cs_log("connect(fd=%d) failed: (errno=%d %s)", sd, errno, strerror(errno)); 275 276 fcntl(sd, F_SETFL, fl); //restore blocking mode 277 278 //connect has failed. Block connect for a while: 279 block_connect(rdr); 238 //connect has failed. Block connect for a while: 239 block_connect(rdr); 280 240 281 241 return -1; 282 242 } 283 243 … … 688 648 static int32_t reader_listen(struct s_reader * reader, int32_t fd1, int32_t fd2) 689 649 { 690 int32_t fdmax, tcp_toflag, use_tv=(!(reader->typ & R_IS_CASCADING)); 691 int32_t is_tcp=(reader->ph.type==MOD_CONN_TCP); 692 fd_set fds; 693 struct timeval tv; 694 695 tcp_toflag=(fd2 && is_tcp && reader->tcp_ito && reader->tcp_connected); 696 tv.tv_sec = 0; 697 tv.tv_usec = 100000L; 698 if (tcp_toflag) 699 { 700 tv.tv_sec = reader->tcp_ito*60; 701 tv.tv_usec = 0; 702 use_tv = 1; 703 } 704 FD_ZERO(&fds); 705 FD_SET(fd1, &fds); 706 if (fd2) FD_SET(fd2, &fds); 707 if (logfd) FD_SET(logfd, &fds); 708 fdmax=(fd1>fd2) ? fd1 : fd2; 709 fdmax=(fdmax>logfd) ? fdmax : logfd; 710 if (select(fdmax+1, &fds, 0, 0, (use_tv) ? &tv : 0)<0) return(0); 711 712 if ((logfd) && (FD_ISSET(logfd, &fds))) 713 { 714 cs_debug_mask(D_READER, "select: log-socket ist set"); 715 return(3); 716 } 717 718 if ((fd2) && (FD_ISSET(fd2, &fds))) 719 { 720 cs_debug_mask(D_READER, "select: socket is set"); 721 return(2); 722 } 723 724 if (FD_ISSET(fd1, &fds)) 725 { 726 if (tcp_toflag) 727 { 728 time_t now; 729 int32_t time_diff; 730 time(&now); 731 time_diff = abs(now-reader->last_s); 732 if (time_diff>(reader->tcp_ito*60)) 733 { 734 if (reader->ph.c_idle) 735 reader_do_idle(reader); 736 else { 737 cs_debug_mask(D_READER, "%s inactive_timeout (%d), close connection (fd=%d)", 738 reader->ph.desc, time_diff, fd2); 739 network_tcp_connection_close(reader->client, fd2); 740 } 741 } 742 } 743 cs_debug_mask(D_READER, "select: pipe is set"); 744 return(1); 745 } 746 747 if (tcp_toflag) 748 { 749 if (reader->ph.c_idle) 750 reader_do_idle(reader); 751 else { 752 cs_debug_mask(D_READER, "%s inactive_timeout (%d), close connection (fd=%d)", 753 reader->ph.desc, tv.tv_sec, fd2); 754 network_tcp_connection_close(reader->client, fd2); 755 } 756 return(0); 757 } 650 int32_t i, tcp_toflag, use_tv=(!(reader->typ & R_IS_CASCADING)); 651 int32_t is_tcp=(reader->ph.type==MOD_CONN_TCP); 652 653 tcp_toflag=(fd2 && is_tcp && reader->tcp_ito && reader->tcp_connected); 654 655 int32_t timeout = -1; 656 if (tcp_toflag) 657 timeout = reader->tcp_ito*60*1000; 658 659 struct pollfd pfd[3]; 660 661 int32_t pfdcount = 0; 662 if (fd1) { 663 pfd[pfdcount].fd = fd1; 664 pfd[pfdcount++].events = POLLIN | POLLPRI; 665 } 666 if (fd2) { 667 pfd[pfdcount].fd = fd2; 668 pfd[pfdcount++].events = POLLIN | POLLPRI; 669 } 670 if (logfd) { 671 pfd[pfdcount].fd = logfd; 672 pfd[pfdcount++].events = POLLIN | POLLPRI; 673 } 674 675 int32_t rc = poll(pfd, pfdcount, timeout); 676 677 if (rc>0) { 678 for (i=0; i<pfdcount; i++) { 679 if (!(pfd[i].revents & (POLLIN | POLLPRI))) 680 continue; 681 682 if (pfd[i].fd == logfd) { 683 cs_debug_mask(D_READER, "select: log-socket ist set"); 684 return(3); 685 } 686 687 if (pfd[i].fd == fd2) { 688 cs_debug_mask(D_READER, "select: socket is set"); 689 return(2); 690 } 691 692 if (pfd[i].fd == fd1) { 693 if (tcp_toflag) { 694 time_t now; 695 int32_t time_diff; 696 time(&now); 697 time_diff = abs(now-reader->last_s); 698 if (time_diff>(reader->tcp_ito*60)) { 699 if (reader->ph.c_idle) 700 reader_do_idle(reader); 701 else { 702 cs_debug_mask(D_READER, "%s inactive_timeout (%d), close connection (fd=%d)", reader->ph.desc, time_diff, fd2); 703 network_tcp_connection_close(reader->client, fd2); 704 } 705 } 706 } 707 cs_debug_mask(D_READER, "select: pipe is set"); 708 return(1); 709 } 710 } 711 } 712 713 if (tcp_toflag) { 714 if (reader->ph.c_idle) 715 reader_do_idle(reader); 716 else { 717 cs_debug_mask(D_READER, "%s inactive_timeout (%d), close connection (fd=%d)", reader->ph.desc, timeout/1000, fd2); 718 network_tcp_connection_close(reader->client, fd2); 719 } 720 return(0); 721 } 758 722 759 723 #ifdef WITH_CARDREADER 760 724 if (!(reader->typ & R_IS_CASCADING)) reader_checkhealth(reader); 761 725 #endif 762 726 return(0); 763 727 } 764 728 -
trunk/oscam-simples.c
r5183 r5192 518 518 } 519 519 520 int32_t bytes_available(int32_t fd)521 {522 fd_set rfds;523 fd_set erfds;524 int32_t select_ret;525 int32_t in_fd;526 527 in_fd=fd;528 529 FD_ZERO(&rfds);530 FD_SET(in_fd, &rfds);531 532 FD_ZERO(&erfds);533 FD_SET(in_fd, &erfds);534 535 select_ret = select(in_fd+1, &rfds, NULL, &erfds, NULL);536 if (select_ret==-1)537 {538 cs_log("ERROR reading from fd %d select_ret=%i (errno=%d %s)",in_fd, select_ret, errno, strerror(errno));539 return 0;540 }541 542 if (FD_ISSET(in_fd, &erfds))543 {544 cs_log("ERROR reading from fd %d select_ret=%i (errno=%d %s)",in_fd, select_ret, errno, strerror(errno));545 return 0;546 }547 if (FD_ISSET(in_fd,&rfds))548 return 1;549 else550 return 0;551 }552 553 554 520 #ifdef OS_CYGWIN32 555 521 #include <windows.h> -
trunk/oscam.c
r5190 r5192 1690 1690 rc=PIP_ID_NUL; 1691 1691 1692 if (bytes_available(fd)) { 1693 if (read(fd, buf, sizeof(buf))==sizeof(buf)) { 1694 memcpy(&hdr, buf+3, sizeof(void*)); 1695 } else { 1696 cs_log("WARNING: pipe header to small !"); 1697 return PIP_ID_ERR; 1698 } 1692 1693 if (read(fd, buf, sizeof(buf))==sizeof(buf)) { 1694 memcpy(&hdr, buf+3, sizeof(void*)); 1699 1695 } else { 1700 cs_log(" !bytes_available(fd)");1701 return PIP_ID_ NUL;1696 cs_log("WARNING: pipe header to small !"); 1697 return PIP_ID_ERR; 1702 1698 } 1703 1699 … … 1870 1866 return res; 1871 1867 } 1872 1873 /*1874 static int32_t cs_read_timer(int32_t fd, uchar *buf, int32_t l, int32_t msec)1875 {1876 struct timeval tv;1877 fd_set fds;1878 int32_t rc;1879 1880 if (!fd) return(-1);1881 tv.tv_sec = msec / 1000;1882 tv.tv_usec = (msec % 1000) * 1000;1883 FD_ZERO(&fds);1884 FD_SET(cur_client()->pfd, &fds);1885 1886 select(fd+1, &fds, 0, 0, &tv);1887 1888 rc=0;1889 if (FD_ISSET(cur_client()->pfd, &fds))1890 if (!(rc=read(fd, buf, l)))1891 rc=-1;1892 1893 return(rc);1894 }*/1895 1868 1896 1869 ECM_REQUEST *get_ecmtask() … … 2985 2958 int32_t process_input(uchar *buf, int32_t l, int32_t timeout) 2986 2959 { 2987 int32_t rc; 2988 fd_set fds; 2960 int32_t rc, i, pfdcount; 2989 2961 struct timeb tp; 2990 2962 struct pollfd pfd[2]; 2991 2963 struct s_client *cl = cur_client(); 2992 2964 … … 2995 2967 2996 2968 while (1) { 2997 FD_ZERO(&fds); 2998 2999 if (cl->pfd) 3000 FD_SET(cl->pfd, &fds); 3001 3002 FD_SET(cl->fd_m2c_c, &fds); 3003 3004 rc=select(((cl->pfd > cl->fd_m2c_c) ? cl->pfd : cl->fd_m2c_c)+1, &fds, 0, 0, chk_pending(tp)); 3005 if (rc<0) { 2969 pfdcount = 0; 2970 if (cl->pfd) { 2971 pfd[pfdcount].fd = cl->pfd; 2972 pfd[pfdcount++].events = POLLIN | POLLPRI; 2973 } 2974 2975 if (cl->fd_m2c_c) { 2976 pfd[pfdcount].fd = cl->fd_m2c_c; 2977 pfd[pfdcount++].events = POLLIN | POLLPRI; 2978 } 2979 2980 chk_pending(tp); 2981 int32_t p_rc = poll(pfd, pfdcount, timeout*1000); 2982 2983 if (p_rc < 0) { 3006 2984 if (errno==EINTR) continue; 3007 2985 else return(0); 3008 2986 } 3009 2987 3010 if (FD_ISSET(cl->fd_m2c_c, &fds)) { // read from pipe 3011 if (process_client_pipe(cl, buf, l)==PIP_ID_UDP) { 3012 rc=ph[cl->ctyp].recv(cl, buf, l); 3013 break; 3014 } 3015 } 3016 3017 if (cl->pfd && FD_ISSET(cl->pfd, &fds)) { // read from client 3018 rc=ph[cl->ctyp].recv(cl, buf, l); 3019 break; 3020 } 3021 3022 if (tp.time<=time((time_t *)0)) { // client maxidle reached 2988 if (p_rc == 0) { // client maxidle reached 3023 2989 rc=(-9); 3024 2990 break; 2991 } 2992 2993 for (i=0;i<pfdcount && p_rc > 0;i++) { 2994 if (!(pfd[i].revents & (POLLIN | POLLPRI))) 2995 continue; 2996 2997 if (pfd[i].fd == cl->fd_m2c_c) { 2998 if (process_client_pipe(cl, buf, l)==PIP_ID_UDP) 2999 return ph[cl->ctyp].recv(cl, buf, l); 3000 } 3001 3002 if (pfd[i].fd == cl->pfd) 3003 return ph[cl->ctyp].recv(cl, buf, l); 3025 3004 } 3026 3005 }
Note:
See TracChangeset
for help on using the changeset viewer.