init_socks.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2006 iptelorg GmbH
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. /* History:
  28. * --------
  29. * 2006-02-14 created by andrei
  30. */
  31. #include "init_socks.h"
  32. #include "../../dprint.h"
  33. #include "../../ip_addr.h"
  34. #include "../../resolve.h"
  35. #include <errno.h>
  36. #include <string.h>
  37. #include <sys/types.h>
  38. #include <sys/socket.h>
  39. #include <netinet/in.h>
  40. #include <netinet/in_systm.h>
  41. #include <netinet/ip.h> /*IPTOS_LOWDELAY*/
  42. #include <netinet/tcp.h>
  43. #include <sys/un.h>
  44. #include <unistd.h> /* unlink */
  45. #include <sys/stat.h> /* chmod */
  46. #include <fcntl.h>
  47. #ifndef UNIX_PATH_MAX
  48. #define UNIX_PATH_MAX 108
  49. #endif
  50. static int tcp_proto_no=-1; /* tcp protocol number as returned by
  51. getprotobyname */
  52. /* returns -1 on error */
  53. static int set_non_blocking(int s)
  54. {
  55. int flags;
  56. /* non-blocking */
  57. flags=fcntl(s, F_GETFL);
  58. if (flags==-1){
  59. LOG(L_ERR, "ERROR: set_non_blocking: fnctl failed: (%d) %s\n",
  60. errno, strerror(errno));
  61. goto error;
  62. }
  63. if (fcntl(s, F_SETFL, flags|O_NONBLOCK)==-1){
  64. LOG(L_ERR, "ERROR: set_non_blocking: fcntl: set non-blocking failed:"
  65. " (%d) %s\n", errno, strerror(errno));
  66. goto error;
  67. }
  68. return 0;
  69. error:
  70. return -1;
  71. }
  72. /* opens, binds and listens-on a control unix socket of type 'type'
  73. * it will change the permissions to perm, if perm!=0
  74. * and the ownership to uid.gid if !=-1
  75. * returns socket fd or -1 on error */
  76. int init_unix_sock(struct sockaddr_un* su, char* name, int type, int perm,
  77. int uid, int gid)
  78. {
  79. struct sockaddr_un ifsun;
  80. int s;
  81. int len;
  82. int optval;
  83. s=-1;
  84. unlink(name);
  85. memset(&ifsun, 0, sizeof (struct sockaddr_un));
  86. len=strlen(name);
  87. if (len>UNIX_PATH_MAX){
  88. LOG(L_ERR, "ERROR: init_unix_sock: name too long (%d > %d): %s\n",
  89. len, UNIX_PATH_MAX, name);
  90. goto error;
  91. }
  92. ifsun.sun_family=AF_UNIX;
  93. memcpy(ifsun.sun_path, name, len);
  94. #ifdef HAVE_SOCKADDR_SA_LEN
  95. ifsun.sun_len=len;
  96. #endif
  97. s=socket(PF_UNIX, type, 0);
  98. if (s==-1){
  99. LOG(L_ERR, "ERROR: init_unix_sock: cannot create unix socket %s:"
  100. " %s [%d]\n", name, strerror(errno), errno);
  101. goto error;
  102. }
  103. optval=1;
  104. if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))==-1){
  105. LOG(L_ERR, "ERROR: init_unix_sock: setsockopt: %s [%d]\n",
  106. strerror(errno), errno);
  107. /* continue */
  108. }
  109. if (set_non_blocking(s)==-1){
  110. LOG(L_ERR, "ERROR: init_unix_sock: set non blocking failed\n");
  111. }
  112. if (bind(s, (struct sockaddr *)&ifsun, sizeof(ifsun))==-1){
  113. LOG(L_ERR, "ERROR: init_unix_sock: bind: %s [%d]\n",
  114. strerror(errno), errno);
  115. goto error;
  116. }
  117. /* then the permissions */
  118. if (perm){ /* mode==0 doesn't make sense, nobody can read/write */
  119. if (chmod(name, perm)<0){
  120. LOG(L_ERR, "ERROR: init_unix_sock: failed to change the"
  121. " permissions for %s to %04o: %s[%d]\n",
  122. name, perm, strerror(errno), errno);
  123. goto error;
  124. }
  125. }
  126. /* try to change ownership */
  127. if ((uid!=-1) || (gid!=-1)){
  128. if (chown(name, uid, gid)<0){
  129. LOG(L_ERR, "ERROR: init_unix_sock: failed to change the"
  130. " owner/group for %s to %d.%d: %s[%d]\n",
  131. name, uid, gid, strerror(errno), errno);
  132. goto error;
  133. }
  134. }
  135. if ((type==SOCK_STREAM) && (listen(s, 128)==-1)){
  136. LOG(L_ERR, "ERROR: init_unix_sock: listen: %s [%d]\n",
  137. strerror(errno), errno);
  138. goto error;
  139. }
  140. *su=ifsun;
  141. return s;
  142. error:
  143. if (s!=-1) close(s);
  144. return -1;
  145. }
  146. /* opens, binds and listens-on a control tcp socket
  147. * returns socket fd or -1 on error */
  148. int init_tcpudp_sock(union sockaddr_union* sa_un, char* address, int port,
  149. enum socket_protos type)
  150. {
  151. union sockaddr_union su;
  152. struct hostent* he;
  153. int s;
  154. int optval;
  155. s=-1;
  156. if ((type!=UDP_SOCK) && (type!=TCP_SOCK)){
  157. LOG(L_CRIT, "BUG: init_tcpudp_sock called with bad type: %d\n",
  158. type);
  159. goto error;
  160. }
  161. memset(&su, 0, sizeof (su));
  162. /* if no address specified, or address=='*', listen on all
  163. * ipv4 addresses */
  164. if ((address==0)||((*address)==0)||((*address=='*') && (*(address+1)==0))){
  165. su.sin.sin_family=AF_INET;
  166. su.sin.sin_port=htons(port);
  167. su.sin.sin_addr.s_addr=INADDR_ANY;
  168. #ifdef HAVE_SOCKADDR_SA_LEN
  169. su.sin.sin_len=sizeof(struct sockaddr_in);
  170. #endif
  171. }else{
  172. he=resolvehost(address);
  173. if (he==0){
  174. LOG(L_ERR, "ERROR: init_tcpudp_sock: bad address %s\n", address);
  175. goto error;
  176. }
  177. if (hostent2su(&su, he, 0, port)==-1) goto error;
  178. }
  179. s=socket(AF2PF(su.s.sa_family), (type==TCP_SOCK)?SOCK_STREAM:SOCK_DGRAM,0);
  180. if (s==-1){
  181. LOG(L_ERR, "ERROR: init_tcpudp_sock: cannot create tcp socket:"
  182. " %s [%d]\n", strerror(errno), errno);
  183. goto error;
  184. }
  185. /* REUSEADDR */
  186. optval=1;
  187. if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))==-1){
  188. LOG(L_ERR, "ERROR: init_tcpudp_sock: setsockopt: %s [%d]\n",
  189. strerror(errno), errno);
  190. /* continue */
  191. }
  192. /* tos */
  193. optval=IPTOS_LOWDELAY;
  194. if (setsockopt(s, IPPROTO_IP, IP_TOS, (void*)&optval,sizeof(optval)) ==-1){
  195. LOG(L_WARN, "WARNING: init_tcpudp_sock: setsockopt tos: %s\n",
  196. strerror(errno));
  197. /* continue since this is not critical */
  198. }
  199. if (set_non_blocking(s)==-1){
  200. LOG(L_ERR, "ERROR: init_tcpudp_sock: set non blocking failed\n");
  201. }
  202. if (bind(s, &su.s, sockaddru_len(su))==-1){
  203. LOG(L_ERR, "ERROR: init_tcpudp_sock: bind: %s [%d]\n",
  204. strerror(errno), errno);
  205. goto error;
  206. }
  207. if ((type==TCP_SOCK) && (listen(s, 128)==-1)){
  208. LOG(L_ERR, "ERROR: init_tcpudp_sock: listen: %s [%d]\n",
  209. strerror(errno), errno);
  210. goto error;
  211. }
  212. *sa_un=su;
  213. return s;
  214. error:
  215. if (s!=-1) close(s);
  216. return -1;
  217. }
  218. /* set all socket/fd options: disable nagle, tos lowdelay, non-blocking
  219. * return -1 on error */
  220. int init_sock_opt(int s, enum socket_protos type)
  221. {
  222. int optval;
  223. #ifdef DISABLE_NAGLE
  224. int flags;
  225. struct protoent* pe;
  226. #endif
  227. if ((type==UDP_SOCK)||(type==TCP_SOCK)){
  228. #ifdef DISABLE_NAGLE
  229. if (type==TCP_SOCK){
  230. flags=1;
  231. if (tcp_proto_no==-1){ /* if not already set */
  232. pe=getprotobyname("tcp");
  233. if (pe!=0){
  234. tcp_proto_no=pe->p_proto;
  235. }
  236. }
  237. if ( (tcp_proto_no!=-1) && (setsockopt(s, tcp_proto_no,
  238. TCP_NODELAY, &flags, sizeof(flags))<0) ){
  239. LOG(L_WARN, "WARNING: init_sock_opt: could not disable"
  240. " Nagle: %s\n", strerror(errno));
  241. }
  242. }
  243. #endif
  244. /* tos*/
  245. optval = IPTOS_LOWDELAY;
  246. if (setsockopt(s, IPPROTO_IP, IP_TOS, (void*)&optval,
  247. sizeof(optval)) ==-1){
  248. LOG(L_WARN, "WARNING: init_sock_opt: setsockopt tos: %s\n",
  249. strerror(errno));
  250. /* continue since this is not critical */
  251. }
  252. }
  253. if (set_non_blocking(s)==-1){
  254. LOG(L_ERR, "ERROR: init_sock_opt: set non blocking failed\n");
  255. }
  256. return 0;
  257. }