1 #include "unpxti.h"
2
3 /*
4 * Functions that convert XTI option values into printable strings.
5 *
6 * Note: XTI_DEBUG is defined as returning an array of t_uscalar_t's.
7 * Our function xti_str_uscalard() checks that there is at least one entry
8 * in the array and prints just the first entry.
9 * Ditto for IP_OPTIONS, which is defined as returning an array of u_char's
10 * and our function xti_str_uchard().
11 */
12
13 static char strres[128]; /* assume one opt at a time, so static is OK */
14
15 char *
xti_str_lend(struct t_opthdr * topt)16 xti_str_lend(struct t_opthdr *topt) /* value length, decimal */
17 {
18 snprintf(strres, sizeof(strres), "%lu (length of value)",
19 topt->len - sizeof(struct t_opthdr));
20 return(strres);
21 }
22
23 char *
xti_str_uscalard(struct t_opthdr * topt)24 xti_str_uscalard(struct t_opthdr *topt) /* t_uscalar_t, decimal */
25 {
26 if (topt->len < sizeof(struct t_opthdr) + sizeof(t_uscalar_t))
27 return(xti_str_lend(topt));
28 snprintf(strres, sizeof(strres), "%ld", *((t_uscalar_t *) (topt+1)));
29 return(strres);
30 }
31
32 char *
xti_str_uchard(struct t_opthdr * topt)33 xti_str_uchard(struct t_opthdr *topt) /* unsigned char, decimal */
34 {
35 if (topt->len < sizeof(struct t_opthdr) + sizeof(u_char))
36 return(xti_str_lend(topt));
37 snprintf(strres, sizeof(strres), "%d", *((u_char *) (topt+1)));
38 return(strres);
39 }
40
41 char *
xti_str_ucharx(struct t_opthdr * topt)42 xti_str_ucharx(struct t_opthdr *topt) /* unsigned char, hex */
43 {
44 if (topt->len < sizeof(struct t_opthdr) + sizeof(u_char))
45 return(xti_str_lend(topt));
46 snprintf(strres, sizeof(strres), "0x%02x", *((u_char *) (topt+1)));
47 return(strres);
48 }
49
50 char *
xti_str_yn(t_uscalar_t val)51 xti_str_yn(t_uscalar_t val) /* internal function for yes/no values */
52 {
53 if (val == T_YES)
54 return("T_YES");
55 else if (val == T_NO)
56 return("T_NO");
57 else {
58 snprintf(strres, sizeof(strres), "(unknown value: %lu)", val);
59 return(strres);
60 }
61 }
62
63 char *
xti_str_syng(t_scalar_t val)64 xti_str_syng(t_scalar_t val) /* t_scalar_t, yes/no/gabage */
65 {
66 if (val == T_YES)
67 return("T_YES");
68 else if (val == (T_YES|T_GARBAGE))
69 return("T_YES|T_GARBAGE");
70 else if (val == T_NO)
71 return("T_NO");
72 else {
73 snprintf(strres, sizeof(strres), "(unknown value: %ld)", val);
74 return(strres);
75 }
76 }
77
78 char *
xti_str_uiyn(struct t_opthdr * topt)79 xti_str_uiyn(struct t_opthdr *topt) /* unsigned int, yes/no */
80 {
81 if (topt->len < sizeof(struct t_opthdr) + sizeof(u_int))
82 return(xti_str_lend(topt));
83 return(xti_str_yn((t_uscalar_t) *((u_int *) (topt+1))));
84 }
85
86 char *
xti_str_usyn(struct t_opthdr * topt)87 xti_str_usyn(struct t_opthdr *topt) /* t_uscalar_t, yes/no */
88 {
89 if (topt->len < sizeof(struct t_opthdr) + sizeof(t_uscalar_t))
90 return(xti_str_lend(topt));
91 return(xti_str_yn(*((t_uscalar_t *) (topt+1))));
92 }
93
94 char *
xti_str_linger(struct t_opthdr * topt)95 xti_str_linger(struct t_opthdr *topt) /* t_linger structure */
96 {
97 struct t_linger *tl;
98
99 if (topt->len < sizeof(struct t_opthdr) + sizeof(struct t_linger))
100 return(xti_str_lend(topt));
101 tl = (struct t_linger *) (topt+1);
102 if (tl->l_linger == T_UNSPEC)
103 snprintf(strres, sizeof(strres), "%s, T_UNSPEC",
104 xti_str_yn((t_uscalar_t) tl->l_onoff));
105 else if (tl->l_linger == T_INFINITE)
106 snprintf(strres, sizeof(strres), "%s, T_INFINITE",
107 xti_str_yn((t_uscalar_t) tl->l_onoff));
108 else
109 snprintf(strres, sizeof(strres), "%s, %ld sec",
110 xti_str_yn((t_scalar_t) tl->l_onoff), tl->l_linger);
111 return(strres);
112 }
113
114 char *
xti_str_kpalive(struct t_opthdr * topt)115 xti_str_kpalive(struct t_opthdr *topt) /* t_kpalive structure */
116 {
117 struct t_kpalive *tk;
118
119 if (topt->len < sizeof(struct t_opthdr) + sizeof(struct t_kpalive))
120 return(xti_str_lend(topt));
121 tk = (struct t_kpalive *) (topt+1);
122 if (tk->kp_timeout == T_UNSPEC)
123 snprintf(strres, sizeof(strres), "%s, T_UNSPEC",
124 xti_str_syng((t_scalar_t) tk->kp_onoff));
125 else
126 snprintf(strres, sizeof(strres), "%s, %ld sec",
127 xti_str_syng((t_scalar_t) tk->kp_onoff), tk->kp_timeout);
128 return(strres);
129 }
130
131 char *
xti_str_flags(t_scalar_t flags)132 xti_str_flags(t_scalar_t flags) /* t_optmgmt{}.flags */
133 {
134 if (flags == T_SUCCESS) return("T_SUCCESS");
135 else if (flags == T_FAILURE) return("T_FAILURE");
136 else if (flags == T_NOTSUPPORT) return("T_NOTSUPPORT");
137 else if (flags == T_READONLY) return("T_READONLY");
138 snprintf(strres, sizeof(strres), "(unknown flags value: %ld)", flags);
139 return(strres);
140 }
141