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