Changeset View
Standalone View
usr.sbin/tcp_function_ctrl/tcp_function_ctrl.c
Property | Old Value | New Value |
---|---|---|
svn:eol-style | null | native \ No newline at end of property |
svn:keywords | null | FreeBSD=%H \ No newline at end of property |
svn:mime-type | null | text/plain \ No newline at end of property |
#include <sys/types.h> | |||||
#include <stdio.h> | |||||
#include <sys/socket.h> | |||||
#include <stdlib.h> | |||||
#include <unistd.h> | |||||
#include <string.h> | |||||
#include <strings.h> | |||||
#include <sys/sysctl.h> | |||||
#include <sys/errno.h> | |||||
#include <netinet/tcp.h> | |||||
#include <getopt.h> | |||||
#include <err.h> | |||||
int | |||||
main(int argc, char **argv) | |||||
{ | |||||
struct tcp_function_set *set; | |||||
char def[TCP_FUNCTION_NAME_LEN_MAX]; | |||||
size_t sz; | |||||
int num, i; | |||||
char *name=NULL; | |||||
int show_default = 0; | |||||
int list_functions = 0; | |||||
while((i = getopt(argc, argv, "s:ld?h")) != -1) { | |||||
switch(i) { | |||||
case 's': | |||||
name = optarg; | |||||
if (strlen(name) >= 32) { | |||||
printf("Name to large\n"); | |||||
goto use; | |||||
} | |||||
break; | |||||
case 'd': | |||||
show_default = 1; | |||||
break; | |||||
case 'l': | |||||
list_functions = 1; | |||||
break; | |||||
case 'h': | |||||
case '?': | |||||
use: | |||||
printf("Use %s -s name <or> -l <or> -d\n", argv[0]); | |||||
return(1); | |||||
break; | |||||
jtl: Don't usage functions usually return a non-0 exit code? (What if you hit this because you used… | |||||
Not Done Inline ActionsI guess :-0 rrs: I guess :-0 | |||||
} | |||||
} | |||||
if ((show_default == 0) && (name == NULL) && (list_functions == 0)) { | |||||
printf("Options are mutually exclusive please specify only one\n"); | |||||
goto use; | |||||
} | |||||
Not Done Inline ActionsIt looks like the three options are mutually-exclusive. Should we check for that and return a friendly usage error if they are mixed? jtl: It looks like the three options are mutually-exclusive. Should we check for that and return a… | |||||
Not Done Inline ActionsWell they are now, but in theory some can be done together.. I changed that last minute.. probably should add the friendly message :-) rrs: Well they are now, but in theory some can be done together..
I changed that last minute.. | |||||
if (show_default) { | |||||
sz = sizeof(def); | |||||
if (sysctlbyname("net.inet.tcp.functions_default", &def, &sz, | |||||
NULL, 0)) { | |||||
err(-1, "Error retrieving name of TCP default function set"); | |||||
Not Done Inline ActionsHow about...? err(-1, "Error retrieving name of TCP default function set"); jtl: How about...?
err(-1, "Error retrieving name of TCP default function set"); | |||||
Not Done Inline Actionsok rrs: ok | |||||
} | |||||
printf("The TCP default functions is named \"%s\"\n", def); | |||||
return(0); | |||||
Not Done Inline Actionshow about "...named \"%s\" with ..."? jtl: how about "...named \"%s\" with ..."? | |||||
Not Done Inline Actionsok rrs: ok | |||||
} | |||||
if (list_functions) { | |||||
Not Done Inline Actions"Error retrieving list size"? (Also, this isn't a list of "default functions".) jtl: "Error retrieving list size"? (Also, this isn't a list of "default functions".) | |||||
Not Done Inline Actionsok rrs: ok
| |||||
sz = 0; | |||||
if (sysctlbyname("net.inet.tcp.functions_list_detail", NULL, &sz, | |||||
NULL, 0)) { | |||||
err(-1, "Error retrieving list of TCP default functions"); | |||||
} | |||||
sz += sizeof(struct tcp_function_set); | |||||
set = malloc(sz); | |||||
Not Done Inline Actionserr(-1, "Error retrieving list size"); jtl: err(-1, "Error retrieving list size"); | |||||
if (set == NULL) { | |||||
err(-1, "Error allocating memory for list"); | |||||
} | |||||
memset(set, 0, sz); | |||||
Not Done Inline ActionsCan't lines 70 and 71 be simplified to sz += sizeof(struct tcp_function_set)? By the way, why add room for an extra record here? (It isn't wrong; I'm just curious.) jtl: Can't lines 70 and 71 be simplified to sz += sizeof(struct tcp_function_set)?
By the way, why… | |||||
Not Done Inline ActionsI guess it could.. rrs: I guess it could.. | |||||
if (sysctlbyname("net.inet.tcp.functions_list_detail", set, &sz, | |||||
NULL, 0)) { | |||||
err(-1, "Error retrieving list from the kernel"); | |||||
Not Done Inline Actionserr(-1, "Error allocating memory for list"); jtl: err(-1, "Error allocating memory for list"); | |||||
Not Done Inline ActionsYeah I usually just print an errno .. but I can do the err() thing :-) rrs: Yeah I usually just print an errno .. but I can do the err() thing :-) | |||||
Not Done Inline ActionsIts usually friendlier for any user who isn't an in-depth programmer. :-) jtl: Its usually friendlier for any user who isn't an in-depth programmer. :-) | |||||
} | |||||
num = sz/sizeof(struct tcp_function_set); | |||||
printf("The Following TCP function sets are available\n"); | |||||
for(i=0; i<num; i++) { | |||||
printf("TCP Stack:%-32s Number of TCBs:%d\n", | |||||
set[i].function_set_name, | |||||
set[i].pcbcnt); | |||||
Not Done Inline Actionserr(-1, "Error retrieving list from the kernel"); jtl: err(-1, "Error retrieving list from the kernel"); | |||||
} | |||||
return(0); | |||||
} | |||||
if (name != NULL) { | |||||
memset(&def, 0, sizeof(def)); | |||||
strcpy(def, name); | |||||
Not Done Inline ActionsHow about "TCP Stack: %-32s "? If you do that, you should be able to forgo tracking 'cnt' and should be able to eliminate lines 89-93. jtl: How about "TCP Stack: %-32s "? If you do that, you should be able to forgo tracking 'cnt' and… | |||||
Not Done Inline Actionsok rrs: ok | |||||
sz = strlen(def) + 1; | |||||
if (sysctlbyname("net.inet.tcp.functions_default", | |||||
NULL, 0, &def, sz)) { | |||||
err(-1, "Can't set \"%s\" as the default function", name); | |||||
} | |||||
} | |||||
Not Done Inline Actionsname must be non-NULL at this point. jtl: name must be non-NULL at this point. | |||||
Not Done Inline Actionsok rrs: ok | |||||
return(0); | |||||
Not Done Inline ActionsHow about "Number of TCBs: %d\n"? jtl: How about "Number of TCBs: %d\n"? | |||||
Not Done Inline Actionsok rrs: ok | |||||
} | |||||
Not Done Inline ActionsHow about.. err(-1, "Error setting \"%s\" as the new default function set", name); jtl: How about..
err(-1, "Error setting \"%s\" as the new default function set", name); | |||||
Not Done Inline Actionsok rrs: ok |
Don't usage functions usually return a non-0 exit code? (What if you hit this because you used incorrect arguments?)