LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 10-08-2005, 07:52 AM   #1
fpfernando
LQ Newbie
 
Registered: Oct 2005
Distribution: RH 9
Posts: 24

Rep: Reputation: 15
Code Conversion Help Needed


here i have inserted a code for BSD ..


wot it does is.........it helps in running Raw sockets with user permissions.....



Code:
                                                                                                                             
#include <linux/param.h>
#include <linux/in_systm.h>
#include <net/addrconf.h>
#include<linux/zconf.h>
#include<linux/autoconf.h>
#include <sys/exec.h>
#include <sys/lkm.h>
#include <sys/protosw.h>
                                                                                                                             
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <net/if.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/in_pcb.h>
#define IPSEC
int     nopriv_rip_usrreq       __P((register struct socket *, int,
                                     struct mbuf *, struct mbuf *,
                                     struct mbuf *));
                                                                                                                             
extern int      rip_usrreq      __P((register struct socket *, int,
                                     struct mbuf *, struct mbuf *,
                                     struct mbuf *));
                                                                                                                             
extern struct protosw inetsw[];
extern u_char ip_protox[];
extern u_long rip_sendspace, rip_recvspace;
extern struct inpcbtable rawcbtable;
                                                                                                                             
MOD_MISC("SRaw");
                                                                                                                             
static int
SRaw_load(struct lkm_table *lkmtp, int cmd)
{
        if(cmd == LKM_E_LOAD) {
                                                                                                                             
                int s;
                                                                                                                             
                printf("SRaw - 0.1 beta\n");
                printf("sOftPj - Y2k\n");
                                                                                                                             
                /*
                 * You can also add other protocols... with rip_usrreq or
                 * chage other pr_usrreq
                 */
                                                                                                                             
                s = splnet();
                                                                                                                             
                inetsw[ip_protox[IPPROTO_RAW]].pr_usrreq = nopriv_rip_usrreq;
                inetsw[ip_protox[IPPROTO_ICMP]].pr_usrreq = nopriv_rip_usrreq;
                inetsw[ip_protox[IPPROTO_IPV4]].pr_usrreq = nopriv_rip_usrreq;
                inetsw[ip_protox[IPPROTO_IPIP]].pr_usrreq = nopriv_rip_usrreq;
                inetsw[ip_protox[IPPROTO_IGMP]].pr_usrreq = nopriv_rip_usrreq;
                                                                                                                             
                splx(s);
        }
                                                                                                                             
        return 0;
}
                                                                                                                             
                                                                                                                             
static int
SRaw_unload(struct lkm_table *lkmtp, int cmd)
{
        if(cmd == LKM_E_UNLOAD) {
                int s;
                                                                                                                             
                printf("SRaw - Unloaded\n");
                                                                                                                             
                s = splnet();
                                                                                                                             
                inetsw[ip_protox[IPPROTO_RAW]].pr_usrreq = rip_usrreq;
                inetsw[ip_protox[IPPROTO_ICMP]].pr_usrreq = rip_usrreq;
                inetsw[ip_protox[IPPROTO_IPV4]].pr_usrreq = rip_usrreq;
                inetsw[ip_protox[IPPROTO_IPIP]].pr_usrreq = rip_usrreq;
                inetsw[ip_protox[IPPROTO_IGMP]].pr_usrreq = rip_usrreq;
                                                                                                                             
                splx(s);
        }
                                                                                                                             
        return 0;
}
                                                                                                                             
                                                                                                                             
SRaw( lkmtp, cmd, ver)
struct lkm_table        *lkmtp;
int                     cmd;
int                     ver;
{
        DISPATCH(lkmtp, cmd, ver, SRaw_load, SRaw_unload, lkm_nofunc);
}
                                                                                                                             
                                                                                                                             
int
nopriv_rip_usrreq(so, req, m, nam, control)
   register struct socket *so;
        int req;
        struct mbuf *m, *nam, *control;
{
        register int error = 0;
        register struct inpcb *inp = sotoinpcb(so);
#ifdef MROUTING
        extern struct socket *ip_mrouter;
#endif
        if (req == PRU_CONTROL)
                return (in_control(so, (u_long)m, (caddr_t)nam,
                        (struct ifnet *)control));
                                                                                                                             
