Skip to content

Instantly share code, notes, and snippets.

@silvioq
Created May 30, 2016 18:45
Show Gist options
  • Save silvioq/57e4075e4b349f8598b86a633445f78e to your computer and use it in GitHub Desktop.
Save silvioq/57e4075e4b349f8598b86a633445f78e to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
void main(int argc,char** argv)
{
struct ifreq ifr;
int rc;
if( argc != 4 )
{
printf( "Usage: %s iface ip port\n" , argv[0] );
exit( EXIT_FAILURE );
}
int sd = socket(AF_INET, SOCK_DGRAM, 0);
if( sd < 0 )
{
perror( "Error in socket creation" );
exit( EXIT_FAILURE );
}
memset(&ifr, 0, sizeof(ifr));
strncpy( ifr.ifr_name, argv[1], sizeof(ifr.ifr_name) );
if ((rc = setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr))) < 0)
{
perror("Server-setsockopt() error for SO_BINDTODEVICE");
close(sd);
exit(EXIT_FAILURE);
}
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
if( inet_aton( argv[2], &sin.sin_addr ) == 0 )
{
printf( "inet_aton: Invalid address: %s\n", argv[2] );
close(sd);
exit(EXIT_FAILURE);
}
sin.sin_port = htons(atoi(argv[3]));
if( sendto( sd, "aaaa", 4, 0, (struct sockaddr*) &sin, sizeof(sin) ) < 0 )
{
perror( "sendto()" );
close(sd);
exit(EXIT_FAILURE);
}
printf( "Enviado\n" );
close( sd );
exit( EXIT_SUCCESS );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment