Program analysis (pseudocode if possible)


V

Vusi

#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;
}
 
Ad

Advertisements

B

Bruno Desthuilliers

Vusi said:
#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
 
Ad

Advertisements

B

Bruno Desthuilliers

Paul said:
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
 

Top