?? irpmarshall.c
字號:
joinarray(ne->n_aliases, *buffer, COMMA) ; strcat(*buffer, fieldsep); strcat(*buffer, nAddrType); strcat(*buffer, fieldsep); strcat(*buffer, nNet); strcat(*buffer, fieldsep); return (0);}/* * int irp_unmarshall_ne(struct netent *ne, char *buffer) * * notes: * * See note up top. * * return: * * 0 on success and -1 on failure. * */intirp_unmarshall_ne(struct netent *ne, char *buffer) { char *p, *q; int naddrtype; long nnet; int bits; char *name = NULL; char **aliases = NULL; char tmpbuf[24]; char *tb; char fieldsep = ':'; int myerrno = EINVAL; if (ne == NULL || buffer == NULL) { goto error; } p = buffer; /* n_name field */ name = NULL; if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0U) { goto error; } /* n_aliases field. Aliases are separated by commas */ q = strchr(p, fieldsep); if (q == NULL) { goto error; } aliases = splitarray(p, q, COMMA); if (aliases == NULL) { myerrno = errno; goto error; } p = q + 1; /* h_addrtype field */ tb = tmpbuf; if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL || strlen(tb) == 0U) { goto error; } if (strcmp(tmpbuf, "AF_INET") == 0) naddrtype = AF_INET; else if (strcmp(tmpbuf, "AF_INET6") == 0) naddrtype = AF_INET6; else goto error; /* n_net field */ tb = tmpbuf; if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL || strlen(tb) == 0U) { goto error; } bits = inet_net_pton(naddrtype, tmpbuf, &nnet, sizeof nnet); if (bits < 0) { goto error; } nnet = ntohl(nnet); ne->n_name = name; ne->n_aliases = aliases; ne->n_addrtype = naddrtype; ne->n_net = nnet; return (0); error: errno = myerrno; if (name != NULL) free(name); free_array(aliases, 0); return (-1);}/* ------------------------- struct netent ------------------------- *//* =========================================================================== *//* * static char ** splitarray(const char *buffer, const char *buffend, char delim) * * notes: * * Split a delim separated astring. Not allowed * to have two delims next to each other. BUFFER points to begining of * string, BUFFEND points to one past the end of the string * (i.e. points at where the null byte would be if null * terminated). * * return: * * Returns a malloced array of pointers, each pointer pointing to a * malloced string. If BUFEER is an empty string, then return values is * array of 1 pointer that is NULL. Returns NULL on failure. * */static char **splitarray(const char *buffer, const char *buffend, char delim) { const char *p, *q; int count = 0; char **arr = NULL; char **aptr; if (buffend < buffer) return (NULL); else if (buffend > buffer && *buffer == delim) return (NULL); else if (buffend > buffer && *(buffend - 1) == delim) return (NULL); /* count the number of field and make sure none are empty */ if (buffend > buffer + 1) { for (count = 1, q = buffer ; q != buffend ; q++) { if (*q == delim) { if (q > buffer && (*(q - 1) == delim)) { errno = EINVAL; return (NULL); } count++; } } } if (count > 0) { count++ ; /* for NULL at end */ aptr = arr = malloc(count * sizeof (char *)); if (aptr == NULL) { errno = ENOMEM; return (NULL); } memset(arr, 0x0, count * sizeof (char *)); for (p = buffer ; p < buffend ; p++) { for (q = p ; *q != delim && q != buffend ; q++) /* nothing */; *aptr = strndup(p, q - p); p = q; aptr++; } *aptr = NULL; } else { arr = malloc(sizeof (char *)); if (arr == NULL) { errno = ENOMEM; return (NULL); } *arr = NULL; } return (arr);}/* * static size_t joinlength(char * const *argv) * * return: * * the number of bytes in all the arrays pointed at * by argv, including their null bytes(which will usually be turned * into commas). * * */static size_tjoinlength(char * const *argv) { int len = 0; while (argv && *argv) { len += (strlen(*argv) + 1); argv++; } return (len);}/* * int joinarray(char * const *argv, char *buffer, char delim) * * notes: * * Copy all the ARGV strings into the end of BUFFER * separating them with DELIM. BUFFER is assumed to have * enough space to hold everything and to be already null-terminated. * * return: * * 0 unless argv or buffer is NULL. * * */static intjoinarray(char * const *argv, char *buffer, char delim) { char * const *p; char sep[2]; if (argv == NULL || buffer == NULL) { errno = EINVAL; return (-1); } sep[0] = delim; sep[1] = 0x0; for (p = argv ; *p != NULL ; p++) { strcat(buffer, *p); if (*(p + 1) != NULL) { strcat(buffer, sep); } } return (0);}/* * static char * getfield(char **res, size_t reslen, char **ptr, char delim) * * notes: * * Stores in *RES, which is a buffer of length RESLEN, a * copy of the bytes from *PTR up to and including the first * instance of DELIM. If *RES is NULL, then it will be * assigned a malloced buffer to hold the copy. *PTR is * modified to point at the found delimiter. * * return: * * If there was no delimiter, then NULL is returned, * otherewise *RES is returned. * */static char *getfield(char **res, size_t reslen, char **ptr, char delim) { char *q; if (res == NULL || ptr == NULL || *ptr == NULL) { errno = EINVAL; return (NULL); } q = strchr(*ptr, delim); if (q == NULL) { errno = EINVAL; return (NULL); } else { if (*res == NULL) { *res = strndup(*ptr, q - *ptr); } else { if ((size_t)(q - *ptr + 1) > reslen) { /* to big for res */ errno = EINVAL; return (NULL); } else { strncpy(*res, *ptr, q - *ptr); (*res)[q - *ptr] = 0x0; } } *ptr = q + 1; } return (*res);}#ifndef HAVE_STRNDUP/* * static char * strndup(const char *str, size_t len) * * notes: * * like strdup, except do len bytes instead of the whole string. Always * null-terminates. * * return: * * The newly malloced string. * */static char *strndup(const char *str, size_t len) { char *p = malloc(len + 1); if (p == NULL) return (NULL); strncpy(p, str, len); p[len] = 0x0; return (p);}#endif#if WANT_MAIN/* * static int strcmp_nws(const char *a, const char *b) * * notes: * * do a strcmp, except uneven lengths of whitespace compare the same * * return: * */static intstrcmp_nws(const char *a, const char *b) { while (*a && *b) { if (isspace(*a) && isspace(*b)) { do { a++; } while (isspace(*a)); do { b++; } while (isspace(*b)); } if (*a < *b) return (-1); else if (*a > *b) return (1); a++; b++;; } if (*a == *b) return (0); else if (*a > *b) return (1); else return (-1);}#endif/* * static void free_array(char **argv, size_t entries) * * notes: * * Free argv and each of the pointers inside it. The end of * the array is when a NULL pointer is found inside. If * entries is > 0, then NULL pointers inside the array do * not indicate the end of the array. * */static voidfree_array(char **argv, size_t entries) { char **p = argv; int useEntries = (entries > 0U); if (argv == NULL) return; while ((useEntries && entries > 0U) || *p) { if (*p) free(*p); p++; if (useEntries) entries--; } free(argv);}/* ************************************************** */#if WANT_MAIN/* takes an option to indicate what sort of marshalling(read the code) and an argument. If the argument looks like a marshalled buffer(has a ':' embedded) then it's unmarshalled and the remarshalled and the new string is compared to the old one.*/intmain(int argc, char **argv) { char buffer[1024]; char *b = &buffer[0]; size_t len = sizeof buffer; char option; if (argc < 2 || argv[1][0] != '-') exit(1); option = argv[1][1]; argv++; argc--;#if 0 { char buff[10]; char *p = argv[1], *q = &buff[0]; while (getfield(&q, sizeof buff, &p, ':') != NULL) { printf("field: \"%s\"\n", q); p++; } printf("p is now \"%s\"\n", p); }#endif#if 0 { char **x = splitarray(argv[1], argv[1] + strlen(argv[1]), argv[2][0]); char **p; if (x == NULL) printf("split failed\n"); for (p = x ; p != NULL && *p != NULL ; p++) { printf("\"%s\"\n", *p); } }#endif#if 1 switch(option) { case 'n': { struct nwent ne; int i; if (strchr(argv[1], ':') != NULL) { if (irp_unmarshall_nw(&ne, argv[1]) != 0) { printf("Unmarhsalling failed\n"); exit(1); } printf("Name: \"%s\"\n", ne.n_name); printf("Aliases:"); for (i = 0 ; ne.n_aliases[i] != NULL ; i++) printf("\n\t\"%s\"", ne.n_aliases[i]); printf("\nAddrtype: %s\n", ADDR_T_STR(ne.n_addrtype)); inet_net_ntop(ne.n_addrtype, ne.n_addr, ne.n_length, buffer, sizeof buffer); printf("Net: \"%s\"\n", buffer); *((long*)ne.n_addr) = htonl(*((long*)ne.n_addr)); inet_net_ntop(ne.n_addrtype, ne.n_addr, ne.n_length, buffer, sizeof buffer); printf("Corrected Net: \"%s\"\n", buffer); } else { struct netent *np1 = getnetbyname(argv[1]); ne.n_name = np1->n_name; ne.n_aliases = np1->n_aliases; ne.n_addrtype = np1->n_addrtype; ne.n_addr = &np1->n_net; ne.n_length = (IN_CLASSA(np1->n_net) ? 8 : (IN_CLASSB(np1->n_net) ? 16 : (IN_CLASSC(np1->n_net) ? 24 : -1))); np1->n_net = htonl(np1->n_net); if (irp_marshall_nw(&ne, &b, &len) != 0) { printf("Marshalling failed\n"); } printf("%s\n", b); } break; } case 'r': { char **hosts, **users, **domains; size_t entries; int i; char *buff; size_t size; char *ngname; if (strchr(argv[1], '(') != NULL) { if (irp_unmarshall_ng(&ngname, &entries, &hosts, &users, &domains, argv[1]) != 0) { printf("unmarshall failed\n"); exit(1); }#define STRVAL(x) (x == NULL ? "*" : x) printf("%s {\n", ngname); for (i = 0 ; i < entries ; i++) printf("\t\"%s\" : \"%s\" : \"%s\"\n", STRVAL(hosts[i]), STRVAL(users[i]), STRVAL(domains[i])); printf("}\n\n\n"); irp_marshall_ng_start(ngname, NULL, &size); for (i = 0 ; i < entries ; i++) irp_marshall_ng_next(hosts[i], users[i], domains[i], NULL, &size); irp_marshall_ng_end(NULL, &size); buff = malloc(size); irp_marshall_ng_start(ngname, buff, &size); for (i = 0 ; i < entries ; i++) { if (irp_marshall_ng_next(hosts[i], users[i], domains[i], buff, &size) != 0) printf("next marshalling failed.\n"); } irp_marshall_ng_end(buff, &size); if (strcmp_nws(argv[1], buff) != 0) { printf("compare failed:\n\t%s\n\t%s\n", buffer, argv[1]); } else { printf("compare ok\n"); } } else { char *h, *u, *d, *buff; size_t size; /* run through two times. First to figure out how much of a buffer we need. Second to do the actual marshalling */ setnetgrent(argv[1]); irp_marshall_ng_start(argv[1], NULL, &size); while (getnetgrent(&h, &u, &d) == 1) irp_marshall_ng_next(h, u, d, NULL, &size); irp_marshall_ng_end(NULL, &size); endnetgrent(argv[1]); buff = malloc(size); setnetgrent(argv[1]); if (irp_marshall_ng_start(argv[1], buff, &size) != 0) printf("Marshalling start failed\n"); while (getnetgrent(&h, &u, &d) == 1) { if (irp_marshall_ng_next(h, u, d, buff, &size) != 0) { printf("Marshalling failed\n"); } } irp_marshall_ng_end(buff, &size); endnetgrent(); printf("success: %s\n", buff); } break; } case 'h': { struct hostent he, *hp; int i; if (strchr(argv[1], '@') != NULL) { if (irp_unmarshall_ho(&he, argv[1]) != 0) { printf("unmarshall failed\n"); exit(1); } printf("Host: \"%s\"\nAliases:", he.h_name); for (i = 0 ; he.h_aliases[i] != NULL ; i++) printf("\n\t\t\"%s\"", he.h_aliases[i]); printf("\nAddr Type: \"%s\"\n", ADDR_T_STR(he.h_addrtype)); printf("Length: %d\nAddresses:", he.h_length); for (i = 0 ; he.h_addr_list[i] != 0 ; i++) { inet_ntop(he.h_addrtype, he.h_addr_list[i], buffer, sizeof buffer); printf("\n\t\"%s\"\n", buffer); } printf("\n\n"); irp_marshall_ho(&he, &b, &len); if (strcmp(argv[1], buffer) != 0) { printf("compare failed:\n\t\"%s\"\n\t\"%s\"\n", buffer, argv[1]); } else { printf("compare ok\n"); } } else { if ((hp = gethostbyname(argv[1])) == NULL) { perror("gethostbyname"); printf("\"%s\"\n", argv[1]); exit(1); } if (irp_marshall_ho(hp, &b, &len) != 0) { printf("irp_marshall_ho failed\n"); exit(1); } printf("success: \"%s\"\n", buffer); } break; } case 's': { struct servent *sv; struct servent sv1; if (strchr(argv[1], ':') != NULL) { sv = &sv1; memset(sv, 0xef, sizeof (struct servent)); if (irp_unmarshall_sv(sv, argv[1]) != 0) { printf("unmarshall failed\n"); } irp_marshall_sv(sv, &b, &len); if (strcmp(argv[1], buffer) != 0) { printf("compare failed:\n\t\"%s\"\n\t\"%s\"\n", buffer, argv[1]); } else { printf("compare ok\n"); } } else { if ((sv = getservbyname(argv[1], argv[2])) == NULL) { perror("getservent"); exit(1); } if (irp_marshall_sv(sv, &b, &len) != 0) { printf("irp_marshall_sv failed\n"); exit(1); } printf("success: \"%s\"\n", buffer); } break; } case 'g': { struct group *gr; struct group gr1; if (strchr(argv[1], ':') != NULL) { gr = &gr1; memset(gr, 0xef, sizeof (struct group)); if (irp_unmarshall_gr(gr, argv[1]) != 0) { printf("unmarshall failed\n"); } irp_marshall_gr(gr, &b, &len); if (strcmp(argv[1], buffer) != 0) { printf("compare failed:\n\t\"%s\"\n\t\"%s\"\n", buffer, argv[1]); } else { printf("compare ok\n"); } } else { if ((gr = getgrnam(argv[1])) == NULL) { perror("getgrnam"); exit(1); } if (irp_marshall_gr(gr, &b, &len) != 0) { printf("irp_marshall_gr failed\n"); exit(1); } printf("success: \"%s\"\n", buffer); } break; } case 'p': { struct passwd *pw; struct passwd pw1; if (strchr(argv[1], ':') != NULL) { pw = &pw1; memset(pw, 0xef, sizeof (*pw)); if (irp_unmarshall_pw(pw, argv[1]) != 0) { printf("unmarshall failed\n"); exit(1); } printf("User: \"%s\"\nPasswd: \"%s\"\nUid: %ld\nGid: %ld\n", pw->pw_name, pw->pw_passwd, (long)pw->pw_uid, (long)pw->pw_gid); printf("Class: \"%s\"\nChange: %ld\nGecos: \"%s\"\n", pw->pw_class, (long)pw->pw_change, pw->pw_gecos); printf("Shell: \"%s\"\nDirectory: \"%s\"\n", pw->pw_shell, pw->pw_dir); pw = getpwnam(pw->pw_name); irp_marshall_pw(pw, &b, &len); if (strcmp(argv[1], buffer) != 0) { printf("compare failed:\n\t\"%s\"\n\t\"%s\"\n", buffer, argv[1]); } else { printf("compare ok\n"); } } else { if ((pw = getpwnam(argv[1])) == NULL) { perror("getpwnam"); exit(1); } if (irp_marshall_pw(pw, &b, &len) != 0) { printf("irp_marshall_pw failed\n"); exit(1); } printf("success: \"%s\"\n", buffer); } break; } default: printf("Wrong option: %c\n", option); break; }#endif return (0);}#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -