Hi,
Sorry if I am stepping on your question. But I am also seeing the similar problem. I have a C program that forks multiple processes. Basically its a program that accepts TCP network connetions and spawns a one process for each connection.
When the first new connection is arrived parent process allocates a shared memory and stores the connection info such as src/dst ip, src/dst ports and process that is handling the connection.
The data structure also has a next pointer which points to the next connection entry which is created when subsequent connections arrive. Basically it creates a linked list of connections.
I have a code in program which prints this list but somehow I am not able to get it printed. The list goes in infinite loop with next pointer of my first node pointing back to itself. Now I am sure I am coding it right way. When I tried to print the pointers returned by a function that allocates shared memory, I found one problem. Following is the output of program for 5 connections that are received.
Quote:
*flow: 0x40019000
Head flow Added
startXlistener : PID-30711: Display TCP Connection from Source 101.0.0.5:43217 Destination 192.168.0.4:6003 Socket: 5
*flow: 0x4001a000
Flow Added
startXlistener : PID-30712: Display TCP Connection from Source 101.0.0.5:43218 Destination 192.168.0.4:6003 Socket: 5
startXlistener : PID-30712: Display TCP Connection disconnected on Socket: 5
*flow: 0x4001b000
Flow Added
startXlistener : PID-30713: Display TCP Connection from Source 101.0.0.5:43219 Destination 192.168.0.4:6003 Socket: 5
*flow: 0x4001c000
Flow Added
startXlistener : PID-30714: Display TCP Connection from Source 101.0.0.5:43220 Destination 192.168.0.4:6003 Socket: 5
startXlistener : PID-30713: Display TCP Connection disconnected on Socket: 5
*flow: 0x4001d000
Flow Added
startXlistener : PID-30715: Display TCP Connection from Source 101.0.0.5:43221 Destination 192.168.0.4:6003 Socket: 5
startXlistener : PID-30714: Display TCP Connection disconnected on Socket: 5
|
Flow pointer is the pointer return with "shmat()". I maintain a list (an array) which stores the id to each shared memory allocation (returned via "shmget()"). In my print function when i try to attach to the first memory pointer (head), I see that I am getting attached to the second pointer.
following is my print function code.
Code:
void printFlow()
{
flow_t *flow=NULL;
int i,flowid;
key_t key=1112;
flow = (flow_t *)shmat(shareddb->flowdbid[0],NULL,SHM_RDONLY);
if (flow == (flow_t *)(-1))
{
perror("\nshmat:printdb");
terminateall(__FUNCTION__);
exit(1);
} else {
memset(debugstr,'\0',sizeof(debugstr));
sprintf(debugstr,"Shared memory pointer acquired for flow database: address = 0x%08x\n",&flow);
dbgtrace(__FUNCTION__,debugstr);
}
printf("\nheadptr : 0x%08x\n",flow);
if (flow==NULL)
{
fprintf(stderr,"\nNo Connections.\n");
return;
}
fprintf(stderr,"\nTCP Display connections database\n");
fprintf(stderr,"Received %d Active %d \n",shareddb->displayreceived,shareddb->displayactive);
while (flow != NULL)
{
fprintf(stderr,"PID: %d From: %s:%d To: %s:%d \n",flow->pid,flow->srcIP,flow->srcPort,flow->dstIP,flow->dstPort);
if (flow == flow->next)
{
printf("\nlooped\n");
return;
} else {
flow=flow->next;
continue;
}
}
Following is a the connection data stucture.
Code:
typedef struct _flow_t flow_t;
typedef struct _flow_t
{
pid_t pid;
CARD8 srcIP[20];
CARD16 srcPort;
CARD8 dstIP[20];
CARD16 dstPort;
flow_t *next;
}flow_t_;
Following the output of printflow function.
Quote:
headptr : 0x4001a000
TCP Display connections database
Received 5 Active 2
PID: 30711 From: 101.0.0.5:43217 To: 192.168.0.4:6003
looped
|
As you can see the the headptr in printFlow() function (0x4001a000) return by reattaching to the head shared memory is different than the one actually returned by shmat when it is first created(0x40019000). Instead its a second pointer.
I am not an expert on shared and I must admit I am coding shared memory for the first time. Can some one let me know where the problem is.?
Thanks,