1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
/*
* Copyright (c) 2000-2003 QoSient, LLC
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation, and that the name of QoSient not be
* used in advertising or publicity pertaining to distribution of the
* software without specific, written prior permission.
*
* QOSIENT, LLC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
* SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS, IN NO EVENT SHALL QOSIENT, LLC BE LIABLE FOR ANY
* SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
* CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
/*
* parsePortNum
* this module parses an IANA file to generate the port number
* file used by rasrvstats() and others.
*
* written by Carter Bullard
* QoSient, LLC
*
*/
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/stat.h>
char *portNumFile = NULL;
char *ProgramName = NULL;
void usage (void);
void parsePortNumInit (void);
void parsePortNums (char *);
void printOutPortNums (void);
void
usage ()
{
fprintf (stderr, "usage: %s -f input\n", ProgramName);
_exit (1);
}
void
parsePortNumInit ()
{
}
char *getoptStr = "f:";
int
main (int argc, char **argv)
{
int op;
extern char *optarg;
extern int optind, opterr;
opterr = 0;
ProgramName = argv[0];
while ((op = getopt (argc, argv, getoptStr)) != EOF) {
switch (op) {
case 'f': portNumFile = optarg; break;
default: usage ();
/* NOTREACHED */
}
}
if (portNumFile == NULL)
portNumFile = "-";
parsePortNumInit ();
parsePortNums (portNumFile);
printOutPortNums();
_exit (0);
}
#define MAXSTRLEN 1024
#include <stdlib.h>
#include <ctype.h>
void
parsePortNums (char *file)
{
int lines = 0, len = 0;
char *strend;
char buf[MAXSTRLEN], *str = buf;
char *srvname = NULL;
char *port = NULL;
char *proto = NULL;
char *desc = NULL;
char *ptr, *tmp;
FILE *fd;
if (file) {
if ((fd = fopen (file, "r")) != NULL) {
while ((fgets(str, MAXSTRLEN, fd)) != NULL) {
lines++;
/*
dbbrowse 47557/tcp Databeam Corporation
*/
if (*str && (*str != '#') && (*str != '\n') && (*str != '!')) {
if (strstr (str, "/udp") || strstr (str, "/tcp")) {
len = strlen(str);
strend = str + len;
ptr = str;
while (!isspace(*ptr)) ptr++;
*ptr++ = '\0';
srvname = str;
while (isspace(*ptr)) ptr++;
port = ptr;
if ((proto = strchr (port, '/')) != NULL) {
*proto++ = '\0';
ptr = proto;
while (!isspace(*ptr)) ptr++;
*ptr++ = '\0';
}
if (ptr < strend) {
while (isspace(*ptr)) ptr++;
desc = ptr;
tmp = NULL;
while (*ptr != '\n') {
if (isspace(*ptr)) {
if (tmp == NULL)
tmp = ptr;
} else
tmp = NULL;
ptr++;
}
if (tmp != NULL)
*tmp = '\0';
else
*ptr = '\0';
}
if ((port == NULL) || (proto == NULL))
printf ("parse error line %d\n", lines);
else {
int start, end, i;
proto = (strcmp (proto, "tcp")) ? "6" : "17";
if (srvname == NULL)
srvname = "";
if (desc == NULL)
desc = "";
if ((ptr = strchr(port, '-')) != NULL) {
start = atoi(port);
end = atoi(ptr + 1);
} else {
start = atoi(port);
end = atoi(port);
}
for (i = start; i < (end + 1); i++) {
printf ("0 0 %-2s %-5d \"%s\" \"%s\"\n", proto, i, srvname, desc);
fflush(stdout);
}
}
srvname = NULL; port = NULL; proto = NULL; desc = NULL;
}
}
}
} else
fprintf (stdout, "input file '%s' %s\n", file, strerror(errno));
}
}
void
printOutPortNums()
{
}
|