x25_timer.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:5k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * X.25 Packet Layer release 002
  3.  *
  4.  * This is ALPHA test software. This code may break your machine, randomly fail to work with new 
  5.  * releases, misbehave and/or generally screw up. It might even work. 
  6.  *
  7.  * This code REQUIRES 2.1.15 or higher
  8.  *
  9.  * This module:
  10.  * This module is free software; you can redistribute it and/or
  11.  * modify it under the terms of the GNU General Public License
  12.  * as published by the Free Software Foundation; either version
  13.  * 2 of the License, or (at your option) any later version.
  14.  *
  15.  * History
  16.  * X.25 001 Jonathan Naylor Started coding.
  17.  * X.25 002 Jonathan Naylor New timer architecture.
  18.  * Centralised disconnection processing.
  19.  */
  20. #include <linux/errno.h>
  21. #include <linux/types.h>
  22. #include <linux/socket.h>
  23. #include <linux/in.h>
  24. #include <linux/kernel.h>
  25. #include <linux/sched.h>
  26. #include <linux/timer.h>
  27. #include <linux/string.h>
  28. #include <linux/sockios.h>
  29. #include <linux/net.h>
  30. #include <linux/inet.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/skbuff.h>
  33. #include <net/sock.h>
  34. #include <asm/segment.h>
  35. #include <asm/system.h>
  36. #include <linux/fcntl.h>
  37. #include <linux/mm.h>
  38. #include <linux/interrupt.h>
  39. #include <net/x25.h>
  40. static void x25_heartbeat_expiry(unsigned long);
  41. static void x25_timer_expiry(unsigned long);
  42. void x25_start_heartbeat(struct sock *sk)
  43. {
  44. del_timer(&sk->timer);
  45. sk->timer.data     = (unsigned long)sk;
  46. sk->timer.function = &x25_heartbeat_expiry;
  47. sk->timer.expires  = jiffies + 5 * HZ;
  48. add_timer(&sk->timer);
  49. }
  50. void x25_stop_heartbeat(struct sock *sk)
  51. {
  52. del_timer(&sk->timer);
  53. }
  54. void x25_start_t2timer(struct sock *sk)
  55. {
  56. del_timer(&sk->protinfo.x25->timer);
  57. sk->protinfo.x25->timer.data     = (unsigned long)sk;
  58. sk->protinfo.x25->timer.function = &x25_timer_expiry;
  59. sk->protinfo.x25->timer.expires  = jiffies + sk->protinfo.x25->t2;
  60. add_timer(&sk->protinfo.x25->timer);
  61. }
  62. void x25_start_t21timer(struct sock *sk)
  63. {
  64. del_timer(&sk->protinfo.x25->timer);
  65. sk->protinfo.x25->timer.data     = (unsigned long)sk;
  66. sk->protinfo.x25->timer.function = &x25_timer_expiry;
  67. sk->protinfo.x25->timer.expires  = jiffies + sk->protinfo.x25->t21;
  68. add_timer(&sk->protinfo.x25->timer);
  69. }
  70. void x25_start_t22timer(struct sock *sk)
  71. {
  72. del_timer(&sk->protinfo.x25->timer);
  73. sk->protinfo.x25->timer.data     = (unsigned long)sk;
  74. sk->protinfo.x25->timer.function = &x25_timer_expiry;
  75. sk->protinfo.x25->timer.expires  = jiffies + sk->protinfo.x25->t22;
  76. add_timer(&sk->protinfo.x25->timer);
  77. }
  78. void x25_start_t23timer(struct sock *sk)
  79. {
  80. del_timer(&sk->protinfo.x25->timer);
  81. sk->protinfo.x25->timer.data     = (unsigned long)sk;
  82. sk->protinfo.x25->timer.function = &x25_timer_expiry;
  83. sk->protinfo.x25->timer.expires  = jiffies + sk->protinfo.x25->t23;
  84. add_timer(&sk->protinfo.x25->timer);
  85. }
  86. void x25_stop_timer(struct sock *sk)
  87. {
  88. del_timer(&sk->protinfo.x25->timer);
  89. }
  90. unsigned long x25_display_timer(struct sock *sk)
  91. {
  92. if (!timer_pending(&sk->protinfo.x25->timer))
  93. return 0;
  94. return sk->protinfo.x25->timer.expires - jiffies;
  95. }
  96. static void x25_heartbeat_expiry(unsigned long param)
  97. {
  98. struct sock *sk = (struct sock *)param;
  99.         bh_lock_sock(sk);
  100.         if (sk->lock.users) { /* can currently only occur in state 3 */ 
  101. goto restart_heartbeat;
  102. }
  103. switch (sk->protinfo.x25->state) {
  104. case X25_STATE_0:
  105. /* Magic here: If we listen() and a new link dies before it
  106.    is accepted() it isn't 'dead' so doesn't get removed. */
  107. if (sk->destroy || (sk->state == TCP_LISTEN && sk->dead)) {
  108. x25_destroy_socket(sk);
  109. goto unlock;
  110. }
  111. break;
  112. case X25_STATE_3:
  113. /*
  114.  * Check for the state of the receive buffer.
  115.  */
  116. x25_check_rbuf(sk);
  117. break;
  118. }
  119.  restart_heartbeat:
  120. x25_start_heartbeat(sk);
  121.  unlock:
  122. bh_unlock_sock(sk);
  123. }
  124. /*
  125.  * Timer has expired, it may have been T2, T21, T22, or T23. We can tell
  126.  * by the state machine state.
  127.  */
  128. static inline void x25_do_timer_expiry(struct sock * sk)
  129. {
  130. switch (sk->protinfo.x25->state) {
  131. case X25_STATE_3: /* T2 */
  132. if (sk->protinfo.x25->condition & X25_COND_ACK_PENDING) {
  133. sk->protinfo.x25->condition &= ~X25_COND_ACK_PENDING;
  134. x25_enquiry_response(sk);
  135. }
  136. break;
  137. case X25_STATE_1: /* T21 */
  138. case X25_STATE_4: /* T22 */
  139. x25_write_internal(sk, X25_CLEAR_REQUEST);
  140. sk->protinfo.x25->state = X25_STATE_2;
  141. x25_start_t23timer(sk);
  142. break;
  143. case X25_STATE_2: /* T23 */
  144. x25_disconnect(sk, ETIMEDOUT, 0, 0);
  145. break;
  146. }
  147. }
  148. static void x25_timer_expiry(unsigned long param)
  149. {
  150. struct sock *sk = (struct sock *)param;
  151. bh_lock_sock(sk);
  152. if (sk->lock.users) { /* can currently only occur in state 3 */
  153. if (sk->protinfo.x25->state == X25_STATE_3) {
  154. x25_start_t2timer(sk);
  155. }
  156. } else {
  157. x25_do_timer_expiry(sk);
  158. }
  159. bh_unlock_sock(sk);
  160. }