Back to index

courier  0.68.2
rfc1035mkaddress.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 1998 - 2000 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 #include      "config.h"
00006 #include      "rfc1035.h"
00007 #include      <sys/types.h>
00008 #include      <sys/socket.h>
00009 #include      <arpa/inet.h>
00010 #include      <errno.h>
00011 #include      <string.h>
00012 
00013 
00014 /*
00015 **     Ok, take an address, and a port, and come back with a socket address
00016 **     in the specified address family.
00017 */
00018 
00019 int    rfc1035_mkaddress(int af,
00020                      RFC1035_NETADDR *buf,
00021                      const RFC1035_ADDR *addr,
00022                      int port,
00023                      const struct sockaddr **ptr,
00024                      int *len)
00025 {
00026 struct sockaddr_in sin;
00027 
00028 #if    RFC1035_IPV6
00029 
00030        if (af == AF_INET6)
00031        {
00032        struct sockaddr_in6 sin6;
00033 
00034               memset(&sin6, 0, sizeof(sin6));
00035               sin6.sin6_family=af;
00036               sin6.sin6_addr= *addr;
00037               sin6.sin6_port=port;
00038               memcpy(buf, &sin6, sizeof(sin6));
00039               *ptr=(struct sockaddr *)buf;
00040               *len=sizeof(sin6);
00041               return (0);
00042        }
00043 
00044        if (af != AF_INET || (!IN6_IS_ADDR_V4MAPPED(addr) && memcmp(addr, &in6addr_any, sizeof(*addr))))
00045        {
00046               errno=EAFNOSUPPORT;
00047               return (-1);
00048        }
00049 
00050        memset(&sin, 0, sizeof(sin));
00051        sin.sin_family=af;
00052        rfc1035_ipv6to4(&sin.sin_addr, addr);
00053        sin.sin_port=port;
00054 #else
00055        if (af != AF_INET)
00056        {
00057               errno=EINVAL;
00058               return (-1);
00059        }
00060        memset(&sin, 0, sizeof(sin));
00061        sin.sin_family=af;
00062        sin.sin_addr= *addr;
00063        sin.sin_port=port;
00064 #endif
00065        memcpy(buf, &sin, sizeof(sin));
00066        *ptr=(struct sockaddr *)buf;
00067        *len=sizeof(sin);
00068        return (0);
00069 }