masqmail

annotate src/interface.c @ 411:9b93c0a3bd8c

Ensure lvals, rvals and components of address strucs are stripped.
author markus schnalke <meillo@marmaro.de>
date Wed, 29 Feb 2012 12:06:33 +0100
parents b27f66555ba8
children
rev   line source
meillo@367 1 /*
meillo@367 2 ** MasqMail
meillo@367 3 ** Copyright (C) 2000 Oliver Kurth
meillo@367 4 **
meillo@367 5 ** This program is free software; you can redistribute it and/or modify
meillo@367 6 ** it under the terms of the GNU General Public License as published by
meillo@367 7 ** the Free Software Foundation; either version 2 of the License, or
meillo@367 8 ** (at your option) any later version.
meillo@367 9 **
meillo@367 10 ** This program is distributed in the hope that it will be useful,
meillo@367 11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
meillo@367 12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
meillo@367 13 ** GNU General Public License for more details.
meillo@367 14 **
meillo@367 15 ** You should have received a copy of the GNU General Public License
meillo@367 16 ** along with this program; if not, write to the Free Software
meillo@367 17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
meillo@0 18 */
meillo@0 19
meillo@0 20 #include "masqmail.h"
meillo@0 21
meillo@0 22 /* define if you get problems... */
meillo@367 23 /* #define SOCKADDR_OLD 1 */
meillo@0 24
meillo@10 25 gboolean
meillo@366 26 init_sockaddr(struct sockaddr_in *name, interface *iface)
meillo@0 27 {
meillo@10 28 struct hostent *he;
meillo@10 29 struct in_addr ia;
meillo@10 30
meillo@0 31 #ifdef SOCKADDR_OLD
meillo@10 32 /* here I tried to be intelligent and failed. */
meillo@407 33 if (isalpha(*iface->address)) {
meillo@10 34 if ((he = gethostbyname(iface->address)) == NULL) {
meillo@407 35 logwrite(LOG_ALERT, "local address '%s' unknown. "
meillo@407 36 "(deleting)\n", iface->address);
meillo@10 37 return FALSE;
meillo@10 38 }
meillo@10 39 memcpy(&(name->sin_addr), he->h_addr, sizeof(name->sin_addr));
meillo@407 40 } else if (isdigit(*iface->address)) {
meillo@10 41 if (inet_aton(iface->address, &ia)) {
meillo@10 42 memcpy(&(name->sin_addr), &ia, sizeof(name->sin_addr));
meillo@10 43 } else {
meillo@407 44 logwrite(LOG_ALERT, "invalid address '%s': "
meillo@407 45 "inet_aton() failed (deleting)\n",
meillo@407 46 iface->address);
meillo@10 47 return FALSE;
meillo@10 48 }
meillo@10 49 } else {
meillo@407 50 logwrite(LOG_ALERT, "invalid address '%s', should begin with "
meillo@407 51 "a aphanumeric (deleting)\n", iface->address);
meillo@10 52 return FALSE;
meillo@10 53 }
meillo@0 54 #else
meillo@10 55 /* this is how others to it. I follow the crowd... */
meillo@10 56 if (inet_aton(iface->address, &ia) != 0) {
meillo@10 57 /* IP address */
meillo@10 58 memcpy(&(name->sin_addr), &ia, sizeof(name->sin_addr));
meillo@10 59 } else {
meillo@10 60 if ((he = gethostbyname(iface->address)) == NULL) {
meillo@407 61 logwrite(LOG_ALERT, "local address '%s' unknown. "
meillo@407 62 "(deleting)\n", iface->address);
meillo@10 63 return FALSE;
meillo@10 64 }
meillo@10 65 memcpy(&(name->sin_addr), he->h_addr, sizeof(name->sin_addr));
meillo@10 66 }
meillo@0 67 #endif
meillo@10 68 name->sin_family = AF_INET;
meillo@10 69 name->sin_port = htons(iface->port);
meillo@0 70
meillo@10 71 return TRUE;
meillo@0 72 }
meillo@0 73
meillo@10 74 int
meillo@366 75 make_server_socket(interface *iface)
meillo@0 76 {
meillo@10 77 int sock = -1;
meillo@10 78 struct sockaddr_in server;
meillo@0 79
meillo@10 80 memset(&server, 0, sizeof(struct sockaddr_in));
meillo@10 81
meillo@10 82 /* Create the socket. */
meillo@10 83 sock = socket(PF_INET, SOCK_STREAM, 0);
meillo@10 84 if (sock < 0) {
meillo@10 85 logwrite(LOG_ALERT, "socket: %s\n", strerror(errno));
meillo@10 86 return -1;
meillo@10 87 }
meillo@10 88
meillo@10 89 if (init_sockaddr(&server, iface)) {
meillo@10 90 /* bind the socket */
meillo@407 91 if (bind(sock, (struct sockaddr *) &server,
meillo@407 92 sizeof(server)) < 0) {
meillo@10 93 logwrite(LOG_ALERT, "bind: %s\n", strerror(errno));
meillo@10 94 return -1;
meillo@10 95 }
meillo@10 96 } else {
meillo@10 97 close(sock);
meillo@10 98 return -1;
meillo@10 99 }
meillo@10 100
meillo@10 101 return sock;
meillo@0 102 }