masqmail-0.2

diff 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
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/interface.c	Fri Sep 26 17:05:23 2008 +0200
     1.3 @@ -0,0 +1,99 @@
     1.4 +/*  MasqMail
     1.5 +    Copyright (C) 2000 Oliver Kurth
     1.6 +
     1.7 +    This program is free software; you can redistribute it and/or modify
     1.8 +    it under the terms of the GNU General Public License as published by
     1.9 +    the Free Software Foundation; either version 2 of the License, or
    1.10 +    (at your option) any later version.
    1.11 +
    1.12 +    This program is distributed in the hope that it will be useful,
    1.13 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 +    GNU General Public License for more details.
    1.16 +
    1.17 +    You should have received a copy of the GNU General Public License
    1.18 +    along with this program; if not, write to the Free Software
    1.19 +    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    1.20 +*/
    1.21 +
    1.22 +#include "masqmail.h"
    1.23 +
    1.24 +/* define if you get problems... */
    1.25 +/*#define SOCKADDR_OLD 1*/
    1.26 +
    1.27 +gboolean init_sockaddr(struct sockaddr_in *name, interface *iface)
    1.28 +{
    1.29 +  struct hostent *he;
    1.30 +  struct in_addr ia;
    1.31 +  
    1.32 +#ifdef SOCKADDR_OLD
    1.33 +  /* here I tried to be intelligent and failed. */
    1.34 +  if(isalpha(iface->address[0])){
    1.35 +    if ((he = gethostbyname(iface->address)) == NULL) {
    1.36 +      logwrite(LOG_ALERT,
    1.37 +	       "local address '%s' unknown. (deleting)\n",
    1.38 +	       iface->address);
    1.39 +      return FALSE;
    1.40 +    }
    1.41 +    memcpy(&(name->sin_addr), he->h_addr, sizeof(name->sin_addr));
    1.42 +  }else if(isdigit(iface->address[0])){
    1.43 +    if(inet_aton(iface->address, &ia)){
    1.44 +      memcpy(&(name->sin_addr), &ia, sizeof(name->sin_addr));
    1.45 +    }else{
    1.46 +      logwrite(LOG_ALERT,
    1.47 +	       "invalid address '%s': inet_aton() failed (deleting)\n",
    1.48 +	       iface->address);
    1.49 +      return FALSE;
    1.50 +    }
    1.51 +  }else{
    1.52 +    logwrite(LOG_ALERT,
    1.53 +	     "invalid address '%s', should begin with a aphanumeric (deleting)\n",
    1.54 +	     iface->address);
    1.55 +    return FALSE;
    1.56 +  }
    1.57 +#else
    1.58 +  /* this is how others to it. I follow the crowd... */
    1.59 +  if(inet_aton(iface->address, &ia) != 0){
    1.60 +    /* IP address */
    1.61 +    memcpy(&(name->sin_addr), &ia, sizeof(name->sin_addr));
    1.62 +  }else{
    1.63 +    if ((he = gethostbyname(iface->address)) == NULL) {
    1.64 +      logwrite(LOG_ALERT, "local address '%s' unknown. (deleting)\n", iface->address);
    1.65 +      return FALSE;
    1.66 +    }
    1.67 +    memcpy(&(name->sin_addr), he->h_addr, sizeof(name->sin_addr));
    1.68 +  }
    1.69 +#endif
    1.70 +  name->sin_family = AF_INET;
    1.71 +  name->sin_port = htons(iface->port);
    1.72 +
    1.73 +  return TRUE;
    1.74 +}
    1.75 +
    1.76 +int make_server_socket(interface *iface)
    1.77 +{
    1.78 +  int sock = -1;
    1.79 +  struct sockaddr_in server;
    1.80 +        
    1.81 +  memset(&server, 0, sizeof(struct sockaddr_in));
    1.82 +
    1.83 +  /* Create the socket. */
    1.84 +  sock = socket (PF_INET, SOCK_STREAM, 0);
    1.85 +  if (sock < 0){
    1.86 +    logwrite(LOG_ALERT, "socket: %s\n", strerror(errno));
    1.87 +    return -1;
    1.88 +  }
    1.89 +        
    1.90 +  if(init_sockaddr(&server, iface)){
    1.91 +    /* bind the socket */
    1.92 +    if (bind (sock, (struct sockaddr *) &server, sizeof (server)) < 0){
    1.93 +      logwrite(LOG_ALERT, "bind: %s\n", strerror(errno));
    1.94 +      return -1;
    1.95 +    }
    1.96 +  }else{
    1.97 +    close(sock);
    1.98 +    return -1;
    1.99 +  }
   1.100 +        
   1.101 +  return sock;
   1.102 +}