6 * Author: Harti Brandt <harti@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $Begemot: bsnmp/lib/support.c,v 1.1 2004/08/06 08:47:58 brandt Exp $
31 * Functions that are missing on certain systems.
43 warnx(const char *fmt
, ...)
48 fprintf(stderr
, "warning: ");
49 vfprintf(stderr
, fmt
, ap
);
50 fprintf(stderr
, "\n");
55 warn(const char *fmt
, ...)
61 fprintf(stderr
, "warning: ");
62 vfprintf(stderr
, fmt
, ap
);
63 fprintf(stderr
, ": %s\n", strerror(e
));
68 errx(int code
, const char *fmt
, ...)
73 fprintf(stderr
, "error: ");
74 vfprintf(stderr
, fmt
, ap
);
75 fprintf(stderr
, "\n");
81 err(int code
, const char *fmt
, ...)
87 fprintf(stderr
, "error: ");
88 vfprintf(stderr
, fmt
, ap
);
89 fprintf(stderr
, ": %s\n", strerror(e
));
99 strlcpy(char *dst
, const char *src
, size_t len
)
101 size_t ret
= strlen(dst
);
114 #ifndef HAVE_GETADDRINFO
116 #include <sys/types.h>
117 #include <sys/socket.h>
118 #include <netinet/in.h>
123 extern const char *h_errlist
[];
126 * VERY poor man's implementation
129 getaddrinfo(const char *host
, const char *port
, const struct addrinfo
*hints
,
130 struct addrinfo
**res
)
132 struct hostent
*hent
;
133 struct sockaddr_in
*s
;
134 struct servent
*sent
;
136 if ((hent
= gethostbyname(host
)) == NULL
)
138 if (hent
->h_addrtype
!= hints
->ai_family
)
139 return (HOST_NOT_FOUND
);
140 if (hent
->h_addrtype
!= AF_INET
)
141 return (HOST_NOT_FOUND
);
143 if ((*res
= malloc(sizeof(**res
))) == NULL
)
144 return (HOST_NOT_FOUND
);
146 (*res
)->ai_flags
= hints
->ai_flags
;
147 (*res
)->ai_family
= hints
->ai_family
;
148 (*res
)->ai_socktype
= hints
->ai_socktype
;
149 (*res
)->ai_protocol
= hints
->ai_protocol
;
150 (*res
)->ai_next
= NULL
;
152 if (((*res
)->ai_addr
= malloc(sizeof(struct sockaddr_in
))) == NULL
) {
154 return (HOST_NOT_FOUND
);
156 (*res
)->ai_addrlen
= sizeof(struct sockaddr_in
);
157 s
= (struct sockaddr_in
*)(*res
)->ai_addr
;
158 s
->sin_family
= hints
->ai_family
;
159 s
->sin_len
= sizeof(*s
);
160 memcpy(&s
->sin_addr
, hent
->h_addr
, 4);
162 if ((sent
= getservbyname(port
, NULL
)) == NULL
) {
164 return (HOST_NOT_FOUND
);
166 s
->sin_port
= sent
->s_port
;
175 if (e
< 0 || e
>= h_nerr
)
176 return ("unknown error");
177 return (h_errlist
[e
]);
181 freeaddrinfo(struct addrinfo
*p
)
183 struct addrinfo
*next
;
187 if (p
->ai_addr
!= NULL
)