开发者

Problems in configuring OpenWrt for socket programming

I have a code which receives packets from the Ethernet and sends it via wireless network. This works on a wireless box which uses OpenWrt. The code is written below. It gives me the following error at runtime. Based on the printf statements I feel the error is somewhere on this syntax, but I cant figure out whats wrong.

Probable region of error:

  if(sendto(b_sock, buf, sizeof(v2vmessage), 0, (struct sockaddr *)&tx_addr, sizeof(tx_addr)) < 0)

Error:

Receving packets
Packets received
Sending packet started
Variables initialized
Send test
./dsrc_dsrc: can't resolve symbol 'sendto' in lib './dsrc_dsrc'.

Code:

#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <netdb.h>
#include <stdio.h>

#include <fcntl.h>
#include <string.h>
#include <sys/time.h>
#include <arpa/inet.h>  /* for sockaddr_in */

#define BROADCAST_IP "192.168.255.255"
#define BROADCAST_PORT 4545

struct v2vmessage
{
  ...
  ...
};

int b_sock=-1;

void init_socket()
{
  unsigned short b_port = BROADCAST_PORT;
  struct sockaddr_in b_addr;
  int broadcastPermission;
  char* rx_ip = BROADCAST_IP;

  if ((b_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
    perror("socket() failed");

  /* Set socket to allow broadcast */
  broadcastPermission = 1;
  if (setsockopt(b_sock, SOL_SOCKET, SO_BROADCAST, (void *) &broadcastPermission, sizeof(broadcastPermission)) < 0)
    perror("setsockopt() failed");

  int opts;
  opts = fcntl(b_sock,F_GETFL);
  if(opts < 0)
    perror("fcntl get failed");

  opts = (opts | O_NONBLOCK);
  if(fcntl(b_sock,F_SETFL,opts) < 0)
    perror("fcntl set failed");

  memset(&b_addr, 0, sizeof(b_addr));   /* Zero out structure */
  b_addr.sin_family = AF_INET;                 /* Internet address family */
  b_addr.sin_addr.s_addr = inet_addr(rx_ip);/* Broadcast IP address */
  b_addr.sin_port = htons(b_port);         /* Broadcast port */

  if (bind(b_sock, (struct sockaddr *) &b_addr, sizeof(b_addr)) < 0)
    perror("rx bind() failed");
}


void send_thread_body(v2vmessage *buf)
{
  printf("Sending packet started\n");
  char *tx_ip = BROADCAST_IP;
  unsigned short tx_port = BROADCAST_PORT;
  struct sockaddr_in tx_addr;

  printf("Variables initialized\n");

  memset(&tx_addr, 0, sizeof(tx_addr));   /* Zero out structure */
  tx_addr.sin_family = AF_INET;                 /* Internet address family */
  tx_addr.sin_addr.s_addr = inet_addr(tx_ip);/* Broadcast IP address */
  tx_addr.sin_port = htons(tx_port);         /* Broadcast port */

  printf("Send test\n");

  if(sendto(b_sock, buf, sizeof(v2vmessage), 0, (struct sockaddr *)&tx_addr, sizeof(tx_addr)) < 0)
    perror("tx sent diff num bytes than expected");
}

int main(int argc, char *argv[])
{
  init_socket();
  {
    int sock, length, n,p;
    socklen_t fromlen;
    struct sockaddr_in server;
    struct sockaddr_in from;

    if (argc < 2)
    {
      fprintf(stderr, "ERROR, no port provided\n");
      exit(0);
    }

    sock=socket(AF_INET, SOCK_DGRAM, 0);
    if (sock < 0)
      perror("Opening socket");
    length = sizeof(server);
    bzero(&server,length);
    server.sin_family=AF_INET;
    server.sin_addr.s_addr=INADDR_ANY;
    server.sin_port=htons(atoi(argv[1]));
    if (bind(sock,(struct sockaddr *)&server,length)<0) 
      perror("binding");
    fromlen = sizeof(struct sockaddr_in);
    v2vmessage *buf;

    while (1)
    {
      printf("Receving packets\n");
      n = recvfrom(sock,buf,sizeof(v2vmessage),0,(struct sockaddr *)&from,&fromlen);
      if 开发者_如何转开发(n < 0)
        perror("recvfrom");
      printf("Packets received\n");
      send_thread_body(buf);
      printf("Packets sent\n");
    }
  }
  return 0;
}


In the main(), replace v2vmessage* buf by v2vmessage buf and in the following while(1), replace the buf by &buf to solve the problem. Since, no memory was allocated to the pointer, hence it gave an error at runtime.


That's a linker error, are you sure your linker is including all the required lib files? Im not familiar with the posix sockets (as far as which headers to include and wich libs to build against) , but i do know sendto is a call to send a UDP packet, and it could be failing there. if the connection is tcp, used send(). Hope this helps.


You are missing a shared library. Use ldd(1) to figure out which one. You probably just need to set LD_LIBRARY_PATH environment variable, see ld.so(8) manual.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