networkbrowser: main: fix various memory leaks
[enigma2-plugins.git] / networkbrowser / src / lib / main.c
1 /*###########################################################################
2 #
3 # Copyright (C) 2007 - 2009 by
4 # nixkoenner <nixkoenner@newnigma2.to>
5 # License: GPL
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #
21 ###########################################################################*/
22
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 #include "nbtscan.h"
27 #include "smbinfo.h"
28 #include "showmount.h"
29
30 PyObject *_netInfo(PyObject *self, PyObject *args)
31 {
32         const unsigned int max_hosts = 256;
33         netinfo *nInfo;
34         char *s;
35         PyObject *result;
36         unsigned int i, n;
37
38         if(!PyArg_ParseTuple(args, "s", &s)) {
39                 PyErr_SetString(PyExc_TypeError, "netInfo(ip/24)");
40                 return NULL;
41         }
42
43         result = PyList_New(0);
44         if (result == NULL)
45                 return NULL;
46
47         nInfo = PyMem_New(netinfo, max_hosts);
48         if (nInfo == NULL)
49                 return result;
50
51         Py_BEGIN_ALLOW_THREADS
52         n = netInfo(s, nInfo, max_hosts);
53         Py_END_ALLOW_THREADS
54
55         for (i = 0; i < n; i++) {
56                 PyObject *plist = PyList_New(0);
57                 if (plist == NULL)
58                         break;
59                 PyList_Append(plist, PyString_FromString("host"));
60                 PyList_Append(plist, PyString_FromString(nInfo[i].name));
61                 PyList_Append(plist, PyString_FromString(nInfo[i].ip));
62                 PyList_Append(plist, PyString_FromString(nInfo[i].mac));
63                 PyList_Append(plist, PyString_FromString(nInfo[i].domain));
64                 PyList_Append(plist, PyString_FromString(nInfo[i].service));
65                 PyList_Append(result, plist);
66         }
67
68         PyMem_Free(nInfo);
69         return result;
70 }
71
72 PyObject *_nfsShare(PyObject *self, PyObject *args)
73 {
74         nfsinfo *nfsInfo;
75         char *s;
76         char *r;
77         PyObject *plist, *result, *share, *ip, *nfsShare, *rech, *rechip, *leer;
78         int i = 0;
79         int err = 0;
80
81         if(!PyArg_ParseTuple(args, "ss", &s, &r)) {
82                 PyErr_SetString(PyExc_TypeError, "nfsShare(ip,rechnername)");
83                 return NULL;
84         }
85
86         if(!(plist= PyList_New(0)))  return NULL;
87         if(!(result= PyList_New(0)))  return NULL;
88
89         nfsInfo = newNfsInfo();
90         Py_BEGIN_ALLOW_THREADS
91         err = showNfsShare(s, nfsInfo);
92         Py_END_ALLOW_THREADS
93         if (err == 0)
94         {
95                 for (i=0; i<256; i++) 
96                 { 
97                         if(nfsInfo[i].ip[0] == '\0') {
98                                 break;
99                         }
100                         else
101                         {
102                                 ip = Py_BuildValue("s", nfsInfo[i].ip);
103                                 share = Py_BuildValue("s", nfsInfo[i].share);
104                                 nfsShare = Py_BuildValue("s", "nfsShare");
105                                 rech = Py_BuildValue("s",r);
106                                 rechip = Py_BuildValue("s",s);
107                                 leer = Py_BuildValue("s","");
108                                 PyList_Append(plist, nfsShare);
109                                 PyList_Append(plist, rech);
110                                 PyList_Append(plist, rechip);
111                                 PyList_Append(plist, ip);
112                                 PyList_Append(plist, share);
113                                 PyList_Append(plist, leer);
114                                 PyList_Append(result, plist);
115                                 if(!(plist= PyList_New(0)))  return NULL;               
116                         }
117                 }
118         }
119         else
120         {
121                 share = Py_BuildValue("s", nfsInfo[0].share);
122                 PyList_Append(plist, share);
123                 PyList_Append(result, plist);
124                 if(!(plist= PyList_New(0)))  return NULL;
125         }
126         freeNfsInfo(nfsInfo);   
127         return result;
128 }
129
130 PyObject *_smbShare(PyObject *self, PyObject *args)
131 {
132         int i = 0;
133         char *s;
134         char *r;
135         char *u;
136         char *p;
137         shareinfo *sInfo;
138         PyObject *plist, *name, *typ, *comment, *result, *smbShare, *rech, *rechip;
139
140         
141         if(!PyArg_ParseTuple(args, "ssss", &s,&r,&u,&p)) {
142                 PyErr_SetString(PyExc_TypeError, "getInfo(ip, rechnername, username, passwort)");
143                 return NULL;
144         }
145         if(!(plist= PyList_New(0)))  return NULL;
146         if(!(result= PyList_New(0)))  return NULL;
147
148         Py_BEGIN_ALLOW_THREADS
149         sInfo = newShareInfo();
150         smbInfo(s,r,u,p,sInfo);
151         Py_END_ALLOW_THREADS
152
153         for (i=0; i<128; i++) 
154         { 
155                 if(sInfo[i].sharename[0] == '\0') {
156                         break;
157                 }
158                 else
159                 {
160                         name = Py_BuildValue("s", sInfo[i].sharename);
161                         typ = Py_BuildValue("s", sInfo[i].typ);
162                         comment = Py_BuildValue("s", sInfo[i].comment);
163                         smbShare = Py_BuildValue("s", "smbShare");
164                         rech = Py_BuildValue("s",r);
165                         rechip = Py_BuildValue("s",s);
166                         PyList_Append(plist, smbShare);
167                         PyList_Append(plist, rech);
168                         PyList_Append(plist, rechip);
169                         PyList_Append(plist, name);
170                         PyList_Append(plist, typ);
171                         PyList_Append(plist, comment);
172                         PyList_Append(result, plist);
173                         if(!(plist= PyList_New(0)))  return NULL;
174                 }
175         }
176         freeShareInfo(sInfo);
177         return result;
178         //return Py_BuildValue("s",s);
179 }
180
181 static PyMethodDef netscanmethods[] = {
182         {"netInfo", _netInfo, METH_VARARGS},
183         {"smbShare", _smbShare, METH_VARARGS},
184         {"nfsShare", _nfsShare, METH_VARARGS},
185         {NULL, NULL}
186 };
187
188 void initnetscan(void)
189 {
190         Py_InitModule("netscan", netscanmethods);
191 }
192