Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hello World send / receive #492

Open
fghoussen opened this issue Nov 27, 2022 · 0 comments
Open

Hello World send / receive #492

fghoussen opened this issue Nov 27, 2022 · 0 comments

Comments

@fghoussen
Copy link

fghoussen commented Nov 27, 2022

Hello World send / receive doesn't work?!... Or, more probably, I can't get it to work! :(

I am new to network related stuffs (I had a hard time to learn basics about OSI model and various protocols - not sure to get all right... so reading the doc is not always easy) and barely know about pcap. I am trying to send / recv a payload in a TCP / IP packet. Even after reading the doc, I can't get that to work... So hoping to get some help here.

Code of the sender:

>> cat packet_sender.cpp 
/*
 * Using libtins to send / receive custom data / protocol.
 *
 * Need sudo rights:
 * >> sudo ./packet_receiver
 * >> sudo ./packet_sender
 */

#include <iostream>
#include <cstdlib>
#include <string>

#include <tins/tins.h>

using namespace std;

bool send(Tins::PacketSender &sender,
          const string &ip, const int &port,
          const string &iface) {
  // Send packets.
  while (1) {
    string payload;
    cout << "Enter payload: "; // Let user define a message as payload.
    cin >> payload;
    Tins::IP pkt = Tins::IP(ip) / Tins::TCP(port) / Tins::RawPDU(payload);
    // FIXME: send seems not to work? Why?
    sender.send(pkt, iface);
  }
}

int main(int argc, char ** argv) {
  // Handle command line arguments.
  string ip = "127.0.0.1"; // Default IP.
  int port = 123; // Default port.
  string iface = "eth0"; // Default interface.
  for (int i = 0; i < argc; i++) {
    string option = argv[i];
    if (option == "--ip") {i++; ip = argv[i];}
    if (option == "--port") {i++; port = atoi(argv[i]);}
    if (option == "--iface") {i++; iface = argv[i];}
  }
  cout << "ip " << ip << endl;
  cout << "port " << port << endl;
  cout << "iface " << iface << endl;

  // Send packets.
  Tins::PacketSender sender;
  send(sender, ip, port, iface);

  return 0;
}

Code of the receiver:

>> cat packet_receiver.cpp 
/*
 * Using libtins to send / receive custom data / protocol.
 *
 * Need sudo rights:
 * >> sudo ./packet_receiver
 * >> sudo ./packet_sender
 */

#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>

#include <tins/tins.h>

using namespace std;

bool receive(const Tins::PDU &pdu) {
  // Find the IP layer.
  const Tins::IP &ip = pdu.rfind_pdu<Tins::IP>();
  // Find the TCP layer.
  const Tins::TCP &tcp = pdu.rfind_pdu<Tins::TCP>();
  // Find the payload.
  const Tins::RawPDU &raw = pdu.rfind_pdu<Tins::RawPDU>();

  // Print received packet and associated payload.
  string payload = ""; // FIXME: how to get payload from raw?
  cout << ip.src_addr() << ':' << tcp.sport() << " -> ";
  cout << "payload: " << payload;
  cout << " -> " << ip.dst_addr() << ':' << tcp.dport() << endl;
  return true;
}

int main(int argc, char ** argv) {
  // Handle command line arguments.
  string ip = "127.0.0.1"; // Default IP.
  int port = 123; // Default port.
  string iface = "eth0"; // Default interface.
  for (int i = 0; i < argc; i++) {
    string option = argv[i];
    if (option == "--ip") {i++; ip = argv[i];}
    if (option == "--port") {i++; port = atoi(argv[i]);}
    if (option == "--iface") {i++; iface = argv[i];}
  }
  cout << "ip " << ip << endl;
  cout << "port " << port << endl;
  cout << "iface " << iface << endl;

  // Receive packets.
  Tins::SnifferConfiguration config;
  config.set_promisc_mode(true); // Show all data going over the network.
  config.set_immediate_mode(true); // Receive packets as soon as possible (no buffering).
  config.set_filter("ip src " + ip);
  stringstream ssport; ssport << port;
  config.set_filter("port " + ssport.str());
  Tins::Sniffer sniffer(iface, config);
  sniffer.sniff_loop(receive);

  return 0;
}

Running with sudo sender and receiver:

>> sudo ./packet_sender 
ip 127.0.0.1
port 123
iface eth0
Enter payload: hello
Enter payload: world
Enter payload: 

>> sudo ./packet_receiver 
ip 127.0.0.1
port 123
iface eth0


The expected result is that "hello" and "world" appears at the receiver side... But Nothing shows up?! Using wlan0 doesn't help.

I have 2 questions about why this doesn't work:

>> grep FIXME *.cpp
packet_receiver.cpp:27:  string payload = ""; // FIXME: how to get payload from raw?
packet_sender.cpp:26:    // FIXME: send seems not to work? Why?

Any help is appreciated! Question may look like a rooky one... But it's actually a difficult one for me as I do not know much/enough about networking... What's wrong/missing in this code?

Note: running debian.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant