network.c

Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * XMPP Module
00005  * This file is part of Kamailio, a free SIP server.
00006  *
00007  * Copyright (C) 2006 Voice Sistem S.R.L.
00008  *
00009  * Kamailio is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version
00013  *
00014  * Kamailio is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  *
00023  * Author: Andreea Spirea
00024  *
00025  */
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <unistd.h>
00035 #include <stdarg.h>
00036 #include <errno.h>
00037 
00038 #include <sys/socket.h>
00039 #include <netinet/in.h>
00040 #include <arpa/inet.h>
00041 #include <netdb.h>
00042 
00043 #include "../../sr_module.h"
00044 
00045 int net_listen(char *server, int port)
00046 {
00047         int fd;
00048         struct sockaddr_in sin;
00049         int on = 1;
00050         
00051         memset(&sin, 0, sizeof(struct sockaddr_in));
00052         sin.sin_family = AF_INET;
00053         sin.sin_port = htons(port);
00054         
00055         if (!inet_aton(server, &sin.sin_addr)) {
00056                 struct hostent *host;
00057                 
00058                 LM_DBG("resolving %s...\n", server);
00059                 
00060                 if (!(host = gethostbyname(server))) {
00061                         LM_ERR("resolving %s failed (%s).\n", server,
00062                                         hstrerror(h_errno));
00063                         return -1;
00064                 }
00065                 memcpy(&sin.sin_addr, host->h_addr_list[0], host->h_length);
00066         }
00067         
00068         if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
00069                 LM_ERR("socket() failed: %s\n", strerror(errno));
00070                 return -1;
00071         }
00072         
00073         LM_DBG("listening on %s:%d\n", inet_ntoa(sin.sin_addr), port);
00074         
00075         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
00076                 LM_WARN("setsockopt(SO_REUSEADDR) failed: %s\n",strerror(errno));
00077         }
00078 
00079         if (bind(fd, (struct sockaddr *) &sin, sizeof(struct sockaddr_in)) < 0) {
00080                 LM_ERR("bind() failed: %s\n", strerror(errno));
00081                 close(fd);
00082                 return -1;
00083         }
00084 
00085         if (listen(fd, 1) < 0) {
00086                 LM_ERR("listen() failed: %s\n", strerror(errno));
00087                 close(fd);
00088                 return -1;
00089         }
00090 
00091         return fd;
00092 }
00093 
00094 int net_connect(char *server, int port)
00095 {
00096         int fd;
00097         struct sockaddr_in sin;
00098         
00099         memset(&sin, 0, sizeof(struct sockaddr_in));
00100         sin.sin_family = AF_INET;
00101         sin.sin_port = htons(port);
00102         
00103         if (!inet_aton(server, &sin.sin_addr)) {
00104                 struct hostent *host;
00105                 
00106                 LM_DBG("resolving %s...\n", server);
00107                 
00108                 if (!(host = gethostbyname(server))) {
00109                         LM_ERR("resolving %s failed (%s).\n", server,
00110                                         hstrerror(h_errno));
00111                         return -1;
00112                 }
00113                 memcpy(&sin.sin_addr, host->h_addr_list[0], host->h_length);
00114         }
00115         
00116         if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
00117                 LM_ERR("socket() failed: %s\n", strerror(errno));
00118                 return -1;
00119         }
00120         
00121         LM_DBG("connecting to %s:%d...\n", inet_ntoa(sin.sin_addr), port);
00122         
00123         if (connect(fd, (struct sockaddr *) &sin, sizeof(struct sockaddr_in)) < 0) {
00124                 LM_ERR("connect() failed: %s\n", strerror(errno));
00125                 close(fd);
00126                 return -1;
00127         }
00128 
00129         LM_DBG("connected to %s:%d...\n", inet_ntoa(sin.sin_addr), port);
00130         return fd;
00131 }
00132 
00133 int net_send(int fd, const char *buf, int len)
00134 {
00135         const char *p = buf;
00136         int res;
00137 
00138         do {
00139                 res = send(fd, p, len, 0);
00140                 if (res <= 0)
00141                         return res;
00142                 len -= res;
00143                 p += res;
00144         } while (len);
00145 
00146         return (p - buf);
00147 }
00148 
00149 int net_printf(int fd, char *format, ...)
00150 {
00151         va_list args;
00152         char buf[4096];
00153         
00154         va_start(args, format);
00155         vsnprintf(buf, sizeof(buf) - 1, format, args);
00156         va_end(args);
00157 
00158         LM_DBG("net_printf: [%s]\n", buf);
00159         
00160         return net_send(fd, buf, strlen(buf));
00161 }
00162 
00163 char *net_read_static(int fd)
00164 {
00165         static char buf[4096];
00166         int res;
00167 
00168         res = recv(fd, buf, sizeof(buf) - 1, 0);
00169         if (res < 0) {
00170                 LM_ERR("recv() failed: %s\n", strerror(errno));
00171                 return NULL;
00172         }
00173         if (!res)
00174                 return NULL;
00175         buf[res] = 0;
00176         return buf;
00177 }