socket_info.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* $Id$
  2. *
  3. * find & manage listen addresses
  4. *
  5. * Copyright (C) 2001-2003 FhG Fokus
  6. *
  7. * This file is part of ser, a free SIP server.
  8. *
  9. * ser is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version
  13. *
  14. * For a license to use the ser software under conditions
  15. * other than those described here, or to purchase support for this
  16. * software, please contact iptel.org by e-mail at the following addresses:
  17. * [email protected]
  18. *
  19. * ser is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to" the Free Software
  26. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  27. */
  28. /*
  29. * This file contains code that initializes and handles ser listen addresses
  30. * lists (struct socket_info). It is used mainly on startup.
  31. *
  32. * History:
  33. * --------
  34. * 2003-10-22 created by andrei
  35. * 2008-08-08 sctp support (andrei)
  36. */
  37. #ifndef socket_info_h
  38. #define socket_info_h
  39. #include "ip_addr.h"
  40. #include "dprint.h"
  41. #include "globals.h"
  42. /* This macro evaluates to the maximum length of string buffer needed to print
  43. * the text description of any socket, not counting the terminating zero added
  44. * by socket2str */
  45. #define MAX_SOCKET_STR (sizeof("unknown") - 1 + IP_ADDR_MAX_STR_SIZE + \
  46. INT2STR_MAX_LEN + 2 + 2)
  47. int socket2str(char* s, int* len, struct socket_info* si);
  48. int socketinfo2str(char* s, int* len, struct socket_info* si, int mode);
  49. /* struct socket_info is defined in ip_addr.h */
  50. extern struct socket_info* udp_listen;
  51. #ifdef USE_TCP
  52. extern struct socket_info* tcp_listen;
  53. #endif
  54. #ifdef USE_TLS
  55. extern struct socket_info* tls_listen;
  56. #endif
  57. #ifdef USE_SCTP
  58. extern struct socket_info* sctp_listen;
  59. #endif
  60. extern enum sip_protos nxt_proto[PROTO_LAST+1];
  61. /* flags for finding out the address types */
  62. #define SOCKET_T_IPV4 1
  63. #define SOCKET_T_IPV6 2
  64. #define SOCKET_T_UDP 4
  65. #define SOCKET_T_TCP 8
  66. #define SOCKET_T_TLS 16
  67. #define SOCKET_T_SCTP 32
  68. extern int socket_types;
  69. void init_proto_order(void);
  70. int add_listen_iface(char* name, struct name_lst* nlst,
  71. unsigned short port, unsigned short proto,
  72. enum si_flags flags);
  73. int add_listen_advertise_iface(char* name, struct name_lst* nlst,
  74. unsigned short port, unsigned short proto,
  75. char *useaddr, unsigned short useport,
  76. enum si_flags flags);
  77. int fix_all_socket_lists(void);
  78. void print_all_socket_lists(void);
  79. void print_aliases(void);
  80. struct socket_info* grep_sock_info(str* host, unsigned short port,
  81. unsigned short proto);
  82. struct socket_info* grep_sock_info_by_port(unsigned short port,
  83. unsigned short proto);
  84. struct socket_info* find_si(struct ip_addr* ip, unsigned short port,
  85. unsigned short proto);
  86. struct socket_info** get_sock_info_list(unsigned short proto);
  87. int parse_phostport(char* s, char** host, int* hlen,
  88. int* port, int* proto);
  89. int parse_proto(unsigned char* s, long len, int* proto);
  90. char* get_valid_proto_name(unsigned short proto);
  91. /* helper function:
  92. * returns next protocol, if the last one is reached return 0
  93. * useful for cycling on the supported protocols
  94. * order: udp, tcp, tls, sctp */
  95. static inline int next_proto(unsigned short proto)
  96. {
  97. if (proto>PROTO_LAST)
  98. LM_ERR("unknown proto %d\n", proto);
  99. else
  100. return nxt_proto[proto];
  101. return 0;
  102. }
  103. /* gets first non-null socket_info structure
  104. * (useful if for. e.g we are not listening on any udp sockets )
  105. */
  106. inline static struct socket_info* get_first_socket(void)
  107. {
  108. if (udp_listen) return udp_listen;
  109. #ifdef USE_TCP
  110. else if (tcp_listen) return tcp_listen;
  111. #endif
  112. #ifdef USE_SCTP
  113. else if (sctp_listen) return sctp_listen;
  114. #endif
  115. #ifdef USE_TCP
  116. #ifdef USE_TLS
  117. else if (tls_listen) return tls_listen;
  118. #endif
  119. #endif
  120. return 0;
  121. }
  122. /* structure to break down 'proto:host:port' */
  123. typedef struct _sr_phostp {
  124. int proto;
  125. str host;
  126. int port;
  127. } sr_phostp_t;
  128. struct socket_info* lookup_local_socket(str *phostp);
  129. #endif