OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / freeswan / lib / initaddr.c
1 /*
2  * initialize address structure
3  * Copyright (C) 2000  Henry Spencer.
4  * 
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Library General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.  See <http://www.fsf.org/copyleft/lgpl.txt>.
9  * 
10  * This library is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
13  * License for more details.
14  *
15  * RCSID $Id: initaddr.c,v 1.2 2000/08/25 14:40:05 henry Exp $
16  */
17 #include "internal.h"
18 #include "freeswan.h"
19
20 /*
21  - initaddr - initialize ip_address from bytes
22  */
23 err_t                           /* NULL for success, else string literal */
24 initaddr(src, srclen, af, dst)
25 const unsigned char *src;
26 size_t srclen;
27 int af;                         /* address family */
28 ip_address *dst;
29 {
30         switch (af) {
31         case AF_INET:
32                 if (srclen != 4)
33                         return "IPv4 address must be exactly 4 bytes";
34                 dst->u.v4.sin_family = af;
35                 dst->u.v4.sin_port = 0;         /* unused */
36                 memcpy((char *)&dst->u.v4.sin_addr.s_addr, src, srclen);
37                 break;
38         case AF_INET6:
39                 if (srclen != 16)
40                         return "IPv6 address must be exactly 16 bytes";
41                 dst->u.v6.sin6_family = af;
42                 dst->u.v6.sin6_flowinfo = 0;            /* unused */
43                 dst->u.v6.sin6_port = 0;                /* unused */
44                 memcpy((char *)&dst->u.v6.sin6_addr, src, srclen);
45                 break;
46         default:
47                 return "unknown address family in initaddr";
48                 break;
49         }
50         return NULL;
51 }