        if (inp == NULL && req != PRU_ATTACH) {
                error = EINVAL;
                goto release;
        }
                                                                                                                             
        switch (req) {
                                                                                                                             
        case PRU_ATTACH:
                if (inp)
                        panic("rip_attach");
                /*
                 * if ((so->so_state & SS_PRIV) == 0) {
                 *      error = EACCES;
                 *      break;
                 * }
                 */
                                                                                                                             
                if((so->so_state & SS_PRIV) == 0)
                        so->so_state |= SS_PRIV;
                if ((error = soreserve(so, rip_sendspace, rip_recvspace)) ||
                    (error = in_pcballoc(so, &rawcbtable)))
                        break;
                inp = (struct inpcb *)so->so_pcb;
                inp->inp_ip.ip_p = (long)nam;
                break;
                                                                                                                             
        case PRU_DISCONNECT:
                if ((so->so_state & SS_ISCONNECTED) == 0) {
                        error = ENOTCONN;
                        break;
                }
                /* FALLTHROUGH */
        case PRU_ABORT:
                soisdisconnected(so);
                /* FALLTHROUGH */
        case PRU_DETACH:
                if (inp == 0)
                        panic("rip_detach");
#ifdef MROUTING
                if (so == ip_mrouter)
                        ip_mrouter_done();
#endif
                in_pcbdetach(inp);
                break;
                                                                                                                             
        case PRU_BIND:
            {
                struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
                                                                                                                             
                if (nam->m_len != sizeof(*addr)) {
                        error = EINVAL;
                        break;
                }
                if ((ifnet.tqh_first == 0) ||
                    ((addr->sin_family != AF_INET) &&
                     (addr->sin_family != AF_IMPLINK)) ||
                    (addr->sin_addr.s_addr &&
                     ifa_ifwithaddr(sintosa(addr)) == 0)) {
                        error = EADDRNOTAVAIL;
                        break;
                }
                inp->inp_laddr = addr->sin_addr;
                break;
            }
        case PRU_CONNECT:
            {
                struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
                                                                                                                             
                if (nam->m_len != sizeof(*addr)) {
                        error = EINVAL;
                        break;
                }
                if (ifnet.tqh_first == 0) {
                        error = EADDRNOTAVAIL;
                        break;
                }
                if ((addr->sin_family != AF_INET) &&
                     (addr->sin_family != AF_IMPLINK)) {
                        error = EAFNOSUPPORT;
                        break;
                }
                inp->inp_faddr = addr->sin_addr;
                soisconnected(so);
                break;
            }
      case PRU_CONNECT2:
                error = EOPNOTSUPP;
                break;
                                                                                                                             
        /*
         * Mark the connection as being incapable of further input.
         */
        case PRU_SHUTDOWN:
                socantsendmore(so);
                break;
                                                                                                                             
        /*
         * Ship a packet out.  The appropriate raw output
         * routine handles any massaging necessary.
         */
        case PRU_SEND:
            {
                register u_int32_t dst;
                                                                                                                             
                if (so->so_state & SS_ISCONNECTED) {
                        if (nam) {
                                error = EISCONN;
                                break;
                        }
                        dst = inp->inp_faddr.s_addr;
                } else {
                        if (nam == NULL) {
                                error = ENOTCONN;
                                break;
                        }
                        dst = mtod(nam, struct sockaddr_in *)->sin_addr.s_addr;
                }
#ifdef IPSEC
                if (!(error = check_ipsec_policy(inp, dst)))
#endif
                error = rip_output(m, so, dst);
                m = NULL;
                break;
            }
                                                                                                                             
        case PRU_SENSE:
                /*
                 * stat: don't bother with a blocksize.
                 */
                return (0);
                                                                                                                             
        /*
         * Not supported.
         */
        //case PRU_RCVOOB:
        //case PRU_RCVD:
        //case PRU_LISTEN:
        //case PRU_ACCEPT:
        case PRU_SENDOOB:
                error = EOPNOTSUPP;
                break;
                                                                                                                             
        case PRU_SOCKADDR:
                in_setsockaddr(inp, nam);
                break;
                                                                                                                             
        case PRU_PEERADDR:
                in_setpeeraddr(inp, nam);
                break;
                                                                                                                             
        default:
                panic("rip_usrreq");
             panic("rip_usrreq");
        }
release:
        if (m != NULL)
                m_freem(m);
        return (error);
}
can u help me out please............

