By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,548 Members | 1,495 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,548 IT Pros & Developers. It's quick & easy.

Program analysis (pseudocode if possible)

P: n/a
#include <err.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/route.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if_ether.h>
#include <net/if_dl.h>
#include <ifaddrs.h>
#include <stdio.h>

#include "ipschema.h"
#include "ssrv/sockaddr.h"

#ifndef lint
__RCSID("$Id: ethip.c 2314 2004-12-29 01:00:25Z dyoung $");
#endif

#define ETHIP "ethip"
#define WLANIP "wlanip"
#define RTMSGIP "rtip"
#define ETH6IP "eth6ip"
#define WLAN6IP "wlan6ip"

#define ENDSWITH(s, end) (strlen((s)) >= strlen((end)) && \
strcmp((s) + strlen((s)) - strlen((end)),
end) == 0)

void
usage(const char *proggie)
{
errx(EXIT_FAILURE, "Usage: %s <interface>", proggie);
}

int
main(int argc, char **argv)
{
char buf[100];
struct sockaddr_dl *sdl;
int masklen, rc;
struct sockaddr *iaddr;
struct ether_addr mac;
struct ifaddrs *ifap, *ifa_iter;
ab_to_ip_t ab_to_ip;

if (argc < 2)
usage(argv[0]);

if (ENDSWITH(argv[0], ETHIP)) {
ab_to_ip = ab_to_eth_ip;
} else if (ENDSWITH(argv[0], WLANIP)) {
ab_to_ip = ab_to_wlan_ip;
} else if (ENDSWITH(argv[0], RTMSGIP)) {
ab_to_ip = ab_to_routemsg_ip;
} else if (ENDSWITH(argv[0], ETH6IP)) {
ab_to_ip = ab_to_eth_ip6;
} else {
errx(EXIT_FAILURE, "%s: unimplemented.", argv[0]);
}

if (getifaddrs(&ifap) == -1)
err(EXIT_FAILURE, "ioctl");

for (ifa_iter = ifap; ifa_iter; ifa_iter = ifa_iter->ifa_next)
if (strcmp(ifa_iter->ifa_name, argv[1]) == 0)
break;

if (!ifa_iter)
errx(EXIT_FAILURE, "no such interface as %s", argv[1]);

for (ifa_iter = ifap; ifa_iter; ifa_iter = ifa_iter->ifa_next) {
if (strcmp(ifa_iter->ifa_name, argv[1]) != 0)
continue;
if (ifa_iter->ifa_addr->sa_family == AF_LINK)
break;
}

if (!ifa_iter)
errx(EXIT_FAILURE, "no link-level address for %s", argv[1]);

sdl = (struct sockaddr_dl *)ifa_iter->ifa_addr;

(void)memcpy(&mac, LLADDR(sdl), sizeof(mac));
masklen = mac_to_ip(&mac, ab_to_ip, &iaddr);
rc = sockaddr_snprintf(buf, sizeof(buf), "%a", iaddr);
if (rc == -1 || rc >= sizeof(buf))
errx(EXIT_FAILURE, "%s: sockaddr_snprintf", getprogname());
sockaddr_free(iaddr);
(void)printf("%s/%d\n", buf, masklen);

freeifaddrs(ifap);
return 0;
}

Jul 3 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Vusi wrote:
#include <err.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/route.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if_ether.h>
#include <net/if_dl.h>
#include <ifaddrs.h>
#include <stdio.h>

#include "ipschema.h"
#include "ssrv/sockaddr.h"

#ifndef lint
__RCSID("$Id: ethip.c 2314 2004-12-29 01:00:25Z dyoung $");
#endif

#define ETHIP "ethip"
#define WLANIP "wlanip"
#define RTMSGIP "rtip"
#define ETH6IP "eth6ip"
#define WLAN6IP "wlan6ip"

#define ENDSWITH(s, end) (strlen((s)) >= strlen((end)) && \
strcmp((s) + strlen((s)) - strlen((end)),
end) == 0)

void
usage(const char *proggie)
{
errx(EXIT_FAILURE, "Usage: %s <interface>", proggie);
}

int
main(int argc, char **argv)
{
char buf[100];
struct sockaddr_dl *sdl;
int masklen, rc;
struct sockaddr *iaddr;
struct ether_addr mac;
struct ifaddrs *ifap, *ifa_iter;
ab_to_ip_t ab_to_ip;

if (argc < 2)
usage(argv[0]);

if (ENDSWITH(argv[0], ETHIP)) {
ab_to_ip = ab_to_eth_ip;
} else if (ENDSWITH(argv[0], WLANIP)) {
ab_to_ip = ab_to_wlan_ip;
} else if (ENDSWITH(argv[0], RTMSGIP)) {
ab_to_ip = ab_to_routemsg_ip;
} else if (ENDSWITH(argv[0], ETH6IP)) {
ab_to_ip = ab_to_eth_ip6;
} else {
errx(EXIT_FAILURE, "%s: unimplemented.", argv[0]);
}

if (getifaddrs(&ifap) == -1)
err(EXIT_FAILURE, "ioctl");

for (ifa_iter = ifap; ifa_iter; ifa_iter = ifa_iter->ifa_next)
if (strcmp(ifa_iter->ifa_name, argv[1]) == 0)
break;

if (!ifa_iter)
errx(EXIT_FAILURE, "no such interface as %s", argv[1]);

for (ifa_iter = ifap; ifa_iter; ifa_iter = ifa_iter->ifa_next) {
if (strcmp(ifa_iter->ifa_name, argv[1]) != 0)
continue;
if (ifa_iter->ifa_addr->sa_family == AF_LINK)
break;
}

if (!ifa_iter)
errx(EXIT_FAILURE, "no link-level address for %s", argv[1]);

sdl = (struct sockaddr_dl *)ifa_iter->ifa_addr;

(void)memcpy(&mac, LLADDR(sdl), sizeof(mac));
masklen = mac_to_ip(&mac, ab_to_ip, &iaddr);
rc = sockaddr_snprintf(buf, sizeof(buf), "%a", iaddr);
if (rc == -1 || rc >= sizeof(buf))
errx(EXIT_FAILURE, "%s: sockaddr_snprintf", getprogname());
sockaddr_free(iaddr);
(void)printf("%s/%d\n", buf, masklen);

freeifaddrs(ifap);
return 0;
}
<pseudocode>

BEGIN PROGRAM
read_this('http://www.catb.org/~esr/faqs/smart-questions.html')
do_your_homework()
END PROGRAM

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 3 '06 #3

P: n/a
"Bruno Desthuilliers" <on***@xiludom.growrote in message
news:44***********************@news.free.fr...
<snip>
<pseudocode>

BEGIN PROGRAM
read_this('http://www.catb.org/~esr/faqs/smart-questions.html')
do_your_homework()
if (cant_figure_it_out)
post_specific_question_on_relevant_newsgroup("comp .lang.c")
END PROGRAM

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"

Jul 3 '06 #4

P: n/a
Paul McGuire wrote:
"Bruno Desthuilliers" <on***@xiludom.growrote in message
news:44***********************@news.free.fr...
<snip>
>><pseudocode>

BEGIN PROGRAM
read_this('http://www.catb.org/~esr/faqs/smart-questions.html')
do_your_homework()

if (cant_figure_it_out)
BEGIN
post_specific_question_on_relevant_newsgroup("comp .lang.c")
answer =
post_specific_question_on_relevant_newsgroup("comp .lang.c")
assert answer == DO_YOUR_HOMEWORK
END IF
>>END PROGRAM

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"



--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 3 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.