Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
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.
 
 
 
 

112 lines
2.7 KiB

  1. #include "socket.h"
  2. #include "io.h"
  3. #include "buffer.h"
  4. #include "ip6.h"
  5. #include <errno.h>
  6. int main() {
  7. int s=socket_tcp6();
  8. uint32 scope_id;
  9. char ip[16];
  10. uint16 port;
  11. if (socket_bind6_reuse(s,V6any,1234,0)==-1) {
  12. buffer_puts(buffer_2,"socket_bind6_reuse: ");
  13. buffer_puterror(buffer_2);
  14. buffer_putnlflush(buffer_2);
  15. return 111;
  16. }
  17. if (!io_fd(s)) {
  18. buffer_puts(buffer_2,"io_fd: ");
  19. buffer_puterror(buffer_2);
  20. buffer_putnlflush(buffer_2);
  21. return 111;
  22. }
  23. if (socket_listen(s,16)==-1) {
  24. buffer_puts(buffer_2,"socket_listen: ");
  25. buffer_puterror(buffer_2);
  26. buffer_putnlflush(buffer_2);
  27. return 111;
  28. }
  29. io_wantread(s);
  30. buffer_puts(buffer_2,"listening on port 1234 (fd #");
  31. buffer_putulong(buffer_2,s);
  32. buffer_putsflush(buffer_2,")\n");
  33. for (;;) {
  34. int64 i;
  35. io_wait();
  36. while ((i=io_canread())!=-1) {
  37. if (i==s) {
  38. int n;
  39. while ((n=socket_accept6(s,ip,&port,&scope_id))!=-1) {
  40. char buf[IP6_FMT];
  41. buffer_puts(buffer_2,"accepted new connection from ");
  42. buffer_put(buffer_2,buf,fmt_ip6(buf,ip));
  43. buffer_puts(buffer_2,":");
  44. buffer_putulong(buffer_2,port);
  45. buffer_puts(buffer_2," (fd ");
  46. buffer_putulong(buffer_2,n);
  47. buffer_puts(buffer_2,")");
  48. if (io_fd(n)) {
  49. io_nonblock(n);
  50. io_wantread(n);
  51. } else {
  52. buffer_puts(buffer_2,", but io_fd failed.");
  53. io_close(n);
  54. }
  55. buffer_putnlflush(buffer_2);
  56. }
  57. if (errno!=EAGAIN) {
  58. buffer_puts(buffer_2,"socket_accept6: ");
  59. buffer_puterror(buffer_2);
  60. buffer_putnlflush(buffer_2);
  61. }
  62. } else {
  63. char buf[1024];
  64. int l=io_tryread(i,buf,sizeof buf);
  65. if (l==-1) {
  66. buffer_puts(buffer_2,"io_tryread(");
  67. buffer_putulong(buffer_2,i);
  68. buffer_puts(buffer_2,"): ");
  69. buffer_puterror(buffer_2);
  70. buffer_putnlflush(buffer_2);
  71. io_close(i);
  72. } else if (l==0) {
  73. buffer_puts(buffer_2,"eof on fd #");
  74. buffer_putulong(buffer_2,i);
  75. buffer_putnlflush(buffer_2);
  76. io_close(i);
  77. } else {
  78. int r;
  79. switch (r=io_trywrite(i,buf,l)) {
  80. case -1:
  81. buffer_puts(buffer_2,"io_tryread(");
  82. buffer_putulong(buffer_2,i);
  83. buffer_puts(buffer_2,"): ");
  84. buffer_puterror(buffer_2);
  85. buffer_putnlflush(buffer_2);
  86. io_close(i);
  87. break;
  88. case 0:
  89. buffer_puts(buffer_2,"write eof on fd #");
  90. buffer_putulong(buffer_2,i);
  91. buffer_putnlflush(buffer_2);
  92. io_close(i);
  93. default:
  94. if (r!=l) {
  95. buffer_puts(buffer_2,"short write on fd #");
  96. buffer_putulong(buffer_2,i);
  97. buffer_puts(buffer_2,": wrote ");
  98. buffer_putulong(buffer_2,r);
  99. buffer_puts(buffer_2,", wanted to write ");
  100. buffer_putulong(buffer_2,l);
  101. buffer_putsflush(buffer_2,").\n");
  102. }
  103. }
  104. }
  105. }
  106. }
  107. }
  108. return 0;
  109. }