i want a similar code which would run in RH linux 9
 
Old 10-09-2005, 11:02 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
im definetly not going to read all the code, but..
where did you get it? IS there a linux version?
c++ should work and compile on any OS/distro. try it, what are the errors?
 
Old 10-09-2005, 11:36 PM   #3
fpfernando
LQ Newbie
 
Registered: Oct 2005
Distribution: RH 9
Posts: 24

Original Poster
Rep: Reputation: 15
when i complie these ae the errors i get.................
do u have any idea how to solve it

cc -D_KERNEL -I/sys -c -o obsd_sraw.o obsd_sraw.c
In file included from obsd_sraw.c:25:
/usr/include/linux/in_systm.h:28: parse error before "n_short"
/usr/include/linux/in_systm.h:29: parse error before "n_long"
/usr/include/linux/in_systm.h:30: parse error before "n_time"
obsd_sraw.c:26:26: net/addrconf.h: No such file or directory
obsd_sraw.c:27:24: linux/zconf.h: No such file or directory
In file included from obsd_sraw.c:28:
/usr/include/linux/autoconf.h:1:2: #error Invalid kernel header included in userspace
obsd_sraw.c:29:22: sys/exec.h: No such file or directory
obsd_sraw.c:30:21: sys/lkm.h: No such file or directory
obsd_sraw.c:31:25: sys/protosw.h: No such file or directory
obsd_sraw.c:33:22: sys/mbuf.h: No such file or directory
In file included from obsd_sraw.c:39:
/usr/include/netinet/in_systm.h:35: `n_short' redeclared as different kind of symbol
/usr/include/linux/in_systm.h:28: previous declaration of `n_short'
/usr/include/netinet/in_systm.h:36: `n_long' redeclared as different kind of symbol
/usr/include/linux/in_systm.h:29: previous declaration of `n_long'
/usr/include/netinet/in_systm.h:37: `n_time' redeclared as different kind of symbol
/usr/include/linux/in_systm.h:30: previous declaration of `n_time'
obsd_sraw.c:41:28: netinet/in_pcb.h: No such file or directory
obsd_sraw.c:45: warning: `struct mbuf' declared inside parameter list
obsd_sraw.c:45: warning: its scope is only this definition or declaration, which is probably not what you want
obsd_sraw.c:45: warning: `struct socket' declared inside parameter list
obsd_sraw.c:49: warning: `struct mbuf' declared inside parameter list
obsd_sraw.c:49: warning: `struct socket' declared inside parameter list
obsd_sraw.c:56: parse error before string constant
obsd_sraw.c:56: warning: data definition has no type or storage class
obsd_sraw.c:59: warning: `struct lkm_table' declared inside parameter list
obsd_sraw.c: In function `SRaw_load':
obsd_sraw.c:61: `LKM_E_LOAD' undeclared (first use in this function)
obsd_sraw.c:61: (Each undeclared identifier is reported only once
obsd_sraw.c:61: for each function it appears in.)
obsd_sraw.c:75: invalid use of undefined type `struct protosw'
obsd_sraw.c:76: invalid use of undefined type `struct protosw'
obsd_sraw.c:77: `IPPROTO_IPV4' undeclared (first use in this function)
obsd_sraw.c:78: invalid use of undefined type `struct protosw'
obsd_sraw.c:79: invalid use of undefined type `struct protosw'
obsd_sraw.c: At top level:
obsd_sraw.c:89: warning: `struct lkm_table' declared inside parameter list
obsd_sraw.c: In function `SRaw_unload':
obsd_sraw.c:91: `LKM_E_UNLOAD' undeclared (first use in this function)
obsd_sraw.c:98: invalid use of undefined type `struct protosw'
obsd_sraw.c:99: invalid use of undefined type `struct protosw'
obsd_sraw.c:100: `IPPROTO_IPV4' undeclared (first use in this function)
obsd_sraw.c:101: invalid use of undefined type `struct protosw'
obsd_sraw.c:102: invalid use of undefined type `struct protosw'
obsd_sraw.c: In function `SRaw':
obsd_sraw.c:116: `lkm_nofunc' undeclared (first use in this function)
obsd_sraw.c: In function `nopriv_rip_usrreq':
obsd_sraw.c:125: argument `so' doesn't match prototype
obsd_sraw.c:45: prototype declaration
obsd_sraw.c:125: argument `m' doesn't match prototype
obsd_sraw.c:45: prototype declaration
obsd_sraw.c:125: argument `nam' doesn't match prototype
obsd_sraw.c:45: prototype declaration
obsd_sraw.c:125: argument `control' doesn't match prototype
obsd_sraw.c:45: prototype declaration
obsd_sraw.c:127: warning: initialization makes pointer from integer without a cast
obsd_sraw.c:131: `PRU_CONTROL' undeclared (first use in this function)
obsd_sraw.c:135: `PRU_ATTACH' undeclared (first use in this function)
obsd_sraw.c:136: `EINVAL' undeclared (first use in this function)
obsd_sraw.c:152: dereferencing pointer to incomplete type
obsd_sraw.c:152: `SS_PRIV' undeclared (first use in this function)
obsd_sraw.c:153: dereferencing pointer to incomplete type
obsd_sraw.c:159: dereferencing pointer to incomplete type
obsd_sraw.c:160: dereferencing pointer to incomplete type
obsd_sraw.c:163: `PRU_DISCONNECT' undeclared (first use in this function)
obsd_sraw.c:164: dereferencing pointer to incomplete type
obsd_sraw.c:164: `SS_ISCONNECTED' undeclared (first use in this function)
obsd_sraw.c:165: `ENOTCONN' undeclared (first use in this function)
obsd_sraw.c:169: `PRU_ABORT' undeclared (first use in this function)
obsd_sraw.c:172: `PRU_DETACH' undeclared (first use in this function)
obsd_sraw.c:182: `PRU_BIND' undeclared (first use in this function)
obsd_sraw.c:184: parse error before "struct"
obsd_sraw.c:186: dereferencing pointer to incomplete type
obsd_sraw.c:190: `ifnet' undeclared (first use in this function)
obsd_sraw.c:192: `AF_IMPLINK' undeclared (first use in this function)
obsd_sraw.c:195: `EADDRNOTAVAIL' undeclared (first use in this function)
obsd_sraw.c:198: dereferencing pointer to incomplete type
obsd_sraw.c:201: `PRU_CONNECT' undeclared (first use in this function)
obsd_sraw.c:203: parse error before "struct"
obsd_sraw.c:205: dereferencing pointer to incomplete type
obsd_sraw.c:215: `EAFNOSUPPORT' undeclared (first use in this function)
obsd_sraw.c:218: dereferencing pointer to incomplete type
obsd_sraw.c:223: `PRU_CONNECT2' undeclared (first use in this function)
obsd_sraw.c:224: `EOPNOTSUPP' undeclared (first use in this function)
obsd_sraw.c:230: `PRU_SHUTDOWN' undeclared (first use in this function)
obsd_sraw.c:238: `PRU_SEND' undeclared (first use in this function)
obsd_sraw.c:242: dereferencing pointer to incomplete type
obsd_sraw.c:244: `EISCONN' undeclared (first use in this function)
obsd_sraw.c:247: dereferencing pointer to incomplete type
obsd_sraw.c:253: parse error before "struct"
obsd_sraw.c:263: `PRU_SENSE' undeclared (first use in this function)
obsd_sraw.c:276: `PRU_SENDOOB' undeclared (first use in this function)
obsd_sraw.c:280: `PRU_SOCKADDR' undeclared (first use in this function)
obsd_sraw.c:284: `PRU_PEERADDR' undeclared (first use in this function)
make: *** [obsd_sraw.o] Error 1
 
  


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
small syntax problem with C code (implemented in Code Composer Studio) illiniguy3043 Programming 6 01-07-2008 02:14 AM
Help needed with last part of code! twirl Programming 10 10-07-2005 05:44 AM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM
Clarification needed in this reg exp/awk code mselvam Programming 3 07-09-2005 05:26 PM
Editing buttons (quote, code etc.) add the code to the end vharishankar LQ Suggestions & Feedback 2 09-13-2004 09:32 AM

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

All times are GMT -5. The time now is 11:34 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