LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-09-2004, 04:32 PM   #1
Deep-Blue-ro
LQ Newbie
 
Registered: Mar 2004
Location: Romania - TM
Distribution: slackware10, rh9
Posts: 21

Rep: Reputation: 15
It is there any posibility...


... to make MyApp to receive UDP packets on port 138 (netbios) while samba is running ?

I mean to receive packets in a "normal way" from a socket, not from libpcap if it's possible.

The goal for this is to make linux version of intranetChat without server, only on mailslots.
Follwing code works when samba is stoped.
Any ideeas to make it work together with samba are welcome:

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

#define PORT 138 //8081
#define MAXBUFLEN 1024

extern int errno;

int main ()
{
  struct sockaddr_in server;
  struct sockaddr_in from;
  char buffer[100];
  int sd;
    int addr_len, numbytes, i;
    char buf[MAXBUFLEN];

  if ((sd = socket (AF_INET, SOCK_DGRAM, 0)) == -1)
    {
      perror ("socket err().\n");
      return errno;
    }

  bzero (&server, sizeof (server));
  bzero (&from, sizeof (from));
  
  /* umplem structura folosita de server */
  server.sin_family = AF_INET;	
  server.sin_addr.s_addr = htonl (INADDR_ANY);
  server.sin_port = htons (PORT);

  if (bind (sd, (struct sockaddr *) &server, sizeof (struct sockaddr)) == -1)
    {
      perror ("bind err().\n");
      return errno;
    }

    i=0;
    while (i<25) 
	{
    addr_len = sizeof (struct sockaddr);
    if ((numbytes=recvfrom(sd, buf, MAXBUFLEN, 0, (struct sockaddr*)&from, &addr_len))==-1)
	{
	perror ("receive err");
	return errno;
	}
    printf ("Got %d bytes from %s\n", numbytes, inet_ntoa(from.sin_addr));
    buf[numbytes]='\0';
    printf ("CONTENT: '%s'\n\n", buf);
	i++;
	}
    close (sd);

}	/* main */
 
Old 11-09-2004, 04:41 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
The answer is 'rather not'. Ports were designed to distiguish between applications running on the same machine. In your case, there's no way to redirect the packets to the right application. If you are able to do it, you can write an extra app that would listen on the port alone and then redirect what it gets to the apps. Doable, but rather hard and not very effective.
 
Old 11-09-2004, 04:55 PM   #3
Deep-Blue-ro
LQ Newbie
 
Registered: Mar 2004
Location: Romania - TM
Distribution: slackware10, rh9
Posts: 21

Original Poster
Rep: Reputation: 15
I understand that i have 3 options:

1. libpcap
2. samba API (if exist) or modify samba sources (sounds very ugly)
3. go directly to kernel and build an "packet dispatcher" (mutch more ugly)

Remains to see how can i use lib pcap or, if someone knows if samba provides something usefull, please let me know.
 
Old 11-09-2004, 05:00 PM   #4
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Here's tutorial for pcap: http://www.tcpdump.org/pcap.htm
When dealing with packets I use libnetdude - can be used as a wrapper for libpcap and is quite easy to program. I have tried it only with existing traces (no sniffing), so don't know how it'd be with performance etc.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
free music download (p2p, Linux DC++, other posibility) ??? alinsoar General 23 10-03-2005 11:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration