You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

filter.c 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /**
  19. * @file
  20. *
  21. * Filter streams
  22. */
  23. #include <stddef.h>
  24. #include <errno.h>
  25. #include <gpxe/stream.h>
  26. #include <gpxe/filter.h>
  27. /**
  28. * Connection established
  29. *
  30. * @v app Stream application
  31. */
  32. void filter_connected ( struct stream_application *app ) {
  33. struct filter_stream *filter =
  34. container_of ( app, struct filter_stream, downstream );
  35. stream_connected ( &filter->upstream );
  36. }
  37. /**
  38. * Connection closed
  39. *
  40. * @v app Stream application
  41. * @v rc Error code, if any
  42. */
  43. void filter_closed ( struct stream_application *app, int rc ) {
  44. struct filter_stream *filter =
  45. container_of ( app, struct filter_stream, downstream );
  46. stream_closed ( &filter->upstream, rc );
  47. }
  48. /**
  49. * Transmit data
  50. *
  51. * @v app Stream application
  52. * @v buf Temporary data buffer
  53. * @v len Length of temporary data buffer
  54. */
  55. void filter_senddata ( struct stream_application *app,
  56. void *data, size_t len ) {
  57. struct filter_stream *filter =
  58. container_of ( app, struct filter_stream, downstream );
  59. stream_senddata ( &filter->upstream, data, len );
  60. }
  61. /**
  62. * Transmitted data acknowledged
  63. *
  64. * @v app Stream application
  65. * @v len Length of acknowledged data
  66. */
  67. void filter_acked ( struct stream_application *app, size_t len ) {
  68. struct filter_stream *filter =
  69. container_of ( app, struct filter_stream, downstream );
  70. stream_acked ( &filter->upstream, len );
  71. }
  72. /**
  73. * Receive new data
  74. *
  75. * @v app Stream application
  76. * @v data Data
  77. * @v len Length of data
  78. */
  79. void filter_newdata ( struct stream_application *app,
  80. void *data, size_t len ) {
  81. struct filter_stream *filter =
  82. container_of ( app, struct filter_stream, downstream );
  83. stream_newdata ( &filter->upstream, data, len );
  84. }
  85. /**
  86. * Bind to local address
  87. *
  88. * @v conn Stream connection
  89. * @v local Local address
  90. * @ret rc Return status code
  91. */
  92. int filter_bind ( struct stream_connection *conn, struct sockaddr *local ) {
  93. struct filter_stream *filter =
  94. container_of ( conn, struct filter_stream, upstream );
  95. return stream_bind ( &filter->downstream, local );
  96. }
  97. /**
  98. * Connect to remote address
  99. *
  100. * @v conn Stream connection
  101. * @v peer Remote address
  102. * @ret rc Return status code
  103. */
  104. int filter_connect ( struct stream_connection *conn, struct sockaddr *peer ) {
  105. struct filter_stream *filter =
  106. container_of ( conn, struct filter_stream, upstream );
  107. return stream_connect ( &filter->downstream, peer );
  108. }
  109. /**
  110. * Close connection
  111. *
  112. * @v conn Stream connection
  113. */
  114. void filter_close ( struct stream_connection *conn ) {
  115. struct filter_stream *filter =
  116. container_of ( conn, struct filter_stream, upstream );
  117. stream_close ( &filter->downstream );
  118. }
  119. /**
  120. * Send data via connection
  121. *
  122. * @v conn Stream connection
  123. * @v data Data to send
  124. * @v len Length of data
  125. * @ret rc Return status code
  126. */
  127. int filter_send ( struct stream_connection *conn, void *data, size_t len ) {
  128. struct filter_stream *filter =
  129. container_of ( conn, struct filter_stream, upstream );
  130. return stream_send ( &filter->downstream, data, len );
  131. }
  132. /**
  133. * Notify connection that data is available to send
  134. *
  135. * @v conn Stream connection
  136. * @ret rc Return status code
  137. */
  138. int filter_kick ( struct stream_connection *conn ) {
  139. struct filter_stream *filter =
  140. container_of ( conn, struct filter_stream, upstream );
  141. return stream_kick ( &filter->downstream );
  142. }
  143. /**
  144. * Insert filter into stream
  145. *
  146. * @v app Stream application
  147. * @v filter Filter stream
  148. * @ret rc Return status code
  149. */
  150. int insert_filter ( struct stream_application *app,
  151. struct filter_stream *filter ) {
  152. struct stream_connection *conn = app->conn;
  153. if ( ! conn ) {
  154. DBGC ( filter, "Filter %p cannot insert onto closed stream\n",
  155. filter );
  156. return -ENOTCONN;
  157. }
  158. DBGC ( filter, "Filter %p inserted on stream %p\n", filter, app );
  159. filter->upstream.app = app;
  160. filter->downstream.conn = conn;
  161. app->conn = &filter->upstream;
  162. conn->app = &filter->downstream;
  163. return 0;
  164. }