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.

118 lines
3.2KB

  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 <string.h>
  22. #include <sys/types.h>
  23. #include <sys/socket.h>
  24. #include <netinet/in.h>
  25. #include <arpa/inet.h>
  26. int parse_sockaddr(const char *addr, struct sockaddr_in *sa)
  27. {
  28. char *buf = strdup(addr);
  29. if (buf == NULL)
  30. return -1;
  31. char *tmp;
  32. char *ipstr = strtok_r(buf, ":", &tmp);
  33. if (ipstr == NULL) {
  34. free(buf);
  35. return -2;
  36. }
  37. sa->sin_family = AF_INET;
  38. if (inet_pton(AF_INET, ipstr, &sa->sin_addr) <= 0) {
  39. free(buf);
  40. return -3;
  41. }
  42. char *portstr = strtok_r(NULL, " \r\n", &tmp);
  43. if (portstr == NULL) {
  44. free(buf);
  45. return -4;
  46. }
  47. int port = atoi(portstr);
  48. if (port < 0 || port > 65535) {
  49. free(buf);
  50. return -5;
  51. }
  52. sa->sin_port = htons(port);
  53. free(buf);
  54. return 0;
  55. }
  56. int parse_subnet(const char *addr, struct in_addr *net, struct in_addr *mask)
  57. {
  58. char *buf = strdup(addr);
  59. if (buf == NULL)
  60. return -1;
  61. char *tmp;
  62. char *netstr = strtok_r(buf, "/", &tmp);
  63. if (netstr == NULL) {
  64. free(buf);
  65. return -2;
  66. }
  67. if (inet_pton(AF_INET, netstr, net) <= 0) {
  68. free(buf);
  69. return -3;
  70. }
  71. char *maskstr = strtok_r(NULL, " \r\n", &tmp);
  72. if (maskstr == NULL) {
  73. mask->s_addr = ~0;
  74. } else if (inet_pton(AF_INET, maskstr, mask) <= 0) {
  75. int maskbits = atoi(maskstr);
  76. if (maskbits < 0 || maskbits > 32) {
  77. free(buf);
  78. return -4;
  79. }
  80. mask->s_addr = htonl(~0 << (32 - maskbits));
  81. }
  82. free(buf);
  83. return 0;
  84. }
  85. int get_sockaddr(char *buf, int size, struct sockaddr_in *addr)
  86. {
  87. return snprintf(buf, size, "%s:%d", inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
  88. }
  89. char * get_sockaddr_buf(struct sockaddr_in *addr)
  90. {
  91. static char ret[24];
  92. get_sockaddr(ret, sizeof(ret), addr);
  93. return ret;
  94. }
  95. int same_sockaddr(struct sockaddr_in *a, struct sockaddr_in *b)
  96. {
  97. return !((a->sin_family ^ b->sin_family) |
  98. (a->sin_addr.s_addr ^ b->sin_addr.s_addr) |
  99. (a->sin_port ^ b->sin_port));
  100. }