masqmail

annotate src/interface.c @ 0:08114f7dcc23

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