]>
Commit | Line | Data |
---|---|---|
b85218d8 JR |
1 | #include <stdio.h> |
2 | #include <stdlib.h> | |
3 | #include <unistd.h> | |
4 | #include <errno.h> | |
5 | #include <sys/ioctl.h> | |
6 | #include <string.h> | |
7 | #include <atm.h> | |
8 | #include <linux/atmdev.h> | |
9 | #include <linux/atmbr2684.h> | |
10 | ||
11 | /* Written by Marcell GAL <cell@sch.bme.hu> to make use of the */ | |
12 | /* ioctls defined in the br2684... kernel patch */ | |
13 | /* Compile with cc -o br2684ctl br2684ctl.c -latm */ | |
14 | ||
15 | int lastsock, lastitf; | |
16 | ||
17 | void fatal(char *str, int i) | |
18 | { | |
19 | perror(str); | |
20 | exit(-2); | |
21 | }; | |
22 | ||
23 | int create_br(char *nstr) | |
24 | { | |
25 | int num, err; | |
26 | ||
27 | if(lastsock<0) { | |
28 | lastsock = socket(PF_ATMPVC, SOCK_DGRAM, ATM_AAL5); | |
29 | } | |
30 | if (lastsock<0) { | |
31 | perror("socket creation"); | |
32 | } else { | |
33 | /* create the device with ioctl: */ | |
34 | num=atoi(nstr); | |
35 | if( num>=0 && num<1234567890){ | |
36 | struct atm_newif_br2684 ni; | |
eb40d7a1 | 37 | ni.backend_num = ATM_BACKEND_BR2684; |
b85218d8 JR |
38 | ni.media = BR2684_MEDIA_ETHERNET; |
39 | ni.mtu = 1500; | |
40 | sprintf(ni.ifname, "nas%d", num); | |
41 | err=ioctl (lastsock, ATM_NEWBACKENDIF, &ni); | |
42 | printf("create:%d\n",err); | |
43 | if (err>=0) ;;; | |
44 | lastitf=num; /* even if we didn't create, because existed, assign_vcc wil want to know it! */ | |
45 | } else { | |
46 | fprintf (stderr, "err: strange itf number %d", num ); | |
47 | } | |
48 | } | |
49 | return 0; | |
50 | } | |
51 | ||
52 | int assign_vcc(char *astr, int encap, int bufsize) | |
53 | { | |
54 | int err, errno; | |
55 | struct atm_qos qos; | |
56 | struct sockaddr_atmpvc addr; | |
57 | int fd; | |
58 | struct atm_backend_br2684 be; | |
59 | ||
60 | memset(&addr, 0, sizeof(addr)); | |
61 | err=text2atm(astr,(struct sockaddr *)(&addr), sizeof(addr), T2A_PVC); | |
62 | printf("text2atm:%d\n",err); | |
63 | #if 0 | |
64 | addr.sap_family = AF_ATMPVC; | |
65 | addr.sap_addr.itf = itf; | |
66 | addr.sap_addr.vpi = 0; | |
67 | addr.sap_addr.vci = vci; | |
68 | #endif | |
69 | fprintf(stderr,"Communicating over ATM %d.%d.%d\n", addr.sap_addr.itf, | |
70 | addr.sap_addr.vpi, | |
71 | addr.sap_addr.vci); | |
72 | ||
73 | if ((fd = socket(PF_ATMPVC, SOCK_DGRAM, ATM_AAL5)) < 0) | |
74 | fprintf(stderr,"failed to create socket %d", errno); | |
75 | ||
76 | ||
77 | memset(&qos, 0, sizeof(qos)); | |
78 | qos.aal = ATM_AAL5; | |
79 | qos.txtp.traffic_class = ATM_UBR; | |
80 | qos.txtp.max_sdu = 1524; | |
81 | qos.txtp.pcr = ATM_MAX_PCR; | |
82 | qos.rxtp = qos.txtp; | |
83 | ||
84 | err=setsockopt(fd,SOL_SOCKET,SO_SNDBUF, &bufsize ,sizeof(bufsize)); | |
85 | fprintf(stderr,"setsockopt SO_SNDBUF: (%d) %s\n",err, strerror(err)); | |
86 | ||
87 | if (setsockopt(fd, SOL_ATM, SO_ATMQOS, &qos, sizeof(qos)) < 0) | |
88 | fprintf(stderr,"setsockopt SO_ATMQOS %d", errno); | |
89 | ||
90 | err = connect(fd, (struct sockaddr*)&addr, sizeof(struct sockaddr_atmpvc)); | |
91 | ||
92 | if (err < 0) | |
93 | fatal("failed to connect on socket: ", err); | |
94 | ||
95 | /* attach the vcc to device: */ | |
96 | ||
eb40d7a1 | 97 | be.backend_num = ATM_BACKEND_BR2684; |
b85218d8 JR |
98 | be.ifspec.method = BR2684_FIND_BYIFNAME; |
99 | sprintf(be.ifspec.spec.ifname, "nas%d", lastitf); | |
100 | be.fcs_in = BR2684_FCSIN_NO; | |
101 | be.fcs_out = BR2684_FCSOUT_NO; | |
102 | be.fcs_auto = 0; | |
103 | be.encaps = encap ? BR2684_ENCAPS_VC : BR2684_ENCAPS_LLC; | |
104 | be.has_vpiid = 0; | |
105 | be.send_padding = 0; | |
106 | be.min_size = 0; | |
107 | err=ioctl (fd, ATM_SETBACKEND, &be); | |
108 | fprintf(stderr, "assign:%d\n",err); | |
109 | ||
110 | return fd ; | |
111 | } | |
112 | ||
113 | void usage(char *s) | |
114 | { | |
115 | printf("usage: %s [-b] [[-c number] [-a [itf.]vpi.vci]*]*\n", s); | |
116 | exit(1); | |
117 | } | |
118 | ||
119 | int main (int argc, char **argv) | |
120 | { | |
121 | int c, background=0, encap=0, sndbuf=8192; | |
122 | ||
123 | lastsock=-1; | |
124 | lastitf=0; | |
125 | ||
126 | while ((c = getopt(argc, argv,"a:bc:e:s:")) !=EOF) | |
127 | switch (c) { | |
128 | case 'a': | |
129 | assign_vcc(optarg, encap, sndbuf); | |
130 | break; | |
131 | case 'b': | |
132 | background=1; | |
133 | break; | |
134 | case 'c': | |
135 | create_br(optarg); | |
136 | break; | |
137 | case 'e': | |
138 | encap=(atoi(optarg)); | |
139 | if(encap<0){ | |
140 | fprintf(stderr, "invalid encap: %s:\n",optarg); | |
141 | encap=0; | |
142 | } | |
143 | break; | |
144 | case 's': | |
145 | sndbuf=(atoi(optarg)); | |
146 | if(sndbuf<0){ | |
147 | fprintf(stderr, "invalid sndbuf: %s:\n",optarg); | |
148 | sndbuf=8192; | |
149 | } | |
150 | break; | |
151 | default: | |
152 | usage(argv[0]); | |
153 | } | |
154 | if (argc != optind) usage(argv[0]); | |
155 | ||
156 | if(lastsock>=0) close(lastsock); | |
157 | ||
158 | if (background) { | |
159 | pid_t pid; | |
160 | /* this seems to be broken, do not use -b for now */ | |
161 | pid=fork(); | |
162 | if (pid < 0) { | |
163 | fprintf(stderr,"fork returned negative: %d\n", pid); | |
164 | exit(2); | |
165 | } else if (pid) { | |
166 | fprintf(stderr,"Background pid: %d\n",pid); | |
167 | exit(0); | |
168 | } | |
169 | } | |
170 | ||
171 | while (1) sleep(30); /* to keep the sockets... */ | |
172 | return 0; | |
173 | } |