Skip to content

Commit

Permalink
exit on error, check if interface provided
Browse files Browse the repository at this point in the history
  • Loading branch information
Draradech committed Jul 13, 2022
1 parent 740a84b commit 8d4694d
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/modules/out_5a_75e.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include <arpa/inet.h>
#include <linux/if_packet.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
Expand Down Expand Up @@ -120,21 +122,40 @@ void wait_until_break(int _modno)
#endif
}

void p_error(char* str)
{
if(errno)
{
perror(str);
}
else
{
fputs(str, stderr);
fputs("\n", stderr);
}
exit(-1);
}

int init(int moduleno, char *argstr) {
struct ifreq if_idx;

if (!argstr)
{
p_error("no network interface provided. use \"sled -o 5a_75e:ifname\" to specify interface");
}

/* Open RAW socket to send on */
if ((sockfd = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW)) < 0)
{
perror("socket");
p_error("socket");
}

/* Get the index of the interface to send on */
memset(&if_idx, 0, sizeof(struct ifreq));
strncpy(if_idx.ifr_name, argstr, IFNAMSIZ-1);
if (ioctl(sockfd, SIOCGIFINDEX, &if_idx) < 0)
{
perror("SIOCGIFINDEX");
p_error("SIOCGIFINDEX");
}

/* Index of the network device */
Expand Down Expand Up @@ -204,7 +225,7 @@ int render(void)
/* Send line packet */
if (sendto(sockfd, line, sizeof(line)/sizeof(line[0]), 0, (struct sockaddr*)&socket_address, sizeof(struct sockaddr_ll)) < 0)
{
perror("Send line failed");
p_error("Send line failed");
}
}

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

return 0;
Expand Down

0 comments on commit 8d4694d

Please sign in to comment.