No empty .Rs/.Re
[netbsd-mini2440.git] / usr.sbin / pvctxctl / pvctxctl.c
blob6c576e0eeded4ced19fd34339acea363e9697963
1 /* $NetBSD: pvctxctl.c,v 1.5 2007/01/16 17:32:05 hubertf Exp $ */
3 /*
4 * Copyright (C) 1998
5 * Sony Computer Science Laboratory Inc. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
28 * ALTQ Id: pvctxctl.c,v 0.4 1999/05/19 11:31:11 kjc Exp
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <ctype.h>
36 #include <err.h>
38 #include <sys/socket.h>
39 #include <sys/ioctl.h>
40 #include <net/if.h>
42 #include <net/if_atm.h>
44 static int str2vc(char *str, int *vpi, int *vci);
45 static void usage(void);
47 static void
48 usage(void)
50 fprintf(stderr, "usage: pvctxctl interface [vpi:]vci\n");
51 fprintf(stderr, " [-p pcr] [-b pcr_in_bps] [-j [vpi:]vci\n");
52 fprintf(stderr, " [-n] \n");
53 exit(1);
56 int vpi = 0;
57 int vci = 0;
58 int joint_vpi = 0;
59 int joint_vci = 0;
60 int pcr = 0;
61 int llcsnap = ATM_PH_LLCSNAP;
62 int getinfo = 1;
63 int subinterface = 0;
64 int verbose = 1;
66 int
67 main(int argc, char **argv)
69 struct pvctxreq pvcreq;
70 int s, ch;
71 long bandwidth;
72 char *if_name, *cp;
74 if (argc < 2)
75 usage();
77 if_name = argv[1];
78 if (argc > 2 && isdigit((unsigned char)argv[2][0]))
79 str2vc(argv[2], &vpi, &vci);
81 optind = 3;
82 while ((ch = getopt(argc, argv, "p:b:j:snv")) != -1) {
83 switch (ch) {
84 case 'p':
85 pcr = strtol(optarg, NULL, 0);
86 getinfo = 0;
87 break;
88 case 'b':
89 cp = NULL;
90 bandwidth = strtol(optarg, &cp, 0);
91 if (cp != NULL) {
92 if (*cp == 'K' || *cp == 'k')
93 bandwidth *= 1000;
94 if (*cp == 'M' || *cp == 'm')
95 bandwidth *= 1000000;
97 pcr = bandwidth / 8 / 48;
98 getinfo = 0;
99 break;
100 case 'j':
101 str2vc(optarg, &joint_vpi, &joint_vci);
102 break;
103 case 'n':
104 llcsnap = 0;
105 break;
106 case 'v':
107 verbose = 1;
108 break;
109 default:
110 usage();
114 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
115 err(1, "can't open socket");
117 pvcreq.pvc_ifname[IFNAMSIZ-1] = '\0';
118 strncpy(pvcreq.pvc_ifname, if_name, IFNAMSIZ-1);
120 if (strncmp(if_name, "pvc", 3) == 0)
121 /* pvc subinterface */
122 subinterface = 1;
124 ATM_PH_FLAGS(&pvcreq.pvc_aph) = ATM_PH_AAL5 | llcsnap;
125 ATM_PH_VPI(&pvcreq.pvc_aph) = vpi;
126 ATM_PH_SETVCI(&pvcreq.pvc_aph, vci);
128 ATM_PH_FLAGS(&pvcreq.pvc_joint) = 0;
129 ATM_PH_VPI(&pvcreq.pvc_joint) = joint_vpi;
130 ATM_PH_SETVCI(&pvcreq.pvc_joint, joint_vci);
132 pvcreq.pvc_pcr = pcr;
134 if (getinfo) {
135 if (ioctl(s, SIOCGPVCTX, &pvcreq) < 0)
136 err(1, "SIOCSPVCTX");
138 else {
139 if (verbose) {
140 printf("setting pvc tx: interface:%s vc:%d:%d ph=0x%x\n",
141 if_name, vpi, vci, ATM_PH_FLAGS(&pvcreq.pvc_aph));
142 printf(" joint:%d:%d, setting pcr:%d\n",
143 joint_vpi, joint_vci, pcr);
146 if (ioctl(s, SIOCSPVCTX, &pvcreq) < 0)
147 err(1, "SIOCSPVCTX");
150 pcr = pvcreq.pvc_pcr;
153 * print info
155 printf(" %s", if_name);
156 if (subinterface)
157 printf(" (bound to %s)", pvcreq.pvc_ifname);
158 printf(": vci:[%d:%d] (",
159 ATM_PH_VPI(&pvcreq.pvc_aph), ATM_PH_VCI(&pvcreq.pvc_aph));
160 if (ATM_PH_FLAGS(&pvcreq.pvc_aph) & ATM_PH_AAL5)
161 printf("AAL5");
162 if (ATM_PH_FLAGS(&pvcreq.pvc_aph) & ATM_PH_LLCSNAP)
163 printf("/LLCSNAP");
164 printf(") ");
165 if (pcr < 0)
166 printf("(invalid)\n");
167 else if (pcr == 0)
168 printf("pcr:%d(full speed)\n", pcr);
169 else if (pcr < 1000)
170 printf("pcr:%d(%dbps)\n", pcr, pcr * 48 * 8);
171 else if (pcr < 1000000)
172 printf("pcr:%d(%dKbps)\n", pcr, pcr * 48 * 8 / 1000);
173 else
174 printf("pcr:%d(%dMbps)\n", pcr, pcr * 48 * 8 / 1000000);
176 close(s);
178 if (getinfo && pcr < 0) {
179 fprintf(stderr, "can't get pvc info for vci:%d\n", vci);
180 fprintf(stderr, "to setup a vci, use -p or -b option\n");
183 return (0);
186 static int
187 str2vc(char *str, int *vpip, int *vcip)
189 char *c;
191 if ((c = strchr(str, ':')) != NULL) {
192 *c = '\0';
193 *vpip = strtol(str, NULL, 0);
194 str = c + 1;
196 else
197 *vpip = 0;
199 *vcip = strtol(str, NULL, 0);
200 return (0);