gtk2 sam7fc telemetrie application
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.

136 lines
3.8KB

  1. /***************************************************************************
  2. * Copyright (C) 07/2007 by Olaf Rempel *
  3. * razzor@kopf-tisch.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; version 2 of the License *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU 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 *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. #include <netinet/ip.h>
  24. #include <arpa/inet.h>
  25. #include <fcntl.h>
  26. #include <errno.h>
  27. #include "logging.h"
  28. #include "sockaddr.h"
  29. int tcp_listen(struct sockaddr_in *sa)
  30. {
  31. int sock = socket(AF_INET, SOCK_STREAM, 0);
  32. if (sock < 0 ) {
  33. log_print(LOG_ERROR, "tcp_listen_socket(): socket()");
  34. return -1;
  35. }
  36. int i = 1;
  37. if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i))) {
  38. log_print(LOG_ERROR, "tcp_listen_socket(): setsockopt(SO_REUSEADDR)");
  39. close(sock);
  40. return -1;
  41. }
  42. if (bind(sock, (struct sockaddr *)sa, sizeof(*sa))) {
  43. log_print(LOG_ERROR, "tcp_listen_socket(): bind(%s)", get_sockaddr_buf(sa));
  44. close(sock);
  45. return -1;
  46. }
  47. if (listen(sock, 8)) {
  48. log_print(LOG_ERROR, "tcp_listen_socket(): listen()");
  49. close(sock);
  50. return -1;
  51. }
  52. return sock;
  53. }
  54. int tcp_connect(struct sockaddr_in *sa)
  55. {
  56. int sock = socket(AF_INET, SOCK_STREAM, 0);
  57. if (sock < 0 ) {
  58. log_print(LOG_ERROR, "tcp_connect_socket(): socket()");
  59. return -1;
  60. }
  61. int ret = connect(sock, (struct sockaddr *)sa, sizeof(*sa));
  62. if (ret != 0) {
  63. log_print(LOG_ERROR, "tcp_connect(): connect(%s)", get_sockaddr_buf(sa));
  64. close(sock);
  65. return -1;
  66. }
  67. return sock;
  68. }
  69. int tcp_connect_nonblock(struct sockaddr_in *sa)
  70. {
  71. int sock = socket(AF_INET, SOCK_STREAM, 0);
  72. if (sock < 0 ) {
  73. log_print(LOG_ERROR, "tcp_connect_nonblock(): socket()");
  74. return -1;
  75. }
  76. int flags = fcntl(sock, F_GETFL, 0);
  77. if (flags < 0) {
  78. log_print(LOG_ERROR, "tcp_connect_nonblock(): fcntl(F_GETFL)");
  79. close(sock);
  80. return -1;
  81. }
  82. /* non-blocking connect() */
  83. if (fcntl(sock, F_SETFL, flags | O_NONBLOCK)) {
  84. log_print(LOG_ERROR, "tcp_connect_nonblock(): fcntl(F_SETFL)");
  85. close(sock);
  86. return -1;
  87. }
  88. int ret = connect(sock, (struct sockaddr *)sa, sizeof(*sa));
  89. if (ret && errno != EINPROGRESS) {
  90. log_print(LOG_ERROR, "tcp_connect_nonblock(): connect(%s)", get_sockaddr_buf(sa));
  91. close(sock);
  92. return -1;
  93. }
  94. /* reset EINPROGRESS */
  95. errno = 0;
  96. /* all further actions are blocking */
  97. if (fcntl(sock, F_SETFL, flags)) {
  98. log_print(LOG_ERROR, "tcp_connect_nonblock(): fcntl(F_SETFL)");
  99. close(sock);
  100. return -1;
  101. }
  102. return sock;
  103. }
  104. int tcp_connect_error(int fd)
  105. {
  106. int err;
  107. unsigned int err_size = sizeof(err);
  108. if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &err_size)) {
  109. log_print(LOG_ERROR, "tcp_connect_error(): getsockopt(SO_ERROR)");
  110. return -1;
  111. }
  112. if (err) {
  113. errno = err;
  114. return -1;
  115. }
  116. return 0;
  117. }