meillo@0: /* MasqMail meillo@0: Copyright (C) 2000 Oliver Kurth meillo@0: meillo@0: This program is free software; you can redistribute it and/or modify meillo@0: it under the terms of the GNU General Public License as published by meillo@0: the Free Software Foundation; either version 2 of the License, or meillo@0: (at your option) any later version. meillo@0: meillo@0: This program is distributed in the hope that it will be useful, meillo@0: but WITHOUT ANY WARRANTY; without even the implied warranty of meillo@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the meillo@0: GNU General Public License for more details. meillo@0: meillo@0: You should have received a copy of the GNU General Public License meillo@0: along with this program; if not, write to the Free Software meillo@0: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. meillo@0: */ meillo@0: meillo@0: #include "masqmail.h" meillo@0: meillo@0: /* define if you get problems... */ meillo@0: /*#define SOCKADDR_OLD 1*/ meillo@0: meillo@0: gboolean init_sockaddr(struct sockaddr_in *name, interface *iface) meillo@0: { meillo@0: struct hostent *he; meillo@0: struct in_addr ia; meillo@0: meillo@0: #ifdef SOCKADDR_OLD meillo@0: /* here I tried to be intelligent and failed. */ meillo@0: if(isalpha(iface->address[0])){ meillo@0: if ((he = gethostbyname(iface->address)) == NULL) { meillo@0: logwrite(LOG_ALERT, meillo@0: "local address '%s' unknown. (deleting)\n", meillo@0: iface->address); meillo@0: return FALSE; meillo@0: } meillo@0: memcpy(&(name->sin_addr), he->h_addr, sizeof(name->sin_addr)); meillo@0: }else if(isdigit(iface->address[0])){ meillo@0: if(inet_aton(iface->address, &ia)){ meillo@0: memcpy(&(name->sin_addr), &ia, sizeof(name->sin_addr)); meillo@0: }else{ meillo@0: logwrite(LOG_ALERT, meillo@0: "invalid address '%s': inet_aton() failed (deleting)\n", meillo@0: iface->address); meillo@0: return FALSE; meillo@0: } meillo@0: }else{ meillo@0: logwrite(LOG_ALERT, meillo@0: "invalid address '%s', should begin with a aphanumeric (deleting)\n", meillo@0: iface->address); meillo@0: return FALSE; meillo@0: } meillo@0: #else meillo@0: /* this is how others to it. I follow the crowd... */ meillo@0: if(inet_aton(iface->address, &ia) != 0){ meillo@0: /* IP address */ meillo@0: memcpy(&(name->sin_addr), &ia, sizeof(name->sin_addr)); meillo@0: }else{ meillo@0: if ((he = gethostbyname(iface->address)) == NULL) { meillo@0: logwrite(LOG_ALERT, "local address '%s' unknown. (deleting)\n", iface->address); meillo@0: return FALSE; meillo@0: } meillo@0: memcpy(&(name->sin_addr), he->h_addr, sizeof(name->sin_addr)); meillo@0: } meillo@0: #endif meillo@0: name->sin_family = AF_INET; meillo@0: name->sin_port = htons(iface->port); meillo@0: meillo@0: return TRUE; meillo@0: } meillo@0: meillo@0: int make_server_socket(interface *iface) meillo@0: { meillo@0: int sock = -1; meillo@0: struct sockaddr_in server; meillo@0: meillo@0: memset(&server, 0, sizeof(struct sockaddr_in)); meillo@0: meillo@0: /* Create the socket. */ meillo@0: sock = socket (PF_INET, SOCK_STREAM, 0); meillo@0: if (sock < 0){ meillo@0: logwrite(LOG_ALERT, "socket: %s\n", strerror(errno)); meillo@0: return -1; meillo@0: } meillo@0: meillo@0: if(init_sockaddr(&server, iface)){ meillo@0: /* bind the socket */ meillo@0: if (bind (sock, (struct sockaddr *) &server, sizeof (server)) < 0){ meillo@0: logwrite(LOG_ALERT, "bind: %s\n", strerror(errno)); meillo@0: return -1; meillo@0: } meillo@0: }else{ meillo@0: close(sock); meillo@0: return -1; meillo@0: } meillo@0: meillo@0: return sock; meillo@0: }