Chris Costello <[EMAIL PROTECTED]> wrote:
> On Saturday, November 24, 2001, Igor M Podlesny wrote:
> > i = inet_aton(argv[3], &in);
> > - if (!i)
> > - errx(1, "Couldn't make sense of ip-number\n");
> > + if (!i) {
> > + /* check if it is resolveable */
> > + struct hostent *hp;
> > + hp = gethostbyname(argv[3]);
> > + if (hp == NULL) {
> > + errx(1, "Couldn't make sense of the jail address\n");
> > + }
> > + else {
> > + char **p = hp->h_addr_list;
> > + if (p[1] != NULL) {
> > + errx(1, "Jail should have only one ip-address
>associated with!\n");
> > + }
> > + else {
> > + memcpy(&in.s_addr, p[0], sizeof(in.s_addr));
> > + }
> > + }
> > + }
>
> I'd rewrite the above (`i = inet_aton' all the way down) as
>
> hp = gethostbyname(argv[3]);
> if (hp == NULL) {
> errx(1, "%s: %s", argv[3], hstrerror(h_errno));
> }
> in = *(struct in_addr *)hp->h_addr_list[0];
>
> This makes the call to inet_aton() unnecessary (and really
> shortens the code!).
As discussed off-list, this is a good idea. Attached is the final
patch that I plan to commit unless I hear objections. Please review.
Thanks.
Index: jail.8
===================================================================
RCS file: /ref/cvsf/src/usr.sbin/jail/jail.8,v
retrieving revision 1.30
diff -u -r1.30 jail.8
--- jail.8 2001/09/03 15:42:10 1.30
+++ jail.8 2001/12/09 19:14:30
@@ -43,7 +43,7 @@
.Nm
.Ar path
.Ar hostname
-.Ar ip-number
+.Ar hostname
.Ar command
.Ar ...
.Sh DESCRIPTION
Index: jail.c
===================================================================
RCS file: /ref/cvsf/src/usr.sbin/jail/jail.c,v
retrieving revision 1.7
diff -u -r1.7 jail.c
--- jail.c 2001/06/24 20:28:19 1.7
+++ jail.c 2001/12/09 19:14:11
@@ -17,6 +17,7 @@
#include <arpa/inet.h>
#include <err.h>
+#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -25,12 +26,13 @@
int
main(int argc, char **argv)
{
+ struct hostent *hp;
struct jail j;
int i;
struct in_addr in;
if (argc < 5)
- errx(1, "Usage: %s path hostname ip-number command ...\n",
+ errx(1, "Usage: %s path hostname hostname command ...\n",
argv[0]);
i = chdir(argv[1]);
if (i)
@@ -39,9 +41,11 @@
j.version = 0;
j.path = argv[1];
j.hostname = argv[2];
+ hp = gethostbyname(argv[3]);
+ if (hp == NULL)
+ errx(1, "gethostbyname(%s): %s", argv[3], hstrerror(h_errno));
i = inet_aton(argv[3], &in);
- if (!i)
- errx(1, "Couldn't make sense of ip-number\n");
+ in = *(struct in_addr *)hp->h_addr;
j.ip_number = ntohl(in.s_addr);
i = jail(&j);
if (i)
To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message