Skip to content

Commit 8d4694d

Browse files
committed
exit on error, check if interface provided
1 parent 740a84b commit 8d4694d

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

src/modules/out_5a_75e.c

+25-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#include <arpa/inet.h>
1818
#include <linux/if_packet.h>
19+
#include <stdlib.h>
20+
#include <errno.h>
1921
#include <stdio.h>
2022
#include <string.h>
2123
#include <sys/ioctl.h>
@@ -120,21 +122,40 @@ void wait_until_break(int _modno)
120122
#endif
121123
}
122124

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+
123139
int init(int moduleno, char *argstr) {
124140
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+
}
125146

126147
/* Open RAW socket to send on */
127148
if ((sockfd = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW)) < 0)
128149
{
129-
perror("socket");
150+
p_error("socket");
130151
}
131152

132153
/* Get the index of the interface to send on */
133154
memset(&if_idx, 0, sizeof(struct ifreq));
134155
strncpy(if_idx.ifr_name, argstr, IFNAMSIZ-1);
135156
if (ioctl(sockfd, SIOCGIFINDEX, &if_idx) < 0)
136157
{
137-
perror("SIOCGIFINDEX");
158+
p_error("SIOCGIFINDEX");
138159
}
139160

140161
/* Index of the network device */
@@ -204,7 +225,7 @@ int render(void)
204225
/* Send line packet */
205226
if (sendto(sockfd, line, sizeof(line)/sizeof(line[0]), 0, (struct sockaddr*)&socket_address, sizeof(struct sockaddr_ll)) < 0)
206227
{
207-
perror("Send line failed");
228+
p_error("Send line failed");
208229
}
209230
}
210231

@@ -213,7 +234,7 @@ int render(void)
213234
/* Send bufferswap packet */
214235
if (sendto(sockfd, bufferswap, sizeof(bufferswap)/sizeof(bufferswap[0]), 0, (struct sockaddr*)&socket_address, sizeof(struct sockaddr_ll)) < 0)
215236
{
216-
perror("Send bufferswap failed");
237+
p_error("Send bufferswap failed");
217238
}
218239

219240
return 0;

0 commit comments

Comments
 (0)