Apple Enterprise - NeXTanswers Support Archive
/*
* niping
*
* usage: niping host/tag
* compile with: cc -o niping niping.c
*
* This program connects to a server and gets the ni_id of the
* root directory. Its main function is to test to see if the
* server is up. The parse_serve_tag() function is useful for
* dealing with strings of the form "ranger/network" or
* "192.42.172.34/local". It accepts a string as input and
* gives bask a server's address and tag in the format that
* are expected by ni_connect().
*
* myhost> niping mustang/local
* connected to NetInfo server mustang/local
*
* myhost> niping 192.42.172.34/network
* connected to NetInfo server 192.42.172.34/network
*
* myhost> niping mumble
* mumble: can't understand arg
* usage: niping host/tag
* host can be a host name or IP address
*
* myhost> niping casper/ghost
* Communication failure
*
* Author: Marc Majka
* You may freely copy, distribute and reuse the code in this example.
* NeXT disclaims any warranty of any kind, expressed or implied, as to
* its fitness for any particular use.
*/
#import <netinfo/ni.h>
#import <ansi/stdio.h>
#import <bsd/netinet/in.h>
#import <bsd/sys/socket.h>
#import <bsd/errno.h>
#import <ansi/ctype.h>
#import <bsd/netdb.h>
#import <bsd/c.h>
#import <bsd/strings.h>
main(int argc, char *argv[])
{
char *tag;
void *dom;
ni_id rootdir;
ni_status ret;
bool parse_server_tag();
struct sockaddr_in server;
/* check usage */
if (argc != 2) {
fprintf(stderr, "usage: %s host/tag\n", argv[0]);
fprintf(stderr, "host can be a host name or IP address\n");
exit(1);
}
/* call a function to parse the input arg */
if (FALSE == parse_server_tag(argv[1], &server, &tag)) {
fprintf(stderr, "%s: can't understand arg %s\n", argv[1]);
fprintf(stderr, "usage: %s host/tag\n", argv[0]);
fprintf(stderr, "host can be a host name or IP address\n");
exit(1);
}
/* connect to the specified server */
dom = ni_connect(&server, tag);
if (dom == NULL) {
fprintf(stderr, "connection to %s failed\n", argv[1]);
free(tag);
exit(1);
}
/* abort on errors */
ni_setabort(dom, 1);
/* set a 4 second read timeout */
ni_setreadtimeout(dom, 4);
/* try to get the id of the root directory */
ret = ni_root(dom, &rootdir);
if (ret != NI_OK) {
fprintf(stderr, "%s\n", ni_error(ret));
exit(1);
}
printf("connected to NetInfo server %s\n", argv[1]);
/* clean up */
free(tag);
ni_free(dom);
exit(0);
}
bool parse_server_tag(char *str, struct sockaddr_in *server, char **t)
{
int len, i;
char *host, *tag, *slash;
struct hostent *hent;
len = strlen(str);
/* find the "/" character */
slash = index(str, '/');
/* check to see if the "/" is missing */
if (slash == NULL) {
/* error: no "/" character */
return(FALSE);
}
/* find the location of the '/' */
i = slash - str;
/* allocate some space for the host and tag */
host = (char *)malloc(i + 1);
*t = (char *)malloc(len - i);
tag = *t;
/* copy out the host */
strncpy(host, str, i);
/* copy out the tag */
strcpy(tag, slash + 1);
/* assume the host is a hostname */
hent = gethostbyname(host);
if (hent != NULL) {
/* found a host with that name */
bcopy(hent->h_addr, &server->sin_addr, hent->h_length);
}
else {
/* no host with that name */
/* try interpreting it as an address */
server->sin_addr.s_addr = inet_addr(host);
/* bail out if inet_addr() returned -1 */
/* it isn't a sensible address */
if (server->sin_addr.s_addr == -1) {
fprintf(stderr, "Can't find address for %s\n", host);
free(host);
free(tag);
return(1);
}
}
free(host);
return(TRUE);
}