Visit Our UNIX and Linux User Community


Network Code


 
Thread Tools Search this Thread
Top Forums Programming Network Code
# 1  
Old 02-01-2011
Network Code

Hi,

This has been bugging me all day, I'm trying to get this code to send an ipv6 destination options header to any client that connects to it. I keep getting "Error returned from sendmsg" which is my error checking on the sendmsg function but I don't know why. Any Ideas?

Code:
#include <sys/types.h>
#include <sys/socket.h>

#include <netinet/in.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <netdb.h>

#include <netinet/ip6.h>

int 
main()
{
  struct sockaddr_in6 sin6, sin6_accept;
  socklen_t sin6_len;
  int s0, s;
  int on;
  char hbuf[NI_MAXHOST];
  
  //ext header
  int rv;
  struct msghdr msg;
  struct cmsghdr *cmsg;
  int cmsglen = 0;
  
  char tstmsg[13] = "Hello World";
  
  struct iovec vec;
  
  struct ip6_dest dest;
  
  memset(&sin6, 0, sizeof(sin6));
  sin6.sin6_family = AF_INET6;
  sin6.sin6_len = sizeof(sin6);
  sin6.sin6_port = htons(5001);
  
  s0 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
  on = 1;
  
  //setsockopt(s0, SOL_SOCKET, SO_RESUEADDR, &on, sizeof(on));
  
  setsockopt(s0, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on));
  
  bind(s0, (const struct sockaddr *)&sin6, sizeof(sin6));
  listen(s0, 1);
  while(1) {
            sin6_len = sizeof(sin6_accept);
            s = accept(s0, (struct sockaddr *)&sin6_accept, &sin6_len);
            getnameinfo((struct sockaddr *)&sin6_accept, sin6_len,
                          hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST);            
            printf("accept a connection from %s\n", hbuf);
            
            
            memset(&dest, 0, sizeof(dest));
            
            /* Ancilliary Data  */
            msg.msg_name = &sin6;
            msg.msg_namelen = sizeof(sin6);
            
            msg.msg_iov = &vec;
            msg.msg_iovlen = 1;
            msg.msg_iov->iov_base = tstmsg;
            msg.msg_iov->iov_len = 13;
            
            
            cmsglen = CMSG_SPACE(sizeof(dest));
            
            cmsg = malloc(cmsglen);
            
            if(cmsg == NULL) {
              puts("cmsg malloc failed");
              exit(1);
            }          
            
            msg.msg_control = cmsg;
            msg.msg_controllen = cmsglen;
            
            cmsg = CMSG_FIRSTHDR(&msg);                        
            cmsg->cmsg_level = IPPROTO_IPV6;
            cmsg->cmsg_type = IPV6_DSTOPTS;
            cmsg->cmsg_len = CMSG_LEN(sizeof(dest));
            memcpy(CMSG_DATA(cmsg), &dest, sizeof(dest));
            
            setsockopt(s0, IPPROTO_IPV6, IPV6_DSTOPTS, &on, sizeof(int)); 
            
            //send message back
            rv = sendmsg( s0 , &msg, 0);          
              if(rv == -1)
               {
                 puts("Error returned from sendmsg");
               }
           }
    exit(0);
}


Previous Thread | Next Thread
Test Your Knowledge in Computers #825
Difficulty: Medium
The term API seems to appear for the first time in the article of Ira W. Cotton, Data structures and techniques for remote computer graphics, published in 1968.
True or False?

8 More Discussions You Might Find Interesting

1. IP Networking

I would like to monitor network traffic for a computer on my network

My son does homework on a school laptop. I was thinking about setting up a gateway on my home network, so that I can monitor web traffic and know if he is doing his homework without standing over his shoulder. Ideally I would like to use the Raspberry Pi Model b that I already have. However, I... (15 Replies)
Discussion started by: gandolf989
15 Replies

2. Red Hat

Network becomes slow and return fast only after restart network

Hi, I have 2 machines in production environment: 1. redhat machine for application 2. DB machine (oracle) The application doing a lot of small read&writes from and to the DB machine. The problem is that after some few hours the network from the application to the DB becomes very slow and... (4 Replies)
Discussion started by: moshesa
4 Replies

3. Solaris

No network cable But Network interface is UP and Running

I've one Netra 240 After changing main board and system configuration card reader, Network is not accessible any more, Network interfaces are always UP and Running even when there is no cable connected to Network interfaces. I tried to restart and plumb/unplumb with no luck. ifconfig -a... (7 Replies)
Discussion started by: samer.odeh
7 Replies

4. UNIX for Dummies Questions & Answers

Does SCP return an error code for network issues

Hello everyone, In a script, I am using SCP to copy huge file to another host. scp -qrp hugefile.txt /opt/perf05/tmp However, we have noticed that this file is not being copied. I am suspecting this was because we are losing connection while copying this... (1 Reply)
Discussion started by: qwarentine
1 Replies

5. UNIX and Linux Applications

Access to network interface (Mac-network)

Hi, I'm a italian student. For my thesis I develop a gateway with protocol 6lowpan. For that I must access to network interface to develope my personal stack based on standard 802.15.4. Can you help me? I need an explanation for that. (0 Replies)
Discussion started by: berny88
0 Replies

6. Solaris

configure zones to have different network interface and network

i need to configure a zone to use different interface (bge2) than global and have connected to completely different network switch & to use its own defaultrouter and hosts file .. is it possible ..if so ..how ? Thanks (9 Replies)
Discussion started by: skamal4u
9 Replies

7. IP Networking

ssh server is attachable from local network not from another network

hello i have a ubuntu ssh server that i can acess from any of my comnputers but only if they are on the same wireless network as the server. i tested trhis my tehtehring my samsung blackjack to my windows partition and installing openssh to windows it works when windows is on the wireless but no... (1 Reply)
Discussion started by: old noob
1 Replies

8. Programming

Help in developing a Network Appliation to monitor pc in a network

I am developing a Network Appliation to monitor computers in a network. Specs are App monitors the current web page viewed in each system App also can shutdown the computer in the network App can show all process run by each computer in the network I am now confused how to start my... (2 Replies)
Discussion started by: valaparambil88
2 Replies

Featured Tech Videos