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@10: gboolean meillo@10: 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@10: if (isalpha(iface->address[0])) { meillo@10: if ((he = gethostbyname(iface->address)) == NULL) { meillo@10: logwrite(LOG_ALERT, "local address '%s' unknown. (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: } else if (isdigit(iface->address[0])) { meillo@10: if (inet_aton(iface->address, &ia)) { meillo@10: memcpy(&(name->sin_addr), &ia, sizeof(name->sin_addr)); meillo@10: } else { meillo@10: logwrite(LOG_ALERT, "invalid address '%s': inet_aton() failed (deleting)\n", iface->address); meillo@10: return FALSE; meillo@10: } meillo@10: } else { meillo@10: logwrite(LOG_ALERT, "invalid address '%s', should begin with 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@10: logwrite(LOG_ALERT, "local address '%s' unknown. (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@10: 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@10: if (bind(sock, (struct sockaddr *) &server, 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: }