16
16
17
17
#include <arpa/inet.h>
18
18
#include <linux/if_packet.h>
19
+ #include <stdlib.h>
20
+ #include <errno.h>
19
21
#include <stdio.h>
20
22
#include <string.h>
21
23
#include <sys/ioctl.h>
@@ -120,21 +122,40 @@ void wait_until_break(int _modno)
120
122
#endif
121
123
}
122
124
125
+ void p_error (char * str )
126
+ {
127
+ if (errno )
128
+ {
129
+ perror (str );
130
+ }
131
+ else
132
+ {
133
+ fputs (str , stderr );
134
+ fputs ("\n" , stderr );
135
+ }
136
+ exit (-1 );
137
+ }
138
+
123
139
int init (int moduleno , char * argstr ) {
124
140
struct ifreq if_idx ;
141
+
142
+ if (!argstr )
143
+ {
144
+ p_error ("no network interface provided. use \"sled -o 5a_75e:ifname\" to specify interface" );
145
+ }
125
146
126
147
/* Open RAW socket to send on */
127
148
if ((sockfd = socket (AF_PACKET , SOCK_RAW , IPPROTO_RAW )) < 0 )
128
149
{
129
- perror ("socket" );
150
+ p_error ("socket" );
130
151
}
131
152
132
153
/* Get the index of the interface to send on */
133
154
memset (& if_idx , 0 , sizeof (struct ifreq ));
134
155
strncpy (if_idx .ifr_name , argstr , IFNAMSIZ - 1 );
135
156
if (ioctl (sockfd , SIOCGIFINDEX , & if_idx ) < 0 )
136
157
{
137
- perror ("SIOCGIFINDEX" );
158
+ p_error ("SIOCGIFINDEX" );
138
159
}
139
160
140
161
/* Index of the network device */
@@ -204,7 +225,7 @@ int render(void)
204
225
/* Send line packet */
205
226
if (sendto (sockfd , line , sizeof (line )/sizeof (line [0 ]), 0 , (struct sockaddr * )& socket_address , sizeof (struct sockaddr_ll )) < 0 )
206
227
{
207
- perror ("Send line failed" );
228
+ p_error ("Send line failed" );
208
229
}
209
230
}
210
231
@@ -213,7 +234,7 @@ int render(void)
213
234
/* Send bufferswap packet */
214
235
if (sendto (sockfd , bufferswap , sizeof (bufferswap )/sizeof (bufferswap [0 ]), 0 , (struct sockaddr * )& socket_address , sizeof (struct sockaddr_ll )) < 0 )
215
236
{
216
- perror ("Send bufferswap failed" );
237
+ p_error ("Send bufferswap failed" );
217
238
}
218
239
219
240
return 0 ;
0 commit comments