diff options
author | mjacob <mjacob@FreeBSD.org> | 2001-07-07 19:09:21 +0000 |
---|---|---|
committer | mjacob <mjacob@FreeBSD.org> | 2001-07-07 19:09:21 +0000 |
commit | 4d2ef515ad618dc8230813a5d4c7f47260edfbde (patch) | |
tree | 2ae4a4e265bfcca96dd52d6319eabaf12aa099cc /sbin/ping/ping.c | |
parent | 3f215375a01b2e96415cc28492ca5495e6ed6714 (diff) | |
download | FreeBSD-src-4d2ef515ad618dc8230813a5d4c7f47260edfbde.zip FreeBSD-src-4d2ef515ad618dc8230813a5d4c7f47260edfbde.tar.gz |
Ian Dowse writes:
The original code was certainly broken; it knows that whereto is
to be used for a sockaddr_in, so it should be declared as such.
To support multiple protocols, there is also a sockaddr_storage
struct that can be used; I don't think struct sockaddr is supposed
to be used anywhere other than for casts and pointers.
Submitted by: Ian Dowse <iedowse@maths.tcd.ie>
MFC after: 3 weeks
Diffstat (limited to 'sbin/ping/ping.c')
-rw-r--r-- | sbin/ping/ping.c | 23 |
1 files changed, 7 insertions, 16 deletions
diff --git a/sbin/ping/ping.c b/sbin/ping/ping.c index 8e111bb..5840b8f 100644 --- a/sbin/ping/ping.c +++ b/sbin/ping/ping.c @@ -146,15 +146,7 @@ int options; int mx_dup_ck = MAX_DUP_CHK; char rcvd_tbl[MAX_DUP_CHK / 8]; -/* - * Use a union to coerce alignment to at least sockaddr_in's alignment. - * This avoids unaligned access faults on alpha. - */ -union { - struct sockaddr _w2; /* who to ping */ - struct sockaddr_in _w2_in; /* who to ping */ -} ww; -#define whereto ww._w2 +struct sockaddr_in whereto; /* who to ping */ int datalen = DEFDATALEN; int s; /* socket file descriptor */ u_char outpack[MAXPACKET]; @@ -433,9 +425,10 @@ main(argc, argv) err(1, "bind"); } - bzero((char *)&whereto, sizeof(struct sockaddr)); - to = (struct sockaddr_in *)&whereto; + bzero(&whereto, sizeof(whereto)); + to = &whereto; to->sin_family = AF_INET; + to->sin_len = sizeof *to; if (inet_aton(target, &to->sin_addr) != 0) { hostname = target; } else { @@ -444,7 +437,6 @@ main(argc, argv) errx(EX_NOHOST, "cannot resolve %s: %s", target, hstrerror(h_errno)); - to->sin_len = sizeof *to; if (hp->h_length > sizeof(to->sin_addr)) errx(1,"gethostbyname2 returned an illegal address"); memcpy(&to->sin_addr, hp->h_addr_list[0], sizeof to->sin_addr); @@ -769,8 +761,8 @@ pinger(void) /* compute ICMP checksum here */ icp->icmp_cksum = in_cksum((u_short *)icp, cc); - i = sendto(s, (char *)outpack, cc, 0, &whereto, - sizeof(struct sockaddr)); + i = sendto(s, (char *)outpack, cc, 0, (struct sockaddr *)&whereto, + sizeof(whereto)); if (i < 0 || i != cc) { if (i < 0) { @@ -922,8 +914,7 @@ pr_pack(buf, cc, from, tv) if (((options & F_VERBOSE) && uid == 0) || (!(options & F_QUIET2) && - (oip->ip_dst.s_addr == - ((struct sockaddr_in *)&whereto)->sin_addr.s_addr) && + (oip->ip_dst.s_addr == whereto.sin_addr.s_addr) && (oip->ip_p == IPPROTO_ICMP) && (oicmp->icmp_type == ICMP_ECHO) && (oicmp->icmp_id == ident))) { |