Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sbin / atm / atm / atm_inet.c
1 /*
2  *
3  * ===================================
4  * HARP  |  Host ATM Research Platform
5  * ===================================
6  *
7  *
8  * This Host ATM Research Platform ("HARP") file (the "Software") is
9  * made available by Network Computing Services, Inc. ("NetworkCS")
10  * "AS IS".  NetworkCS does not provide maintenance, improvements or
11  * support of any kind.
12  *
13  * NETWORKCS MAKES NO WARRANTIES OR REPRESENTATIONS, EXPRESS OR IMPLIED,
14  * INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
15  * AND FITNESS FOR A PARTICULAR PURPOSE, AS TO ANY ELEMENT OF THE
16  * SOFTWARE OR ANY SUPPORT PROVIDED IN CONNECTION WITH THIS SOFTWARE.
17  * In no event shall NetworkCS be responsible for any damages, including
18  * but not limited to consequential damages, arising from or relating to
19  * any use of the Software or related support.
20  *
21  * Copyright 1994-1998 Network Computing Services, Inc.
22  *
23  * Copies of this Software may be made, however, the above copyright
24  * notice must be reproduced on all copies.
25  *
26  *      @(#) $FreeBSD: src/sbin/atm/atm/atm_inet.c,v 1.3.2.1 2000/07/01 06:02:14 ps Exp $
27  *
28  */
29
30 /*
31  * User configuration and display program
32  * --------------------------------------
33  *
34  * IP support
35  *
36  */
37
38 #include <sys/param.h>  
39 #include <sys/socket.h> 
40 #include <net/if.h>
41 #include <netinet/in.h>
42 #include <netatm/port.h>
43 #include <netatm/atm.h>
44 #include <netatm/atm_if.h> 
45 #include <netatm/atm_sap.h>
46 #include <netatm/atm_sys.h>
47 #include <netatm/atm_ioctl.h>
48
49 #include <errno.h>
50 #include <libatm.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54
55 #include "atm.h"
56
57 #ifndef lint
58 __RCSID("@(#) $FreeBSD: src/sbin/atm/atm/atm_inet.c,v 1.3.2.1 2000/07/01 06:02:14 ps Exp $");
59 #endif
60
61
62 /*
63  * Process add command for a TCP/IP PVC
64  * 
65  * Command format: 
66  *      atm add pvc <intf> <vpi> <vci> <aal> <encaps> IP <netif>
67  *              <IP addr> | dynamic
68  *
69  * Arguments:
70  *      argc    number of remaining arguments to command
71  *      argv    pointer to remaining argument strings
72  *      cmdp    pointer to command description 
73  *      app     pointer to AIOCAPVC structure
74  *      intp    pointer to air_int_rsp structure with information
75  *              about the physical interface that is the PVC is for.
76  *
77  * Returns:
78  *      none
79  *
80  */
81 void
82 ip_pvcadd(argc, argv, cmdp, app, intp)
83         int                     argc;
84         char                    **argv;
85         struct cmd              *cmdp;
86         struct atmaddreq        *app;
87         struct air_int_rsp      *intp;
88 {
89         char    *cp;
90         char    nhelp[128];
91         int     i, netif_pref_len, netif_no;
92
93         /*
94          * Yet more validation
95          */
96         if (argc != 2) {
97                 strcpy(nhelp, cmdp->help);
98                 cp = strstr(nhelp, "<netif>");
99                 if (cp)
100                         strcpy(cp, "ip {dyn|<dst>}");
101                 fprintf(stderr, "%s: Invalid number of arguments:\n",
102                                 prog);
103                 fprintf(stderr, "\tformat is: %s%s %s\n",
104                                 prefix, cmdp->name, nhelp);
105                 exit(1);
106         }
107
108         /*
109          * Validate and set network interface
110          */
111         UM_ZERO(app->aar_pvc_intf, sizeof(app->aar_pvc_intf));
112         netif_pref_len = strlen(intp->anp_nif_pref);
113         cp = &argv[0][netif_pref_len];
114         netif_no = atoi(cp);
115         for (i=0; i<strlen(cp); i++) {
116                 if (cp[i] < '0' || cp[i] > '9') {
117                         netif_no = -1;
118                         break;
119                 }
120         }
121         if ((strlen(argv[0]) > sizeof(app->aar_pvc_intf) - 1) ||
122                         (netif_no < 0)) {
123                 fprintf(stderr, "%s: Illegal network interface name\n",
124                                 prog);
125                 exit(1);
126         }
127         if (strncasecmp(intp->anp_nif_pref, argv[0], netif_pref_len) ||
128                         strlen (argv[0]) <= netif_pref_len ||
129                         netif_no > intp->anp_nif_cnt - 1) {
130                 fprintf(stderr, "%s: network interface %s is not associated with interface %s\n",
131                                 prog,
132                                 argv[0],
133                                 intp->anp_intf);
134                 exit(1);
135         }
136         strcpy(app->aar_pvc_intf, argv[0]);
137         argc--; argv++;
138
139         /*
140          * Set PVC destination address
141          */
142         UM_ZERO(&app->aar_pvc_dst, sizeof(struct sockaddr));
143         if (strcasecmp(argv[0], "dynamic") == 0 ||
144                         strcasecmp(argv[0], "dyn") == 0) {
145
146                 /*
147                  * Destination is dynamically determined
148                  */
149                 app->aar_pvc_flags |= PVC_DYN;
150         } else {
151
152                 /*
153                  * Get destination IP address
154                  */
155                 struct sockaddr_in      *sin;
156
157                 sin = (struct sockaddr_in *) &app->aar_pvc_dst;
158                 sin->sin_addr.s_addr =
159                                 get_ip_addr(argv[0])->sin_addr.s_addr;
160         }
161         argc--; argv++;
162 }
163